forked from matthewmcneely/modusGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_test.go
More file actions
407 lines (347 loc) · 12.5 KB
/
update_test.go
File metadata and controls
407 lines (347 loc) · 12.5 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
/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package modusgraph_test
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestClientUpdate(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "UpdateWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "UpdateWithDgraphURI",
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 Update 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.UID, uid, "UID should match")
entity.Description = "Four score and seven years ago"
err = client.Update(ctx, &entity)
require.NoError(t, err, "Update should succeed")
err = client.Get(ctx, &entity, uid)
require.NoError(t, err, "Get should succeed")
require.Equal(t, entity.Description, "Four score and seven years ago", "Description should match")
entity = TestEntity{
Name: "Test Entity 2",
}
err = client.Insert(ctx, &entity)
require.NoError(t, err, "Insert should succeed")
require.NotEmpty(t, entity.UID, "UID should be assigned")
require.NotEqual(t, entity.UID, uid, "UID should be different")
entity.Name = "Test Entity"
err = client.Update(ctx, &entity)
require.Error(t, err, "Update should fail because Name is unique")
})
}
}
func TestClientUpdateWithSlices(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "UpdateWithSlicesWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "UpdateWithSlicesWithDgraph",
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()
err := client.DropAll(ctx)
require.NoError(t, err, "DropAll should succeed")
// 1. Test successful batch update
entitiesToInsert := []*TestEntity{
{Name: "Batch Entity 1", Description: "Initial description 1"},
{Name: "Batch Entity 2", Description: "Initial description 2"},
{Name: "Batch Entity 3", Description: "Initial description 3"},
}
err = client.Insert(ctx, entitiesToInsert)
require.NoError(t, err, "Batch insert should succeed")
for i, entity := range entitiesToInsert {
require.NotEmpty(t, entity.UID, "Entity %d should have a UID", i)
}
for i := range entitiesToInsert {
entitiesToInsert[i].Description = fmt.Sprintf("Updated description %d", i+1)
}
err = client.Update(ctx, entitiesToInsert)
require.NoError(t, err, "Batch update should succeed")
for i, entity := range entitiesToInsert {
var fetched TestEntity
err = client.Get(ctx, &fetched, entity.UID)
require.NoError(t, err, "Get should succeed for entity %d", i)
require.Equal(t, fmt.Sprintf("Updated description %d", i+1), fetched.Description,
"Description should be updated for entity %d", i)
}
// 2. Test in-memory duplicate detection in slice
duplicateEntities := []*TestEntity{
{UID: entitiesToInsert[0].UID, Name: "Duplicate Name"},
{UID: entitiesToInsert[1].UID, Name: "Duplicate Name"}, // Same name as first entity
}
err = client.Update(ctx, &duplicateEntities)
require.Error(t, err, "Update should fail due to duplicate names in the slice")
// 3. Test database-level unique constraint violation
uniqueName := "Unique Entity Name"
newEntity := TestEntity{Name: uniqueName}
err = client.Insert(ctx, &newEntity)
require.NoError(t, err, "Insert should succeed for new entity")
conflictEntity := TestEntity{
UID: entitiesToInsert[0].UID,
Name: uniqueName, // This will conflict with newEntity
}
err = client.Update(ctx, &conflictEntity)
require.Error(t, err, "Update should fail due to unique constraint violation")
// 4. Test mixed batch with some valid and some invalid updates
mixedEntities := []*TestEntity{
{UID: entitiesToInsert[0].UID, Description: "This update would be valid"}, // Valid
{UID: entitiesToInsert[1].UID, Name: uniqueName}, // Invalid - name conflict
}
err = client.Update(ctx, mixedEntities)
require.Error(t, err, "Update should fail due to unique constraint violation in batch")
})
}
}
type EmbeddedNodeType struct {
Name string `json:"node.name,omitempty" dgraph:"predicate=node.name"`
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
type AllTypes struct {
Name string `json:"name,omitempty" dgraph:"index=exact"`
Age int `json:"age,omitempty" dgraph:"index=int"`
Bool bool `json:"bool,omitempty"`
Value float64 `json:"value,omitempty" dgraph:"index=float"`
CreatedAt time.Time `json:"createdAt,omitzero" dgraph:"index=day"`
Strings []string `json:"strings,omitempty" dgraph:"index=term"`
Node *EmbeddedNodeType `json:"node,omitempty"`
Nodes []*EmbeddedNodeType `json:"nodes,omitempty"`
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
func TestClientUpdateAllTypes(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "UpdateWithAllTypes",
uri: "file://" + GetTempDir(t),
},
{
name: "UpdateWithAllTypesWithDgraph",
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 := AllTypes{
Name: "Test Entity",
Age: 42,
Value: 3.14,
CreatedAt: time.Now(),
Strings: []string{"one", "two", "three"},
Bool: true,
Node: &EmbeddedNodeType{
Name: "Test Node",
},
Nodes: []*EmbeddedNodeType{
{
Name: "Node In Array, 1",
},
{
Name: "Node In Array, 2",
},
},
}
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 = AllTypes{}
err = client.Get(ctx, &entity, uid)
require.NoError(t, err, "Get should succeed")
require.Equal(t, "Test Entity", entity.Name, "Name should match")
require.Equal(t, 42, entity.Age, "Age should match")
require.Equal(t, 3.14, entity.Value, "Value should match")
require.Equal(t, entity.CreatedAt, entity.CreatedAt, "CreatedAt should match")
require.Equal(t, []string{"one", "two", "three"}, entity.Strings, "Strings should match")
require.Equal(t, true, entity.Bool, "Bool should match")
require.Equal(t, "Test Node", entity.Node.Name, "Node Name should match")
require.NotEmpty(t, entity.Node.UID, "Node UID should be assigned")
nodeNames := []string{entity.Nodes[0].Name, entity.Nodes[1].Name}
require.ElementsMatch(t, []string{"Node In Array, 1", "Node In Array, 2"},
nodeNames, "Node In Array Names should match")
entity.Age = 43
entity.Node.Name = "Updated Node"
entity.Nodes[0].Name = "Updated Node In Array, 1"
entity.Nodes[1].Name = "Updated Node In Array, 2"
err = client.Update(ctx, &entity)
require.NoError(t, err, "Update should succeed")
entity = AllTypes{}
err = client.Get(ctx, &entity, uid)
require.NoError(t, err, "Get should succeed")
require.Equal(t, 43, entity.Age, "Age should match")
require.Equal(t, "Updated Node", entity.Node.Name, "Node Name should match")
nodeNames = []string{entity.Nodes[0].Name, entity.Nodes[1].Name}
require.ElementsMatch(t, []string{"Updated Node In Array, 1", "Updated Node In Array, 2"},
nodeNames, "Node In Array Names should match")
})
}
}
type UniqueEmbeddedNode struct {
Email string `json:"email,omitempty" dgraph:"unique"`
Name string `json:"name,omitempty"`
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
type ParentWithUniqueEmbedded struct {
Name string `json:"name,omitempty"`
Node *UniqueEmbeddedNode `json:"node,omitempty"`
Nodes []*UniqueEmbeddedNode `json:"nodes,omitempty"`
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
func TestClientUpdateWithUniqueEmbedded(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "UpdateWithUniqueEmbeddedWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "UpdateWithUniqueEmbeddedWithDgraph",
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()
// 1. Test unique constraint violation in embedded node
parent1 := ParentWithUniqueEmbedded{
Name: "Parent 1",
Node: &UniqueEmbeddedNode{
Email: "test@example.com",
Name: "Test Node",
},
}
err := client.Insert(ctx, &parent1)
require.NoError(t, err, "Insert should succeed for parent1")
require.NotEmpty(t, parent1.UID, "Parent1 UID should be assigned")
require.NotEmpty(t, parent1.Node.UID, "Node UID should be assigned")
parent2 := ParentWithUniqueEmbedded{
Name: "Parent 2",
Node: &UniqueEmbeddedNode{
Email: "test2@example.com",
Name: "Test Node 2",
},
}
err = client.Insert(ctx, &parent2)
require.NoError(t, err, "Insert should succeed for parent2")
require.NotEmpty(t, parent2.UID, "Parent2 UID should be assigned")
// Try to update parent2's node to use the same email as parent1's node
parent2.Node.Email = "test@example.com"
err = client.Update(ctx, &parent2)
require.Error(t, err, "Update should fail due to unique constraint violation in embedded node")
// 2. Test unique constraint violation in embedded node array
parent3 := ParentWithUniqueEmbedded{
Name: "Parent 3",
Nodes: []*UniqueEmbeddedNode{
{Email: "node1@example.com", Name: "Node 1"},
{Email: "node2@example.com", Name: "Node 2"},
},
}
err = client.Insert(ctx, &parent3)
require.NoError(t, err, "Insert should succeed for parent3")
require.NotEmpty(t, parent3.Nodes[0].UID, "Nodes[0] UID should be assigned")
require.NotEmpty(t, parent3.Nodes[1].UID, "Nodes[1] UID should be assigned")
t.Logf("After insert: Nodes[0].UID=%s, Nodes[1].UID=%s", parent3.Nodes[0].UID, parent3.Nodes[1].UID)
// Try to update parent3's nodes to have duplicate emails
parent3.Nodes[1].Email = "node1@example.com"
err = client.Update(ctx, &parent3)
require.Error(t, err, "Update should fail due to duplicate emails in embedded node array")
// 3. Test valid update of embedded nodes
updatedNodeUID := parent3.Nodes[1].UID // Save UID before update
parent3.Nodes[1].Email = "node3@example.com"
parent3.Nodes[1].Name = "Updated Node 2"
err = client.Update(ctx, &parent3)
require.NoError(t, err, "Update should succeed with valid embedded node changes")
// Verify the update - find node by UID since Dgraph doesn't guarantee array order
updated := ParentWithUniqueEmbedded{UID: parent3.UID}
err = client.Get(ctx, &updated, parent3.UID)
require.NoError(t, err, "Get should succeed")
var updatedNode *UniqueEmbeddedNode
for _, n := range updated.Nodes {
if n.UID == updatedNodeUID {
updatedNode = n
break
}
}
require.NotNil(t, updatedNode, "Updated node should be found by UID")
require.Equal(t, "node3@example.com", updatedNode.Email, "Email should be updated")
require.Equal(t, "Updated Node 2", updatedNode.Name, "Name should be updated")
})
}
}