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
#![allow(clippy::unwrap_used)] // Reason: test code, panics are acceptable

//! Test `LTree` format validation and edge cases.
//!
//! This test verifies that:
//! 1. `LTree` operators handle various path formats correctly
//! 2. Invalid `LTree` values are preserved for SQL layer to handle
//! 3. `LTree` with very long paths work without truncation
//! 4. `LTree` operator values maintain structural integrity
//!
//! # Risk If Missing
//!
//! Without this test:
//! - Invalid ltree values could cause PostgreSQL errors
//! - Very long paths could be silently truncated
//! - `LTree` operator behavior could be inconsistent

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

#[test]
fn test_ltree_valid_path_formats() {
    // Valid LTree paths should be preserved
    let valid_paths = vec![
        "a",
        "a.b",
        "a.b.c",
        "a.b.c.d.e",
        "org.company.dept.team",
        "level1.level2.level3",
        "simple",
        "CamelCase",
        "with_underscores",
        "with-dashes",
        "123",
        "a1.b2.c3",
    ];

    for path in valid_paths {
        let operators = vec![
            WhereOperator::AncestorOf,
            WhereOperator::DescendantOf,
            WhereOperator::MatchesLquery,
        ];

        for op in operators {
            let clause = WhereClause::Field {
                path:     vec!["hierarchy".to_string()],
                operator: op.clone(),
                value:    json!(path),
            };

            match clause {
                WhereClause::Field {
                    value, operator, ..
                } => {
                    assert_eq!(value, json!(path), "Path {} should be preserved", path);
                    assert_eq!(operator, op);
                },
                _ => panic!("Should be Field variant"),
            }
        }
    }
}

#[test]
fn test_ltree_long_path_preservation() {
    // Very long paths should be preserved without truncation
    let long_path_100 = (0..100).map(|i| format!("level{}", i)).collect::<Vec<_>>().join(".");

    let long_paths: Vec<&str> = vec![
        "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z", // 26 components
        "level1.level2.level3.level4.level5.level6.level7.level8.level9.level10", // 10 levels
    ];

    for path in &long_paths {
        let clause = WhereClause::Field {
            path:     vec!["tree".to_string()],
            operator: WhereOperator::AncestorOf,
            value:    json!(path),
        };

        match clause {
            WhereClause::Field { value, .. } => {
                // Verify path is preserved exactly (no truncation)
                assert_eq!(value, json!(path));

                // Verify we can count the components
                let component_count = path.split('.').count();
                let json_str = value.as_str().unwrap();
                let json_component_count = json_str.split('.').count();
                assert_eq!(json_component_count, component_count);
            },
            _ => panic!("Should be Field variant"),
        }
    }

    // Test 100-component path separately
    let clause = WhereClause::Field {
        path:     vec!["tree".to_string()],
        operator: WhereOperator::AncestorOf,
        value:    json!(&long_path_100),
    };

    match clause {
        WhereClause::Field { value, .. } => {
            assert_eq!(value, json!(&long_path_100));
            let component_count = long_path_100.split('.').count();
            let json_str = value.as_str().unwrap();
            let json_component_count = json_str.split('.').count();
            assert_eq!(json_component_count, component_count);
        },
        _ => panic!("Should be Field variant"),
    }
}

#[test]
fn test_ltree_special_characters_in_labels() {
    // LTree labels can contain letters, numbers, underscores, and dashes
    let label_types = vec![
        "all_lowercase",
        "ALL_UPPERCASE",
        "Mixed_Case",
        "with-dashes",
        "numbers123",
        "123numbers",
        "_leading_underscore",
        "trailing_underscore_",
        "_both_",
        "multi_under_score",
        "multi-dash-label",
        "mixed_under-dash",
    ];

    for label in label_types {
        let clause = WhereClause::Field {
            path:     vec!["path".to_string()],
            operator: WhereOperator::DescendantOf,
            value:    json!(label),
        };

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

#[test]
fn test_ltree_queries_lquery_patterns() {
    // LTree lquery patterns for pattern matching
    let lquery_patterns = vec![
        "a",           // exact label
        "a.b.c",       // exact path
        "a.*",         // a and all children
        "*.b",         // any first then b
        "a.*.c",       // a, any middle, c
        "a|b",         // a or b
        "a.b|c",       // complex
        "!a",          // not a
        "{a,b,c}",     // in set
        "a.{b,c,d}.e", // mixed patterns
    ];

    for pattern in lquery_patterns {
        let clause = WhereClause::Field {
            path:     vec!["path".to_string()],
            operator: WhereOperator::MatchesLquery,
            value:    json!(pattern),
        };

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

#[test]
fn test_ltree_depth_operators() {
    // LTree depth operators (nlevel) for comparing path depth
    let depths = vec![0, 1, 2, 5, 10, 100, 1000];

    for depth in depths {
        let operators = vec![
            WhereOperator::DepthEq,
            WhereOperator::DepthNeq,
            WhereOperator::DepthGt,
            WhereOperator::DepthGte,
            WhereOperator::DepthLt,
            WhereOperator::DepthLte,
        ];

        for op in operators {
            let clause = WhereClause::Field {
                path:     vec!["tree".to_string()],
                operator: op.clone(),
                value:    json!(depth),
            };

            match clause {
                WhereClause::Field {
                    value, operator, ..
                } => {
                    assert_eq!(value, json!(depth));
                    assert_eq!(operator, op);
                },
                _ => panic!("Should be Field variant"),
            }
        }
    }
}

#[test]
fn test_ltree_path_with_numbers() {
    // Numeric path components
    let numeric_paths = vec![
        "0",
        "1",
        "123",
        "0.0.0",
        "1.2.3.4.5",
        "100.200.300",
        "9999.9999.9999",
    ];

    for path in numeric_paths {
        let clause = WhereClause::Field {
            path:     vec!["numeric_path".to_string()],
            operator: WhereOperator::AncestorOf,
            value:    json!(path),
        };

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

#[test]
fn test_ltree_empty_path_handling() {
    // Edge case: empty or minimal paths
    let edge_paths = vec![
        "",     // empty string
        "a",    // single component
        ".",    // just dot (invalid)
        "a.",   // trailing dot (invalid)
        ".a",   // leading dot (invalid)
        "a..b", // double dot (invalid)
    ];

    for path in edge_paths {
        // Should still construct - validation happens at SQL generation
        let clause = WhereClause::Field {
            path:     vec!["path".to_string()],
            operator: WhereOperator::MatchesLquery,
            value:    json!(path),
        };

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

#[test]
fn test_ltree_case_sensitivity() {
    // LTree paths are case-sensitive
    let case_variants = vec![
        ("Org", "ORG", "org"), // Different cases
        ("Company", "COMPANY", "company"),
        ("Department", "DEPARTMENT", "department"),
    ];

    for (variant1, variant2, variant3) in case_variants {
        let clause1 = WhereClause::Field {
            path:     vec!["path".to_string()],
            operator: WhereOperator::DescendantOf,
            value:    json!(variant1),
        };

        let clause2 = WhereClause::Field {
            path:     vec!["path".to_string()],
            operator: WhereOperator::DescendantOf,
            value:    json!(variant2),
        };

        let clause3 = WhereClause::Field {
            path:     vec!["path".to_string()],
            operator: WhereOperator::DescendantOf,
            value:    json!(variant3),
        };

        match (clause1, clause2, clause3) {
            (
                WhereClause::Field { value: v1, .. },
                WhereClause::Field { value: v2, .. },
                WhereClause::Field { value: v3, .. },
            ) => {
                // All should be preserved exactly
                assert_eq!(v1, json!(variant1));
                assert_eq!(v2, json!(variant2));
                assert_eq!(v3, json!(variant3));
                // And they should be different
                assert_ne!(v1, v2);
                assert_ne!(v2, v3);
            },
            _ => panic!("Should be Field variants"),
        }
    }
}

#[test]
fn test_ltree_unicode_labels() {
    // Unicode characters in LTree paths (may be invalid in PostgreSQL but should be preserved)
    let unicode_paths = vec![
        "café",
        "naïve",
        "resumé",
        "piñata",
        "Москва",     // Russian
        "北京",       // Chinese
        "東京",       // Japanese
        "🏢.company", // Emoji (invalid but preserved)
    ];

    for path in unicode_paths {
        let clause = WhereClause::Field {
            path:     vec!["path".to_string()],
            operator: WhereOperator::AncestorOf,
            value:    json!(path),
        };

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

#[test]
fn test_ltree_operators_all_variants() {
    // Verify all LTree operators are available and constructable
    let all_ltree_operators = vec![
        (WhereOperator::AncestorOf, "AncestorOf"),
        (WhereOperator::DescendantOf, "DescendantOf"),
        (WhereOperator::MatchesLquery, "MatchesLquery"),
        (WhereOperator::MatchesLtxtquery, "MatchesLtxtquery"),
        (WhereOperator::MatchesAnyLquery, "MatchesAnyLquery"),
        (WhereOperator::DepthEq, "DepthEq"),
        (WhereOperator::DepthNeq, "DepthNeq"),
        (WhereOperator::DepthGt, "DepthGt"),
        (WhereOperator::DepthGte, "DepthGte"),
        (WhereOperator::DepthLt, "DepthLt"),
        (WhereOperator::DepthLte, "DepthLte"),
        (WhereOperator::Lca, "Lca"), // Lowest Common Ancestor
    ];

    for (operator, _name) in all_ltree_operators {
        let clause = WhereClause::Field {
            path:     vec!["path".to_string()],
            operator: operator.clone(),
            value:    json!("test_value"),
        };

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

#[test]
fn test_ltree_path_depth_constraints() {
    // LTree has depth limits (PostgreSQL: 65535 maximum path length)
    // We test that various depths are handled structurally

    for depth in [1, 10, 100, 1000] {
        let components: Vec<String> = (0..depth).map(|i| format!("l{}", i)).collect();
        let path = components.join(".");

        let clause = WhereClause::Field {
            path:     vec!["tree".to_string()],
            operator: WhereOperator::AncestorOf,
            value:    json!(&path),
        };

        match clause {
            WhereClause::Field { value, .. } => {
                // Path should be preserved
                assert_eq!(value, json!(&path));

                // Component count should match depth
                let actual_depth = value.as_str().unwrap().split('.').count();
                assert_eq!(actual_depth, depth);
            },
            _ => panic!("Should be Field variant"),
        }
    }
}