-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconstraint_test.go
More file actions
419 lines (347 loc) · 11.8 KB
/
constraint_test.go
File metadata and controls
419 lines (347 loc) · 11.8 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
package graphdb
import (
"context"
"errors"
"fmt"
"testing"
)
func TestCreateUniqueConstraint_Basic(t *testing.T) {
db := testDB(t)
// Create some nodes first.
id1, err := db.AddNodeWithLabels([]string{"Person"}, Props{"email": "alice@example.com", "name": "Alice"})
if err != nil {
t.Fatal(err)
}
id2, err := db.AddNodeWithLabels([]string{"Person"}, Props{"email": "bob@example.com", "name": "Bob"})
if err != nil {
t.Fatal(err)
}
// Create unique constraint.
if err := db.CreateUniqueConstraint("Person", "email"); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
// Verify constraint is registered.
if !db.HasUniqueConstraint("Person", "email") {
t.Fatal("expected constraint to be registered")
}
// List constraints.
constraints := db.ListUniqueConstraints()
if len(constraints) != 1 {
t.Fatalf("expected 1 constraint, got %d", len(constraints))
}
// Try to create a duplicate — should fail.
_, err = db.AddNodeWithLabels([]string{"Person"}, Props{"email": "alice@example.com", "name": "Alice2"})
if err == nil {
t.Fatal("expected unique constraint violation")
}
if !errors.Is(err, ErrUniqueConstraintViolation) {
t.Fatalf("expected ErrUniqueConstraintViolation, got: %v", err)
}
// Creating with a different email should work.
_, err = db.AddNodeWithLabels([]string{"Person"}, Props{"email": "charlie@example.com", "name": "Charlie"})
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
// FindByUniqueConstraint should find nodes by unique key.
node, err := db.FindByUniqueConstraint("Person", "email", "alice@example.com")
if err != nil {
t.Fatal(err)
}
if node == nil {
t.Fatal("expected to find node")
}
if node.ID != id1 {
t.Fatalf("expected node ID %d, got %d", id1, node.ID)
}
_ = id2 // just verify it was created
}
func TestUniqueConstraint_DuplicateExistingNodes(t *testing.T) {
db := testDB(t)
// Create two nodes with the same email.
_, _ = db.AddNodeWithLabels([]string{"Person"}, Props{"email": "dup@example.com"})
_, _ = db.AddNodeWithLabels([]string{"Person"}, Props{"email": "dup@example.com"})
// Creating a constraint should fail because of existing duplicates.
err := db.CreateUniqueConstraint("Person", "email")
if err == nil {
t.Fatal("expected error due to existing duplicates")
}
if !errors.Is(err, ErrUniqueConstraintViolation) {
t.Fatalf("expected ErrUniqueConstraintViolation, got: %v", err)
}
}
func TestUniqueConstraint_UpdateNodeViolation(t *testing.T) {
db := testDB(t)
_, _ = db.AddNodeWithLabels([]string{"User"}, Props{"username": "alice"})
id2, _ := db.AddNodeWithLabels([]string{"User"}, Props{"username": "bob"})
if err := db.CreateUniqueConstraint("User", "username"); err != nil {
t.Fatal(err)
}
// Updating bob's username to alice should fail.
err := db.UpdateNode(id2, Props{"username": "alice"})
if err == nil {
t.Fatal("expected unique constraint violation")
}
if !errors.Is(err, ErrUniqueConstraintViolation) {
t.Fatalf("expected ErrUniqueConstraintViolation, got: %v", err)
}
// Updating to a unique username should work.
if err := db.UpdateNode(id2, Props{"username": "charlie"}); err != nil {
t.Fatalf("expected no error, got: %v", err)
}
}
func TestUniqueConstraint_DeleteNodeFreesSlot(t *testing.T) {
db := testDB(t)
id1, _ := db.AddNodeWithLabels([]string{"User"}, Props{"username": "alice"})
if err := db.CreateUniqueConstraint("User", "username"); err != nil {
t.Fatal(err)
}
// Delete alice.
if err := db.DeleteNode(id1); err != nil {
t.Fatal(err)
}
// Now we should be able to create another "alice".
_, err := db.AddNodeWithLabels([]string{"User"}, Props{"username": "alice"})
if err != nil {
t.Fatalf("expected no error after deleting original, got: %v", err)
}
}
func TestUniqueConstraint_DropConstraint(t *testing.T) {
db := testDB(t)
_, _ = db.AddNodeWithLabels([]string{"Person"}, Props{"email": "alice@example.com"})
if err := db.CreateUniqueConstraint("Person", "email"); err != nil {
t.Fatal(err)
}
// Drop the constraint.
if err := db.DropUniqueConstraint("Person", "email"); err != nil {
t.Fatal(err)
}
if db.HasUniqueConstraint("Person", "email") {
t.Fatal("expected constraint to be dropped")
}
// Should now allow duplicates.
_, err := db.AddNodeWithLabels([]string{"Person"}, Props{"email": "alice@example.com"})
if err != nil {
t.Fatalf("expected no error after dropping constraint, got: %v", err)
}
}
func TestUniqueConstraint_PersistsAcrossRestart(t *testing.T) {
dir := t.TempDir()
// Open and create constraint.
db1, err := Open(dir, Options{})
if err != nil {
t.Fatal(err)
}
_, _ = db1.AddNodeWithLabels([]string{"Person"}, Props{"email": "alice@example.com"})
if err := db1.CreateUniqueConstraint("Person", "email"); err != nil {
t.Fatal(err)
}
db1.Close()
// Reopen — constraint should be discovered.
db2, err := Open(dir, Options{})
if err != nil {
t.Fatal(err)
}
defer db2.Close()
if !db2.HasUniqueConstraint("Person", "email") {
t.Fatal("expected constraint to persist across restart")
}
// Should still enforce uniqueness.
_, err = db2.AddNodeWithLabels([]string{"Person"}, Props{"email": "alice@example.com"})
if err == nil {
t.Fatal("expected unique constraint violation after restart")
}
}
func TestCypherMerge_Basic(t *testing.T) {
db := testDB(t)
// Create a unique constraint for fast lookups.
if err := db.CreateUniqueConstraint("Person", "email"); err != nil {
t.Fatal(err)
}
// MERGE should create a new node since none exists.
result1, err := db.Cypher(context.Background(), `MERGE (n:Person {email: "alice@example.com"}) RETURN n`)
if err != nil {
t.Fatalf("MERGE create failed: %v", err)
}
if len(result1.Rows) != 1 {
t.Fatalf("expected 1 row, got %d", len(result1.Rows))
}
node1, ok := result1.Rows[0]["n"].(*Node)
if !ok {
t.Fatal("expected *Node in result")
}
// MERGE again should return the SAME node (not create a new one).
result2, err := db.Cypher(context.Background(), `MERGE (n:Person {email: "alice@example.com"}) RETURN n`)
if err != nil {
t.Fatalf("MERGE match failed: %v", err)
}
if len(result2.Rows) != 1 {
t.Fatalf("expected 1 row, got %d", len(result2.Rows))
}
node2, ok := result2.Rows[0]["n"].(*Node)
if !ok {
t.Fatal("expected *Node in result")
}
if node1.ID != node2.ID {
t.Fatalf("expected same node (ID=%d), got different (ID=%d)", node1.ID, node2.ID)
}
// Total node count should be 1.
count := db.NodeCount()
if count != 1 {
t.Fatalf("expected 1 node, got %d", count)
}
}
func TestCypherMerge_WithoutConstraint(t *testing.T) {
db := testDB(t)
// MERGE without a unique constraint should still work (label scan fallback).
result, err := db.Cypher(context.Background(), `MERGE (n:Person {name: "Alice"}) RETURN n`)
if err != nil {
t.Fatalf("MERGE failed: %v", err)
}
if len(result.Rows) != 1 {
t.Fatalf("expected 1 row, got %d", len(result.Rows))
}
// Second MERGE should find the existing node.
result2, err := db.Cypher(context.Background(), `MERGE (n:Person {name: "Alice"}) RETURN n`)
if err != nil {
t.Fatalf("MERGE failed: %v", err)
}
node1 := result.Rows[0]["n"].(*Node)
node2 := result2.Rows[0]["n"].(*Node)
if node1.ID != node2.ID {
t.Fatalf("expected same node, got different IDs: %d vs %d", node1.ID, node2.ID)
}
}
// ---------------------------------------------------------------------------
// Cypher SET tests
// ---------------------------------------------------------------------------
func TestCypherSet_Basic(t *testing.T) {
db := testDB(t)
// Create a node.
id, err := db.AddNode(Props{"name": "Alice", "age": int64(25)})
if err != nil {
t.Fatal(err)
}
// SET a new property and change an existing one.
result, err := db.Cypher(context.Background(),
`MATCH (n) WHERE id(n) = `+itoa(id)+` SET n.city = "Istanbul", n.age = 30 RETURN n`)
if err != nil {
t.Fatalf("SET failed: %v", err)
}
if len(result.Rows) != 1 {
t.Fatalf("expected 1 row, got %d", len(result.Rows))
}
// Verify changes persisted.
node, err := db.GetNode(id)
if err != nil {
t.Fatal(err)
}
if node.Props["city"] != "Istanbul" {
t.Errorf("expected city=Istanbul, got %v", node.Props["city"])
}
if node.Props["age"] != int64(30) {
t.Errorf("expected age=30, got %v (type %T)", node.Props["age"], node.Props["age"])
}
if node.Props["name"] != "Alice" {
t.Errorf("name should be unchanged, got %v", node.Props["name"])
}
}
func TestCypherSet_FollowerRejects(t *testing.T) {
db := testDB(t, Options{ShardCount: 1, Role: "follower"})
_, err := db.Cypher(context.Background(),
`MATCH (n) WHERE id(n) = 1 SET n.name = "Bob" RETURN n`)
if !errors.Is(err, ErrReadOnlyReplica) {
t.Fatalf("expected ErrReadOnlyReplica for SET on follower, got: %v", err)
}
}
func TestCypherDelete_Basic(t *testing.T) {
db := testDB(t)
// Create a node.
id, err := db.AddNode(Props{"name": "ToDelete"})
if err != nil {
t.Fatal(err)
}
// DELETE via Cypher.
_, err = db.Cypher(context.Background(),
`MATCH (n) WHERE id(n) = `+itoa(id)+` DELETE n RETURN n`)
if err != nil {
t.Fatalf("DELETE failed: %v", err)
}
// Node should be gone.
_, err = db.GetNode(id)
if err == nil {
t.Fatal("expected error after DELETE, got nil")
}
}
// ---------------------------------------------------------------------------
// MERGE ON CREATE SET / ON MATCH SET tests
// ---------------------------------------------------------------------------
func TestCypherMerge_OnCreateSet(t *testing.T) {
db := testDB(t)
// MERGE creates a new node → ON CREATE SET should apply.
result, err := db.Cypher(context.Background(),
`MERGE (n:Person {name: "Alice"}) ON CREATE SET n.source = "created" RETURN n`)
if err != nil {
t.Fatalf("MERGE ON CREATE SET failed: %v", err)
}
if len(result.Rows) != 1 {
t.Fatalf("expected 1 row, got %d", len(result.Rows))
}
node := result.Rows[0]["n"].(*Node)
if node.Props["source"] != "created" {
t.Errorf("expected source=created, got %v", node.Props["source"])
}
// Verify ON CREATE SET persisted.
persisted, _ := db.GetNode(node.ID)
if persisted.Props["source"] != "created" {
t.Errorf("expected persisted source=created, got %v", persisted.Props["source"])
}
}
func TestCypherMerge_OnMatchSet(t *testing.T) {
db := testDB(t)
// Create a node first.
db.Cypher(context.Background(), `MERGE (n:Person {name: "Bob"}) RETURN n`)
// MERGE again → should match → ON MATCH SET should apply.
result, err := db.Cypher(context.Background(),
`MERGE (n:Person {name: "Bob"}) ON MATCH SET n.updated = "yes" RETURN n`)
if err != nil {
t.Fatalf("MERGE ON MATCH SET failed: %v", err)
}
if len(result.Rows) != 1 {
t.Fatalf("expected 1 row, got %d", len(result.Rows))
}
node := result.Rows[0]["n"].(*Node)
if node.Props["updated"] != "yes" {
t.Errorf("expected updated=yes, got %v", node.Props["updated"])
}
}
func TestCypherMerge_OnCreateAndOnMatchSet(t *testing.T) {
db := testDB(t)
// First MERGE → creates → ON CREATE SET applies, ON MATCH SET does NOT.
result1, err := db.Cypher(context.Background(),
`MERGE (n:Person {name: "Eve"}) ON CREATE SET n.source = "new" ON MATCH SET n.source = "existing" RETURN n`)
if err != nil {
t.Fatalf("first MERGE failed: %v", err)
}
node1 := result1.Rows[0]["n"].(*Node)
if node1.Props["source"] != "new" {
t.Errorf("first MERGE: expected source=new, got %v", node1.Props["source"])
}
// Second MERGE → matches → ON MATCH SET applies.
result2, err := db.Cypher(context.Background(),
`MERGE (n:Person {name: "Eve"}) ON CREATE SET n.source = "new" ON MATCH SET n.source = "existing" RETURN n`)
if err != nil {
t.Fatalf("second MERGE failed: %v", err)
}
node2 := result2.Rows[0]["n"].(*Node)
if node2.Props["source"] != "existing" {
t.Errorf("second MERGE: expected source=existing, got %v", node2.Props["source"])
}
// Should still be just 1 node.
if db.NodeCount() != 1 {
t.Fatalf("expected 1 node, got %d", db.NodeCount())
}
}
// helper to convert NodeID to string
func itoa(id NodeID) string {
return fmt.Sprintf("%d", id)
}