forked from hypermodeinc/modusGraph
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinsert_test.go
More file actions
686 lines (599 loc) · 18.1 KB
/
insert_test.go
File metadata and controls
686 lines (599 loc) · 18.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package modusgraph_test
import (
"context"
"errors"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/matthewmcneely/modusgraph"
)
// TestEntity is a test struct used for Insert tests
type TestEntity struct {
UID string `json:"uid,omitempty"`
Name string `json:"name,omitempty" dgraph:"index=term,exact unique"`
Description string `json:"description,omitempty" dgraph:"index=term"`
CreatedAt time.Time `json:"createdAt,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
func TestClientInsert(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "InsertWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "InsertWithDgraphURI",
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()
entity := TestEntity{
Name: "Test Entity",
Description: "This is a test entity for the Insert method",
CreatedAt: time.Now(),
}
ctx := context.Background()
err := client.Insert(ctx, &entity)
require.NoError(t, err, "Insert should succeed")
require.NotEmpty(t, entity.UID, "UID should be assigned")
uid := entity.UID
err = client.Get(ctx, &entity, uid)
require.NoError(t, err, "Get should succeed")
require.Equal(t, entity.Name, "Test Entity", "Name should match")
require.Equal(t, entity.Description, "This is a test entity for the Insert method", "Description should match")
// Try to insert the same entity again, should fail due to unique constraint
entity = TestEntity{
Name: "Test Entity",
Description: "This is a test entity for the Insert method 2",
CreatedAt: time.Now(),
}
err = client.Insert(ctx, &entity)
require.Error(t, err, "Insert should fail")
if strings.HasPrefix(tc.uri, "file://") {
var uniqueErr *modusgraph.UniqueError
require.True(t, errors.As(err, &uniqueErr), "Error should be a UniqueError")
require.Equal(t, uid, uniqueErr.UID, "UID should match")
}
var entities []TestEntity
err = client.Query(ctx, TestEntity{}).Nodes(&entities)
require.NoError(t, err, "Query should succeed")
require.Len(t, entities, 1, "There should only be one entity")
})
}
}
type OuterTestEntity struct {
Name string `json:"name,omitempty" dgraph:"index=exact unique"`
Entity *TestEntity `json:"entity"`
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
func TestEmbeddedInsert(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "InsertWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "InsertWithDgraphURI",
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()
timestamp := time.Now().UTC().Truncate(time.Second)
entity := OuterTestEntity{
Name: "Test Outer Entity",
Entity: &TestEntity{
Name: "Test Inner Entity",
Description: "This is a test entity for the Insert method",
CreatedAt: timestamp,
},
}
ctx := context.Background()
err := client.Insert(ctx, &entity)
require.NoError(t, err, "Insert should succeed")
require.NotEmpty(t, entity.UID, "UID should be assigned")
uid := entity.UID
entity = OuterTestEntity{}
err = client.Get(ctx, &entity, uid)
require.NoError(t, err, "Get should succeed")
require.Equal(t, "Test Outer Entity", entity.Name, "Name should match")
require.Equal(t, "Test Inner Entity", entity.Entity.Name, "Entity.Name should match")
require.Equal(t, "This is a test entity for the Insert method",
entity.Entity.Description, "Entity.Description should match")
require.Equal(t, timestamp, entity.Entity.CreatedAt, "Entity.CreatedAt should match")
})
}
}
func TestEmbeddedUniqueConstraintViolation(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "EmbeddedUniqueViolationWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "EmbeddedUniqueViolationWithDgraphURI",
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 first entity with embedded unique entity
firstEntity := OuterTestEntity{
Name: "First Outer Entity",
Entity: &TestEntity{
Name: "Unique Inner Name",
Description: "First description",
CreatedAt: time.Now(),
},
}
err := client.Insert(ctx, &firstEntity)
require.NoError(t, err, "First insert should succeed")
require.NotEmpty(t, firstEntity.UID, "First UID should be assigned")
require.NotEmpty(t, firstEntity.Entity.UID, "Embedded entity UID should be assigned")
// Try to insert second entity with same embedded unique name
secondEntity := OuterTestEntity{
Name: "Second Outer Entity",
Entity: &TestEntity{
Name: "Unique Inner Name", // Same name - should violate unique constraint
Description: "Second description",
CreatedAt: time.Now(),
},
}
err = client.Insert(ctx, &secondEntity)
require.Error(t, err, "Second insert should fail due to unique constraint violation in embedded entity")
// Verify the error is a UniqueError
var uniqueErr *modusgraph.UniqueError
require.True(t, errors.As(err, &uniqueErr), "Error should be a UniqueError")
// UID is only available for file URIs (embedded engine checks the database)
// Dgraph errors don't include the UID of the existing entity
if strings.HasPrefix(tc.uri, "file://") {
require.Equal(t, firstEntity.Entity.UID, uniqueErr.UID, "UID should match the first embedded entity")
}
// Verify only the first entity exists
var entities []OuterTestEntity
err = client.Query(ctx, OuterTestEntity{}).Nodes(&entities)
require.NoError(t, err, "Query should succeed")
require.Len(t, entities, 1, "There should only be one outer entity")
require.Equal(t, "First Outer Entity", entities[0].Name, "Should be the first entity")
require.Equal(t, "Unique Inner Name", entities[0].Entity.Name, "Embedded entity name should match")
})
}
}
func TestClientInsertMultipleEntities(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "InsertMultipleWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "InsertMultipleWithDgraphURI",
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()
// Note the `*TestEntity`, the elements in the slice must be pointers
entities := []*TestEntity{
{
Name: "Entity 1",
Description: "First test entity",
CreatedAt: time.Now().Add(-1 * time.Hour),
},
{
Name: "Entity 2",
Description: "Second test entity",
CreatedAt: time.Now(),
},
}
ctx := context.Background()
err := client.Insert(ctx, entities)
require.NoError(t, err, "Insert should succeed")
var result []TestEntity
err = client.Query(ctx, TestEntity{}).OrderDesc("createdAt").First(1).Nodes(&result)
require.NoError(t, err, "Query should succeed")
assert.Len(t, result, 1, "Should have found one entity")
assert.Equal(t, entities[1].Name, result[0].Name, "Name should match")
})
}
}
type Person struct {
Name string `json:"name,omitempty" dgraph:"index=term"`
Friends []*Person `json:"friends,omitempty"`
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
func TestDepthQuery(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "InsertWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "InsertWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
createPerson := func() Person {
return Person{
Name: "Alice",
Friends: []*Person{
{
Name: "Bob",
Friends: []*Person{
{
Name: "Charles",
},
{
Name: "David",
Friends: []*Person{
{
Name: "Eve",
Friends: []*Person{
{
Name: "Frank",
},
},
},
{
Name: "George",
},
},
},
},
},
},
}
}
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()
person := createPerson()
err := client.Insert(ctx, &person)
require.NoError(t, err, "Insert should succeed")
var result []Person
err = client.Query(ctx, Person{}).Filter(`eq(name, "Alice")`).Nodes(&result)
require.NoError(t, err, "Query should succeed")
assert.Equal(t, person.Name, result[0].Name, "Name should match")
verifyPersonStructure(t, &person, &result[0])
})
}
}
func verifyPersonStructure(t *testing.T, expected *Person, actual *Person) {
t.Helper()
require.NotNil(t, actual, "Person should not be nil")
assert.Equal(t, expected.Name, actual.Name, "Name should match")
if expected.Friends == nil {
assert.Empty(t, actual.Friends, "Should have no friends")
return
}
require.Len(t, actual.Friends, len(expected.Friends),
"%s should have %d friends", expected.Name, len(expected.Friends))
// Create a map of expected friends by name for easier lookup
expectedFriends := make(map[string]*Person)
for _, friend := range expected.Friends {
expectedFriends[friend.Name] = friend
}
// Verify each actual friend
for _, actualFriend := range actual.Friends {
expectedFriend, ok := expectedFriends[actualFriend.Name]
require.True(t, ok, "%s should have a friend named %s",
expected.Name, actualFriend.Name)
// Recursively verify this friend's structure
verifyPersonStructure(t, expectedFriend, actualFriend)
}
}
func TestClientInsertRaw(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "InsertWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "InsertWithDgraphURI",
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()
entity := TestEntity{
Name: "Test Entity",
Description: "This is a test entity for the Insert method",
CreatedAt: time.Now(),
UID: "_:foo",
}
ctx := context.Background()
err := client.InsertRaw(ctx, &entity)
require.NoError(t, err, "Insert should succeed")
require.NotEmpty(t, entity.UID, "UID should be assigned")
uid := entity.UID
err = client.Get(ctx, &entity, uid)
require.NoError(t, err, "Get should succeed")
require.Equal(t, entity.Name, "Test Entity", "Name should match")
require.Equal(t, entity.Description, "This is a test entity for the Insert method", "Description should match")
var entities []TestEntity
err = client.Query(ctx, TestEntity{}).Nodes(&entities)
require.NoError(t, err, "Query should succeed")
require.Len(t, entities, 1, "There should only be one entity")
require.Equal(t, "Test Entity", entities[0].Name, "Name should match")
})
}
}
func TestDepthQueryInsertRaw(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "InsertWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "InsertWithDgraphURI",
uri: "dgraph://" + os.Getenv("MODUSGRAPH_TEST_ADDR"),
skip: os.Getenv("MODUSGRAPH_TEST_ADDR") == "",
},
}
createPerson := func() Person {
return Person{
Name: "Alice",
UID: "_:alice",
Friends: []*Person{
{
Name: "Bob",
UID: "_:bob",
Friends: []*Person{
{
Name: "Charles",
UID: "_:charles",
},
{
Name: "David",
UID: "_:david",
Friends: []*Person{
{
Name: "Eve",
UID: "_:eve",
Friends: []*Person{
{
Name: "Frank",
UID: "_:frank",
},
},
},
{
Name: "George",
UID: "_:george",
},
},
},
},
},
},
}
}
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()
person := createPerson()
err := client.InsertRaw(ctx, &person)
require.NoError(t, err, "Insert should succeed")
var result []Person
err = client.Query(ctx, Person{}).Filter(`eq(name, "Alice")`).Nodes(&result)
require.NoError(t, err, "Query should succeed")
assert.Equal(t, person.Name, result[0].Name, "Name should match")
verifyPersonStructure(t, &person, &result[0])
})
}
}
func TestClientInsertRawMultipleEntities(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "InsertMultipleWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "InsertMultipleWithDgraphURI",
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()
// Note the `*TestEntity`, the elements in the slice must be pointers
entities := []*TestEntity{
{
Name: "Entity 1",
UID: "_:entity1",
Description: "First test entity",
CreatedAt: time.Now().Add(-1 * time.Hour),
},
{
Name: "Entity 2",
UID: "_:entity2",
Description: "Second test entity",
CreatedAt: time.Now(),
},
}
ctx := context.Background()
err := client.InsertRaw(ctx, entities)
require.NoError(t, err, "Insert should succeed")
var result []TestEntity
err = client.Query(ctx, TestEntity{}).OrderDesc("createdAt").First(1).Nodes(&result)
require.NoError(t, err, "Query should succeed")
assert.Len(t, result, 1, "Should have found one entity")
assert.Equal(t, entities[1].Name, result[0].Name, "Name should match")
})
}
}
func TestClientInsertMultipleEntitiesWithEmbeddedStructs(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "InsertMultipleEmbeddedWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "InsertMultipleEmbeddedWithDgraphURI",
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()
timestamp := time.Now().UTC().Truncate(time.Second)
// Note: elements in the slice must be pointers
entities := []*OuterTestEntity{
{
Name: "Outer Entity 1",
Entity: &TestEntity{
Name: "Inner Entity 1",
Description: "First embedded entity",
CreatedAt: timestamp.Add(-1 * time.Hour),
},
},
{
Name: "Outer Entity 2",
Entity: &TestEntity{
Name: "Inner Entity 2",
Description: "Second embedded entity",
CreatedAt: timestamp,
},
},
{
Name: "Outer Entity 3",
Entity: &TestEntity{
Name: "Inner Entity 3",
Description: "Third embedded entity",
CreatedAt: timestamp.Add(1 * time.Hour),
},
},
}
ctx := context.Background()
err := client.Insert(ctx, entities)
require.NoError(t, err, "Insert should succeed")
// Verify all entities got UIDs assigned
for i, entity := range entities {
require.NotEmpty(t, entity.UID, "UID should be assigned for entity %d", i)
require.NotEmpty(t, entity.Entity.UID, "Embedded entity UID should be assigned for entity %d", i)
}
// Verify we can retrieve each entity with its embedded struct
for _, original := range entities {
var retrieved OuterTestEntity
err = client.Get(ctx, &retrieved, original.UID)
require.NoError(t, err, "Get should succeed for %s", original.Name)
assert.Equal(t, original.Name, retrieved.Name, "Name should match")
require.NotNil(t, retrieved.Entity, "Embedded entity should not be nil")
assert.Equal(t, original.Entity.Name, retrieved.Entity.Name, "Embedded entity name should match")
assert.Equal(t, original.Entity.Description, retrieved.Entity.Description,
"Embedded entity description should match")
}
// Query all outer entities and verify count
var results []OuterTestEntity
err = client.Query(ctx, OuterTestEntity{}).Nodes(&results)
require.NoError(t, err, "Query should succeed")
assert.Len(t, results, 3, "Should have found all three entities")
})
}
}