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 pathupdate_test.go
More file actions
287 lines (243 loc) · 8.57 KB
/
update_test.go
File metadata and controls
287 lines (243 loc) · 8.57 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
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
* 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")
})
}
}