nirv-engine 0.1.0

Universal data virtualization and compute orchestration engine with SQL Server, PostgreSQL, REST API, and file system connectors
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
use async_trait::async_trait;
use std::collections::HashMap;
use std::time::{Duration, Instant};
use crate::connectors::connector_trait::{Connector, ConnectorInitConfig, ConnectorCapabilities};
use crate::utils::{
    types::{
        ConnectorType, ConnectorQuery, QueryResult, Schema, ColumnMetadata, 
        DataType, Row, Value, Index, QueryOperation, PredicateOperator
    },
    error::{ConnectorError, NirvResult},
};

/// Mock connector for testing with deterministic in-memory data
#[derive(Debug)]
pub struct MockConnector {
    connected: bool,
    test_data: HashMap<String, TestTable>,
    connection_delay_ms: u64,
}

/// Test table structure for mock data
#[derive(Debug, Clone)]
struct TestTable {
    schema: Schema,
    rows: Vec<Row>,
}

impl MockConnector {
    /// Create a new mock connector with default test data
    pub fn new() -> Self {
        let mut connector = Self {
            connected: false,
            test_data: HashMap::new(),
            connection_delay_ms: 10, // Simulate small connection delay
        };
        
        connector.initialize_test_data();
        connector
    }
    
    /// Create a mock connector with custom connection delay
    pub fn with_delay(delay_ms: u64) -> Self {
        let mut connector = Self::new();
        connector.connection_delay_ms = delay_ms;
        connector
    }
    
    /// Add custom test data for a table
    pub fn add_test_data(&mut self, table_name: &str, rows: Vec<Vec<Value>>) {
        self.add_test_data_with_schema(table_name, rows, None);
    }
    
    /// Add custom test data for a table with column names
    pub fn add_test_data_with_columns(&mut self, table_name: &str, column_names: Vec<&str>, rows: Vec<Vec<Value>>) {
        // Create schema with provided column names
        let columns = if let Some(first_row) = rows.first() {
            first_row.iter().enumerate().map(|(i, value)| {
                let data_type = match value {
                    Value::Integer(_) => DataType::Integer,
                    Value::Float(_) => DataType::Float,
                    Value::Text(_) => DataType::Text,
                    Value::Boolean(_) => DataType::Boolean,
                    Value::Date(_) => DataType::Date,
                    Value::DateTime(_) => DataType::DateTime,
                    Value::Json(_) => DataType::Json,
                    Value::Binary(_) => DataType::Binary,
                    Value::Null => DataType::Text, // Default for null
                };
                
                let column_name = column_names.get(i).unwrap_or(&format!("column_{}", i).as_str()).to_string();
                
                ColumnMetadata {
                    name: column_name,
                    data_type,
                    nullable: true,
                }
            }).collect()
        } else {
            vec![]
        };
        
        let schema = Schema {
            name: table_name.to_string(),
            columns,
            primary_key: None,
            indexes: vec![],
        };
        
        let table_rows: Vec<Row> = rows.into_iter().map(Row::new).collect();
        
        self.test_data.insert(table_name.to_string(), TestTable {
            schema,
            rows: table_rows,
        });
    }
    
    /// Add custom test data for a table with optional schema
    fn add_test_data_with_schema(&mut self, table_name: &str, rows: Vec<Vec<Value>>, _schema: Option<Schema>) {
        // Create a simple schema based on the first row
        let columns = if let Some(first_row) = rows.first() {
            first_row.iter().enumerate().map(|(i, value)| {
                let data_type = match value {
                    Value::Integer(_) => DataType::Integer,
                    Value::Float(_) => DataType::Float,
                    Value::Text(_) => DataType::Text,
                    Value::Boolean(_) => DataType::Boolean,
                    Value::Date(_) => DataType::Date,
                    Value::DateTime(_) => DataType::DateTime,
                    Value::Json(_) => DataType::Json,
                    Value::Binary(_) => DataType::Binary,
                    Value::Null => DataType::Text, // Default for null
                };
                
                ColumnMetadata {
                    name: format!("column_{}", i),
                    data_type,
                    nullable: true,
                }
            }).collect()
        } else {
            vec![]
        };
        
        let schema = Schema {
            name: table_name.to_string(),
            columns,
            primary_key: None,
            indexes: vec![],
        };
        
        let table_rows: Vec<Row> = rows.into_iter().map(Row::new).collect();
        
        self.test_data.insert(table_name.to_string(), TestTable {
            schema,
            rows: table_rows,
        });
    }
    
    /// Initialize deterministic test data
    fn initialize_test_data(&mut self) {
        // Users table
        let users_schema = Schema {
            name: "users".to_string(),
            columns: vec![
                ColumnMetadata {
                    name: "id".to_string(),
                    data_type: DataType::Integer,
                    nullable: false,
                },
                ColumnMetadata {
                    name: "name".to_string(),
                    data_type: DataType::Text,
                    nullable: false,
                },
                ColumnMetadata {
                    name: "email".to_string(),
                    data_type: DataType::Text,
                    nullable: true,
                },
                ColumnMetadata {
                    name: "age".to_string(),
                    data_type: DataType::Integer,
                    nullable: true,
                },
                ColumnMetadata {
                    name: "active".to_string(),
                    data_type: DataType::Boolean,
                    nullable: false,
                },
            ],
            primary_key: Some(vec!["id".to_string()]),
            indexes: vec![
                Index {
                    name: "idx_users_email".to_string(),
                    columns: vec!["email".to_string()],
                    unique: true,
                },
            ],
        };
        
        let users_rows = vec![
            Row::new(vec![
                Value::Integer(1),
                Value::Text("Alice Johnson".to_string()),
                Value::Text("alice@example.com".to_string()),
                Value::Integer(30),
                Value::Boolean(true),
            ]),
            Row::new(vec![
                Value::Integer(2),
                Value::Text("Bob Smith".to_string()),
                Value::Text("bob@example.com".to_string()),
                Value::Integer(25),
                Value::Boolean(true),
            ]),
            Row::new(vec![
                Value::Integer(3),
                Value::Text("Charlie Brown".to_string()),
                Value::Null,
                Value::Integer(35),
                Value::Boolean(false),
            ]),
        ];
        
        self.test_data.insert("users".to_string(), TestTable {
            schema: users_schema,
            rows: users_rows,
        });
        
        // Products table
        let products_schema = Schema {
            name: "products".to_string(),
            columns: vec![
                ColumnMetadata {
                    name: "id".to_string(),
                    data_type: DataType::Integer,
                    nullable: false,
                },
                ColumnMetadata {
                    name: "name".to_string(),
                    data_type: DataType::Text,
                    nullable: false,
                },
                ColumnMetadata {
                    name: "price".to_string(),
                    data_type: DataType::Float,
                    nullable: false,
                },
                ColumnMetadata {
                    name: "category".to_string(),
                    data_type: DataType::Text,
                    nullable: true,
                },
            ],
            primary_key: Some(vec!["id".to_string()]),
            indexes: vec![],
        };
        
        let products_rows = vec![
            Row::new(vec![
                Value::Integer(1),
                Value::Text("Laptop".to_string()),
                Value::Float(999.99),
                Value::Text("Electronics".to_string()),
            ]),
            Row::new(vec![
                Value::Integer(2),
                Value::Text("Coffee Mug".to_string()),
                Value::Float(12.50),
                Value::Text("Kitchen".to_string()),
            ]),
        ];
        
        self.test_data.insert("products".to_string(), TestTable {
            schema: products_schema,
            rows: products_rows,
        });
    }
    
    /// Apply WHERE clause filtering to rows
    fn apply_filters(&self, rows: &[Row], query: &ConnectorQuery) -> Vec<Row> {
        if query.query.predicates.is_empty() {
            return rows.to_vec();
        }
        
        let table_name = if let Some(source) = query.query.sources.first() {
            &source.identifier
        } else {
            return rows.to_vec();
        };
        
        let schema = if let Some(table) = self.test_data.get(table_name) {
            &table.schema
        } else {
            return rows.to_vec();
        };
        
        rows.iter()
            .filter(|row| {
                query.query.predicates.iter().all(|predicate| {
                    // Find column index
                    let col_index = schema.columns.iter()
                        .position(|col| col.name == predicate.column);
                    
                    if let Some(index) = col_index {
                        if let Some(value) = row.get(index) {
                            self.evaluate_predicate(value, &predicate.operator, &predicate.value)
                        } else {
                            false
                        }
                    } else {
                        false
                    }
                })
            })
            .cloned()
            .collect()
    }
    
    /// Evaluate a single predicate against a value
    fn evaluate_predicate(&self, value: &Value, operator: &PredicateOperator, predicate_value: &crate::utils::types::PredicateValue) -> bool {
        use crate::utils::types::PredicateValue;
        
        match operator {
            PredicateOperator::Equal => {
                match (value, predicate_value) {
                    (Value::Integer(v), PredicateValue::Integer(p)) => v == p,
                    (Value::Text(v), PredicateValue::String(p)) => v == p,
                    (Value::Float(v), PredicateValue::Number(p)) => (v - p).abs() < f64::EPSILON,
                    (Value::Boolean(v), PredicateValue::Boolean(p)) => v == p,
                    (Value::Null, PredicateValue::Null) => true,
                    _ => false,
                }
            },
            PredicateOperator::NotEqual => !self.evaluate_predicate(value, &PredicateOperator::Equal, predicate_value),
            PredicateOperator::GreaterThan => {
                match (value, predicate_value) {
                    (Value::Integer(v), PredicateValue::Integer(p)) => v > p,
                    (Value::Float(v), PredicateValue::Number(p)) => v > p,
                    _ => false,
                }
            },
            PredicateOperator::GreaterThanOrEqual => {
                self.evaluate_predicate(value, &PredicateOperator::GreaterThan, predicate_value) ||
                self.evaluate_predicate(value, &PredicateOperator::Equal, predicate_value)
            },
            PredicateOperator::LessThan => {
                match (value, predicate_value) {
                    (Value::Integer(v), PredicateValue::Integer(p)) => v < p,
                    (Value::Float(v), PredicateValue::Number(p)) => v < p,
                    _ => false,
                }
            },
            PredicateOperator::LessThanOrEqual => {
                self.evaluate_predicate(value, &PredicateOperator::LessThan, predicate_value) ||
                self.evaluate_predicate(value, &PredicateOperator::Equal, predicate_value)
            },
            PredicateOperator::Like => {
                match (value, predicate_value) {
                    (Value::Text(v), PredicateValue::String(p)) => {
                        // Simple LIKE implementation (% as wildcard)
                        let pattern = p.replace('%', ".*");
                        regex::Regex::new(&pattern).map(|re| re.is_match(v)).unwrap_or(false)
                    },
                    _ => false,
                }
            },
            PredicateOperator::IsNull => matches!(value, Value::Null),
            PredicateOperator::IsNotNull => !matches!(value, Value::Null),
            PredicateOperator::In => {
                if let PredicateValue::List(values) = predicate_value {
                    values.iter().any(|pv| self.evaluate_predicate(value, &PredicateOperator::Equal, pv))
                } else {
                    false
                }
            },
        }
    }
    
    /// Apply LIMIT clause to rows
    fn apply_limit(&self, rows: Vec<Row>, limit: Option<u64>) -> Vec<Row> {
        if let Some(limit_count) = limit {
            rows.into_iter().take(limit_count as usize).collect()
        } else {
            rows
        }
    }
}

impl Default for MockConnector {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl Connector for MockConnector {
    async fn connect(&mut self, _config: ConnectorInitConfig) -> NirvResult<()> {
        // Simulate connection delay
        if self.connection_delay_ms > 0 {
            tokio::time::sleep(Duration::from_millis(self.connection_delay_ms)).await;
        }
        
        self.connected = true;
        Ok(())
    }
    
    async fn execute_query(&self, query: ConnectorQuery) -> NirvResult<QueryResult> {
        if !self.connected {
            return Err(ConnectorError::ConnectionFailed("Not connected".to_string()).into());
        }
        
        let start_time = Instant::now();
        
        // Add a small delay to ensure execution time is recorded
        tokio::time::sleep(tokio::time::Duration::from_millis(1)).await;
        
        match query.query.operation {
            QueryOperation::Select => {
                if let Some(source) = query.query.sources.first() {
                    if let Some(table) = self.test_data.get(&source.identifier) {
                        let filtered_rows = self.apply_filters(&table.rows, &query);
                        // Note: Limit is handled by the query executor, not the connector
                        
                        let result = QueryResult {
                            columns: table.schema.columns.clone(),
                            rows: filtered_rows,
                            affected_rows: None,
                            execution_time: start_time.elapsed(),
                        };
                        
                        Ok(result)
                    } else {
                        Err(ConnectorError::QueryExecutionFailed(
                            format!("Table '{}' not found", source.identifier)
                        ).into())
                    }
                } else {
                    Err(ConnectorError::QueryExecutionFailed(
                        "No data source specified in query".to_string()
                    ).into())
                }
            },
            _ => Err(ConnectorError::UnsupportedOperation(
                format!("Operation {:?} not supported by MockConnector", query.query.operation)
            ).into()),
        }
    }
    
    async fn get_schema(&self, object_name: &str) -> NirvResult<Schema> {
        if !self.connected {
            return Err(ConnectorError::ConnectionFailed("Not connected".to_string()).into());
        }
        
        if let Some(table) = self.test_data.get(object_name) {
            Ok(table.schema.clone())
        } else {
            Err(ConnectorError::SchemaRetrievalFailed(
                format!("Object '{}' not found", object_name)
            ).into())
        }
    }
    
    async fn disconnect(&mut self) -> NirvResult<()> {
        self.connected = false;
        Ok(())
    }
    
    fn get_connector_type(&self) -> ConnectorType {
        ConnectorType::Mock
    }
    
    fn supports_transactions(&self) -> bool {
        false
    }
    
    fn is_connected(&self) -> bool {
        self.connected
    }
    
    fn get_capabilities(&self) -> ConnectorCapabilities {
        ConnectorCapabilities {
            supports_joins: false,
            supports_aggregations: false,
            supports_subqueries: false,
            supports_transactions: false,
            supports_schema_introspection: true,
            max_concurrent_queries: Some(10),
        }
    }
}#[
cfg(test)]
mod tests {
    use super::*;
    use crate::utils::types::{
        InternalQuery, QueryOperation, DataSource, Predicate, PredicateOperator, PredicateValue
    };

    #[tokio::test]
    async fn test_mock_connector_creation() {
        let connector = MockConnector::new();
        
        assert!(!connector.is_connected());
        assert_eq!(connector.get_connector_type(), ConnectorType::Mock);
        assert!(!connector.supports_transactions());
        assert_eq!(connector.test_data.len(), 2); // users and products tables
    }

    #[tokio::test]
    async fn test_mock_connector_with_delay() {
        let connector = MockConnector::with_delay(50);
        
        assert!(!connector.is_connected());
        assert_eq!(connector.connection_delay_ms, 50);
    }

    #[tokio::test]
    async fn test_mock_connector_connection_lifecycle() {
        let mut connector = MockConnector::new();
        let config = ConnectorInitConfig::new();
        
        // Initially not connected
        assert!(!connector.is_connected());
        
        // Connect
        let result = connector.connect(config).await;
        assert!(result.is_ok());
        assert!(connector.is_connected());
        
        // Disconnect
        let result = connector.disconnect().await;
        assert!(result.is_ok());
        assert!(!connector.is_connected());
    }

    #[tokio::test]
    async fn test_mock_connector_connection_delay() {
        let mut connector = MockConnector::with_delay(10);
        let config = ConnectorInitConfig::new();
        
        let start = std::time::Instant::now();
        let result = connector.connect(config).await;
        let elapsed = start.elapsed();
        
        assert!(result.is_ok());
        assert!(connector.is_connected());
        assert!(elapsed >= std::time::Duration::from_millis(10));
    }

    #[tokio::test]
    async fn test_mock_connector_query_without_connection() {
        let connector = MockConnector::new();
        let query = ConnectorQuery {
            connector_type: ConnectorType::Mock,
            query: InternalQuery::new(QueryOperation::Select),
            connection_params: std::collections::HashMap::new(),
        };
        
        let result = connector.execute_query(query).await;
        assert!(result.is_err());
        
        match result.unwrap_err() {
            crate::utils::error::NirvError::Connector(ConnectorError::ConnectionFailed(msg)) => {
                assert_eq!(msg, "Not connected");
            }
            _ => panic!("Expected ConnectionFailed error"),
        }
    }

    #[tokio::test]
    async fn test_mock_connector_select_users_table() {
        let mut connector = MockConnector::new();
        let config = ConnectorInitConfig::new();
        connector.connect(config).await.unwrap();
        
        let mut query = InternalQuery::new(QueryOperation::Select);
        query.sources.push(DataSource {
            object_type: "mock".to_string(),
            identifier: "users".to_string(),
            alias: None,
        });
        
        let connector_query = ConnectorQuery {
            connector_type: ConnectorType::Mock,
            query,
            connection_params: std::collections::HashMap::new(),
        };
        
        let result = connector.execute_query(connector_query).await;
        assert!(result.is_ok());
        
        let query_result = result.unwrap();
        assert_eq!(query_result.columns.len(), 5); // id, name, email, age, active
        assert_eq!(query_result.rows.len(), 3); // Alice, Bob, Charlie
        assert!(query_result.execution_time > std::time::Duration::from_nanos(0));
        
        // Check first row (Alice)
        let first_row = &query_result.rows[0];
        assert_eq!(first_row.get(0), Some(&Value::Integer(1)));
        assert_eq!(first_row.get(1), Some(&Value::Text("Alice Johnson".to_string())));
        assert_eq!(first_row.get(2), Some(&Value::Text("alice@example.com".to_string())));
        assert_eq!(first_row.get(3), Some(&Value::Integer(30)));
        assert_eq!(first_row.get(4), Some(&Value::Boolean(true)));
    }

    #[tokio::test]
    async fn test_mock_connector_select_products_table() {
        let mut connector = MockConnector::new();
        let config = ConnectorInitConfig::new();
        connector.connect(config).await.unwrap();
        
        let mut query = InternalQuery::new(QueryOperation::Select);
        query.sources.push(DataSource {
            object_type: "mock".to_string(),
            identifier: "products".to_string(),
            alias: None,
        });
        
        let connector_query = ConnectorQuery {
            connector_type: ConnectorType::Mock,
            query,
            connection_params: std::collections::HashMap::new(),
        };
        
        let result = connector.execute_query(connector_query).await;
        assert!(result.is_ok());
        
        let query_result = result.unwrap();
        assert_eq!(query_result.columns.len(), 4); // id, name, price, category
        assert_eq!(query_result.rows.len(), 2); // Laptop, Coffee Mug
        
        // Check first row (Laptop)
        let first_row = &query_result.rows[0];
        assert_eq!(first_row.get(0), Some(&Value::Integer(1)));
        assert_eq!(first_row.get(1), Some(&Value::Text("Laptop".to_string())));
        assert_eq!(first_row.get(2), Some(&Value::Float(999.99)));
        assert_eq!(first_row.get(3), Some(&Value::Text("Electronics".to_string())));
    }

    #[tokio::test]
    async fn test_mock_connector_query_non_existent_table() {
        let mut connector = MockConnector::new();
        let config = ConnectorInitConfig::new();
        connector.connect(config).await.unwrap();
        
        let mut query = InternalQuery::new(QueryOperation::Select);
        query.sources.push(DataSource {
            object_type: "mock".to_string(),
            identifier: "non_existent".to_string(),
            alias: None,
        });
        
        let connector_query = ConnectorQuery {
            connector_type: ConnectorType::Mock,
            query,
            connection_params: std::collections::HashMap::new(),
        };
        
        let result = connector.execute_query(connector_query).await;
        assert!(result.is_err());
        
        match result.unwrap_err() {
            crate::utils::error::NirvError::Connector(ConnectorError::QueryExecutionFailed(msg)) => {
                assert!(msg.contains("Table 'non_existent' not found"));
            }
            _ => panic!("Expected QueryExecutionFailed error"),
        }
    }

    #[tokio::test]
    async fn test_mock_connector_query_with_where_clause() {
        let mut connector = MockConnector::new();
        let config = ConnectorInitConfig::new();
        connector.connect(config).await.unwrap();
        
        let mut query = InternalQuery::new(QueryOperation::Select);
        query.sources.push(DataSource {
            object_type: "mock".to_string(),
            identifier: "users".to_string(),
            alias: None,
        });
        
        // Add WHERE age > 25
        query.predicates.push(Predicate {
            column: "age".to_string(),
            operator: PredicateOperator::GreaterThan,
            value: PredicateValue::Integer(25),
        });
        
        let connector_query = ConnectorQuery {
            connector_type: ConnectorType::Mock,
            query,
            connection_params: std::collections::HashMap::new(),
        };
        
        let result = connector.execute_query(connector_query).await;
        assert!(result.is_ok());
        
        let query_result = result.unwrap();
        assert_eq!(query_result.rows.len(), 2); // Alice (30) and Charlie (35)
        
        // Check that returned users have age > 25
        for row in &query_result.rows {
            if let Some(Value::Integer(age)) = row.get(3) {
                assert!(*age > 25);
            }
        }
    }

    #[tokio::test]
    async fn test_mock_connector_query_with_limit() {
        let mut connector = MockConnector::new();
        let config = ConnectorInitConfig::new();
        connector.connect(config).await.unwrap();
        
        let mut query = InternalQuery::new(QueryOperation::Select);
        query.sources.push(DataSource {
            object_type: "mock".to_string(),
            identifier: "users".to_string(),
            alias: None,
        });
        query.limit = Some(2);
        
        let connector_query = ConnectorQuery {
            connector_type: ConnectorType::Mock,
            query,
            connection_params: std::collections::HashMap::new(),
        };
        
        let result = connector.execute_query(connector_query).await;
        assert!(result.is_ok());
        
        let query_result = result.unwrap();
        assert_eq!(query_result.rows.len(), 2); // Limited to 2 rows
    }

    #[tokio::test]
    async fn test_mock_connector_query_with_equal_predicate() {
        let mut connector = MockConnector::new();
        let config = ConnectorInitConfig::new();
        connector.connect(config).await.unwrap();
        
        let mut query = InternalQuery::new(QueryOperation::Select);
        query.sources.push(DataSource {
            object_type: "mock".to_string(),
            identifier: "users".to_string(),
            alias: None,
        });
        
        // Add WHERE name = 'Alice Johnson'
        query.predicates.push(Predicate {
            column: "name".to_string(),
            operator: PredicateOperator::Equal,
            value: PredicateValue::String("Alice Johnson".to_string()),
        });
        
        let connector_query = ConnectorQuery {
            connector_type: ConnectorType::Mock,
            query,
            connection_params: std::collections::HashMap::new(),
        };
        
        let result = connector.execute_query(connector_query).await;
        assert!(result.is_ok());
        
        let query_result = result.unwrap();
        assert_eq!(query_result.rows.len(), 1); // Only Alice
        
        let alice_row = &query_result.rows[0];
        assert_eq!(alice_row.get(1), Some(&Value::Text("Alice Johnson".to_string())));
    }

    #[tokio::test]
    async fn test_mock_connector_query_with_null_predicate() {
        let mut connector = MockConnector::new();
        let config = ConnectorInitConfig::new();
        connector.connect(config).await.unwrap();
        
        let mut query = InternalQuery::new(QueryOperation::Select);
        query.sources.push(DataSource {
            object_type: "mock".to_string(),
            identifier: "users".to_string(),
            alias: None,
        });
        
        // Add WHERE email IS NULL
        query.predicates.push(Predicate {
            column: "email".to_string(),
            operator: PredicateOperator::IsNull,
            value: PredicateValue::Null,
        });
        
        let connector_query = ConnectorQuery {
            connector_type: ConnectorType::Mock,
            query,
            connection_params: std::collections::HashMap::new(),
        };
        
        let result = connector.execute_query(connector_query).await;
        assert!(result.is_ok());
        
        let query_result = result.unwrap();
        assert_eq!(query_result.rows.len(), 1); // Only Charlie has null email
        
        let charlie_row = &query_result.rows[0];
        assert_eq!(charlie_row.get(1), Some(&Value::Text("Charlie Brown".to_string())));
        assert_eq!(charlie_row.get(2), Some(&Value::Null));
    }

    #[tokio::test]
    async fn test_mock_connector_unsupported_operation() {
        let mut connector = MockConnector::new();
        let config = ConnectorInitConfig::new();
        connector.connect(config).await.unwrap();
        
        let query = InternalQuery::new(QueryOperation::Insert);
        let connector_query = ConnectorQuery {
            connector_type: ConnectorType::Mock,
            query,
            connection_params: std::collections::HashMap::new(),
        };
        
        let result = connector.execute_query(connector_query).await;
        assert!(result.is_err());
        
        match result.unwrap_err() {
            crate::utils::error::NirvError::Connector(ConnectorError::UnsupportedOperation(msg)) => {
                assert!(msg.contains("Operation Insert not supported"));
            }
            _ => panic!("Expected UnsupportedOperation error"),
        }
    }

    #[tokio::test]
    async fn test_mock_connector_get_schema() {
        let mut connector = MockConnector::new();
        let config = ConnectorInitConfig::new();
        connector.connect(config).await.unwrap();
        
        // Get users table schema
        let result = connector.get_schema("users").await;
        assert!(result.is_ok());
        
        let schema = result.unwrap();
        assert_eq!(schema.name, "users");
        assert_eq!(schema.columns.len(), 5);
        assert_eq!(schema.primary_key, Some(vec!["id".to_string()]));
        assert_eq!(schema.indexes.len(), 1);
        
        // Check column metadata
        assert_eq!(schema.columns[0].name, "id");
        assert_eq!(schema.columns[0].data_type, DataType::Integer);
        assert!(!schema.columns[0].nullable);
        
        assert_eq!(schema.columns[1].name, "name");
        assert_eq!(schema.columns[1].data_type, DataType::Text);
        assert!(!schema.columns[1].nullable);
        
        assert_eq!(schema.columns[2].name, "email");
        assert_eq!(schema.columns[2].data_type, DataType::Text);
        assert!(schema.columns[2].nullable);
    }

    #[tokio::test]
    async fn test_mock_connector_get_schema_non_existent() {
        let mut connector = MockConnector::new();
        let config = ConnectorInitConfig::new();
        connector.connect(config).await.unwrap();
        
        let result = connector.get_schema("non_existent").await;
        assert!(result.is_err());
        
        match result.unwrap_err() {
            crate::utils::error::NirvError::Connector(ConnectorError::SchemaRetrievalFailed(msg)) => {
                assert!(msg.contains("Object 'non_existent' not found"));
            }
            _ => panic!("Expected SchemaRetrievalFailed error"),
        }
    }

    #[tokio::test]
    async fn test_mock_connector_get_schema_without_connection() {
        let connector = MockConnector::new();
        
        let result = connector.get_schema("users").await;
        assert!(result.is_err());
        
        match result.unwrap_err() {
            crate::utils::error::NirvError::Connector(ConnectorError::ConnectionFailed(msg)) => {
                assert_eq!(msg, "Not connected");
            }
            _ => panic!("Expected ConnectionFailed error"),
        }
    }

    #[tokio::test]
    async fn test_mock_connector_capabilities() {
        let connector = MockConnector::new();
        let capabilities = connector.get_capabilities();
        
        assert!(!capabilities.supports_joins);
        assert!(!capabilities.supports_aggregations);
        assert!(!capabilities.supports_subqueries);
        assert!(!capabilities.supports_transactions);
        assert!(capabilities.supports_schema_introspection);
        assert_eq!(capabilities.max_concurrent_queries, Some(10));
    }

    #[test]
    fn test_mock_connector_default() {
        let connector = MockConnector::default();
        
        assert!(!connector.is_connected());
        assert_eq!(connector.get_connector_type(), ConnectorType::Mock);
        assert_eq!(connector.test_data.len(), 2);
    }
}