|
| 1 | +package graphgorm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync/atomic" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "gorm.io/driver/sqlite" |
| 11 | + "gorm.io/gorm" |
| 12 | + "gorm.io/gorm/logger" |
| 13 | + |
| 14 | + requestctx "github.com/tae2089/code-context-graph/internal/ctx" |
| 15 | + "github.com/tae2089/code-context-graph/internal/domain/graph" |
| 16 | +) |
| 17 | + |
| 18 | +type annotationSQLCounter struct { |
| 19 | + statements atomic.Int64 |
| 20 | +} |
| 21 | + |
| 22 | +func (c *annotationSQLCounter) LogMode(logger.LogLevel) logger.Interface { return c } |
| 23 | +func (c *annotationSQLCounter) Info(context.Context, string, ...any) {} |
| 24 | +func (c *annotationSQLCounter) Warn(context.Context, string, ...any) {} |
| 25 | +func (c *annotationSQLCounter) Error(context.Context, string, ...any) {} |
| 26 | +func (c *annotationSQLCounter) Trace(_ context.Context, _ time.Time, fc func() (string, int64), _ error) { |
| 27 | + c.statements.Add(1) |
| 28 | + _, _ = fc() |
| 29 | +} |
| 30 | + |
| 31 | +func (c *annotationSQLCounter) reset() { |
| 32 | + c.statements.Store(0) |
| 33 | +} |
| 34 | + |
| 35 | +func setupAnnotationSQLMeasurement(t *testing.T, count int) (*Store, context.Context, []*graph.Annotation, *annotationSQLCounter) { |
| 36 | + t.Helper() |
| 37 | + counter := &annotationSQLCounter{} |
| 38 | + db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{Logger: counter}) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("open SQLite: %v", err) |
| 41 | + } |
| 42 | + store := New(db) |
| 43 | + if err := store.AutoMigrate(); err != nil { |
| 44 | + t.Fatalf("auto migrate: %v", err) |
| 45 | + } |
| 46 | + ctx := requestctx.WithNamespace(context.Background(), "annotation-sql-measurement") |
| 47 | + nodes := make([]graph.Node, count) |
| 48 | + for i := range nodes { |
| 49 | + nodes[i] = graph.Node{ |
| 50 | + QualifiedName: fmt.Sprintf("sample.F%d", i), |
| 51 | + Kind: graph.NodeKindFunction, |
| 52 | + Name: fmt.Sprintf("F%d", i), |
| 53 | + FilePath: "sample.go", |
| 54 | + StartLine: i + 1, |
| 55 | + EndLine: i + 1, |
| 56 | + Language: "go", |
| 57 | + } |
| 58 | + } |
| 59 | + if err := store.UpsertNodes(ctx, nodes); err != nil { |
| 60 | + t.Fatalf("upsert nodes: %v", err) |
| 61 | + } |
| 62 | + stored, err := store.GetNodesByFile(ctx, "sample.go") |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("load nodes: %v", err) |
| 65 | + } |
| 66 | + annotations := make([]*graph.Annotation, len(stored)) |
| 67 | + for i := range stored { |
| 68 | + annotations[i] = &graph.Annotation{ |
| 69 | + NodeID: stored[i].ID, |
| 70 | + Summary: fmt.Sprintf("summary %d", i), |
| 71 | + Tags: []graph.DocTag{{ |
| 72 | + Kind: graph.TagIntent, |
| 73 | + Value: fmt.Sprintf("intent %d", i), |
| 74 | + Ordinal: 0, |
| 75 | + }}, |
| 76 | + } |
| 77 | + } |
| 78 | + counter.reset() |
| 79 | + return store, ctx, annotations, counter |
| 80 | +} |
| 81 | + |
| 82 | +func TestUpsertAnnotations_BatchesSQLAndPreservesTags(t *testing.T) { |
| 83 | + store, ctx, annotations, counter := setupAnnotationSQLMeasurement(t, 20) |
| 84 | + started := time.Now() |
| 85 | + if err := store.UpsertAnnotations(ctx, annotations); err != nil { |
| 86 | + t.Fatalf("upsert annotations: %v", err) |
| 87 | + } |
| 88 | + statements := counter.statements.Load() |
| 89 | + t.Logf("bulk annotation candidate: annotations=%d statements=%d elapsed=%s", len(annotations), statements, time.Since(started)) |
| 90 | + if statements > 5 { |
| 91 | + t.Fatalf("bulk annotation statements = %d, want <= 5", statements) |
| 92 | + } |
| 93 | + |
| 94 | + for i, annotation := range annotations { |
| 95 | + got, err := store.GetAnnotation(ctx, annotation.NodeID) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("get annotation %d: %v", i, err) |
| 98 | + } |
| 99 | + if got == nil || got.Summary != annotation.Summary { |
| 100 | + t.Fatalf("annotation %d = %+v, want summary %q", i, got, annotation.Summary) |
| 101 | + } |
| 102 | + if len(got.Tags) != 1 || got.Tags[0].Value != annotation.Tags[0].Value { |
| 103 | + t.Fatalf("annotation %d tags = %+v, want %+v", i, got.Tags, annotation.Tags) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + for i, annotation := range annotations { |
| 108 | + annotation.Summary = fmt.Sprintf("updated summary %d", i) |
| 109 | + annotation.Tags = []graph.DocTag{{Kind: graph.TagDomainRule, Value: fmt.Sprintf("rule %d", i), Ordinal: 0}} |
| 110 | + } |
| 111 | + counter.reset() |
| 112 | + if err := store.UpsertAnnotations(ctx, annotations); err != nil { |
| 113 | + t.Fatalf("update annotations: %v", err) |
| 114 | + } |
| 115 | + if statements := counter.statements.Load(); statements > 5 { |
| 116 | + t.Fatalf("bulk annotation update statements = %d, want <= 5", statements) |
| 117 | + } |
| 118 | + for i, annotation := range annotations { |
| 119 | + got, err := store.GetAnnotation(ctx, annotation.NodeID) |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("get updated annotation %d: %v", i, err) |
| 122 | + } |
| 123 | + if got == nil || got.Summary != annotation.Summary || len(got.Tags) != 1 || got.Tags[0].Value != annotation.Tags[0].Value { |
| 124 | + t.Fatalf("updated annotation %d = %+v, want %+v", i, got, annotation) |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestUpsertAnnotations_RejectsForeignNodeBeforeWrites(t *testing.T) { |
| 130 | + store, ctx, annotations, _ := setupAnnotationSQLMeasurement(t, 1) |
| 131 | + foreignCtx := requestctx.WithNamespace(context.Background(), "foreign") |
| 132 | + foreignNodes := []graph.Node{{ |
| 133 | + QualifiedName: "foreign.F", |
| 134 | + Kind: graph.NodeKindFunction, |
| 135 | + Name: "F", |
| 136 | + FilePath: "foreign.go", |
| 137 | + StartLine: 1, |
| 138 | + EndLine: 1, |
| 139 | + Language: "go", |
| 140 | + }} |
| 141 | + if err := store.UpsertNodes(foreignCtx, foreignNodes); err != nil { |
| 142 | + t.Fatalf("upsert foreign node: %v", err) |
| 143 | + } |
| 144 | + storedForeign, err := store.GetNodesByFile(foreignCtx, "foreign.go") |
| 145 | + if err != nil || len(storedForeign) != 1 { |
| 146 | + t.Fatalf("load foreign node: nodes=%+v err=%v", storedForeign, err) |
| 147 | + } |
| 148 | + foreignAnnotation := &graph.Annotation{NodeID: storedForeign[0].ID, Summary: "foreign"} |
| 149 | + |
| 150 | + err = store.UpsertAnnotations(ctx, []*graph.Annotation{annotations[0], foreignAnnotation}) |
| 151 | + if err == nil { |
| 152 | + t.Fatal("expected foreign-node batch to be rejected") |
| 153 | + } |
| 154 | + got, getErr := store.GetAnnotation(ctx, annotations[0].NodeID) |
| 155 | + if getErr != nil { |
| 156 | + t.Fatalf("get owned annotation after rejection: %v", getErr) |
| 157 | + } |
| 158 | + if got != nil { |
| 159 | + t.Fatalf("owned annotation was written before batch rejection: %+v", got) |
| 160 | + } |
| 161 | +} |
0 commit comments