sochdb-query 2.0.7

SochDB query engine (sync-first execution and vector query planning)
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
// SPDX-License-Identifier: AGPL-3.0-or-later
// SochDB - LLM-Optimized Embedded Database
// Copyright (C) 2026 Sushanth Reddy Vanagala (https://github.com/sushanthpy)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! SQL Integration Tests
//!
//! Comprehensive test suite for SQL query execution with proper setup/teardown.
//! Tests cover all SQL statement types: SELECT, INSERT, UPDATE, DELETE, CREATE, DROP.

use sochdb_core::SochValue;
use sochdb_query::sql::{ExecutionResult, SqlError, SqlExecutor};

/// Test fixture providing setup and teardown for SQL tests
struct SqlTestFixture {
    executor: SqlExecutor,
}

impl SqlTestFixture {
    /// Create a new test fixture with empty database
    fn new() -> Self {
        Self {
            executor: SqlExecutor::new(),
        }
    }

    /// Setup a users table with sample data
    fn setup_users_table(&mut self) -> Result<(), SqlError> {
        self.executor.execute(
            "CREATE TABLE users (
            id INTEGER,
            name TEXT,
            email TEXT,
            age INTEGER,
            active INTEGER
        )",
        )?;

        self.executor.execute("INSERT INTO users (id, name, email, age, active) VALUES (1, 'Alice', 'alice@example.com', 30, 1)")?;
        self.executor.execute("INSERT INTO users (id, name, email, age, active) VALUES (2, 'Bob', 'bob@example.com', 25, 1)")?;
        self.executor.execute("INSERT INTO users (id, name, email, age, active) VALUES (3, 'Charlie', 'charlie@example.com', 35, 0)")?;
        self.executor.execute("INSERT INTO users (id, name, email, age, active) VALUES (4, 'Diana', 'diana@example.com', 28, 1)")?;
        self.executor.execute("INSERT INTO users (id, name, email, age, active) VALUES (5, 'Eve', 'eve@example.com', 32, 0)")?;

        Ok(())
    }

    /// Setup a products table with sample data
    fn setup_products_table(&mut self) -> Result<(), SqlError> {
        self.executor.execute(
            "CREATE TABLE products (
            id INTEGER,
            name TEXT,
            price REAL,
            stock INTEGER,
            category TEXT
        )",
        )?;

        self.executor.execute("INSERT INTO products (id, name, price, stock, category) VALUES (1, 'Laptop', 999.99, 50, 'Electronics')")?;
        self.executor.execute("INSERT INTO products (id, name, price, stock, category) VALUES (2, 'Mouse', 29.99, 200, 'Electronics')")?;
        self.executor.execute("INSERT INTO products (id, name, price, stock, category) VALUES (3, 'Keyboard', 79.99, 150, 'Electronics')")?;
        self.executor.execute("INSERT INTO products (id, name, price, stock, category) VALUES (4, 'Desk', 299.99, 30, 'Furniture')")?;
        self.executor.execute("INSERT INTO products (id, name, price, stock, category) VALUES (5, 'Chair', 199.99, 75, 'Furniture')")?;

        Ok(())
    }

    /// Setup an orders table with sample data
    #[allow(dead_code)]
    fn setup_orders_table(&mut self) -> Result<(), SqlError> {
        self.executor.execute(
            "CREATE TABLE orders (
            id INTEGER,
            user_id INTEGER,
            product_id INTEGER,
            quantity INTEGER,
            total REAL
        )",
        )?;

        self.executor.execute("INSERT INTO orders (id, user_id, product_id, quantity, total) VALUES (1, 1, 1, 1, 999.99)")?;
        self.executor.execute("INSERT INTO orders (id, user_id, product_id, quantity, total) VALUES (2, 1, 2, 2, 59.98)")?;
        self.executor.execute("INSERT INTO orders (id, user_id, product_id, quantity, total) VALUES (3, 2, 3, 1, 79.99)")?;
        self.executor.execute("INSERT INTO orders (id, user_id, product_id, quantity, total) VALUES (4, 3, 4, 1, 299.99)")?;
        self.executor.execute("INSERT INTO orders (id, user_id, product_id, quantity, total) VALUES (5, 4, 5, 2, 399.98)")?;

        Ok(())
    }

    /// Teardown by dropping all tables
    fn teardown(&mut self) {
        let _ = self.executor.execute("DROP TABLE IF EXISTS users");
        let _ = self.executor.execute("DROP TABLE IF EXISTS products");
        let _ = self.executor.execute("DROP TABLE IF EXISTS orders");
    }

    /// Execute SQL and return result
    fn exec(&mut self, sql: &str) -> Result<ExecutionResult, SqlError> {
        self.executor.execute(sql)
    }

    /// Get row count from result
    fn row_count(&self, result: &ExecutionResult) -> usize {
        match result {
            ExecutionResult::Rows { rows, .. } => rows.len(),
            ExecutionResult::RowsAffected(n) => *n,
            ExecutionResult::Ok => 0,
        }
    }
}

impl Drop for SqlTestFixture {
    fn drop(&mut self) {
        self.teardown();
    }
}

// ============================================================================
// CREATE TABLE Tests
// ============================================================================

#[test]
fn test_create_table_basic() {
    let mut fixture = SqlTestFixture::new();

    let result = fixture.exec("CREATE TABLE test (id INTEGER, name TEXT)");
    assert!(result.is_ok(), "CREATE TABLE should succeed");
}

#[test]
fn test_create_table_if_not_exists() {
    let mut fixture = SqlTestFixture::new();

    fixture.exec("CREATE TABLE test (id INTEGER)").unwrap();

    // Should not error with IF NOT EXISTS
    let result = fixture.exec("CREATE TABLE IF NOT EXISTS test (id INTEGER)");
    assert!(result.is_ok());
}

#[test]
fn test_create_table_duplicate_error() {
    let mut fixture = SqlTestFixture::new();

    fixture.exec("CREATE TABLE test (id INTEGER)").unwrap();

    // Should error without IF NOT EXISTS
    let result = fixture.exec("CREATE TABLE test (id INTEGER)");
    assert!(result.is_err());
}

#[test]
fn test_create_table_with_multiple_columns() {
    let mut fixture = SqlTestFixture::new();

    let result = fixture.exec(
        "CREATE TABLE employees (
        id INTEGER,
        first_name TEXT,
        last_name TEXT,
        salary REAL,
        department TEXT,
        hire_date TEXT
    )",
    );
    assert!(result.is_ok());

    // Insert a row to verify columns work
    let insert = fixture.exec(
        "INSERT INTO employees (id, first_name, last_name, salary, department, hire_date) 
         VALUES (1, 'John', 'Doe', 75000.00, 'Engineering', '2024-01-15')",
    );
    assert!(insert.is_ok());
}

// ============================================================================
// INSERT Tests
// ============================================================================

#[test]
fn test_insert_basic() {
    let mut fixture = SqlTestFixture::new();
    fixture
        .exec("CREATE TABLE test (id INTEGER, name TEXT)")
        .unwrap();

    let result = fixture.exec("INSERT INTO test (id, name) VALUES (1, 'Alice')");
    assert!(result.is_ok());
    assert_eq!(fixture.row_count(&result.unwrap()), 1);
}

#[test]
fn test_insert_multiple_rows() {
    let mut fixture = SqlTestFixture::new();
    fixture
        .exec("CREATE TABLE test (id INTEGER, value TEXT)")
        .unwrap();

    for i in 1..=100 {
        let sql = format!("INSERT INTO test (id, value) VALUES ({}, 'value{}')", i, i);
        fixture.exec(&sql).unwrap();
    }

    let result = fixture.exec("SELECT * FROM test").unwrap();
    assert_eq!(fixture.row_count(&result), 100);
}

#[test]
fn test_insert_null_values() {
    let mut fixture = SqlTestFixture::new();
    fixture
        .exec("CREATE TABLE test (id INTEGER, optional TEXT)")
        .unwrap();

    let result = fixture.exec("INSERT INTO test (id, optional) VALUES (1, NULL)");
    assert!(result.is_ok());

    let select = fixture.exec("SELECT * FROM test").unwrap();
    if let ExecutionResult::Rows { rows, .. } = select {
        assert_eq!(rows.len(), 1);
        let row = &rows[0];
        assert!(matches!(row.get("optional"), Some(SochValue::Null)));
    }
}

#[test]
fn test_insert_with_expressions() {
    let mut fixture = SqlTestFixture::new();
    fixture
        .exec("CREATE TABLE test (id INTEGER, computed INTEGER)")
        .unwrap();

    // Insert with arithmetic expression
    let result = fixture.exec("INSERT INTO test (id, computed) VALUES (1, 10 + 5 * 2)");
    assert!(result.is_ok());

    let select = fixture.exec("SELECT * FROM test").unwrap();
    if let ExecutionResult::Rows { rows, .. } = select {
        let row = &rows[0];
        // 10 + 5 * 2 = 20 (multiplication has precedence)
        assert!(matches!(row.get("computed"), Some(SochValue::Int(20))));
    }
}

// ============================================================================
// SELECT Tests
// ============================================================================

#[test]
fn test_select_all() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec("SELECT * FROM users").unwrap();
    assert_eq!(fixture.row_count(&result), 5);
}

#[test]
fn test_select_specific_columns() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec("SELECT name, email FROM users").unwrap();
    if let ExecutionResult::Rows { columns, rows } = result {
        assert_eq!(columns.len(), 2);
        assert!(columns.contains(&"name".to_string()));
        assert!(columns.contains(&"email".to_string()));
        assert_eq!(rows.len(), 5);
    } else {
        panic!("Expected rows");
    }
}

#[test]
fn test_select_with_where_equals() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec("SELECT * FROM users WHERE id = 1").unwrap();
    if let ExecutionResult::Rows { rows, .. } = result {
        assert_eq!(rows.len(), 1);
        let row = &rows[0];
        assert!(matches!(row.get("name"), Some(SochValue::Text(s)) if s == "Alice"));
    } else {
        panic!("Expected rows");
    }
}

#[test]
fn test_select_with_where_greater_than() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec("SELECT * FROM users WHERE age > 30").unwrap();
    assert_eq!(fixture.row_count(&result), 2); // Charlie (35), Eve (32)
}

#[test]
fn test_select_with_where_less_than() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec("SELECT * FROM users WHERE age < 30").unwrap();
    assert_eq!(fixture.row_count(&result), 2); // Bob (25), Diana (28)
}

#[test]
fn test_select_with_where_and() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("SELECT * FROM users WHERE age > 25 AND active = 1")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 2); // Alice (30, active), Diana (28, active)
}

#[test]
fn test_select_with_where_or() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("SELECT * FROM users WHERE name = 'Alice' OR name = 'Bob'")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 2);
}

#[test]
fn test_select_with_where_between() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("SELECT * FROM users WHERE age BETWEEN 28 AND 32")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 3); // Alice (30), Diana (28), Eve (32)
}

#[test]
fn test_select_with_where_in() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("SELECT * FROM users WHERE id IN (1, 3, 5)")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 3);
}

#[test]
fn test_select_with_where_like() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("SELECT * FROM users WHERE email LIKE '%@example.com'")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 5); // All emails match
}

#[test]
fn test_select_with_order_by_asc() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("SELECT * FROM users ORDER BY age ASC")
        .unwrap();
    if let ExecutionResult::Rows { rows, .. } = result {
        let ages: Vec<i64> = rows
            .iter()
            .filter_map(|r| r.get("age"))
            .filter_map(|v| {
                if let SochValue::Int(n) = v {
                    Some(*n)
                } else {
                    None
                }
            })
            .collect();
        assert_eq!(ages, vec![25, 28, 30, 32, 35]);
    }
}

#[test]
fn test_select_with_order_by_desc() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("SELECT * FROM users ORDER BY age DESC")
        .unwrap();
    if let ExecutionResult::Rows { rows, .. } = result {
        let ages: Vec<i64> = rows
            .iter()
            .filter_map(|r| r.get("age"))
            .filter_map(|v| {
                if let SochValue::Int(n) = v {
                    Some(*n)
                } else {
                    None
                }
            })
            .collect();
        assert_eq!(ages, vec![35, 32, 30, 28, 25]);
    }
}

#[test]
fn test_select_with_limit() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec("SELECT * FROM users LIMIT 3").unwrap();
    assert_eq!(fixture.row_count(&result), 3);
}

#[test]
fn test_select_with_offset() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("SELECT * FROM users LIMIT 3 OFFSET 2")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 3);
}

#[test]
fn test_select_with_functions() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    // Test UPPER function
    let result = fixture
        .exec("SELECT UPPER(name) FROM users WHERE id = 1")
        .unwrap();
    if let ExecutionResult::Rows { rows, .. } = result {
        assert!(
            rows[0]
                .values()
                .any(|v| matches!(v, SochValue::Text(s) if s == "ALICE"))
        );
    }
}

#[test]
fn test_select_with_lower_function() {
    let mut fixture = SqlTestFixture::new();
    fixture.exec("CREATE TABLE test (name TEXT)").unwrap();
    fixture
        .exec("INSERT INTO test (name) VALUES ('HELLO WORLD')")
        .unwrap();

    let result = fixture.exec("SELECT LOWER(name) FROM test").unwrap();
    if let ExecutionResult::Rows { rows, .. } = result {
        assert!(
            rows[0]
                .values()
                .any(|v| matches!(v, SochValue::Text(s) if s == "hello world"))
        );
    }
}

#[test]
fn test_select_with_length_function() {
    let mut fixture = SqlTestFixture::new();
    fixture.exec("CREATE TABLE test (name TEXT)").unwrap();
    fixture
        .exec("INSERT INTO test (name) VALUES ('Hello')")
        .unwrap();

    let result = fixture.exec("SELECT LENGTH(name) FROM test").unwrap();
    if let ExecutionResult::Rows { rows, .. } = result {
        assert!(rows[0].values().any(|v| matches!(v, SochValue::Int(5))));
    }
}

#[test]
fn test_select_with_coalesce() {
    let mut fixture = SqlTestFixture::new();
    fixture
        .exec("CREATE TABLE test (a INTEGER, b INTEGER)")
        .unwrap();
    fixture
        .exec("INSERT INTO test (a, b) VALUES (NULL, 5)")
        .unwrap();

    let result = fixture.exec("SELECT COALESCE(a, b) FROM test").unwrap();
    if let ExecutionResult::Rows { rows, .. } = result {
        assert!(rows[0].values().any(|v| matches!(v, SochValue::Int(5))));
    }
}

#[test]
fn test_select_with_arithmetic() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_products_table().unwrap();

    let result = fixture
        .exec("SELECT price * 1.1 FROM products WHERE id = 1")
        .unwrap();
    if let ExecutionResult::Rows { rows, .. } = result {
        // Laptop price 999.99 * 1.1 = 1099.989
        let value = rows[0].values().next().unwrap();
        if let SochValue::Float(f) = value {
            assert!((*f - 1099.989).abs() < 0.01);
        }
    }
}

// ============================================================================
// UPDATE Tests
// ============================================================================

#[test]
fn test_update_single_row() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("UPDATE users SET age = 31 WHERE id = 1")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 1);

    let select = fixture.exec("SELECT age FROM users WHERE id = 1").unwrap();
    if let ExecutionResult::Rows { rows, .. } = select {
        assert!(matches!(rows[0].get("age"), Some(SochValue::Int(31))));
    }
}

#[test]
fn test_update_multiple_columns() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("UPDATE users SET name = 'Alicia', age = 31 WHERE id = 1")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 1);

    let select = fixture.exec("SELECT * FROM users WHERE id = 1").unwrap();
    if let ExecutionResult::Rows { rows, .. } = select {
        assert!(matches!(rows[0].get("name"), Some(SochValue::Text(s)) if s == "Alicia"));
        assert!(matches!(rows[0].get("age"), Some(SochValue::Int(31))));
    }
}

#[test]
fn test_update_multiple_rows() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("UPDATE users SET active = 1 WHERE active = 0")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 2); // Charlie and Eve

    let select = fixture
        .exec("SELECT * FROM users WHERE active = 0")
        .unwrap();
    assert_eq!(fixture.row_count(&select), 0); // No more inactive users
}

#[test]
fn test_update_all_rows() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec("UPDATE users SET active = 0").unwrap();
    assert_eq!(fixture.row_count(&result), 5);
}

#[test]
fn test_update_with_expression() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("UPDATE users SET age = age + 1 WHERE id = 1")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 1);

    let select = fixture.exec("SELECT age FROM users WHERE id = 1").unwrap();
    if let ExecutionResult::Rows { rows, .. } = select {
        assert!(matches!(rows[0].get("age"), Some(SochValue::Int(31)))); // Was 30
    }
}

// ============================================================================
// DELETE Tests
// ============================================================================

#[test]
fn test_delete_single_row() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec("DELETE FROM users WHERE id = 1").unwrap();
    assert_eq!(fixture.row_count(&result), 1);

    let select = fixture.exec("SELECT * FROM users").unwrap();
    assert_eq!(fixture.row_count(&select), 4);
}

#[test]
fn test_delete_multiple_rows() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec("DELETE FROM users WHERE active = 0").unwrap();
    assert_eq!(fixture.row_count(&result), 2); // Charlie and Eve

    let select = fixture.exec("SELECT * FROM users").unwrap();
    assert_eq!(fixture.row_count(&select), 3);
}

#[test]
fn test_delete_all_rows() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec("DELETE FROM users").unwrap();
    assert_eq!(fixture.row_count(&result), 5);

    let select = fixture.exec("SELECT * FROM users").unwrap();
    assert_eq!(fixture.row_count(&select), 0);
}

#[test]
fn test_delete_with_complex_where() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture
        .exec("DELETE FROM users WHERE age > 30 OR active = 0")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 2); // Charlie (35, inactive=0), Eve (32, inactive=0)

    // Remaining: Alice (30, active), Bob (25, active), Diana (28, active)
}

// ============================================================================
// DROP TABLE Tests
// ============================================================================

#[test]
fn test_drop_table() {
    let mut fixture = SqlTestFixture::new();
    fixture.exec("CREATE TABLE test (id INTEGER)").unwrap();

    let result = fixture.exec("DROP TABLE test");
    assert!(result.is_ok());

    // Table should no longer exist
    let select = fixture.exec("SELECT * FROM test");
    assert!(select.is_err());
}

#[test]
fn test_drop_table_if_exists() {
    let mut fixture = SqlTestFixture::new();

    // Should not error even if table doesn't exist
    let result = fixture.exec("DROP TABLE IF EXISTS nonexistent");
    assert!(result.is_ok());
}

#[test]
fn test_drop_table_error_if_not_exists() {
    let mut fixture = SqlTestFixture::new();

    let result = fixture.exec("DROP TABLE nonexistent");
    assert!(result.is_err());
}

// ============================================================================
// Complex Query Tests
// ============================================================================

#[test]
fn test_complex_query_with_multiple_conditions() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec(
        "SELECT name, age FROM users WHERE (age >= 28 AND age <= 32) AND active = 1 ORDER BY age DESC"
    ).unwrap();

    if let ExecutionResult::Rows { rows, .. } = result {
        // Alice (30, active), Diana (28, active)
        assert_eq!(rows.len(), 2);

        let ages: Vec<i64> = rows
            .iter()
            .filter_map(|r| r.get("age"))
            .filter_map(|v| {
                if let SochValue::Int(n) = v {
                    Some(*n)
                } else {
                    None
                }
            })
            .collect();
        assert_eq!(ages, vec![30, 28]); // Ordered DESC
    }
}

#[test]
fn test_workflow_crud_operations() {
    let mut fixture = SqlTestFixture::new();

    // Create
    fixture
        .exec("CREATE TABLE items (id INTEGER, name TEXT, quantity INTEGER)")
        .unwrap();

    // Insert
    fixture
        .exec("INSERT INTO items (id, name, quantity) VALUES (1, 'Widget', 100)")
        .unwrap();
    fixture
        .exec("INSERT INTO items (id, name, quantity) VALUES (2, 'Gadget', 50)")
        .unwrap();

    // Read
    let result = fixture.exec("SELECT * FROM items").unwrap();
    assert_eq!(fixture.row_count(&result), 2);

    // Update
    fixture
        .exec("UPDATE items SET quantity = 75 WHERE id = 2")
        .unwrap();
    let result = fixture
        .exec("SELECT quantity FROM items WHERE id = 2")
        .unwrap();
    if let ExecutionResult::Rows { rows, .. } = result {
        assert!(matches!(rows[0].get("quantity"), Some(SochValue::Int(75))));
    }

    // Delete
    fixture.exec("DELETE FROM items WHERE id = 1").unwrap();
    let result = fixture.exec("SELECT * FROM items").unwrap();
    assert_eq!(fixture.row_count(&result), 1);

    // Drop
    fixture.exec("DROP TABLE items").unwrap();
}

#[test]
fn test_products_price_filtering() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_products_table().unwrap();

    // Find products under $100
    let result = fixture
        .exec("SELECT * FROM products WHERE price < 100.0")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 2); // Mouse (29.99), Keyboard (79.99)

    // Find electronics
    let result = fixture
        .exec("SELECT * FROM products WHERE category = 'Electronics'")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 3);
}

#[test]
fn test_null_handling() {
    let mut fixture = SqlTestFixture::new();
    fixture
        .exec("CREATE TABLE nullable (id INTEGER, value TEXT)")
        .unwrap();
    fixture
        .exec("INSERT INTO nullable (id, value) VALUES (1, 'not null')")
        .unwrap();
    fixture
        .exec("INSERT INTO nullable (id, value) VALUES (2, NULL)")
        .unwrap();

    // IS NULL
    let result = fixture
        .exec("SELECT * FROM nullable WHERE value IS NULL")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 1);

    // IS NOT NULL
    let result = fixture
        .exec("SELECT * FROM nullable WHERE value IS NOT NULL")
        .unwrap();
    assert_eq!(fixture.row_count(&result), 1);
}

#[test]
fn test_case_expression() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec(
        "SELECT name, CASE WHEN age < 30 THEN 'young' WHEN age < 35 THEN 'middle' ELSE 'senior' END FROM users"
    ).unwrap();

    assert_eq!(fixture.row_count(&result), 5);
}

// ============================================================================
// Error Handling Tests
// ============================================================================

#[test]
fn test_error_select_nonexistent_table() {
    let mut fixture = SqlTestFixture::new();

    let result = fixture.exec("SELECT * FROM nonexistent");
    assert!(result.is_err());
}

#[test]
fn test_error_insert_nonexistent_table() {
    let mut fixture = SqlTestFixture::new();

    let result = fixture.exec("INSERT INTO nonexistent (id) VALUES (1)");
    assert!(result.is_err());
}

#[test]
fn test_error_update_nonexistent_table() {
    let mut fixture = SqlTestFixture::new();

    let result = fixture.exec("UPDATE nonexistent SET id = 1");
    assert!(result.is_err());
}

#[test]
fn test_error_delete_nonexistent_table() {
    let mut fixture = SqlTestFixture::new();

    let result = fixture.exec("DELETE FROM nonexistent");
    assert!(result.is_err());
}

#[test]
fn test_syntax_error_handling() {
    let mut fixture = SqlTestFixture::new();

    let result = fixture.exec("SELEKT * FROM users"); // Typo
    assert!(result.is_err());
}

// ============================================================================
// Performance/Stress Tests
// ============================================================================

#[test]
fn test_bulk_insert_1000_rows() {
    let mut fixture = SqlTestFixture::new();
    fixture
        .exec("CREATE TABLE bulk (id INTEGER, data TEXT)")
        .unwrap();

    for i in 0..1000 {
        let sql = format!("INSERT INTO bulk (id, data) VALUES ({}, 'data_{}')", i, i);
        fixture.exec(&sql).unwrap();
    }

    let result = fixture.exec("SELECT * FROM bulk").unwrap();
    assert_eq!(fixture.row_count(&result), 1000);
}

#[test]
fn test_select_with_many_conditions() {
    let mut fixture = SqlTestFixture::new();
    fixture.setup_users_table().unwrap();

    let result = fixture.exec(
        "SELECT * FROM users WHERE id >= 1 AND id <= 5 AND age >= 25 AND age <= 35 AND active = 1"
    ).unwrap();

    // Should return Alice (30), Bob (25), Diana (28)
    assert_eq!(fixture.row_count(&result), 3);
}