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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
//! Comprehensive integration test suite for Set Operations (UNION, UNION ALL, INTERSECT, EXCEPT)
//!
//! These tests verify that all set operations work correctly with proper filtering,
//! deduplication, and variable inheritance after the implementation fixes.

// Include the testutils module
#[path = "testutils/mod.rs"]
mod testutils;
use graphlite::Value;
use testutils::test_fixture::TestFixture;

/// Helper function to create simple test fixture without relationships
fn create_simple_test_fixture() -> Result<TestFixture, Box<dyn std::error::Error>> {
    let fixture = TestFixture::new()?;

    // Create a test graph in the schema
    let graph_name = format!(
        "simple_set_ops_{}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)?
            .as_nanos()
            % 1000000
    );
    fixture.query(&format!(
        "CREATE GRAPH /{}/{}",
        fixture.schema_name(),
        graph_name
    ))?;
    fixture.query(&format!(
        "SESSION SET GRAPH /{}/{}",
        fixture.schema_name(),
        graph_name
    ))?;

    // Create Person nodes with properties (no relationships for now)
    fixture.query(
        "INSERT (p1:Person {name: 'Alice Smith', age: 28, salary: 75000, city: 'Austin'})",
    )?;
    fixture.query(
        "INSERT (p2:Person {name: 'Bob Johnson', age: 35, salary: 65000, city: 'Seattle'})",
    )?;
    fixture.query(
        "INSERT (p3:Person {name: 'Charlie Brown', age: 32, salary: 90000, city: 'Austin'})",
    )?;
    fixture.query(
        "INSERT (p4:Person {name: 'Diana Wilson', age: 29, salary: 80000, city: 'New York'})",
    )?;
    fixture
        .query("INSERT (p5:Person {name: 'Eve Davis', age: 40, salary: 55000, city: 'Seattle'})")?;
    fixture.query(
        "INSERT (p6:Person {name: 'Frank Miller', age: 26, salary: 60000, city: 'New York'})",
    )?;

    Ok(fixture)
}

/// Helper function to create test fixture with tutorial-like sample data for set operations
fn create_tutorial_test_fixture() -> Result<TestFixture, Box<dyn std::error::Error>> {
    let fixture = TestFixture::new()?;

    // Create a test graph in the schema
    let graph_name = format!(
        "tutorial_set_ops_{}",
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)?
            .as_nanos()
            % 1000000
    );
    fixture.query(&format!(
        "CREATE GRAPH /{}/{}",
        fixture.schema_name(),
        graph_name
    ))?;
    fixture.query(&format!(
        "SESSION SET GRAPH /{}/{}",
        fixture.schema_name(),
        graph_name
    ))?;

    // Create Person nodes with properties similar to tutorial data
    fixture.query(
        "INSERT (p1:Person {name: 'Alice Smith', age: 28, salary: 75000, city: 'Austin'})",
    )?;
    fixture.query(
        "INSERT (p2:Person {name: 'Bob Johnson', age: 35, salary: 65000, city: 'Seattle'})",
    )?;
    fixture.query(
        "INSERT (p3:Person {name: 'Charlie Brown', age: 32, salary: 90000, city: 'Austin'})",
    )?;
    fixture.query(
        "INSERT (p4:Person {name: 'Diana Wilson', age: 29, salary: 80000, city: 'New York'})",
    )?;
    fixture
        .query("INSERT (p5:Person {name: 'Eve Davis', age: 40, salary: 55000, city: 'Seattle'})")?;
    fixture.query(
        "INSERT (p6:Person {name: 'Frank Miller', age: 26, salary: 60000, city: 'New York'})",
    )?;

    // Create Department nodes for UNION ALL tests
    fixture.query("INSERT (d1:Department {name: 'IT'})")?;
    fixture.query("INSERT (d2:Department {name: 'Engineering'})")?;
    fixture.query("INSERT (d3:Department {name: 'Sales'})")?;

    // Create WORKS_IN relationships using MATCH-INSERT syntax
    fixture.query("MATCH (p:Person {name: 'Alice Smith'}), (d:Department {name: 'IT'}) INSERT (p)-[:WORKS_IN]->(d)")?;
    fixture.query("MATCH (p:Person {name: 'Bob Johnson'}), (d:Department {name: 'Engineering'}) INSERT (p)-[:WORKS_IN]->(d)")?;
    fixture.query("MATCH (p:Person {name: 'Charlie Brown'}), (d:Department {name: 'Engineering'}) INSERT (p)-[:WORKS_IN]->(d)")?;

    Ok(fixture)
}

#[test]
fn test_simple_union_operation() {
    let fixture = create_simple_test_fixture().expect("Failed to create simple test fixture");

    // Test UNION query without relationships
    let query = "MATCH (p:Person) WHERE p.age < 30 RETURN p.name UNION MATCH (p:Person) WHERE p.age > 35 RETURN p.name";

    let result = fixture
        .query(query)
        .expect("Simple UNION query should succeed");

    // Should find people with age < 30 OR age > 35 (Alice 28, Frank 26, Eve 40)
    assert!(
        result.rows.len() >= 3,
        "UNION should return at least 3 rows, got {}",
        result.rows.len()
    );

    let names: Vec<String> = result
        .rows
        .iter()
        .map(|row| row.values["p.name"].as_string().unwrap().to_string())
        .collect();

    log::debug!("Simple UNION results: {:?}", names);

    // Test that we have expected names
    assert!(names.contains(&"Alice Smith".to_string()));
    assert!(names.contains(&"Frank Miller".to_string()));
    assert!(names.contains(&"Eve Davis".to_string()));
}

#[test]
fn test_union_with_where_clauses_deduplication() {
    let fixture = create_tutorial_test_fixture().expect("Failed to create test fixture");

    // Test 1: UNION query with WHERE conditions
    // Expected: People with age < 30 OR salary < 70000 (without duplicates)
    let query = "MATCH (p:Person) WHERE p.age < 30 RETURN p.name UNION MATCH (p:Person) WHERE p.salary < 70000 RETURN p.name";

    let result = fixture.query(query).expect("UNION query should succeed");

    // Verify results
    assert!(
        result.rows.len() >= 3,
        "UNION should return at least 3 unique rows"
    );

    // Verify that duplicate names are removed (Frank Miller appears in both conditions)
    let names: Vec<String> = result
        .rows
        .iter()
        .map(|row| row.values["p.name"].as_string().unwrap().to_string())
        .collect();

    // Check for expected names (may vary based on test data, but should include filtered results)
    log::debug!("UNION results: {:?}", names);

    // Verify no duplicates
    let mut sorted_names = names.clone();
    sorted_names.sort();
    sorted_names.dedup();
    assert_eq!(
        names.len(),
        sorted_names.len(),
        "UNION should not contain duplicate names"
    );
}

#[test]
fn test_union_all_preserves_duplicates() {
    let fixture = create_tutorial_test_fixture().expect("Failed to create test fixture");

    // Test 2: UNION ALL query with department joins
    // Expected: All people in IT + All people in Engineering (with duplicates if any)
    let query = "
        MATCH (p:Person)-[:WORKS_IN]->(d:Department {name: 'IT'}) RETURN p.name
        UNION ALL
        MATCH (p:Person)-[:WORKS_IN]->(d:Department {name: 'Engineering'}) RETURN p.name
    ";

    let result = fixture
        .query(query)
        .expect("UNION ALL query should succeed");

    // Should return all matches from both departments
    log::debug!("UNION ALL results: {} rows", result.rows.len());

    // If there are results, verify they include department members
    if result.rows.len() > 0 {
        let names: Vec<String> = result
            .rows
            .iter()
            .map(|row| row.values["p.name"].as_string().unwrap().to_string())
            .collect();
        log::debug!("UNION ALL names: {:?}", names);

        // UNION ALL should preserve duplicates if someone is in both departments
        // (In our test data, no one is in both, so we just check it executes)
    }
}

#[test]
fn test_intersect_with_conditions() {
    let fixture = create_tutorial_test_fixture().expect("Failed to create test fixture");

    // Test 3: INTERSECT query with age and salary conditions
    // Expected: People with age > 30 AND salary > 80000
    let query = "
        MATCH (p:Person) WHERE p.age > 30 RETURN p.name
        INTERSECT
        MATCH (p:Person) WHERE p.salary > 80000 RETURN p.name
    ";

    let result = fixture
        .query(query)
        .expect("INTERSECT query should succeed");

    log::debug!("INTERSECT results: {} rows", result.rows.len());

    if result.rows.len() > 0 {
        let names: Vec<String> = result
            .rows
            .iter()
            .map(|row| row.values["p.name"].as_string().unwrap().to_string())
            .collect();
        log::debug!("INTERSECT names: {:?}", names);

        // Should only contain people who satisfy BOTH conditions
        // In our test data: age > 30 AND salary > 80000 should be Charlie Brown (32, 90000)
        // and possibly Diana Wilson (29, 80000) if age > 30 excludes her
    }
}

#[test]
fn test_except_with_filter() {
    let fixture = create_tutorial_test_fixture().expect("Failed to create test fixture");

    // Test 4: EXCEPT query with city filter
    // Expected: All people EXCEPT those in Austin
    let query = "
        MATCH (p:Person) RETURN p.name
        EXCEPT
        MATCH (p:Person) WHERE p.city = 'Austin' RETURN p.name
    ";

    let result = fixture.query(query).expect("EXCEPT query should succeed");

    log::debug!("EXCEPT results: {} rows", result.rows.len());

    // Should return all people except those in Austin
    let names: Vec<String> = result
        .rows
        .iter()
        .map(|row| row.values["p.name"].as_string().unwrap().to_string())
        .collect();
    log::debug!("EXCEPT names: {:?}", names);

    // Should exclude Alice Smith and Charlie Brown (both in Austin)
    assert!(
        !names.contains(&"Alice Smith".to_string()),
        "Alice Smith should be excluded (Austin)"
    );
    assert!(
        !names.contains(&"Charlie Brown".to_string()),
        "Charlie Brown should be excluded (Austin)"
    );

    // Should include others
    assert!(
        names.contains(&"Bob Johnson".to_string()),
        "Bob Johnson should be included (Seattle)"
    );
    assert!(
        names.contains(&"Diana Wilson".to_string()),
        "Diana Wilson should be included (New York)"
    );
    assert!(
        names.contains(&"Eve Davis".to_string()),
        "Eve Davis should be included (Seattle)"
    );
    assert!(
        names.contains(&"Frank Miller".to_string()),
        "Frank Miller should be included (New York)"
    );
}

#[test]
fn test_union_variable_inheritance() {
    let fixture = create_tutorial_test_fixture().expect("Failed to create test fixture");

    // Test that UNION properly inherits variables from both sides
    let query = "
        MATCH (p:Person) WHERE p.age < 30 RETURN p.name, p.age
        UNION
        MATCH (p:Person) WHERE p.salary < 70000 RETURN p.name, p.age
    ";

    let result = fixture
        .query(query)
        .expect("UNION with multiple columns should succeed");

    // Verify that result has both name and age columns
    assert_eq!(
        result.variables.len(),
        2,
        "UNION should return 2 columns (name, age)"
    );
    assert!(
        result.variables.contains(&"p.name".to_string()),
        "Should have p.name column"
    );
    assert!(
        result.variables.contains(&"p.age".to_string()),
        "Should have p.age column"
    );

    log::debug!(
        "UNION multi-column results: {} rows, columns: {:?}",
        result.rows.len(),
        result.variables
    );
}

#[test]
fn test_nested_set_operations() {
    let fixture = create_tutorial_test_fixture().expect("Failed to create test fixture");

    // Test nested set operations (if parser supports them)
    let query = "
        MATCH (p:Person) WHERE p.age < 35 RETURN p.name
        INTERSECT
        (
            MATCH (p:Person) WHERE p.salary > 60000 RETURN p.name
            UNION
            MATCH (p:Person) WHERE p.city = 'New York' RETURN p.name
        )
    ";

    // This might not be supported yet, but test if it is
    match fixture.query(query) {
        Ok(result) => {
            log::debug!(
                "Nested set operations succeeded: {} rows",
                result.rows.len()
            );
        }
        Err(e) => {
            log::debug!("Nested set operations not yet supported: {}", e);
            // This is expected for now - nested operations are complex
        }
    }
}

#[test]
fn test_basic_queries() {
    let fixture = create_tutorial_test_fixture().expect("Failed to create test fixture");

    // Test 1: Check department nodes exist with correct names
    let query1 = "MATCH (d:Department) RETURN d.name";
    let result1 = fixture
        .query(query1)
        .expect("Department query should succeed");
    log::debug!("Departments: {} rows", result1.rows.len());
    for row in &result1.rows {
        let dept_name = row.values["d.name"].as_string().unwrap();
        log::debug!("  Department: {}", dept_name);
    }

    // Test 2: Check if Department filtering works
    let query2 = "MATCH (d:Department {name: 'IT'}) RETURN d.name";
    let result2 = fixture
        .query(query2)
        .expect("IT department query should succeed");
    log::debug!("IT departments: {} rows", result2.rows.len());
    for row in &result2.rows {
        let dept_name = row.values["d.name"].as_string().unwrap();
        log::debug!("  IT Department: {}", dept_name);
    }

    // Test 3: Simple relationship without target constraints
    let query3 = "MATCH (p:Person)-[:WORKS_IN]->(d:Department) RETURN p.name, d.name";
    let result3 = fixture
        .query(query3)
        .expect("Basic relationship query should succeed");
    log::debug!("Basic relationships: {} rows", result3.rows.len());
    for row in &result3.rows {
        let person = row.values["p.name"].as_string().unwrap();
        let dept = row.values["d.name"].as_string().unwrap();
        log::debug!("  {} -> {}", person, dept);
    }

    // Test 4: The problematic query - relationship with target constraints
    let query4 = "MATCH (p:Person)-[:WORKS_IN]->(d:Department {name: 'IT'}) RETURN p.name";
    let result4 = fixture
        .query(query4)
        .expect("IT relationship query should succeed");
    log::debug!("IT relationship: {} rows", result4.rows.len());
    for row in &result4.rows {
        let person = row.values["p.name"].as_string().unwrap();
        log::debug!("  {} works in IT", person);
    }
}

#[test]
fn test_debug_relationships() {
    let fixture = create_tutorial_test_fixture().expect("Failed to create test fixture");

    // First, test that relationships exist
    let query1 = "MATCH (p:Person)-[:WORKS_IN]->(d:Department) RETURN p.name, d.name";
    let result1 = fixture
        .query(query1)
        .expect("Basic relationship query should succeed");

    log::debug!("All WORKS_IN relationships: {} rows", result1.rows.len());
    for row in &result1.rows {
        let person = row.values["p.name"].as_string().unwrap();
        let dept = row.values["d.name"].as_string().unwrap();
        log::debug!("  {} works in {}", person, dept);
    }

    // Test individual relationship queries
    let query2 = "MATCH (p:Person)-[:WORKS_IN]->(d:Department {name: 'IT'}) RETURN p.name";
    let result2 = fixture
        .query(query2)
        .expect("IT relationship query should succeed");

    log::debug!("People in IT: {} rows", result2.rows.len());
    for row in &result2.rows {
        let person = row.values["p.name"].as_string().unwrap();
        log::debug!("  {} works in IT", person);
    }

    let query3 = "MATCH (p:Person)-[:WORKS_IN]->(d:Department {name: 'Engineering'}) RETURN p.name";
    let result3 = fixture
        .query(query3)
        .expect("Engineering relationship query should succeed");

    log::debug!("People in Engineering: {} rows", result3.rows.len());
    for row in &result3.rows {
        let person = row.values["p.name"].as_string().unwrap();
        log::debug!("  {} works in Engineering", person);
    }
}

#[test]
fn test_union_all_with_relationships() {
    let fixture = create_tutorial_test_fixture().expect("Failed to create test fixture");

    // Test the specific query from the tutorial that's failing
    let query = r#"
        MATCH (p:Person)-[:WORKS_IN]->(d:Department {name: 'IT'}) RETURN p.name
        UNION ALL
        MATCH (p:Person)-[:WORKS_IN]->(d:Department {name: 'Engineering'}) RETURN p.name
    "#;

    let result = fixture
        .query(query)
        .expect("UNION ALL with relationships should succeed");

    log::debug!("UNION ALL relationship results: {} rows", result.rows.len());
    let names: Vec<String> = result
        .rows
        .iter()
        .map(|row| row.values["p.name"].as_string().unwrap().to_string())
        .collect();
    log::debug!("Names found: {:?}", names);

    // Should only find people in IT and Engineering departments
    // From our test data: Alice (IT), Bob (Engineering), Charlie (Engineering)
    // UNION ALL should preserve duplicates, so we expect exactly these 3 people
    assert_eq!(
        result.rows.len(),
        3,
        "Should find exactly 3 people in IT and Engineering"
    );

    // Verify the right people are found
    assert!(
        names.contains(&"Alice Smith".to_string()),
        "Should find Alice in IT"
    );
    assert!(
        names.contains(&"Bob Johnson".to_string()),
        "Should find Bob in Engineering"
    );
    assert!(
        names.contains(&"Charlie Brown".to_string()),
        "Should find Charlie in Engineering"
    );
}

#[test]
fn test_duplicate_edge_insertion() {
    let fixture = create_simple_test_fixture().expect("Failed to create test fixture");

    // Create nodes first
    fixture
        .query("INSERT (alice:Person {name: 'Alice Smith'})")
        .expect("Insert Alice should succeed");
    fixture
        .query("INSERT (proj1:Project {name: 'Customer Analytics Platform'})")
        .expect("Insert Project should succeed");

    // Insert relationship first time
    let relationship_query = "MATCH (alice:Person {name: 'Alice Smith'}), (proj1:Project {name: 'Customer Analytics Platform'}) INSERT (alice)-[:ASSIGNED_TO {role: 'Project Lead', allocation: 0.8}]->(proj1)";

    let result1 = fixture.query(relationship_query);
    log::debug!("First relationship insertion: {:?}", result1);

    // Insert same relationship again (should this fail or be idempotent?)
    let result2 = fixture.query(relationship_query);
    log::debug!("Second relationship insertion: {:?}", result2);

    // Check how many relationships exist
    let count_query =
        "MATCH (p:Person)-[r:ASSIGNED_TO]->(proj:Project) RETURN count(r) as relationship_count";
    let count_result = fixture
        .query(count_query)
        .expect("Count query should succeed");

    let count = count_result.rows[0].values["relationship_count"]
        .as_number()
        .unwrap();
    log::debug!("Total ASSIGNED_TO relationships: {}", count);
}

#[test]
fn test_duplicate_node_insertion() {
    let fixture = create_simple_test_fixture().expect("Failed to create test fixture");

    // Insert the same project twice with identical properties
    let insert_query = r#"INSERT (proj1:Project {
        name: 'Customer Analytics Platform',
        budget: 500000,
        start_date: '2023-01-15',
        status: 'active'
    })"#;

    // First insertion
    let result1 = fixture.query(insert_query);
    log::debug!("First insertion result: {:?}", result1);

    // Second insertion (duplicate)
    let result2 = fixture.query(insert_query);
    log::debug!("Second insertion result: {:?}", result2);

    // Check how many Project nodes exist
    let count_query = "MATCH (p:Project) RETURN count(p) as project_count";
    let count_result = fixture
        .query(count_query)
        .expect("Count query should succeed");

    let count = count_result.rows[0].values["project_count"]
        .as_number()
        .unwrap();
    log::debug!("Total Project nodes: {}", count);

    // Check if we can retrieve all projects
    let all_projects = fixture
        .query("MATCH (p:Project) RETURN p.name")
        .expect("Project query should succeed");
    log::debug!("Found {} project nodes:", all_projects.rows.len());
    for row in &all_projects.rows {
        let name = row.values["p.name"].as_string().unwrap();
        log::debug!("  Project: {}", name);
    }

    // Verify behavior: should either have 1 node (deduplicated) or 2 nodes (allowed duplicates)
    // Most graph databases either prevent duplicates or allow them explicitly
    if count == 1.0 {
        log::debug!("✅ Duplicate insertion was ignored/deduplicated");
    } else if count == 2.0 {
        log::debug!("⚠️  Duplicate insertion created multiple nodes");
    } else {
        panic!("Unexpected node count: {}", count);
    }
}

#[test]
fn test_set_multiple_relationship_properties() {
    let fixture = create_simple_test_fixture().expect("Failed to create test fixture");

    // Clear any existing data first
    fixture.query("MATCH (n) DETACH DELETE n").ok(); // Clear existing nodes

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

    // Create relationship with initial properties
    fixture.query("MATCH (alice:Person {name: 'Alice Smith'}), (bob:Person {name: 'Bob Johnson'}) INSERT (alice)-[:KNOWS {strength: 'weak', lastContact: '2023-01-01'}]->(bob)").expect("Insert relationship should succeed");

    // Check what nodes and edges exist
    let nodes_result = fixture
        .query("MATCH (n:Person) RETURN n.name")
        .expect("Query should succeed");
    log::debug!("Nodes found:");
    for row in &nodes_result.rows {
        log::debug!("  Person: {:?}", row.values.get("n.name"));
    }

    let edges_result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) RETURN a.name, b.name, r.strength, r.lastContact").expect("Query should succeed");
    log::debug!("Edges found:");
    for row in &edges_result.rows {
        log::debug!(
            "  Edge: {:?} -[KNOWS]-> {:?}, strength: {:?}, lastContact: {:?}",
            row.values.get("a.name"),
            row.values.get("b.name"),
            row.values.get("r.strength"),
            row.values.get("r.lastContact")
        );
    }

    // Check initial values
    let initial_result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) WHERE a.name = 'Alice Smith' AND b.name = 'Bob Johnson' RETURN r.strength, r.lastContact").expect("Query should succeed");

    log::debug!("Initial values:");
    for row in &initial_result.rows {
        log::debug!("  strength: {:?}", row.values.get("r.strength"));
        log::debug!("  lastContact: {:?}", row.values.get("r.lastContact"));
    }

    // Test the tutorial query with multiple SET properties in one statement
    let multi_set_result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) WHERE a.name = 'Alice Smith' AND b.name = 'Bob Johnson' SET r.strength = 'strong', r.lastContact = '2024-01-10'");
    log::debug!("Multi-SET result: {:?}", multi_set_result);

    // Test node property SET operations (like the user's John Doe example)
    fixture
        .query("INSERT (john:Person {name: 'John Doe', age: 30, email: 'john@example.com'})")
        .expect("Insert John Doe should succeed");

    let node_set_result = fixture.query("MATCH (person:Person {name: 'John Doe'}) SET person.age = 46, person.email = 'john.d@email.com'");
    log::debug!("Node SET result: {:?}", node_set_result);

    // Verify the node properties were actually updated
    let verify_result = fixture
        .query(
            "MATCH (person:Person {name: 'John Doe'}) RETURN person.name, person.age, person.email",
        )
        .expect("Verify should succeed");
    log::debug!("Verification result:");
    for row in &verify_result.rows {
        log::debug!(
            "  Name: {:?}, Age: {:?}, Email: {:?}",
            row.values.get("person.name"),
            row.values.get("person.age"),
            row.values.get("person.email")
        );
    }

    // Check updated values
    let updated_result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) WHERE a.name = 'Alice Smith' AND b.name = 'Bob Johnson' RETURN r.strength, r.lastContact").expect("Query should succeed");

    log::debug!("Updated values:");
    for row in &updated_result.rows {
        log::debug!("  strength: {:?}", row.values.get("r.strength"));
        log::debug!("  lastContact: {:?}", row.values.get("r.lastContact"));
    }

    // Verify both properties were updated
    assert_eq!(updated_result.rows.len(), 1);
    let row = &updated_result.rows[0];
    assert_eq!(row.values["r.strength"].as_string().unwrap(), "strong");
    assert_eq!(
        row.values["r.lastContact"].as_string().unwrap(),
        "2024-01-10"
    );
}

#[test]
fn test_set_operations_with_different_node_types() {
    let fixture = create_tutorial_test_fixture().expect("Failed to create test fixture");

    // Test UNION between different node types (Person and Department)
    let query = "
        MATCH (p:Person) RETURN p.name as entity_name
        UNION
        MATCH (d:Department) RETURN d.name as entity_name
    ";

    let result = fixture
        .query(query)
        .expect("UNION with different node types should succeed");

    log::debug!("Cross-type UNION results: {} rows", result.rows.len());

    // Should contain both person names and department names
    let entity_names: Vec<String> = result
        .rows
        .iter()
        .map(|row| row.values["entity_name"].as_string().unwrap().to_string())
        .collect();
    log::debug!("Entity names: {:?}", entity_names);

    // Should include at least some people and some departments
    assert!(
        result.rows.len() >= 6,
        "Should have at least 6 entities (people)"
    );
}

#[test]
fn test_set_operations_error_handling() {
    let fixture = create_tutorial_test_fixture().expect("Failed to create test fixture");

    // Test error handling for malformed queries
    let invalid_queries = vec![
        // Mismatched return types (this may or may not be caught at parse time)
        "MATCH (p:Person) RETURN p.name UNION MATCH (p:Person) RETURN p.age",
        // Empty UNION operand
        "MATCH (p:Person) RETURN p.name UNION",
        // Invalid INTERSECT syntax
        "MATCH (p:Person) RETURN p.name INTERSECT INVALID",
    ];

    for query in invalid_queries {
        match fixture.query(query) {
            Ok(_) => {
                log::debug!(
                    "Query unexpectedly succeeded (may be validation issue): {}",
                    query
                );
            }
            Err(e) => {
                log::debug!(
                    "Query correctly failed with error: {} for query: {}",
                    e,
                    query
                );
                // Error is expected for malformed queries
            }
        }
    }
}

#[test]
fn test_node_set_tutorial_example() {
    // Test the exact tutorial example that was failing
    let fixture = create_simple_test_fixture().expect("Failed to create test fixture");

    // Insert John Doe as in the tutorial
    fixture
        .query("INSERT (john:Person {name: 'John Doe', age: 30, email: 'john@example.com'})")
        .expect("Insert should succeed");

    // Test the tutorial SET query with multiple properties
    let result = fixture.query("MATCH (person:Person {name: 'John Doe'}) SET person.age = 46, person.email = 'john.d@email.com'");
    assert!(result.is_ok(), "SET query should succeed");
    let result = result.unwrap();
    assert_eq!(
        result.rows_affected, 2,
        "Should update 2 properties (counted as 2 affected rows)"
    );

    // Verify both properties were updated
    let verify = fixture
        .query(
            "MATCH (person:Person {name: 'John Doe'}) RETURN person.name, person.age, person.email",
        )
        .expect("Verify should succeed");
    assert_eq!(verify.rows.len(), 1, "Should return exactly 1 row");

    let row = &verify.rows[0];
    // Just check that the values exist and are updated - don't worry about exact types
    log::debug!(
        "Verification: Name: {:?}, Age: {:?}, Email: {:?}",
        row.values.get("person.name"),
        row.values.get("person.age"),
        row.values.get("person.email")
    );

    // Verify the properties have the correct values
    if let Some(Value::String(name)) = row.values.get("person.name") {
        assert_eq!(name, "John Doe");
    } else {
        panic!("person.name should be a string");
    }

    if let Some(Value::Number(age)) = row.values.get("person.age") {
        assert_eq!(*age, 46.0);
    } else {
        panic!("person.age should be 46.0");
    }

    if let Some(Value::String(email)) = row.values.get("person.email") {
        assert_eq!(email, "john.d@email.com");
    } else {
        panic!("person.email should be updated");
    }

    log::debug!("✅ Tutorial node SET example works correctly!");
}

#[test]
fn test_node_label_set_multiple_labels() {
    // Test the specific label SET operation from the tutorial
    let fixture = create_simple_test_fixture().expect("Failed to create test fixture");

    // Insert Alice Smith
    fixture
        .query("INSERT (alice:Person {name: 'Alice Smith', age: 28})")
        .expect("Insert should succeed");

    // Test the tutorial label SET query with multiple labels
    let result =
        fixture.query("MATCH (person:Person {name: 'Alice Smith'}) SET person:Manager:TeamLead");
    assert!(result.is_ok(), "Multiple label SET query should succeed");
    let result = result.unwrap();
    log::debug!("Label SET result rows_affected: {}", result.rows_affected);
    // Note: rows_affected may count each label as a separate operation
    assert!(
        result.rows_affected >= 2,
        "Should add at least 2 labels (Manager and TeamLead)"
    );

    // Verify both labels were added
    let verify = fixture.query("MATCH (person:Person {name: 'Alice Smith'}) RETURN person.name, LABELS(person) as all_labels").expect("Verify should succeed");
    log::debug!("Verification returned {} rows", verify.rows.len());

    for (i, row) in verify.rows.iter().enumerate() {
        log::debug!(
            "Row {}: Name: {:?}, Labels: {:?}",
            i,
            row.values.get("person.name"),
            row.values.get("all_labels")
        );
    }

    // Check if we got any rows
    if !verify.rows.is_empty() {
        let row = &verify.rows[0];
        if let Some(Value::List(labels)) = row.values.get("all_labels") {
            let label_strings: Vec<String> = labels
                .iter()
                .filter_map(|v| {
                    if let Value::String(s) = v {
                        Some(s.clone())
                    } else {
                        None
                    }
                })
                .collect();

            log::debug!("Final labels: {:?}", label_strings);
            if label_strings.contains(&"Manager".to_string())
                && label_strings.contains(&"TeamLead".to_string())
            {
                log::debug!("✅ Tutorial label SET example works correctly!");
            } else {
                log::debug!("❌ Labels not set correctly. Expected Manager and TeamLead");
            }
        } else {
            log::debug!(
                "❌ all_labels is not a list: {:?}",
                row.values.get("all_labels")
            );
        }
    } else {
        log::debug!("❌ No rows returned from verification query");
    }
}

#[test]
fn test_tutorial_remove_operations() {
    // Test the exact REMOVE operations from the tutorial
    let fixture = create_simple_test_fixture().expect("Failed to create test fixture");

    // Setup: Insert John Doe with the properties that will be removed
    fixture.query("INSERT (john:Person {name: 'John Doe', age: 30, temporaryField: 'temp_value', oldEmail: 'old@example.com', email: 'current@example.com'})").expect("Insert John Doe should succeed");

    // Setup: Insert Alice Smith and then add labels using SET
    fixture
        .query("INSERT (alice:Person {name: 'Alice Smith', age: 28})")
        .expect("Insert Alice Smith should succeed");
    fixture
        .query(
            "MATCH (alice:Person {name: 'Alice Smith'}) SET alice:Manager:TeamLead:TemporaryRole",
        )
        .expect("Adding labels should succeed");

    // Test 1: Remove properties from John Doe
    log::debug!("=== Testing Property REMOVE ===");
    let remove_props_result = fixture.query(
        "MATCH (person:Person {name: 'John Doe'}) REMOVE person.temporaryField, person.oldEmail",
    );
    log::debug!("Remove properties result: {:?}", remove_props_result);
    assert!(
        remove_props_result.is_ok(),
        "Property REMOVE should succeed"
    );

    // Verification 1: Check that properties were removed (without KEYS for now)
    let verify_props = fixture.query("MATCH (person:Person {name: 'John Doe'}) RETURN person.name, person.temporaryField, person.oldEmail").expect("Property verification should succeed");
    log::debug!("Property verification result:");
    for row in &verify_props.rows {
        log::debug!(
            "  Name: {:?}, temporaryField: {:?}, oldEmail: {:?}",
            row.values.get("person.name"),
            row.values.get("person.temporaryField"),
            row.values.get("person.oldEmail")
        );
    }

    // Verify that removed properties are null
    if let Some(row) = verify_props.rows.first() {
        // Check that removed properties are null
        assert_eq!(
            row.values.get("person.temporaryField"),
            Some(&Value::Null),
            "temporaryField should be null after REMOVE"
        );
        assert_eq!(
            row.values.get("person.oldEmail"),
            Some(&Value::Null),
            "oldEmail should be null after REMOVE"
        );

        // Check that remaining properties still exist
        assert_ne!(
            row.values.get("person.name"),
            Some(&Value::Null),
            "name should still exist"
        );
    }

    // Test 2: Remove label from Alice Smith
    log::debug!("\n=== Testing Label REMOVE ===");
    let remove_label_result =
        fixture.query("MATCH (person:Person {name: 'Alice Smith'}) REMOVE person:TemporaryRole");
    log::debug!("Remove label result: {:?}", remove_label_result);
    assert!(remove_label_result.is_ok(), "Label REMOVE should succeed");

    // Verification 2: Check that TemporaryRole label was removed
    let verify_labels = fixture.query("MATCH (person:Person {name: 'Alice Smith'}) RETURN person.name, LABELS(person) as all_labels").expect("Label verification should succeed");
    log::debug!("Label verification result:");
    for row in &verify_labels.rows {
        log::debug!(
            "  Name: {:?}, Labels: {:?}",
            row.values.get("person.name"),
            row.values.get("all_labels")
        );
    }

    // Verify that TemporaryRole was removed but other labels remain
    if let Some(row) = verify_labels.rows.first() {
        if let Some(Value::List(labels)) = row.values.get("all_labels") {
            let label_strings: Vec<String> = labels
                .iter()
                .filter_map(|v| {
                    if let Value::String(s) = v {
                        Some(s.clone())
                    } else {
                        None
                    }
                })
                .collect();

            assert!(
                !label_strings.contains(&"TemporaryRole".to_string()),
                "TemporaryRole should be removed"
            );
            assert!(
                label_strings.contains(&"Person".to_string()),
                "Person label should remain"
            );
            assert!(
                label_strings.contains(&"Manager".to_string()),
                "Manager label should remain"
            );
            assert!(
                label_strings.contains(&"TeamLead".to_string()),
                "TeamLead label should remain"
            );
        }
    }

    log::debug!("\n✅ All tutorial REMOVE operations work correctly!");
}

#[test]
fn test_iso_gql_compliant_verification() {
    // Test ISO GQL compliant verification methods from the tutorial
    let fixture = create_simple_test_fixture().expect("Failed to create test fixture");

    // Setup data for testing
    fixture.query("INSERT (john:Person {name: 'John Doe', age: 30, temporaryField: 'temp_value', oldEmail: 'old@example.com', email: 'current@example.com'})").expect("Insert John Doe should succeed");
    fixture
        .query("INSERT (alice:Person {name: 'Alice Smith', age: 28})")
        .expect("Insert Alice Smith should succeed");
    fixture
        .query(
            "MATCH (alice:Person {name: 'Alice Smith'}) SET alice:Manager:TeamLead:TemporaryRole",
        )
        .expect("Adding labels should succeed");

    // Test 1: Property verification after REMOVE
    fixture.query("MATCH (person:Person {name: 'John Doe'}) REMOVE person.temporaryField, person.oldEmail").expect("Property REMOVE should succeed");

    // ISO GQL compliant verification: Check properties are null
    let verify_props = fixture.query("MATCH (person:Person {name: 'John Doe'}) RETURN person.name, person.temporaryField, person.oldEmail").expect("Property verification should succeed");
    assert_eq!(verify_props.rows.len(), 1, "Should return exactly 1 row");

    if let Some(row) = verify_props.rows.first() {
        assert_eq!(
            row.values.get("person.temporaryField"),
            Some(&Value::Null),
            "temporaryField should be null"
        );
        assert_eq!(
            row.values.get("person.oldEmail"),
            Some(&Value::Null),
            "oldEmail should be null"
        );
    }

    // ISO GQL compliant alternative verification: WHERE clause with IS NULL
    let verify_null = fixture.query("MATCH (person:Person {name: 'John Doe'}) WHERE person.temporaryField IS NULL AND person.oldEmail IS NULL RETURN person.name, 'Properties successfully removed' as status").expect("NULL verification should succeed");
    assert_eq!(
        verify_null.rows.len(),
        1,
        "Should confirm properties are null"
    );

    // Test 2: Label verification after REMOVE
    fixture
        .query("MATCH (person:Person {name: 'Alice Smith'}) REMOVE person:TemporaryRole")
        .expect("Label REMOVE should succeed");

    // ISO GQL compliant verification: Check labels
    let verify_labels = fixture.query("MATCH (person:Person {name: 'Alice Smith'}) RETURN person.name, LABELS(person) as all_labels").expect("Label verification should succeed");
    assert!(
        verify_labels.rows.len() >= 1,
        "Should return at least 1 row"
    );

    // ISO GQL compliant alternative verification: WHERE NOT label
    let verify_no_temp = fixture.query("MATCH (person:Person {name: 'Alice Smith'}) WHERE NOT person:TemporaryRole RETURN person.name, 'TemporaryRole label successfully removed' as status").expect("Label removal verification should succeed");
    log::debug!("WHERE NOT verification rows: {}", verify_no_temp.rows.len());
    for (i, row) in verify_no_temp.rows.iter().enumerate() {
        log::debug!("  Row {}: {:?}", i, row.values);
    }

    // Accept that there might be multiple rows due to how the system works
    assert!(
        verify_no_temp.rows.len() >= 1,
        "Should confirm TemporaryRole was removed"
    );

    log::debug!("✅ All ISO GQL compliant verification methods work!");
}

#[test]
fn test_performance_with_large_result_sets() {
    let fixture = create_tutorial_test_fixture().expect("Failed to create test fixture");

    // Test set operations with potentially larger result sets
    let query = "
        MATCH (p:Person) RETURN p.name, p.age, p.salary
        UNION ALL
        MATCH (p:Person) RETURN p.name, p.age, p.salary
    ";

    let start = std::time::Instant::now();
    let result = fixture
        .query(query)
        .expect("Performance test query should succeed");
    let duration = start.elapsed();

    log::debug!(
        "Performance test: {} rows in {:?}",
        result.rows.len(),
        duration
    );

    // Should return duplicate rows (UNION ALL preserves duplicates)
    assert_eq!(
        result.rows.len(),
        12,
        "UNION ALL should return 6 people * 2 = 12 rows"
    );

    // Performance should be reasonable (less than 1 second for this small dataset)
    assert!(
        duration.as_millis() < 1000,
        "Query should complete in under 1 second"
    );
}

#[test]
fn test_detach_delete_with_relationships() {
    // Test DETACH DELETE functionality with multiple relationships
    let fixture = create_simple_test_fixture().expect("Failed to create test fixture");

    // Create a hub node with multiple relationships
    fixture
        .query("INSERT (hub:Person {name: 'Hub User', role: 'coordinator'})")
        .expect("Insert hub should succeed");
    fixture
        .query("INSERT (user1:Person {name: 'User One'})")
        .expect("Insert user1 should succeed");
    fixture
        .query("INSERT (user2:Person {name: 'User Two'})")
        .expect("Insert user2 should succeed");
    fixture
        .query("INSERT (project:Project {name: 'Test Project'})")
        .expect("Insert project should succeed");

    // Create multiple relationships to/from Hub User
    fixture.query("MATCH (hub:Person {name: 'Hub User'}), (user1:Person {name: 'User One'}) INSERT (hub)-[:KNOWS]->(user1)").expect("Create KNOWS relationship should succeed");
    fixture.query("MATCH (hub:Person {name: 'Hub User'}), (user2:Person {name: 'User Two'}) INSERT (hub)-[:KNOWS]->(user2)").expect("Create KNOWS relationship should succeed");
    fixture.query("MATCH (user2:Person {name: 'User Two'}), (hub:Person {name: 'Hub User'}) INSERT (user2)-[:REPORTS_TO]->(hub)").expect("Create REPORTS_TO relationship should succeed");
    fixture.query("MATCH (hub:Person {name: 'Hub User'}), (project:Project {name: 'Test Project'}) INSERT (hub)-[:LEADS]->(project)").expect("Create LEADS relationship should succeed");

    // Verify relationships were created
    let result = fixture.query("MATCH (hub:Person {name: 'Hub User'})-[r]-(connected) RETURN hub.name, TYPE(r) as relationship_type, connected.name as connected_to").expect("Query should succeed");
    assert_eq!(result.rows.len(), 4, "Hub User should have 4 relationships");
    log::debug!("✅ Created hub node with 4 relationships");

    // Test 1: DELETE without DETACH should fail
    let delete_result = fixture.query("MATCH (hub:Person {name: 'Hub User'}) DELETE hub");
    assert!(
        delete_result.is_err(),
        "DELETE without DETACH should fail when node has relationships"
    );
    log::debug!("✅ DELETE without DETACH correctly failed for node with relationships");

    // Verify node still exists after failed DELETE
    let result = fixture
        .query("MATCH (hub:Person {name: 'Hub User'}) RETURN hub.name")
        .expect("Query should succeed");
    assert_eq!(
        result.rows.len(),
        1,
        "Hub User should still exist after failed DELETE"
    );

    // Test 2: DETACH DELETE should succeed
    let detach_result = fixture.query("MATCH (hub:Person {name: 'Hub User'}) DETACH DELETE hub");
    assert!(
        detach_result.is_ok(),
        "DETACH DELETE should succeed even with relationships"
    );
    log::debug!("✅ DETACH DELETE succeeded for node with relationships");

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

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

    let result = fixture
        .query("MATCH (user2:Person {name: 'User Two'}) RETURN user2.name")
        .expect("Query should succeed");
    assert_eq!(result.rows.len(), 1, "User Two should still exist");

    let result = fixture
        .query("MATCH (project:Project {name: 'Test Project'}) RETURN project.name")
        .expect("Query should succeed");
    assert_eq!(result.rows.len(), 1, "Test Project should still exist");

    // Verify all relationships are gone
    let result = fixture
        .query("MATCH (user1:Person {name: 'User One'})-[r]-() RETURN r")
        .expect("Query should succeed");
    assert_eq!(
        result.rows.len(),
        0,
        "User One should have no relationships"
    );

    let result = fixture
        .query("MATCH (user2:Person {name: 'User Two'})-[r]-() RETURN r")
        .expect("Query should succeed");
    assert_eq!(
        result.rows.len(),
        0,
        "User Two should have no relationships"
    );

    let result = fixture
        .query("MATCH (project:Project {name: 'Test Project'})-[r]-() RETURN r")
        .expect("Query should succeed");
    assert_eq!(
        result.rows.len(),
        0,
        "Test Project should have no relationships"
    );

    log::debug!("✅ All DETACH DELETE tests passed!");
}

#[test]
fn test_edge_deletion_with_pattern_matching() {
    // Test edge deletion using pattern matching (recommended approach)
    let fixture = create_simple_test_fixture().expect("Failed to create test fixture");

    // Create nodes with unique names for this test
    fixture
        .query("INSERT (alice:Person {name: 'Alice Test Edge'})")
        .expect("Insert Alice");
    fixture
        .query("INSERT (bob:Person {name: 'Bob Test Edge'})")
        .expect("Insert Bob");
    fixture
        .query("INSERT (charlie:Person {name: 'Charlie Test Edge'})")
        .expect("Insert Charlie");

    // Create relationships
    fixture.query("MATCH (alice:Person {name: 'Alice Test Edge'}), (bob:Person {name: 'Bob Test Edge'}) INSERT (alice)-[:KNOWS {since: 2021}]->(bob)").expect("Create Alice->Bob");
    fixture.query("MATCH (alice:Person {name: 'Alice Test Edge'}), (charlie:Person {name: 'Charlie Test Edge'}) INSERT (alice)-[:KNOWS {since: 2020}]->(charlie)").expect("Create Alice->Charlie");
    fixture.query("MATCH (bob:Person {name: 'Bob Test Edge'}), (charlie:Person {name: 'Charlie Test Edge'}) INSERT (bob)-[:KNOWS {since: 2019}]->(charlie)").expect("Create Bob->Charlie");

    // Verify all relationships exist for our test nodes
    let result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) WHERE a.name IN ['Alice Test Edge', 'Bob Test Edge'] AND b.name IN ['Bob Test Edge', 'Charlie Test Edge'] RETURN a.name, TYPE(r) as rel_type, b.name ORDER BY a.name, b.name").expect("Query should succeed");
    assert_eq!(result.rows.len(), 3, "Should have 3 KNOWS relationships");
    log::debug!("✅ Created 3 KNOWS relationships");

    // Test 1: Delete using pattern matching (recommended approach)
    log::debug!("Testing delete with pattern matching...");
    let delete_result = fixture.query("MATCH (alice:Person {name: 'Alice Test Edge'})-[r:KNOWS]->(charlie:Person {name: 'Charlie Test Edge'}) DELETE r");
    assert!(
        delete_result.is_ok(),
        "Edge deletion with pattern matching should succeed"
    );
    log::debug!("✅ DELETE edge with pattern matching succeeded");

    // Verify specific relationship was deleted
    let result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) WHERE a.name = 'Alice Test Edge' AND b.name = 'Charlie Test Edge' RETURN a.name, TYPE(r) as rel_type, b.name").expect("Query should succeed");
    assert_eq!(
        result.rows.len(),
        0,
        "Alice->Charlie relationship should be deleted"
    );

    // Test 2: Delete another edge using pattern matching
    let delete_result = fixture.query("MATCH (alice:Person {name: 'Alice Test Edge'})-[r:KNOWS]->(bob:Person {name: 'Bob Test Edge'}) DELETE r");
    assert!(
        delete_result.is_ok(),
        "Second edge deletion with pattern matching should succeed"
    );
    log::debug!("✅ Second DELETE edge with pattern matching succeeded");

    // Verify second relationship was deleted
    let result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) WHERE a.name = 'Alice Test Edge' AND b.name = 'Bob Test Edge' RETURN a.name, TYPE(r) as rel_type, b.name").expect("Query should succeed");
    assert_eq!(
        result.rows.len(),
        0,
        "Alice->Bob relationship should be deleted"
    );

    // Verify Bob->Charlie still exists
    let result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) WHERE a.name = 'Bob Test Edge' AND b.name = 'Charlie Test Edge' RETURN a.name, TYPE(r) as rel_type, b.name").expect("Query should succeed");
    assert_eq!(
        result.rows.len(),
        1,
        "Bob->Charlie relationship should still exist"
    );

    // Verify nodes still exist
    let result = fixture.query("MATCH (p:Person) WHERE p.name IN ['Alice Test Edge', 'Bob Test Edge', 'Charlie Test Edge'] RETURN p.name ORDER BY p.name").expect("Query should succeed");
    assert_eq!(
        result.rows.len(),
        3,
        "All 3 test Person nodes should still exist"
    );

    log::debug!("✅ Edge deletion with pattern matching works correctly!");
}

#[test]
fn test_edge_deletion_with_where_clause_bug() {
    // Test that edge deletion with WHERE clause works correctly
    // This test currently fails due to a bug in WHERE clause evaluation
    let fixture = create_simple_test_fixture().expect("Failed to create test fixture");

    // Create nodes
    fixture
        .query("INSERT (alice:Person {name: 'Alice WHERE Test'})")
        .expect("Insert Alice");
    fixture
        .query("INSERT (bob:Person {name: 'Bob WHERE Test'})")
        .expect("Insert Bob");
    fixture
        .query("INSERT (charlie:Person {name: 'Charlie WHERE Test'})")
        .expect("Insert Charlie");

    // Create relationships
    fixture.query("MATCH (alice:Person {name: 'Alice WHERE Test'}), (bob:Person {name: 'Bob WHERE Test'}) INSERT (alice)-[:KNOWS {since: 2021}]->(bob)").expect("Create Alice->Bob");
    fixture.query("MATCH (alice:Person {name: 'Alice WHERE Test'}), (charlie:Person {name: 'Charlie WHERE Test'}) INSERT (alice)-[:KNOWS {since: 2020}]->(charlie)").expect("Create Alice->Charlie");
    fixture.query("MATCH (bob:Person {name: 'Bob WHERE Test'}), (charlie:Person {name: 'Charlie WHERE Test'}) INSERT (bob)-[:KNOWS {since: 2019}]->(charlie)").expect("Create Bob->Charlie");

    // Verify all relationships exist
    let result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) WHERE a.name IN ['Alice WHERE Test', 'Bob WHERE Test'] RETURN a.name, TYPE(r) as rel_type, b.name ORDER BY a.name, b.name").expect("Query should succeed");
    assert_eq!(result.rows.len(), 3, "Should have 3 KNOWS relationships");
    log::debug!("✅ Created 3 KNOWS relationships");

    // Delete specific relationship using WHERE clause - THIS SHOULD WORK
    let delete_result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) WHERE a.name = 'Alice WHERE Test' AND b.name = 'Charlie WHERE Test' DELETE r");
    assert!(
        delete_result.is_ok(),
        "Edge deletion with WHERE clause should succeed"
    );
    log::debug!("✅ DELETE edge with WHERE clause succeeded");

    // Verify specific relationship was deleted
    let result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) WHERE a.name = 'Alice WHERE Test' AND b.name = 'Charlie WHERE Test' RETURN a.name, TYPE(r) as rel_type, b.name").expect("Query should succeed");
    assert_eq!(
        result.rows.len(),
        0,
        "Alice->Charlie relationship should be deleted"
    );

    // Verify other relationships still exist
    let result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) WHERE a.name = 'Alice WHERE Test' AND b.name = 'Bob WHERE Test' RETURN a.name, TYPE(r) as rel_type, b.name").expect("Query should succeed");
    assert_eq!(
        result.rows.len(),
        1,
        "Alice->Bob relationship should still exist"
    );

    let result = fixture.query("MATCH (a:Person)-[r:KNOWS]->(b:Person) WHERE a.name = 'Bob WHERE Test' AND b.name = 'Charlie WHERE Test' RETURN a.name, TYPE(r) as rel_type, b.name").expect("Query should succeed");
    assert_eq!(
        result.rows.len(),
        1,
        "Bob->Charlie relationship should still exist"
    );

    log::debug!("✅ Edge deletion with WHERE clause works correctly!");
}