vantage-expressions 0.4.2

Database agnostic expressions
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
use serde_json::Value;
use std::future::Future;
use std::pin::Pin;
use std::time::Duration;
use vantage_expressions::{
    Expression, Flatten, expr, expr_as, expression::flatten::ExpressionFlattener,
    traits::expressive::ExpressiveEnum,
};

#[test]
fn test_basic_composition_example() {
    let where_expr = expr!("age > {} AND status = {}", 21, "active");
    let query_expr = expr!("SELECT * FROM users WHERE {}", (where_expr));

    // Verify the structure
    assert_eq!(query_expr.template, "SELECT * FROM users WHERE {}");
    assert_eq!(query_expr.parameters.len(), 1);
}

#[test]
fn test_surreal_duration_example() {
    // Test that the expr_any macro works with Value type
    let duration_secs = Duration::from_secs(3600).as_secs();
    let surreal_query = expr_as!(
        Value,
        "SELECT * FROM session WHERE created_at > time::now() - {}",
        duration_secs
    );

    assert_eq!(
        surreal_query.template,
        "SELECT * FROM session WHERE created_at > time::now() - {}"
    );
    assert_eq!(surreal_query.parameters.len(), 1);
}

#[test]
fn test_dynamic_query_building() {
    // Simulate user filter conditions
    struct UserFilter {
        min_age: Option<i32>,
        status: Option<String>,
        active_only: bool,
    }

    let user_filter = UserFilter {
        min_age: Some(25),
        status: Some("premium".to_string()),
        active_only: true,
    };

    let mut conditions = Vec::<Expression<Value>>::new();

    // Conditionally add filters
    if let Some(min_age) = user_filter.min_age {
        conditions.push(expr!("age >= {}", min_age));
    }
    if let Some(status) = user_filter.status {
        conditions.push(expr!("status = {}", status));
    }
    if user_filter.active_only {
        conditions.push(expr!("last_login > NOW() - INTERVAL 30 DAY"));
    }

    // Verify we have the expected conditions
    assert_eq!(conditions.len(), 3);

    // Use from_vec to combine conditions
    let where_clause = Expression::from_vec(conditions, " AND ");
    let final_query = expr!("SELECT * FROM users WHERE {}", (where_clause.clone()));

    // Flatten to see the final template and parameters
    let flattener = ExpressionFlattener::new();
    let flattened = flattener.flatten(&final_query);

    // Debug output to see what we actually get
    println!("WHERE clause template: {}", where_clause.template);
    println!("Final query template: {}", final_query.template);
    println!("Flattened template: {}", flattened.template);

    // The from_vec creates multiple placeholders, one for each condition
    assert_eq!(where_clause.template, "{} AND {} AND {}");
    assert_eq!(final_query.template, "SELECT * FROM users WHERE {}");
    // After flattening, the nested structure should be resolved
    // Leaf expressions inline their templates, only parameterized ones keep {}
    assert_eq!(
        flattened.template,
        "SELECT * FROM users WHERE age >= {} AND status = {} AND last_login > NOW() - INTERVAL 30 DAY"
    );
    // The flattened version should have the parameters from nested expressions
    println!("Template: {}", flattened.template);
    println!("Parameters: {:?}", flattened.parameters);
}

#[test]
fn test_simple_dynamic_example() {
    // Simplified version that should work with current implementation
    let mut conditions = Vec::<String>::new();

    let min_age = Some(25);
    let status = Some("premium");
    let active_only = true;

    if min_age.is_some() {
        conditions.push("age >= 25".to_string());
    }
    if status.is_some() {
        conditions.push("status = 'premium'".to_string());
    }
    if active_only {
        conditions.push("last_login > NOW() - INTERVAL 30 DAY".to_string());
    }

    let where_clause = conditions.join(" AND ");
    let final_query = expr!("SELECT * FROM users WHERE {}", where_clause);

    assert_eq!(final_query.template, "SELECT * FROM users WHERE {}");
    assert_eq!(final_query.parameters.len(), 1);
}

#[test]
fn test_flattening_behavior() {
    // Test what flattening actually does
    let inner_expr = expr!("age > {} AND status = {}", 25, "active");
    let outer_expr = expr!("SELECT * FROM users WHERE {}", (inner_expr));

    let flattener = ExpressionFlattener::new();
    let flattened = flattener.flatten(&outer_expr);

    println!("Original template: {}", outer_expr.template);
    println!("Original params count: {}", outer_expr.parameters.len());
    println!("Flattened template: {}", flattened.template);
    println!("Flattened params count: {}", flattened.parameters.len());

    // This will help us understand what flattening produces
    assert_eq!(outer_expr.template, "SELECT * FROM users WHERE {}");
    assert_eq!(outer_expr.parameters.len(), 1);
}

#[tokio::test]
async fn test_querysource_example() {
    use vantage_expressions::mocks::MockExprDataSource;
    use vantage_expressions::traits::datasource::ExprDataSource;

    // Create a mock database that returns a fixed value
    let db = MockExprDataSource::new(serde_json::json!(42));
    let query = expr!("SELECT COUNT(*) FROM users WHERE age > {}", 21);

    // Execute immediately - returns result now
    let count = db.execute(&query).await.unwrap();
    assert_eq!(count, serde_json::json!(42));

    // Defer execution - returns DeferredFn that can be called later
    let deferred_query = db.defer(query);
    let count = deferred_query.call().await.unwrap(); // Execute when needed
    match count {
        ExpressiveEnum::Scalar(val) => assert_eq!(val, serde_json::json!(42)),
        _ => panic!("Expected scalar result"),
    }
}

#[tokio::test]
async fn test_deferred_as_parameters() {
    use vantage_expressions::mocks::MockExprDataSource;
    use vantage_expressions::traits::datasource::ExprDataSource;
    use vantage_expressions::traits::expressive::ExpressiveEnum;

    // Mock SurrealDB returning user IDs
    let surreal_db = MockExprDataSource::new(serde_json::json!([1, 2, 3]));
    let user_ids_query = expr!("SELECT id FROM user WHERE status = {}", "active");

    // Create deferred query - defer() now returns DeferredFn directly
    let deferred_users = surreal_db.defer(user_ids_query);

    // Use deferred query with [deferred] syntax for DeferredFn
    let orders_query = expr!("SELECT * FROM orders WHERE user_id = ANY({})", {
        deferred_users
    });

    // Verify the structure
    assert_eq!(
        orders_query.template,
        "SELECT * FROM orders WHERE user_id = ANY({})"
    );
    assert_eq!(orders_query.parameters.len(), 1);

    // The parameter should be a deferred expression
    match &orders_query.parameters[0] {
        ExpressiveEnum::Deferred(_) => {} // Expected
        _ => panic!("Expected deferred parameter"),
    }
}

#[tokio::test]
async fn test_closure_syntax() {
    use vantage_expressions::traits::expressive::ExpressiveEnum;

    // Test that {closure} syntax still works with .into()
    let closure =
        move || -> Pin<Box<dyn Future<Output = vantage_core::Result<ExpressiveEnum<serde_json::Value>>> + Send>> {
            Box::pin(async move { Ok(ExpressiveEnum::Scalar(serde_json::json!(42))) })
        };

    let query = expr!("SELECT * FROM test WHERE value = {}", { closure });

    // Verify the structure
    assert_eq!(query.template, "SELECT * FROM test WHERE value = {}");
    assert_eq!(query.parameters.len(), 1);

    // The parameter should be a deferred expression
    match &query.parameters[0] {
        ExpressiveEnum::Deferred(_) => {} // Expected
        _ => panic!("Expected deferred parameter"),
    }
}

#[tokio::test]
async fn test_mutex_deferred_function() {
    use std::sync::{Arc, Mutex};
    use vantage_expressions::traits::expressive::{DeferredFn, ExpressiveEnum};

    // 1. Set mutex value
    let counter = Arc::new(Mutex::new(10i32));

    // 2. Create expression with deferred mutex value
    let deferred_count = DeferredFn::from_mutex(counter.clone());
    let query = expr!("SELECT * FROM items LIMIT {}", { deferred_count });

    // 3. Change value after query construction
    *counter.lock().unwrap() = 25;

    // 4. Execute expression (simulate by calling the deferred function)
    if let ExpressiveEnum::Deferred(deferred_fn) = &query.parameters[0] {
        let result = deferred_fn.call().await.unwrap();
        match result {
            ExpressiveEnum::Scalar(val) => {
                assert_eq!(val, serde_json::json!(25)); // Should use updated value, not original
            }
            _ => panic!("Expected scalar result"),
        }
    } else {
        panic!("Expected deferred parameter");
    }

    // Verify the query structure
    assert_eq!(query.template, "SELECT * FROM items LIMIT {}");
    assert_eq!(query.parameters.len(), 1);
}

#[test]
fn test_type_mapping() {
    use serde_json::Value;
    use vantage_expressions::{
        Expression, expression::mapping::ExpressionMap, traits::expressive::ExpressiveEnum,
    };

    // Create expression with String parameters
    let string_expr: Expression<String> = Expression::new(
        "SELECT * FROM users WHERE name = {}".to_string(),
        vec![ExpressiveEnum::Scalar("John".to_string())],
    );

    // Convert to Expression<Value> using the map() method
    let value_expr: Expression<Value> = string_expr.map();

    // Verify the conversion worked
    assert_eq!(value_expr.template, "SELECT * FROM users WHERE name = {}");
    assert_eq!(value_expr.parameters.len(), 1);

    // Check that the parameter was converted
    match &value_expr.parameters[0] {
        ExpressiveEnum::Scalar(val) => {
            assert_eq!(val, &serde_json::json!("John"));
        }
        _ => panic!("Expected scalar parameter"),
    }
}

#[tokio::test]
async fn test_immediate_vs_deferred_execution() {
    use vantage_expressions::mocks::MockExprDataSource;
    use vantage_expressions::traits::datasource::ExprDataSource;
    use vantage_expressions::traits::expressive::ExpressiveEnum;

    // Create a mock database that returns a fixed value
    let db = MockExprDataSource::new(serde_json::json!(42));

    // Create a query expression
    let query = expr!("SELECT COUNT(*) FROM users WHERE age > {}", 21);

    // Immediate execution - execute now and get result
    let count = db.execute(&query).await;
    assert_eq!(count.unwrap(), serde_json::json!(42));

    // Deferred execution - create a closure for later execution
    let deferred_query = db.defer(query.clone());

    // Execute the deferred query when needed
    let count_later = deferred_query.call().await.unwrap();
    match count_later {
        ExpressiveEnum::Scalar(value) => {
            assert_eq!(value, serde_json::json!(42));
        }
        _ => panic!("Expected scalar result"),
    }
}

#[test]
fn test_cross_database_type_mapping_concept() {
    use serde_json::Value;
    use vantage_expressions::{
        Expression, expression::mapping::ExpressionMap, traits::expressive::ExpressiveEnum,
    };

    // Simulate a deferred query that returns String type
    let string_query: Expression<String> = Expression::new(
        "SELECT user_ids FROM active_users WHERE department = {}".to_string(),
        vec![ExpressiveEnum::Scalar("engineering".to_string())],
    );

    // Map it to Value type for cross-database compatibility
    let value_query: Expression<Value> = string_query.map();

    // Verify the mapping preserved the structure and converted types
    assert_eq!(
        value_query.template,
        "SELECT user_ids FROM active_users WHERE department = {}"
    );
    assert_eq!(value_query.parameters.len(), 1);

    // Check that the parameter was converted from String to Value
    match &value_query.parameters[0] {
        ExpressiveEnum::Scalar(val) => {
            assert_eq!(val, &serde_json::json!("engineering"));
        }
        _ => panic!("Expected scalar parameter"),
    }
}

#[tokio::test]
async fn test_cross_database_api_integration() {
    use vantage_expressions::mocks::MockExprDataSource;
    use vantage_expressions::traits::datasource::ExprDataSource;
    use vantage_expressions::traits::expressive::{DeferredFn, ExpressiveEnum};

    // API call that fetches user IDs asynchronously
    async fn get_user_ids() -> vantage_core::Result<serde_json::Value> {
        // Simulate API call - fetch from external service
        Ok(serde_json::json!([1, 2, 3, 4, 5]))
    }

    // Build query synchronously - no async needed here!
    let query = expr!("SELECT * FROM orders WHERE user_id = ANY({})", {
        DeferredFn::from_fn(get_user_ids)
    });

    // Verify the query structure
    assert_eq!(
        query.template,
        "SELECT * FROM orders WHERE user_id = ANY({})"
    );
    assert_eq!(query.parameters.len(), 1);

    // The parameter should be a deferred expression
    match &query.parameters[0] {
        ExpressiveEnum::Deferred(deferred_fn) => {
            // Execute the deferred function to test the API integration
            let result = deferred_fn.call().await.unwrap();
            match result {
                ExpressiveEnum::Scalar(value) => {
                    assert_eq!(value, serde_json::json!([1, 2, 3, 4, 5]));
                }
                _ => panic!("Expected scalar result from API call"),
            }
        }
        _ => panic!("Expected deferred parameter"),
    }

    // Test with mock database execution
    let db = MockExprDataSource::new(serde_json::json!([{"id": 1, "amount": 100}]));
    let orders = db.execute(&query).await.unwrap();
    assert_eq!(orders, serde_json::json!([{"id": 1, "amount": 100}]));
}

#[tokio::test]
async fn test_deferred_fn_from_fn() {
    use vantage_expressions::traits::expressive::{DeferredFn, ExpressiveEnum};

    // Simple async function that returns a value
    async fn get_number() -> vantage_core::Result<i32> {
        Ok(42)
    }

    // Create deferred function using from_fn
    let deferred_num = DeferredFn::from_fn(get_number);
    let query = expr!("SELECT * FROM test WHERE id = {}", { deferred_num });

    // Verify the query structure
    assert_eq!(query.template, "SELECT * FROM test WHERE id = {}");
    assert_eq!(query.parameters.len(), 1);

    // The parameter should be a deferred expression
    match &query.parameters[0] {
        ExpressiveEnum::Deferred(deferred_fn) => {
            // Execute the deferred function
            let result = deferred_fn.call().await.unwrap();
            match result {
                ExpressiveEnum::Scalar(value) => {
                    assert_eq!(value, serde_json::json!(42));
                }
                _ => panic!("Expected scalar result"),
            }
        }
        _ => panic!("Expected deferred parameter"),
    }
}

#[tokio::test]
async fn test_error_handling_in_deferred_functions() {
    use vantage_core::error;
    use vantage_expressions::traits::expressive::{DeferredFn, ExpressiveEnum};

    // API call that can fail
    async fn failing_api_call() -> vantage_core::Result<serde_json::Value> {
        Err(error!("API connection failed"))
    }

    // Create deferred function that wraps a failing API call
    let deferred_failing = DeferredFn::from_fn(failing_api_call);
    let query = expr!("SELECT * FROM orders WHERE user_id = {}", {
        deferred_failing
    });

    // Verify the query structure
    assert_eq!(query.template, "SELECT * FROM orders WHERE user_id = {}");
    assert_eq!(query.parameters.len(), 1);

    // The parameter should be a deferred expression
    match &query.parameters[0] {
        ExpressiveEnum::Deferred(deferred_fn) => {
            // Execute the deferred function - should return an error
            let result = deferred_fn.call().await;
            assert!(result.is_err());
            let error = result.unwrap_err();
            assert!(error.to_string().contains("API connection failed"));
        }
        _ => panic!("Expected deferred parameter"),
    }
}

#[tokio::test]
async fn test_union_extensibility() {
    use vantage_expressions::traits::expressive::{Expressive, ExpressiveEnum};

    /// A UNION SQL construct that combines two SELECT expressions
    #[derive(Clone)]
    pub struct Union<T> {
        left: Expression<T>,
        right: Expression<T>,
    }

    impl<T> Union<T> {
        pub fn new(left: Expression<T>, right: Expression<T>) -> Self {
            Self { left, right }
        }
    }

    impl<T: Clone> Expressive<T> for Union<T> {
        fn expr(&self) -> Expression<T> {
            Expression::new(
                "{} UNION {}",
                vec![
                    ExpressiveEnum::Nested(self.left.clone()),
                    ExpressiveEnum::Nested(self.right.clone()),
                ],
            )
        }
    }

    // Usage example with nested queries and stored procedure
    let users_query = expr!("CALL get_active_users_by_dept({})", "engineering");
    let admins_query = expr!("SELECT name FROM admins WHERE role = {}", "super");

    let union = Union::new(users_query, admins_query);
    let final_query = expr!("SELECT DISTINCT name FROM ({})", (union.expr()));

    // Verify the structure
    assert_eq!(final_query.template, "SELECT DISTINCT name FROM ({})");
    assert_eq!(final_query.parameters.len(), 1);

    // The parameter should be a nested expression (the union)
    match &final_query.parameters[0] {
        ExpressiveEnum::Nested(union_expr) => {
            assert_eq!(union_expr.template, "{} UNION {}");
            assert_eq!(union_expr.parameters.len(), 2);

            // Both parameters should be nested expressions
            match (&union_expr.parameters[0], &union_expr.parameters[1]) {
                (ExpressiveEnum::Nested(left), ExpressiveEnum::Nested(right)) => {
                    assert_eq!(left.template, "CALL get_active_users_by_dept({})");
                    assert_eq!(right.template, "SELECT name FROM admins WHERE role = {}");
                }
                _ => panic!("Expected nested expressions for both union parts"),
            }
        }
        _ => panic!("Expected nested expression for union"),
    }

    // Test preview functionality to see the rendered query
    let preview = final_query.preview();
    let expected = "SELECT DISTINCT name FROM (CALL get_active_users_by_dept(\"engineering\") UNION SELECT name FROM admins WHERE role = \"super\")";
    assert_eq!(preview, expected);
}