forked from matthewmcneely/modusGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupsert_test.go
More file actions
352 lines (295 loc) · 12.2 KB
/
upsert_test.go
File metadata and controls
352 lines (295 loc) · 12.2 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
/*
* SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
package modusgraph_test
import (
"context"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type UpsertTestEntity struct {
Name string `json:"name,omitempty" dgraph:"index=exact upsert"`
AnotherName string `json:"anotherName,omitempty" dgraph:"index=exact upsert"`
Description string `json:"description,omitempty" dgraph:"index=term"`
CreatedAt time.Time `json:"createdAt,omitzero"`
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
func TestClientUpsert(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()
t.Run("basic upsert", func(t *testing.T) {
entity := UpsertTestEntity{
Name: "Test Entity", // This is the upsert field
Description: "This is a test entity for the Upsert method",
CreatedAt: time.Date(2021, 6, 9, 17, 22, 33, 0, time.UTC),
}
ctx := context.Background()
err := client.Upsert(ctx, &entity)
require.NoError(t, err, "Upsert 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, "Test Entity", entity.Name, "Name should match")
require.Equal(t, "This is a test entity for the Upsert method", entity.Description, "Description should match")
newTime := time.Now().UTC().Truncate(time.Second)
entity = UpsertTestEntity{
Name: "Test Entity", // This is the upsert field
Description: "Updated description",
CreatedAt: newTime,
}
err = client.Upsert(ctx, &entity)
require.NoError(t, err, "Upsert should succeed")
uid = entity.UID
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, "Updated description", entity.Description, "Description should match")
require.Equal(t, newTime, entity.CreatedAt, "CreatedAt should match")
var entities []UpsertTestEntity
err = client.Query(ctx, UpsertTestEntity{}).Nodes(&entities)
require.NoError(t, err, "Query should succeed")
require.Len(t, entities, 1, "There should only be one entity")
})
t.Run("upsert with predicate", func(t *testing.T) {
ctx := context.Background()
require.NoError(t, client.DropAll(ctx), "Drop all should succeed")
entity := UpsertTestEntity{
AnotherName: "Test Entity", // This is another upsert field, we have to define it to the call to upsert
Description: "This is a test entity for the Upsert method",
CreatedAt: time.Date(2021, 6, 9, 17, 22, 33, 0, time.UTC),
}
err := client.Upsert(ctx, &entity, "anotherName")
require.NoError(t, err, "Upsert 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, "Test Entity", entity.AnotherName, "AnotherName should match")
require.Equal(t, "This is a test entity for the Upsert method", entity.Description, "Description should match")
newTime := time.Now().UTC().Truncate(time.Second)
entity = UpsertTestEntity{
AnotherName: "Test Entity",
Description: "Updated description",
CreatedAt: newTime,
}
err = client.Upsert(ctx, &entity, "anotherName")
require.NoError(t, err, "Upsert should succeed")
uid = entity.UID
err = client.Get(ctx, &entity, uid)
require.NoError(t, err, "Get should succeed")
require.Equal(t, "Test Entity", entity.AnotherName, "AnotherName should match")
require.Equal(t, "Updated description", entity.Description, "Description should match")
require.Equal(t, newTime, entity.CreatedAt, "CreatedAt should match")
var entities []UpsertTestEntity
err = client.Query(ctx, UpsertTestEntity{}).Nodes(&entities)
require.NoError(t, err, "Query should succeed")
require.Len(t, entities, 1, "There should only be one entity")
})
})
}
}
func TestClientUpsertSlice(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "UpsertSliceWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "UpsertSliceWithDgraphURI",
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
}
if strings.HasPrefix(tc.uri, "dgraph://") {
t.Skipf("Skipping %s: Dgraph URI not supported for upserting slices", tc.name)
return
}
client, cleanup := CreateTestClient(t, tc.uri)
defer cleanup()
ctx := context.Background()
require.NoError(t, client.DropAll(ctx), "Drop all should succeed")
entities := []*UpsertTestEntity{
{
Name: "Test Entity 1",
Description: "This is a test entity for the Upsert method 1",
CreatedAt: time.Date(2021, 6, 9, 17, 22, 33, 0, time.UTC),
},
{
Name: "Test Entity 2",
Description: "This is a test entity for the Upsert method 2",
CreatedAt: time.Date(2021, 6, 9, 17, 22, 34, 0, time.UTC),
},
}
err := client.Upsert(ctx, &entities) // Here, no need to pass address of entities, but we handle it
require.NoError(t, err, "Upsert should succeed")
var entities2 []UpsertTestEntity
err = client.Query(ctx, UpsertTestEntity{}).Nodes(&entities2)
require.NoError(t, err, "Query should succeed")
require.Len(t, entities2, 2, "There should be two entities")
findMatchingEntity := func(entities []UpsertTestEntity, name string) *UpsertTestEntity {
for i := range entities {
if entities[i].Name == name {
return &entities[i]
}
}
return nil
}
// Check first entity
entity1 := findMatchingEntity(entities2, entities[0].Name)
require.NotNil(t, entity1, "Should find entity with name %s", entities[0].Name)
require.Equal(t, entities[0].Description, entity1.Description, "Description should match")
require.Equal(t, entities[0].CreatedAt, entity1.CreatedAt, "CreatedAt should match")
// Check second entity
entity2 := findMatchingEntity(entities2, entities[1].Name)
require.NotNil(t, entity2, "Should find entity with name %s", entities[1].Name)
require.Equal(t, entities[1].Description, entity2.Description, "Description should match")
require.Equal(t, entities[1].CreatedAt, entity2.CreatedAt, "CreatedAt should match")
entities[0].Name = "Test Entity 1"
entities[0].Description = "Updated description"
entities[1].Name = "Test Entity 2"
entities[1].Description = "Updated description"
err = client.Upsert(ctx, entities)
require.NoError(t, err, "Upsert should succeed")
var entities3 []UpsertTestEntity
err = client.Query(ctx, UpsertTestEntity{}).Nodes(&entities3)
require.NoError(t, err, "Query should succeed")
require.Len(t, entities3, 2, "There should be two entities")
entity1 = findMatchingEntity(entities3, entities[0].Name)
require.NotNil(t, entity1, "Should find entity with name %s", entities[0].Name)
require.Equal(t, entities[0].Description, entity1.Description, "Description should match")
require.Equal(t, entities[0].CreatedAt, entity1.CreatedAt, "CreatedAt should match")
entity2 = findMatchingEntity(entities3, entities[1].Name)
require.NotNil(t, entity2, "Should find entity with name %s", entities[1].Name)
require.Equal(t, entities[1].Description, entity2.Description, "Description should match")
require.Equal(t, entities[1].CreatedAt, entity2.CreatedAt, "CreatedAt should match")
})
}
}
type EmbeddedUpsertEntity struct {
Name string `json:"name,omitempty" dgraph:"index=exact upsert"`
Description string `json:"description,omitempty" dgraph:"index=term"`
CreatedAt time.Time `json:"createdAt,omitzero"`
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
type OuterUpsertEntity struct {
Title string `json:"title,omitempty"`
Entity *EmbeddedUpsertEntity `json:"entity"`
UID string `json:"uid,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
func TestEmbeddedUpsert(t *testing.T) {
testCases := []struct {
name string
uri string
skip bool
}{
{
name: "EmbeddedUpsertWithFileURI",
uri: "file://" + GetTempDir(t),
},
{
name: "EmbeddedUpsertWithDgraphURI",
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()
// First upsert - creates new entity with embedded entity
firstEntity := OuterUpsertEntity{
Title: "First Title",
Entity: &EmbeddedUpsertEntity{
Name: "Unique Embedded Name",
Description: "First description",
CreatedAt: time.Date(2021, 6, 9, 17, 22, 33, 0, time.UTC),
},
}
err := client.Upsert(ctx, &firstEntity)
require.NoError(t, err, "First upsert should succeed")
require.NotEmpty(t, firstEntity.UID, "Outer UID should be assigned")
require.NotEmpty(t, firstEntity.Entity.UID, "Embedded UID should be assigned")
firstUID := firstEntity.UID
firstEmbeddedUID := firstEntity.Entity.UID
// Verify first entity was created correctly
retrieved := OuterUpsertEntity{}
err = client.Get(ctx, &retrieved, firstUID)
require.NoError(t, err, "Get should succeed")
require.Equal(t, "First Title", retrieved.Title, "Title should match")
require.Equal(t, "Unique Embedded Name", retrieved.Entity.Name, "Embedded name should match")
require.Equal(t, "First description", retrieved.Entity.Description, "Embedded description should match")
// Second upsert - embedded entity with same upsert key should be UPDATED, not created
secondEntity := OuterUpsertEntity{
Title: "Second Title",
Entity: &EmbeddedUpsertEntity{
Name: "Unique Embedded Name", // Same name - should trigger upsert on embedded entity
Description: "Updated description",
CreatedAt: time.Date(2022, 7, 10, 18, 30, 45, 0, time.UTC),
},
}
err = client.Upsert(ctx, &secondEntity)
require.NoError(t, err, "Second upsert should succeed")
require.NotEmpty(t, secondEntity.UID, "Outer UID should be assigned")
require.NotEmpty(t, secondEntity.Entity.UID, "Embedded UID should be assigned")
// The embedded entity should have the SAME UID (upserted, not created new)
require.Equal(t, firstEmbeddedUID, secondEntity.Entity.UID, "Embedded UID should be the same (upserted)")
// Verify only ONE embedded entity exists (it was upserted, not duplicated)
var embeddedEntities []EmbeddedUpsertEntity
err = client.Query(ctx, EmbeddedUpsertEntity{}).Nodes(&embeddedEntities)
require.NoError(t, err, "Query embedded entities should succeed")
require.Len(t, embeddedEntities, 1, "There should be only one embedded entity (upserted)")
// Verify the embedded entity has the updated description
require.Equal(t, "Unique Embedded Name", embeddedEntities[0].Name, "Name should match")
require.Equal(t, "Updated description", embeddedEntities[0].Description, "Description should be updated")
// Verify there are two outer entities (they have different titles, so not upserted)
var outerEntities []OuterUpsertEntity
err = client.Query(ctx, OuterUpsertEntity{}).Nodes(&outerEntities)
require.NoError(t, err, "Query outer entities should succeed")
require.Len(t, outerEntities, 2, "There should be two outer entities")
})
}
}