Files
memoryweave/go/internal/pattern/store_test.go
T

117 lines
3.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package pattern
import (
"database/sql"
"encoding/json"
"testing"
_ "modernc.org/sqlite"
)
// newTestStore 指向 t.TempDir() 的空库(modernc 纯 Go,无需 CGO/网络)。
func newTestStore(t *testing.T) *GraphPatternStore {
t.Helper()
dir := t.TempDir()
s := NewGraphPatternStore(dir + "/graph.db")
t.Cleanup(s.Close)
return s
}
// TestStore_EmptyList —— 空库 ListPatterns → 0 条不报错(AC1 判据:SELECT count ≥0 不报错)。
func TestStore_EmptyList(t *testing.T) {
s := newTestStore(t)
got, err := s.ListPatterns(10)
if err != nil {
t.Fatalf("空库 List 不应报错: %v", err)
}
if len(got) != 0 {
t.Fatalf("空库应 0 条 pattern,实际 %d", len(got))
}
}
// TestStore_UpsertEmpty —— 空输入 Upsert → 0,0,nil(AC1 判据)。
func TestStore_UpsertEmpty(t *testing.T) {
s := newTestStore(t)
created, updated, err := s.UpsertPatterns(nil)
if err != nil {
t.Fatalf("空输入 Upsert 不应报错: %v", err)
}
if created != 0 || updated != 0 {
t.Fatalf("空输入应 0/0,实际 %d/%d", created, updated)
}
}
// TestStore_UpsertCreate —— 新 pattern → INSERT 1 条,occurrence_count=1(AC2 前置)。
func TestStore_UpsertCreate(t *testing.T) {
s := newTestStore(t)
p := Pattern{
ID: "pat_tool_combo:curl+git+jq",
Type: "tool_combo",
Name: "tool_combo:curl+git+jq",
Data: "curl+git+jq",
Confidence: 0.3,
Tools: []string{"curl", "git", "jq"},
SourceTasks: []string{"m_a", "m_b", "m_c"},
}
created, updated, err := s.UpsertPatterns([]Pattern{p})
if err != nil {
t.Fatalf("upsert 报错: %v", err)
}
if created != 1 || updated != 0 {
t.Fatalf("新 pattern 应 created=1/updated=0,实际 %d/%d", created, updated)
}
listed, _ := s.ListPatterns(10)
if len(listed) != 1 {
t.Fatalf("应 1 条,实际 %d", len(listed))
}
}
// TestStore_UpsertIdempotent —— 同名 pattern 二次 upsert → updated=1、occurrence_count=2。
// 验证幂等语义(occurrence_count 驱动,AC2 sqlite 查判据):跑两次检测,不产生重复行。
func TestStore_UpsertIdempotent(t *testing.T) {
s := newTestStore(t)
p := Pattern{
ID: "pat_tool_combo:curl+git+jq",
Type: "tool_combo",
Name: "tool_combo:curl+git+jq",
Data: "curl+git+jq",
Confidence: 0.3,
Tools: []string{"curl", "git", "jq"},
SourceTasks: []string{"m_a", "m_b", "m_c"},
}
c1, u1, _ := s.UpsertPatterns([]Pattern{p}) // 新建
p.Confidence = 0.4 // 第二次检测:同组合再次出现(freq 抬升)
p.SourceTasks = []string{"m_d"}
c2, u2, _ := s.UpsertPatterns([]Pattern{p}) // 应幂等更新(updated=1)
if c1 != 1 || u1 != 0 {
t.Fatalf("首次应 created=1/updated=0,实际 %d/%d", c1, u1)
}
if c2 != 0 || u2 != 1 {
t.Fatalf("二次应 created=0/updated=1,实际 %d/%d", c2, u2)
}
// 读库验:仍是 1 行、occurrence_count=2(幂等,非重复)
db, err := sql.Open("sqlite", s.dbPath)
if err != nil {
t.Fatalf("open: %v", err)
}
defer db.Close()
var (
cnt int
occ int
)
if err := db.QueryRow(`SELECT count(*) FROM graph_nodes WHERE type='pattern'`).Scan(&cnt); err != nil {
t.Fatal(err)
}
if cnt != 1 {
t.Fatalf("两次同 pattern 检测后应仍 1 行,实际 %d(幂等失败)", cnt)
}
var raw string
_ = db.QueryRow(`SELECT properties FROM graph_nodes WHERE type='pattern'`).Scan(&raw)
var props map[string]interface{}
_ = json.Unmarshal([]byte(raw), &props)
occ = int(props["occurrence_count"].(float64))
if occ != 2 {
t.Fatalf("occurrence_count 应累计到 2,实际 %d", occ)
}
}