forked from hypermodeinc/modusGraph
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslice_test.go
More file actions
392 lines (339 loc) · 11.1 KB
/
slice_test.go
File metadata and controls
392 lines (339 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package modusgraph_test
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Genre is a simple struct used as a value-type slice element.
type Genre struct {
Name string `json:"name,omitempty" dgraph:"index=exact"`
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
// MovieWithValueSlice uses []Genre (value-type slice) for its genres field.
type MovieWithValueSlice struct {
Title string `json:"title,omitempty" dgraph:"index=exact"`
Genres []Genre `json:"genres,omitempty"`
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
// MovieWithPointerSlice uses []*Genre (pointer-type slice) for its genres field.
type MovieWithPointerSlice struct {
Title string `json:"title,omitempty" dgraph:"index=exact"`
Genres []*Genre `json:"genres,omitempty"`
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
// TestValueTypeSliceInsertAndQuery tests that []T (value-type slice) fields
// round-trip correctly through insert and query.
func TestValueTypeSliceInsertAndQuery(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "ValueTypeSliceWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "ValueTypeSliceWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skip {
t.Skipf("Skipping %s: MODUSGRAPH_TEST_ADDR not set", tc.name)
return
}
client, cleanup := CreateTestClient(t, tc.uri)
defer cleanup()
ctx := context.Background()
// Insert a movie with value-type []Genre slice
movie := MovieWithValueSlice{
Title: "The Matrix",
Genres: []Genre{
{Name: "Action"},
{Name: "Sci-Fi"},
},
}
err := client.Insert(ctx, &movie)
require.NoError(t, err, "Insert with []Genre should succeed")
require.NotEmpty(t, movie.UID, "Movie UID should be assigned")
// Verify that nested Genre UIDs were assigned
for i, g := range movie.Genres {
require.NotEmpty(t, g.UID, "Genre[%d] UID should be assigned", i)
}
// Query the movie back and verify genres are populated
var retrieved MovieWithValueSlice
err = client.Get(ctx, &retrieved, movie.UID)
require.NoError(t, err, "Get should succeed")
require.Equal(t, "The Matrix", retrieved.Title, "Title should match")
require.Len(t, retrieved.Genres, 2, "Should have 2 genres")
genreNames := []string{retrieved.Genres[0].Name, retrieved.Genres[1].Name}
assert.ElementsMatch(t, []string{"Action", "Sci-Fi"}, genreNames,
"Genre names should match")
// Also verify via Query
var results []MovieWithValueSlice
err = client.Query(ctx, MovieWithValueSlice{}).
Filter(`eq(title, "The Matrix")`).
Nodes(&results)
require.NoError(t, err, "Query should succeed")
require.Len(t, results, 1, "Should find 1 movie")
require.Len(t, results[0].Genres, 2, "Queried movie should have 2 genres")
})
}
}
// TestPointerTypeSliceInsertAndQuery tests that []*T (pointer-type slice) fields
// round-trip correctly through insert and query (baseline for comparison).
func TestPointerTypeSliceInsertAndQuery(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "PointerTypeSliceWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "PointerTypeSliceWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skip {
t.Skipf("Skipping %s: MODUSGRAPH_TEST_ADDR not set", tc.name)
return
}
client, cleanup := CreateTestClient(t, tc.uri)
defer cleanup()
ctx := context.Background()
// Insert a movie with pointer-type []*Genre slice
movie := MovieWithPointerSlice{
Title: "Inception",
Genres: []*Genre{
{Name: "Thriller"},
{Name: "Sci-Fi"},
},
}
err := client.Insert(ctx, &movie)
require.NoError(t, err, "Insert with []*Genre should succeed")
require.NotEmpty(t, movie.UID, "Movie UID should be assigned")
for i, g := range movie.Genres {
require.NotEmpty(t, g.UID, "Genre[%d] UID should be assigned", i)
}
// Query the movie back and verify genres are populated
var retrieved MovieWithPointerSlice
err = client.Get(ctx, &retrieved, movie.UID)
require.NoError(t, err, "Get should succeed")
require.Equal(t, "Inception", retrieved.Title, "Title should match")
require.Len(t, retrieved.Genres, 2, "Should have 2 genres")
genreNames := []string{retrieved.Genres[0].Name, retrieved.Genres[1].Name}
assert.ElementsMatch(t, []string{"Thriller", "Sci-Fi"}, genreNames,
"Genre names should match")
// Also verify via Query
var results []MovieWithPointerSlice
err = client.Query(ctx, MovieWithPointerSlice{}).
Filter(`eq(title, "Inception")`).
Nodes(&results)
require.NoError(t, err, "Query should succeed")
require.Len(t, results, 1, "Should find 1 movie")
require.Len(t, results[0].Genres, 2, "Queried movie should have 2 genres")
})
}
}
// TestValueTypeSliceParity compares behavior of []T and []*T to confirm they
// produce equivalent results when round-tripped through modusgraph.
func TestValueTypeSliceParity(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "SliceParityWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "SliceParityWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skip {
t.Skipf("Skipping %s: MODUSGRAPH_TEST_ADDR not set", tc.name)
return
}
client, cleanup := CreateTestClient(t, tc.uri)
defer cleanup()
ctx := context.Background()
// Insert via value-type slice
valueMovie := MovieWithValueSlice{
Title: "Value Movie",
Genres: []Genre{
{Name: "Drama"},
{Name: "Comedy"},
{Name: "Romance"},
},
}
err := client.Insert(ctx, &valueMovie)
require.NoError(t, err, "Insert with []Genre should succeed")
require.NotEmpty(t, valueMovie.UID)
// Insert via pointer-type slice
ptrMovie := MovieWithPointerSlice{
Title: "Pointer Movie",
Genres: []*Genre{
{Name: "Horror"},
{Name: "Mystery"},
{Name: "Thriller"},
},
}
err = client.Insert(ctx, &ptrMovie)
require.NoError(t, err, "Insert with []*Genre should succeed")
require.NotEmpty(t, ptrMovie.UID)
// Query back both
var valueResult MovieWithValueSlice
err = client.Get(ctx, &valueResult, valueMovie.UID)
require.NoError(t, err, "Get value movie should succeed")
var ptrResult MovieWithPointerSlice
err = client.Get(ctx, &ptrResult, ptrMovie.UID)
require.NoError(t, err, "Get pointer movie should succeed")
// Both should have 3 genres
require.Len(t, valueResult.Genres, 3,
"Value-type movie should have 3 genres")
require.Len(t, ptrResult.Genres, 3,
"Pointer-type movie should have 3 genres")
// Verify all genre UIDs are non-empty
for i, g := range valueResult.Genres {
assert.NotEmpty(t, g.UID, "Value genre[%d] should have UID", i)
assert.NotEmpty(t, g.Name, "Value genre[%d] should have Name", i)
}
for i, g := range ptrResult.Genres {
assert.NotEmpty(t, g.UID, "Pointer genre[%d] should have UID", i)
assert.NotEmpty(t, g.Name, "Pointer genre[%d] should have Name", i)
}
// Verify the genre names are as expected
valueGenreNames := make([]string, len(valueResult.Genres))
for i, g := range valueResult.Genres {
valueGenreNames[i] = g.Name
}
assert.ElementsMatch(t, []string{"Drama", "Comedy", "Romance"},
valueGenreNames, "Value-type genre names should match")
ptrGenreNames := make([]string, len(ptrResult.Genres))
for i, g := range ptrResult.Genres {
ptrGenreNames[i] = g.Name
}
assert.ElementsMatch(t, []string{"Horror", "Mystery", "Thriller"},
ptrGenreNames, "Pointer-type genre names should match")
})
}
}
// TestValueTypeSliceUpdate tests that updating a struct with []T slice works.
func TestValueTypeSliceUpdate(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "ValueTypeSliceUpdateWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "ValueTypeSliceUpdateWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skip {
t.Skipf("Skipping %s: MODUSGRAPH_TEST_ADDR not set", tc.name)
return
}
client, cleanup := CreateTestClient(t, tc.uri)
defer cleanup()
ctx := context.Background()
// Insert initial movie with value-type genres
movie := MovieWithValueSlice{
Title: "Test Movie",
Genres: []Genre{
{Name: "Action"},
},
}
err := client.Insert(ctx, &movie)
require.NoError(t, err, "Insert should succeed")
require.NotEmpty(t, movie.UID)
// Update: add a new genre
movie.Genres = append(movie.Genres, Genre{Name: "Adventure"})
err = client.Update(ctx, &movie)
require.NoError(t, err, "Update with additional genre should succeed")
// Verify the update
var updated MovieWithValueSlice
err = client.Get(ctx, &updated, movie.UID)
require.NoError(t, err, "Get should succeed after update")
require.Len(t, updated.Genres, 2, "Should have 2 genres after update")
genreNames := make([]string, len(updated.Genres))
for i, g := range updated.Genres {
genreNames[i] = g.Name
}
assert.ElementsMatch(t, []string{"Action", "Adventure"}, genreNames,
"Genre names should match after update")
})
}
}
// TestValueTypeSliceEmpty tests behavior with empty []T slices.
func TestValueTypeSliceEmpty(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "EmptyValueSliceWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "EmptyValueSliceWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if tc.skip {
t.Skipf("Skipping %s: MODUSGRAPH_TEST_ADDR not set", tc.name)
return
}
client, cleanup := CreateTestClient(t, tc.uri)
defer cleanup()
ctx := context.Background()
// Insert a movie with no genres (nil slice)
movie := MovieWithValueSlice{
Title: "No Genre Movie",
}
err := client.Insert(ctx, &movie)
require.NoError(t, err, "Insert with nil genres should succeed")
require.NotEmpty(t, movie.UID)
// Query back
var retrieved MovieWithValueSlice
err = client.Get(ctx, &retrieved, movie.UID)
require.NoError(t, err, "Get should succeed")
require.Equal(t, "No Genre Movie", retrieved.Title)
assert.Empty(t, retrieved.Genres, "Genres should be empty")
})
}
}