fraiseql-core 2.2.0

Core execution engine for FraiseQL v2 - Compiled GraphQL over SQL
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
#![allow(clippy::unwrap_used)] // Reason: test code, panics are acceptable

//! Test WHERE clause with deeply nested JSON paths.
//!
//! This test verifies that:
//! 1. Deep nested paths (5+ levels) are handled correctly
//! 2. Path component names are preserved at all levels
//! 3. Operators work correctly with nested paths
//! 4. Deep nesting doesn't cause truncation or errors
//!
//! # Risk If Missing
//!
//! Without this test:
//! - Deep nested paths could be silently truncated
//! - Path components could be lost or corrupted
//! - SQL generation could fail on deep nesting

use fraiseql_core::db::where_clause::{WhereClause, WhereOperator};
use serde_json::json;

#[test]
fn test_where_nested_path_3_levels() {
    // 3-level path: user.profile.address
    let clause = WhereClause::Field {
        path:     vec![
            "user".to_string(),
            "profile".to_string(),
            "address".to_string(),
        ],
        operator: WhereOperator::Eq,
        value:    json!("123 Main St"),
    };

    match clause {
        WhereClause::Field { path, value, .. } => {
            assert_eq!(path.len(), 3);
            assert_eq!(path[0], "user");
            assert_eq!(path[1], "profile");
            assert_eq!(path[2], "address");
            assert_eq!(value, json!("123 Main St"));
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_nested_path_5_levels() {
    // 5-level path: user.profile.address.country.region
    let clause = WhereClause::Field {
        path:     vec![
            "user".to_string(),
            "profile".to_string(),
            "address".to_string(),
            "country".to_string(),
            "region".to_string(),
        ],
        operator: WhereOperator::Eq,
        value:    json!("California"),
    };

    match clause {
        WhereClause::Field { path, value, .. } => {
            assert_eq!(path.len(), 5);
            assert_eq!(path[0], "user");
            assert_eq!(path[2], "address");
            assert_eq!(path[4], "region");
            assert_eq!(value, json!("California"));
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_nested_path_10_levels() {
    // 10-level path for deeply nested data structures
    let path: Vec<String> = (0..10).map(|i| format!("level{}", i)).collect();

    let clause = WhereClause::Field {
        path,
        operator: WhereOperator::Contains,
        value: json!("search_term"),
    };

    match clause {
        WhereClause::Field { path: p, value, .. } => {
            assert_eq!(p.len(), 10);
            for (i, component) in p.iter().enumerate() {
                assert_eq!(component, &format!("level{}", i));
            }
            assert_eq!(value, json!("search_term"));
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_nested_path_20_levels() {
    // 20-level path - extreme nesting
    let path: Vec<String> = (0..20).map(|i| format!("l{}", i)).collect();

    let clause = WhereClause::Field {
        path,
        operator: WhereOperator::Eq,
        value: json!("deep_value"),
    };

    match clause {
        WhereClause::Field { path: p, .. } => {
            assert_eq!(p.len(), 20);
            assert_eq!(p[0], "l0");
            assert_eq!(p[10], "l10");
            assert_eq!(p[19], "l19");
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_nested_path_with_different_operators() {
    // Test deep nesting with various operators
    let path = vec![
        "a".to_string(),
        "b".to_string(),
        "c".to_string(),
        "d".to_string(),
        "e".to_string(),
    ];

    let operators = vec![
        (WhereOperator::Eq, "equals"),
        (WhereOperator::Contains, "contains"),
        (WhereOperator::Startswith, "startswith"),
        (WhereOperator::Gt, "greater than"),
        (WhereOperator::Lt, "less than"),
    ];

    for (op, _desc) in operators {
        let clause = WhereClause::Field {
            path:     path.clone(),
            operator: op.clone(),
            value:    json!("test"),
        };

        match clause {
            WhereClause::Field {
                path: p, operator, ..
            } => {
                assert_eq!(p.len(), 5);
                assert_eq!(operator, op);
            },
            _ => panic!("Should be Field variant"),
        }
    }
}

#[test]
fn test_where_nested_path_special_characters() {
    // Deep paths with special characters in component names
    let path = vec![
        "user_id".to_string(),
        "profile-data".to_string(),
        "contact.info".to_string(),
        "email_addresses".to_string(),
        "primary-email".to_string(),
    ];

    let clause = WhereClause::Field {
        path:     path.clone(),
        operator: WhereOperator::Contains,
        value:    json!("example.com"),
    };

    match clause {
        WhereClause::Field { path: p, .. } => {
            assert_eq!(p, path);
            assert_eq!(p[0], "user_id");
            assert_eq!(p[1], "profile-data");
            assert_eq!(p[2], "contact.info");
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_nested_path_numeric_components() {
    // Array-like paths with numeric indices
    let path = vec![
        "users".to_string(),
        "0".to_string(),
        "addresses".to_string(),
        "1".to_string(),
        "zip".to_string(),
    ];

    let clause = WhereClause::Field {
        path,
        operator: WhereOperator::Eq,
        value: json!("90210"),
    };

    match clause {
        WhereClause::Field { path: p, .. } => {
            assert_eq!(p.len(), 5);
            assert_eq!(p[1], "0");
            assert_eq!(p[3], "1");
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_deeply_nested_with_null_value() {
    // Deep path with NULL value (IS NULL)
    let path = vec![
        "data".to_string(),
        "metadata".to_string(),
        "created".to_string(),
        "timestamp".to_string(),
        "timezone".to_string(),
    ];

    let clause = WhereClause::Field {
        path,
        operator: WhereOperator::IsNull,
        value: json!(true),
    };

    match clause {
        WhereClause::Field { path: p, value, .. } => {
            assert_eq!(p.len(), 5);
            assert_eq!(value, json!(true));
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_deeply_nested_with_array_value() {
    // Deep path with array value (IN operator)
    let path = vec![
        "org".to_string(),
        "departments".to_string(),
        "members".to_string(),
        "roles".to_string(),
        "permissions".to_string(),
    ];

    let clause = WhereClause::Field {
        path,
        operator: WhereOperator::In,
        value: json!(["read", "write", "admin"]),
    };

    match clause {
        WhereClause::Field { path: p, value, .. } => {
            assert_eq!(p.len(), 5);
            let arr = value.as_array().unwrap();
            assert_eq!(arr.len(), 3);
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_deeply_nested_unicode_paths() {
    // Deep paths with Unicode characters
    let path = vec![
        "user".to_string(),
        "profil".to_string(),
        "données".to_string(),
        "contact".to_string(),
        "email_français".to_string(),
    ];

    let clause = WhereClause::Field {
        path:     path.clone(),
        operator: WhereOperator::Contains,
        value:    json!("contact@example.fr"),
    };

    match clause {
        WhereClause::Field { path: p, .. } => {
            assert_eq!(p, path);
            assert_eq!(p[2], "données");
            assert_eq!(p[4], "email_français");
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_deeply_nested_mixed_content() {
    // Deep paths with mixed alphanumeric and special characters
    let path = vec![
        "api_v2".to_string(),
        "response_data".to_string(),
        "results[0]".to_string(),
        "user.profile".to_string(),
        "contact-info".to_string(),
    ];

    let clause = WhereClause::Field {
        path,
        operator: WhereOperator::Eq,
        value: json!("test@example.com"),
    };

    match clause {
        WhereClause::Field { path: p, .. } => {
            assert_eq!(p.len(), 5);
            assert_eq!(p[0], "api_v2");
            assert_eq!(p[2], "results[0]");
            assert_eq!(p[3], "user.profile");
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_deeply_nested_case_sensitivity() {
    // Deep paths preserve case sensitivity
    let path_lower = vec![
        "user".to_string(),
        "profile".to_string(),
        "email".to_string(),
        "address".to_string(),
        "domain".to_string(),
    ];
    let path_upper = vec![
        "USER".to_string(),
        "PROFILE".to_string(),
        "EMAIL".to_string(),
        "ADDRESS".to_string(),
        "DOMAIN".to_string(),
    ];

    let clause_lower = WhereClause::Field {
        path:     path_lower,
        operator: WhereOperator::Eq,
        value:    json!("example.com"),
    };

    let clause_upper = WhereClause::Field {
        path:     path_upper,
        operator: WhereOperator::Eq,
        value:    json!("example.com"),
    };

    match (clause_lower, clause_upper) {
        (WhereClause::Field { path: p1, .. }, WhereClause::Field { path: p2, .. }) => {
            // Paths should be different (case matters)
            assert_ne!(p1, p2);
            assert_eq!(p1[0], "user");
            assert_eq!(p2[0], "USER");
        },
        _ => panic!("Should be Field variants"),
    }
}

#[test]
fn test_where_deeply_nested_with_ltree_operators() {
    // Deep paths with LTree operators
    let path = vec![
        "org".to_string(),
        "hierarchy".to_string(),
        "department".to_string(),
        "team".to_string(),
        "structure".to_string(),
    ];

    let ltree_operators = vec![
        WhereOperator::AncestorOf,
        WhereOperator::DescendantOf,
        WhereOperator::MatchesLquery,
    ];

    for op in ltree_operators {
        let clause = WhereClause::Field {
            path:     path.clone(),
            operator: op.clone(),
            value:    json!("a.b.c"),
        };

        match clause {
            WhereClause::Field {
                path: p, operator, ..
            } => {
                assert_eq!(p.len(), 5);
                assert_eq!(operator, op);
            },
            _ => panic!("Should be Field variant"),
        }
    }
}

#[test]
fn test_where_deeply_nested_repeating_components() {
    // Deep paths with repeating component names
    let path = vec![
        "data".to_string(),
        "data".to_string(),
        "data".to_string(),
        "data".to_string(),
        "value".to_string(),
    ];

    let clause = WhereClause::Field {
        path,
        operator: WhereOperator::Eq,
        value: json!("test"),
    };

    match clause {
        WhereClause::Field { path: p, .. } => {
            assert_eq!(p.len(), 5);
            // First 4 should all be "data"
            assert_eq!(p[0], "data");
            assert_eq!(p[1], "data");
            assert_eq!(p[2], "data");
            assert_eq!(p[3], "data");
            assert_eq!(p[4], "value");
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_where_deeply_nested_empty_component() {
    // Edge case: deep path with empty component name
    let path = vec![
        "a".to_string(),
        String::new(),
        "b".to_string(),
        String::new(),
        "c".to_string(),
    ];

    let clause = WhereClause::Field {
        path,
        operator: WhereOperator::Eq,
        value: json!("test"),
    };

    match clause {
        WhereClause::Field { path: p, .. } => {
            assert_eq!(p.len(), 5);
            assert_eq!(p[0], "a");
            assert_eq!(p[1], ""); // Empty component preserved
            assert_eq!(p[2], "b");
            assert_eq!(p[3], ""); // Empty component preserved
            assert_eq!(p[4], "c");
        },
        _ => panic!("Should be Field variant"),
    }
}