Skip to content

Commit 3229cf4

Browse files
committed
unit and integration tests
1 parent fb6f2a2 commit 3229cf4

10 files changed

Lines changed: 48 additions & 36 deletions

File tree

internal/db/collection_index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ func processNewIndexRequest(
522522
// The field must hold a float32 array, and dimensions must be set unless the field is an @embedding,
523523
// whose model fixes the vector length. It also defaults the algorithm, metric, and any missing
524524
// params (mutating desc.Vector), so a request made through the index API works the same as one from
525-
// the @vectorIndex directive.
525+
// the @index directive's vector configuration.
526526
//
527527
// The field is guaranteed to exist here because validateIndexDescription and
528528
// checkExistingFieldsAndAdjustRelFieldNames run before this and already check that.

internal/db/vector_index_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
// newVectorIndexTestDB opens an in-memory badger-backed DB with a collection carrying a
27-
// [Float32!] @vectorIndex field, ready for document writes.
27+
// [Float32!] @index(vector: {...}) field, ready for document writes.
2828
func newVectorIndexTestDB(t *testing.T, dimensions int) (context.Context, *DB, client.Collection) {
2929
t.Helper()
3030
ctx := context.Background()
@@ -35,7 +35,7 @@ func newVectorIndexTestDB(t *testing.T, dimensions int) (context.Context, *DB, c
3535
_, err = db.AddCollection(ctx, `
3636
type Users {
3737
name: String
38-
embedding: [Float32!] @vectorIndex(dimensions: `+strconv.Itoa(dimensions)+`, HNSW: {metric: COSINE})
38+
embedding: [Float32!] @index(vector: {dimensions: `+strconv.Itoa(dimensions)+`, hnsw: {metric: COSINE}})
3939
}
4040
`)
4141
require.NoError(t, err)

internal/request/graphql/schema/index_parse_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,18 @@ func TestParseIndexOnField(t *testing.T) {
376376
},
377377
},
378378
},
379+
{
380+
description: "nested and legacy ordered configs merge when they do not overlap",
381+
sdl: `type user {
382+
name: String @index(ordered: {unique: true}, direction: DESC)
383+
}`,
384+
targetDescriptions: []client.NewIndexRequest{
385+
{
386+
Fields: []client.IndexedFieldDescription{{Name: "name", Descending: true}},
387+
Unique: true,
388+
},
389+
},
390+
},
379391
}
380392

381393
for _, test := range cases {
@@ -456,9 +468,9 @@ func TestParseInvalidIndexOnField(t *testing.T) {
456468
expectedErr: errIndexInvalidArgument,
457469
},
458470
{
459-
description: "nested and legacy ordered configs are competing kind selectors",
471+
description: "nested and legacy ordered configs cannot set the same property",
460472
sdl: `type user {
461-
name: String @index(ordered: {unique: true}, direction: DESC)
473+
name: String @index(ordered: {unique: true}, unique: false)
462474
}`,
463475
expectedErr: errIndexInvalidArgument,
464476
},

tests/integration/collection_version/vector_index_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
testUtils "github.com/sourcenetwork/defradb/tests/integration"
2020
)
2121

22-
// A collection carrying a valid @vectorIndex on a raw [Float32!] field is created end-to-end: the
22+
// A collection carrying a valid @index(vector: {...}) on a raw [Float32!] field is created end-to-end: the
2323
// index is registered as a vector-kind index with its parsed algorithm/metric/dimensions/HNSW
2424
// params. The index performs no graph work yet (Phase 3 wires the HNSW engine); this asserts the
2525
// schema surface + descriptor plumbing only.
@@ -29,7 +29,7 @@ func TestCollectionVersion_VectorIndexOnRawFloat32Array_ShouldSucceed(t *testing
2929
&action.AddCollection{
3030
SDL: `
3131
type Users {
32-
embedding: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
32+
embedding: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
3333
}
3434
`,
3535
},
@@ -64,7 +64,7 @@ func TestCollectionVersion_VectorIndexOnFloat32ArrayWithoutDimensionsOrEmbedding
6464
&action.AddCollection{
6565
SDL: `
6666
type Users {
67-
embedding: [Float32!] @vectorIndex
67+
embedding: [Float32!] @index(kind: vector)
6868
}
6969
`,
7070
ExpectedError: "vector index requires dimensions unless field is an embedding",
@@ -81,7 +81,7 @@ func TestCollectionVersion_VectorIndexOnStringField_ShouldError(t *testing.T) {
8181
&action.AddCollection{
8282
SDL: `
8383
type Users {
84-
embedding: String @vectorIndex(dimensions: 3)
84+
embedding: String @index(vector: {dimensions: 3})
8585
}
8686
`,
8787
ExpectedError: "unsupported field type for vector index",
@@ -98,7 +98,7 @@ func TestCollectionVersion_VectorIndexOnFloat64ArrayField_ShouldError(t *testing
9898
&action.AddCollection{
9999
SDL: `
100100
type Users {
101-
embedding: [Float64!] @vectorIndex(dimensions: 3)
101+
embedding: [Float64!] @index(vector: {dimensions: 3})
102102
}
103103
`,
104104
ExpectedError: "unsupported field type for vector index",
@@ -110,17 +110,17 @@ func TestCollectionVersion_VectorIndexOnFloat64ArrayField_ShouldError(t *testing
110110
}
111111

112112
func TestCollectionVersion_VectorIndexWithUnsupportedAlgorithm_ShouldError(t *testing.T) {
113-
// An unknown algorithm is now an unknown directive argument (the algorithm is the argument key),
114-
// so GraphQL rejects it before the parser runs.
113+
// An unknown algorithm config is an unknown field in the vector config, so GraphQL rejects it
114+
// before the parser runs.
115115
test := testUtils.TestCase{
116116
Actions: []any{
117117
&action.AddCollection{
118118
SDL: `
119119
type Users {
120-
embedding: [Float32!] @vectorIndex(dimensions: 3, IVFFlat: {})
120+
embedding: [Float32!] @index(vector: {dimensions: 3, IVFFlat: {}})
121121
}
122122
`,
123-
ExpectedError: `Unknown argument "IVFFlat" on directive "@vectorIndex"`,
123+
ExpectedError: `In field "IVFFlat": Unknown field.`,
124124
},
125125
},
126126
}
@@ -144,7 +144,7 @@ func vectorIndexMetricTest(sdlMetric string, expected client.DistanceMetric) tes
144144
&action.AddCollection{
145145
SDL: `
146146
type Users {
147-
embedding: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: ` + sdlMetric + `})
147+
embedding: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: ` + sdlMetric + `}})
148148
}
149149
`,
150150
},
@@ -175,7 +175,7 @@ func TestCollectionVersion_VectorIndexWithUnsupportedMetric_ShouldError(t *testi
175175
&action.AddCollection{
176176
SDL: `
177177
type Users {
178-
embedding: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: MANHATTAN})
178+
embedding: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: MANHATTAN}})
179179
}
180180
`,
181181
ExpectedError: `Expected type "VectorDistanceMetric", found MANHATTAN`,

tests/integration/index/new_composite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestCompositeIndexNew_UsingObjectDirective_SetsDefaultDirection(t *testing.
7979
Actions: []any{
8080
&action.AddCollection{
8181
SDL: `
82-
type User @index(direction: DESC, includes: [{field: "name"}, {field: "age"}]) {
82+
type User @index(ordered: {direction: DESC, includes: [{field: "name"}, {field: "age"}]}) {
8383
name: String
8484
age: Int
8585
}

tests/integration/index/patch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func TestPatchCollection_ModifyVectorIndexMetric_ShouldError(t *testing.T) {
134134
SDL: `
135135
type User {
136136
name: String
137-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
137+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
138138
}
139139
`,
140140
},

tests/integration/index/vector_metrics_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ func TestVectorIndex_QueryOnAnyMetric_ShouldUseIndexAndScoreByItsMetric(t *testi
4444
&action.AddCollection{
4545
SDL: `type User {
4646
name: String
47-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: ` +
48-
testCase.sdlMetric + `})
47+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: ` +
48+
testCase.sdlMetric + `}})
4949
}`,
5050
},
5151
&action.AddDoc{DocMap: map[string]any{"name": "x", "vector": []float32{1, 0, 0}}},
@@ -141,8 +141,8 @@ func TestVectorIndex_SameQueryUsingIndexAndFullScan_ReturnsSameResults(t *testin
141141
&action.AddCollection{
142142
SDL: `type User {
143143
name: String
144-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: ` +
145-
testCase.sdlMetric + `})
144+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: ` +
145+
testCase.sdlMetric + `}})
146146
}`,
147147
},
148148
&action.AddDoc{DocMap: map[string]any{"name": "short", "vector": vectors["short"]}},
@@ -177,7 +177,7 @@ func TestVectorIndex_SecondIndexOnFieldWithDifferentMetric_IsRejected(t *testing
177177
&action.AddCollection{
178178
SDL: `type User {
179179
name: String
180-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
180+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
181181
}`,
182182
},
183183
&action.NewIndex{
@@ -204,7 +204,7 @@ func TestVectorIndex_DropThenRecreateWithDifferentMetric_IsAllowed(t *testing.T)
204204
&action.AddCollection{
205205
SDL: `type User {
206206
name: String
207-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
207+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
208208
}`,
209209
},
210210
&action.AddDoc{DocMap: map[string]any{"name": "x", "vector": []float32{1, 0, 0}}},

tests/integration/index/vector_p2p_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
// A document written on one peer and synced to another is added to the replica's graph, so a
2424
// similarity query on the replica finds it. This proves the P2P merge maintains the vector index,
25-
// not just direct writes. The @vectorIndex is in the schema so both peers build it the same way.
25+
// not just direct writes. The vector @index directive is in the schema so both peers build it the same way.
2626
func TestVectorIndexP2P_ReplicatedDoc_IsSearchableOnReplica(t *testing.T) {
2727
test := testUtils.TestCase{
2828
Actions: []any{
@@ -31,7 +31,7 @@ func TestVectorIndexP2P_ReplicatedDoc_IsSearchableOnReplica(t *testing.T) {
3131
&action.AddCollection{
3232
SDL: `type Users {
3333
name: String
34-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
34+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
3535
}`,
3636
},
3737
testUtils.ConnectPeers{

tests/integration/index/vector_params_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestVectorIndex_CreateWithOversizedM_IsRejected(t *testing.T) {
2727
&action.AddCollection{
2828
SDL: `type User {
2929
name: String
30-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE, M: 100000})
30+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE, M: 100000}})
3131
}`,
3232
ExpectedError: "vector index parameter is out of range",
3333
},

tests/integration/query/simple/with_similarity_vector_index_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
testUtils "github.com/sourcenetwork/defradb/tests/integration"
1919
)
2020

21-
// A _similarity + order DESC + limit query on a ready @vectorIndex returns the k nearest documents
21+
// A _similarity + order DESC + limit query on a ready @index(vector: {...}) returns the k nearest documents
2222
// (nearest to [1,0,0] is "x", then "xy") and reads only those k, not the whole collection. The
2323
// explain variant asserts two doc fetches (a full scan would read four).
2424
func TestQuerySimple_WithSimilarityOnVectorIndex_ReturnsKNearest(t *testing.T) {
@@ -27,7 +27,7 @@ func TestQuerySimple_WithSimilarityOnVectorIndex_ReturnsKNearest(t *testing.T) {
2727
&action.AddCollection{
2828
SDL: `type User {
2929
name: String
30-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
30+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
3131
}`,
3232
},
3333
&action.AddDoc{DocMap: map[string]any{"name": "x", "vector": []float32{1, 0, 0}}},
@@ -72,7 +72,7 @@ func TestQuerySimple_WithSimilarityOnVectorIndex_ReflectsUpdatedVector(t *testin
7272
&action.AddCollection{
7373
SDL: `type User {
7474
name: String
75-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
75+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
7676
}`,
7777
},
7878
// a sits off the query axis; b starts even further off. After the update b lands exactly on
@@ -113,7 +113,7 @@ func TestQuerySimple_WithSimilarityOnVectorIndex_ExcludesDeletedDoc(t *testing.T
113113
&action.AddCollection{
114114
SDL: `type User {
115115
name: String
116-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
116+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
117117
}`,
118118
},
119119
// x is nearest to the query [1,0,0], xy is second nearest.
@@ -151,7 +151,7 @@ func TestQuerySimple_WithSimilarityOnVectorIndex_IsMagnitudeInvariant(t *testing
151151
&action.AddCollection{
152152
SDL: `type User {
153153
name: String
154-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
154+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
155155
}`,
156156
},
157157
&action.AddDoc{DocMap: map[string]any{"name": "unit", "vector": []float32{1, 0, 0}}},
@@ -193,7 +193,7 @@ func TestQuerySimple_WithSimilarityOnVectorIndex_AscendingOrderFullScans(t *test
193193
&action.AddCollection{
194194
SDL: `type User {
195195
name: String
196-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
196+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
197197
}`,
198198
},
199199
}, append(docs,
@@ -235,7 +235,7 @@ func TestQuerySimple_WithSimilarityOnVectorIndex_RespectsOffset(t *testing.T) {
235235
&action.AddCollection{
236236
SDL: `type User {
237237
name: String
238-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
238+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
239239
}`,
240240
},
241241
&action.AddDoc{DocMap: map[string]any{"name": "x", "vector": []float32{1, 0, 0}}},
@@ -270,7 +270,7 @@ func TestQuerySimple_WithSimilarityOnVectorIndex_WrongLengthQueryErrors(t *testi
270270
&action.AddCollection{
271271
SDL: `type User {
272272
name: String
273-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
273+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
274274
}`,
275275
},
276276
&action.AddDoc{DocMap: map[string]any{"name": "x", "vector": []float32{1, 0, 0}}},
@@ -299,7 +299,7 @@ func TestQuerySimple_WithSimilarityOnVectorIndex_NoOrderDoesNotUseIndex(t *testi
299299
&action.AddCollection{
300300
SDL: `type User {
301301
name: String
302-
vector: [Float32!] @vectorIndex(dimensions: 3, HNSW: {metric: COSINE})
302+
vector: [Float32!] @index(vector: {dimensions: 3, hnsw: {metric: COSINE}})
303303
}`,
304304
},
305305
&action.AddDoc{DocMap: map[string]any{"name": "x", "vector": []float32{1, 0, 0}}},

0 commit comments

Comments
 (0)