graphlite 0.0.1

GraphLite - A lightweight ISO GQL Graph Database
Documentation
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Test all examples from README.md to ensure they are correct and functional
//!
//! NOTE: These tests must be run with `--test-threads=1` due to shared global
//! SessionManager in the test infrastructure. Run with:
//! ```
//! cargo test --test readme_examples_test -- --test-threads=1
//! ```

#[path = "testutils/mod.rs"]
mod testutils;

use graphlite::Value;
use testutils::test_fixture::TestFixture;

// ============================================================================
// Pattern Matching Examples Tests
// ============================================================================

#[test]
fn test_readme_pattern_matching_setup() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_pattern_test")
        .expect("Failed to setup graph");

    // Create people
    fixture
        .query(
            "INSERT (:Person {name: 'Alice', age: 30, city: 'NYC'}),
                (:Person {name: 'Bob', age: 25, city: 'NYC'}),
                (:Person {name: 'Carol', age: 28, city: 'SF'}),
                (:Person {name: 'Dave', age: 35, city: 'NYC'})",
        )
        .expect("Failed to insert people");

    // Create companies
    fixture
        .query(
            "INSERT (:Company {name: 'TechCorp', founded: '2010-01-01'}),
                (:Company {name: 'DataInc', founded: '2015-06-15'})",
        )
        .expect("Failed to insert companies");

    // Create KNOWS relationships
    fixture
        .query(
            "MATCH (alice:Person {name: 'Alice'}), (bob:Person {name: 'Bob'})
         INSERT (alice)-[:KNOWS {since: '2020-01-01'}]->(bob)",
        )
        .expect("Failed to create Alice-Bob KNOWS");

    fixture
        .query(
            "MATCH (bob:Person {name: 'Bob'}), (carol:Person {name: 'Carol'})
         INSERT (bob)-[:KNOWS {since: '2021-03-15'}]->(carol)",
        )
        .expect("Failed to create Bob-Carol KNOWS");

    fixture
        .query(
            "MATCH (alice:Person {name: 'Alice'}), (dave:Person {name: 'Dave'})
         INSERT (alice)-[:KNOWS {since: '2019-05-20'}]->(dave)",
        )
        .expect("Failed to create Alice-Dave KNOWS");

    fixture
        .query(
            "MATCH (carol:Person {name: 'Carol'}), (dave:Person {name: 'Dave'})
         INSERT (carol)-[:KNOWS {since: '2022-01-10'}]->(dave)",
        )
        .expect("Failed to create Carol-Dave KNOWS");

    // Create WORKS_AT relationships
    fixture
        .query(
            "MATCH (alice:Person {name: 'Alice'}), (tech:Company {name: 'TechCorp'})
         INSERT (alice)-[:WORKS_AT {role: 'Engineer', since: '2020-01-01'}]->(tech)",
        )
        .expect("Failed to create Alice WORKS_AT TechCorp");

    fixture
        .query(
            "MATCH (bob:Person {name: 'Bob'}), (tech:Company {name: 'TechCorp'})
         INSERT (bob)-[:WORKS_AT {role: 'Designer', since: '2021-01-01'}]->(tech)",
        )
        .expect("Failed to create Bob WORKS_AT TechCorp");

    fixture
        .query(
            "MATCH (carol:Person {name: 'Carol'}), (data:Company {name: 'DataInc'})
         INSERT (carol)-[:WORKS_AT {role: 'Analyst', since: '2022-01-01'}]->(data)",
        )
        .expect("Failed to create Carol WORKS_AT DataInc");
}

#[test]
fn test_readme_friends_of_friends() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_fof_test")
        .expect("Failed to setup graph");

    // Setup data
    setup_pattern_matching_data(&fixture);

    // Find friends of friends
    let result = fixture
        .query(
            "MATCH (person:Person)-[:KNOWS]->(friend)-[:KNOWS]->(fof)
         WHERE person.name = 'Alice'
         RETURN fof.name",
        )
        .expect("Friends of friends query should succeed");

    // Should return Carol and Dave
    assert!(
        result.rows.len() >= 1,
        "Should find at least one friend of friend"
    );

    let names: Vec<String> = result
        .rows
        .iter()
        .filter_map(|row| {
            if let Some(Value::String(name)) = row.values.get("fof.name") {
                Some(name.clone())
            } else {
                None
            }
        })
        .collect();

    assert!(
        names.contains(&"Carol".to_string()) || names.contains(&"Dave".to_string()),
        "Should find Carol or Dave as friends of friends"
    );
}

#[test]
#[ignore] // Variable-length path syntax not yet implemented
fn test_readme_variable_length_paths() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_varlen_test")
        .expect("Failed to setup graph");

    setup_pattern_matching_data(&fixture);

    // Variable-length paths - syntax not yet implemented
    let result = fixture
        .query(
            "MATCH (start:Person)-[:KNOWS{1,3}]->(end:Person)
         RETURN start.name, end.name",
        )
        .expect("Variable-length paths query should succeed");

    assert!(result.rows.len() > 0, "Should find paths");
}

#[test]
fn test_readme_multiple_patterns_coworkers() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_coworkers_test")
        .expect("Failed to setup graph");

    setup_pattern_matching_data(&fixture);

    // Multiple patterns - find coworkers who know each other
    let result = fixture
        .query(
            "MATCH (a:Person)-[:WORKS_AT]->(company:Company),
               (a)-[:KNOWS]->(b:Person)-[:WORKS_AT]->(company)
         RETURN a.name, b.name, company.name",
        )
        .expect("Coworkers query should succeed");

    // Should find Alice and Bob at TechCorp
    assert!(
        result.rows.len() > 0,
        "Should find coworkers who know each other"
    );
}

// ============================================================================
// Data Modification Examples Tests
// ============================================================================

#[test]
fn test_readme_insert_simple_node() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_insert_test")
        .expect("Failed to setup graph");

    // Insert nodes and relationships
    fixture
        .query("INSERT (n:Person {name: 'Charlie', age: 35})")
        .expect("Simple INSERT should succeed");

    // Verify
    let result = fixture
        .query("MATCH (p:Person {name: 'Charlie'}) RETURN p.age")
        .expect("Query should succeed");

    assert_eq!(result.rows.len(), 1);
    assert_eq!(
        result.rows[0].values.get("p.age"),
        Some(&Value::Number(35.0))
    );
}

#[test]
fn test_readme_insert_with_functions() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_func_insert_test")
        .expect("Failed to setup graph");

    // Insert with function expressions
    fixture
        .query(
            "INSERT (:Product {
            name: upper('laptop'),
            price: abs(-1299.99),
            warranty: duration('P2Y')
        })",
        )
        .expect("INSERT with functions should succeed");

    // Verify
    let result = fixture
        .query("MATCH (p:Product) RETURN p.name, p.price, p.warranty")
        .expect("Query should succeed");

    assert_eq!(result.rows.len(), 1);
    assert_eq!(
        result.rows[0].values.get("p.name"),
        Some(&Value::String("LAPTOP".to_string()))
    );
    assert_eq!(
        result.rows[0].values.get("p.price"),
        Some(&Value::Number(1299.99))
    );
    // warranty is duration in seconds: P2Y = 2 years ≈ 63072000 seconds
    assert!(result.rows[0].values.get("p.warranty").is_some());
}

#[test]
fn test_readme_insert_edge_with_functions() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_edge_func_test")
        .expect("Failed to setup graph");

    // Create nodes first
    fixture
        .query("INSERT (:Person {name: 'Alice'}), (:Person {name: 'Bob'})")
        .expect("Insert nodes should succeed");

    // Insert edges with function expressions
    fixture
        .query(
            "MATCH (a:Person {name: 'Alice'}), (b:Person {name: 'Bob'})
         INSERT (a)-[:KNOWS {
             since: duration('P5Y'),
             strength: round(0.857)
         }]->(b)",
        )
        .expect("INSERT edge with functions should succeed");

    // Verify
    let result = fixture
        .query(
            "MATCH (a:Person {name: 'Alice'})-[r:KNOWS]->(b:Person {name: 'Bob'})
         RETURN r.since, r.strength",
        )
        .expect("Query should succeed");

    assert_eq!(result.rows.len(), 1);
    assert_eq!(
        result.rows[0].values.get("r.strength"),
        Some(&Value::Number(1.0))
    );
}

#[test]
fn test_readme_nested_functions_insert() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_nested_insert_test")
        .expect("Failed to setup graph");

    // Nested function calls in INSERT
    fixture
        .query(
            "INSERT (:Data {
            value: round(abs(-42.7)),
            text: upper(lower('MiXeD CaSe'))
        })",
        )
        .expect("INSERT with nested functions should succeed");

    // Verify
    let result = fixture
        .query("MATCH (d:Data) RETURN d.value, d.text")
        .expect("Query should succeed");

    assert_eq!(result.rows.len(), 1);
    assert_eq!(
        result.rows[0].values.get("d.value"),
        Some(&Value::Number(43.0))
    );
    assert_eq!(
        result.rows[0].values.get("d.text"),
        Some(&Value::String("MIXED CASE".to_string()))
    );
}

#[test]
fn test_readme_set_node_with_functions() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_set_node_test")
        .expect("Failed to setup graph");

    // Create node
    fixture
        .query("INSERT (:Person {name: 'Alice', age: 30})")
        .expect("Insert should succeed");

    // SET node properties with functions
    fixture
        .query(
            "MATCH (p:Person {name: 'Alice'})
         SET p.age = 31,
             p.name_upper = upper('alice'),
             p.account_age = duration('P5Y')",
        )
        .expect("SET with functions should succeed");

    // Verify
    let result = fixture
        .query(
            "MATCH (p:Person {name: 'Alice'})
         RETURN p.age, p.name_upper, p.account_age",
        )
        .expect("Query should succeed");

    assert_eq!(result.rows.len(), 1);
    assert_eq!(
        result.rows[0].values.get("p.age"),
        Some(&Value::Number(31.0))
    );
    assert_eq!(
        result.rows[0].values.get("p.name_upper"),
        Some(&Value::String("ALICE".to_string()))
    );
}

#[test]
fn test_readme_set_edge_with_functions() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_set_edge_test")
        .expect("Failed to setup graph");

    // Create nodes and edge
    fixture
        .query("INSERT (:Person {name: 'Alice'}), (:Person {name: 'Bob'})")
        .expect("Insert nodes should succeed");

    fixture
        .query(
            "MATCH (a:Person {name: 'Alice'}), (b:Person {name: 'Bob'})
         INSERT (a)-[:KNOWS]->(b)",
        )
        .expect("Insert edge should succeed");

    // SET edge properties with functions
    fixture
        .query(
            "MATCH (a:Person)-[r:KNOWS]->(b:Person)
         SET r.duration = duration('P5Y'),
             r.strength = abs(-0.9),
             r.label = upper('relationship')",
        )
        .expect("SET edge with functions should succeed");

    // Verify
    let result = fixture
        .query(
            "MATCH (a:Person)-[r:KNOWS]->(b:Person)
         RETURN r.strength, r.label",
        )
        .expect("Query should succeed");

    assert_eq!(result.rows.len(), 1);
    assert_eq!(
        result.rows[0].values.get("r.strength"),
        Some(&Value::Number(0.9))
    );
    assert_eq!(
        result.rows[0].values.get("r.label"),
        Some(&Value::String("RELATIONSHIP".to_string()))
    );
}

#[test]
fn test_readme_nested_functions_set() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_nested_set_test")
        .expect("Failed to setup graph");

    // Create node
    fixture
        .query("INSERT (:Product {name: 'Widget', price: 100.0})")
        .expect("Insert should succeed");

    // Nested functions in SET
    fixture
        .query(
            "MATCH (p:Product)
         SET p.price = round(abs(-99.99)),
             p.name = upper(lower('MiXeD'))",
        )
        .expect("SET with nested functions should succeed");

    // Verify
    let result = fixture
        .query("MATCH (p:Product) RETURN p.price, p.name")
        .expect("Query should succeed");

    assert_eq!(result.rows.len(), 1);
    assert_eq!(
        result.rows[0].values.get("p.price"),
        Some(&Value::Number(100.0))
    );
    assert_eq!(
        result.rows[0].values.get("p.name"),
        Some(&Value::String("MIXED".to_string()))
    );
}

#[test]
fn test_readme_delete_node() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_delete_test")
        .expect("Failed to setup graph");

    // Create nodes
    fixture
        .query("INSERT (:Person {name: 'Bob'}), (:Person {name: 'Alice'})")
        .expect("Insert should succeed");

    // Delete nodes
    fixture
        .query("MATCH (p:Person {name: 'Bob'}) DELETE p")
        .expect("DELETE should succeed");

    // Verify Bob is deleted
    let result = fixture
        .query("MATCH (p:Person {name: 'Bob'}) RETURN p")
        .expect("Query should succeed");
    assert_eq!(result.rows.len(), 0, "Bob should be deleted");

    // Verify Alice still exists
    let result = fixture
        .query("MATCH (p:Person {name: 'Alice'}) RETURN p")
        .expect("Query should succeed");
    assert_eq!(result.rows.len(), 1, "Alice should still exist");
}

// ============================================================================
// Aggregation Examples Tests
// ============================================================================

#[test]
fn test_readme_count_relationships() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_count_test")
        .expect("Failed to setup graph");

    setup_pattern_matching_data(&fixture);

    // Count relationships per person
    let result = fixture
        .query(
            "MATCH (p:Person)-[r:KNOWS]->()
         RETURN p.name, COUNT(r) AS friend_count",
        )
        .expect("COUNT query should succeed");

    assert!(result.rows.len() > 0, "Should have results");

    // Verify Alice has 2 friends
    let alice_row = result
        .rows
        .iter()
        .find(|row| row.values.get("p.name") == Some(&Value::String("Alice".to_string())));

    if let Some(row) = alice_row {
        assert_eq!(
            row.values.get("friend_count"),
            Some(&Value::Number(2.0)),
            "Alice should have 2 KNOWS relationships"
        );
    }
}

#[test]
fn test_readme_group_by_aggregate() {
    let fixture = TestFixture::new().expect("Failed to create fixture");
    fixture
        .setup_graph("readme_groupby_test")
        .expect("Failed to setup graph");

    setup_pattern_matching_data(&fixture);

    // Group by city and calculate statistics
    let result = fixture
        .query(
            "MATCH (p:Person)
         RETURN p.city, AVG(p.age) AS avg_age, COUNT(*) AS population
         GROUP BY p.city",
        )
        .expect("GROUP BY query should succeed");

    assert!(result.rows.len() >= 2, "Should have at least 2 cities");

    // Find NYC row
    let nyc_row = result
        .rows
        .iter()
        .find(|row| row.values.get("p.city") == Some(&Value::String("NYC".to_string())));

    if let Some(row) = nyc_row {
        // NYC has Alice (30), Bob (25), Dave (35) = avg 30.0, count 3
        assert_eq!(
            row.values.get("population"),
            Some(&Value::Number(3.0)),
            "NYC should have 3 people"
        );
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Setup the standard pattern matching test data
fn setup_pattern_matching_data(fixture: &TestFixture) {
    // Create people
    fixture
        .query(
            "INSERT (:Person {name: 'Alice', age: 30, city: 'NYC'}),
                (:Person {name: 'Bob', age: 25, city: 'NYC'}),
                (:Person {name: 'Carol', age: 28, city: 'SF'}),
                (:Person {name: 'Dave', age: 35, city: 'NYC'})",
        )
        .expect("Failed to insert people");

    // Create companies
    fixture
        .query(
            "INSERT (:Company {name: 'TechCorp', founded: '2010-01-01'}),
                (:Company {name: 'DataInc', founded: '2015-06-15'})",
        )
        .expect("Failed to insert companies");

    // Create KNOWS relationships
    fixture
        .query(
            "MATCH (alice:Person {name: 'Alice'}), (bob:Person {name: 'Bob'})
         INSERT (alice)-[:KNOWS {since: '2020-01-01'}]->(bob)",
        )
        .expect("Failed to create Alice-Bob KNOWS");

    fixture
        .query(
            "MATCH (bob:Person {name: 'Bob'}), (carol:Person {name: 'Carol'})
         INSERT (bob)-[:KNOWS {since: '2021-03-15'}]->(carol)",
        )
        .expect("Failed to create Bob-Carol KNOWS");

    fixture
        .query(
            "MATCH (alice:Person {name: 'Alice'}), (dave:Person {name: 'Dave'})
         INSERT (alice)-[:KNOWS {since: '2019-05-20'}]->(dave)",
        )
        .expect("Failed to create Alice-Dave KNOWS");

    fixture
        .query(
            "MATCH (carol:Person {name: 'Carol'}), (dave:Person {name: 'Dave'})
         INSERT (carol)-[:KNOWS {since: '2022-01-10'}]->(dave)",
        )
        .expect("Failed to create Carol-Dave KNOWS");

    // Create WORKS_AT relationships
    fixture
        .query(
            "MATCH (alice:Person {name: 'Alice'}), (tech:Company {name: 'TechCorp'})
         INSERT (alice)-[:WORKS_AT {role: 'Engineer', since: '2020-01-01'}]->(tech)",
        )
        .expect("Failed to create Alice WORKS_AT TechCorp");

    fixture
        .query(
            "MATCH (bob:Person {name: 'Bob'}), (tech:Company {name: 'TechCorp'})
         INSERT (bob)-[:WORKS_AT {role: 'Designer', since: '2021-01-01'}]->(tech)",
        )
        .expect("Failed to create Bob WORKS_AT TechCorp");

    fixture
        .query(
            "MATCH (carol:Person {name: 'Carol'}), (data:Company {name: 'DataInc'})
         INSERT (carol)-[:WORKS_AT {role: 'Analyst', since: '2022-01-01'}]->(data)",
        )
        .expect("Failed to create Carol WORKS_AT DataInc");
}