This repository was archived by the owner on Sep 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinsert_test.go
More file actions
325 lines (280 loc) · 8.34 KB
/
insert_test.go
File metadata and controls
325 lines (280 loc) · 8.34 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
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/
package modusgraph_test
import (
"context"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/hypermodeinc/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://") {
require.IsType(t, &modusgraph.UniqueError{}, err, "Error should be a UniqueError")
require.Equal(t, uid, err.(*modusgraph.UniqueError).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 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)
}
}