fraiseql-cli 2.3.0

CLI tools for FraiseQL v2 - Schema compilation and development utilities
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
#![allow(clippy::unwrap_used, clippy::panic)] // Reason: test code, panics acceptable
//! Integration tests for rich filter compiler.
//!
//! Tests the complete pipeline:
//! 1. Parse schema.json with rich scalar types
//! 2. Compile to schema.compiled.json
//! 3. Verify GraphQL types generated
//! 4. Verify SQL templates embedded
//! 5. Verify lookup data present

use fraiseql_cli::schema::{converter::SchemaConverter, intermediate::IntermediateSchema};
use fraiseql_core::schema::NamingConvention;

/// Test: Complete rich filter compilation pipeline
#[test]
fn test_rich_filter_compilation_pipeline() {
    // 1. Build minimal intermediate schema
    // Rich types are auto-generated, so we just need an empty schema
    let intermediate = IntermediateSchema {
        security:             None,
        version:              "2.0.0".to_string(),
        types:                vec![],
        enums:                vec![],
        input_types:          vec![],
        interfaces:           vec![],
        unions:               vec![],
        queries:              vec![],
        mutations:            vec![],
        subscriptions:        vec![],
        fragments:            None,
        directives:           None,
        fact_tables:          None,
        aggregate_queries:    None,
        observers:            None,
        custom_scalars:       None,
        observers_config:     None,
        subscriptions_config: None,
        validation_config:    None,
        federation_config:    None,
        debug_config:         None,
        mcp_config:           None,
        rest_config:          None,
        query_defaults:       None,
        naming_convention:    NamingConvention::default(),
        session_variables:    None,
        hierarchies_config:   None,
    };

    // 2. Compile to schema
    let compiled = SchemaConverter::convert(intermediate).expect("Compilation should succeed");

    // 3. Verify GraphQL WhereInput types were generated
    assert!(
        compiled.input_types.iter().any(|t| t.name == "EmailAddressWhereInput"),
        "EmailAddressWhereInput should be generated"
    );
    assert!(
        compiled.input_types.iter().any(|t| t.name == "CoordinatesWhereInput"),
        "CoordinatesWhereInput should be generated"
    );

    // 4. Verify SQL templates are embedded
    let email_where = compiled
        .input_types
        .iter()
        .find(|t| t.name == "EmailAddressWhereInput")
        .expect("EmailAddressWhereInput should exist");

    assert!(email_where.metadata.is_some(), "EmailAddressWhereInput should have metadata");
    let metadata = email_where.metadata.as_ref().unwrap();
    assert!(metadata.get("operators").is_some(), "Metadata should contain operators");

    let operators = metadata["operators"].as_object().unwrap();
    assert!(operators.contains_key("domainEq"), "Should have domainEq operator template");

    // Verify templates for all databases
    let domain_eq = operators["domainEq"].as_object().unwrap();
    for db in &["postgres", "mysql", "sqlite", "sqlserver"] {
        assert!(domain_eq.contains_key(*db), "Should have {} template for domainEq", db);
    }

    // 5. Verify lookup data is present
    assert!(compiled.security.is_some(), "Security section should exist for lookup data");
    let security = compiled.security.as_ref().unwrap();
    assert!(
        security.additional.contains_key("lookup_data"),
        "Lookup data should be embedded"
    );

    let lookup = security.additional["lookup_data"].as_object().unwrap();
    assert!(lookup.contains_key("countries"), "Countries lookup should be present");
    assert!(lookup.contains_key("currencies"), "Currencies lookup should be present");
    assert!(lookup.contains_key("timezones"), "Timezones lookup should be present");
    assert!(lookup.contains_key("languages"), "Languages lookup should be present");
}

/// Test: All 49 rich types generate `WhereInput`
#[test]
fn test_all_rich_types_generate_where_input() {
    let intermediate = IntermediateSchema {
        security:             None,
        version:              "2.0.0".to_string(),
        types:                vec![],
        enums:                vec![],
        input_types:          vec![],
        interfaces:           vec![],
        unions:               vec![],
        queries:              vec![],
        mutations:            vec![],
        subscriptions:        vec![],
        fragments:            None,
        directives:           None,
        fact_tables:          None,
        aggregate_queries:    None,
        observers:            None,
        custom_scalars:       None,
        observers_config:     None,
        subscriptions_config: None,
        validation_config:    None,
        federation_config:    None,
        debug_config:         None,
        mcp_config:           None,
        rest_config:          None,
        query_defaults:       None,
        naming_convention:    NamingConvention::default(),
        session_variables:    None,
        hierarchies_config:   None,
    };

    let compiled = SchemaConverter::convert(intermediate).expect("Compilation should succeed");

    // Check that all 49 rich types generated WhereInput
    assert_eq!(
        compiled.input_types.len(),
        49,
        "Should have 49 rich type WhereInput definitions"
    );

    // Sample of important types
    let expected_types = vec![
        "EmailAddressWhereInput",
        "PhoneNumberWhereInput",
        "URLWhereInput",
        "VINWhereInput",
        "IBANWhereInput",
        "CountryCodeWhereInput",
        "CoordinatesWhereInput",
        "DateRangeWhereInput",
        "DurationWhereInput",
        "CurrencyCodeWhereInput",
    ];

    for expected in expected_types {
        assert!(
            compiled.input_types.iter().any(|t| t.name == expected),
            "Should have {} type",
            expected
        );
    }
}

/// Test: `WhereInput` types have correct fields
#[test]
fn test_where_input_fields_include_standard_operators() {
    let intermediate = IntermediateSchema {
        security:             None,
        version:              "2.0.0".to_string(),
        types:                vec![],
        enums:                vec![],
        input_types:          vec![],
        interfaces:           vec![],
        unions:               vec![],
        queries:              vec![],
        mutations:            vec![],
        subscriptions:        vec![],
        fragments:            None,
        directives:           None,
        fact_tables:          None,
        aggregate_queries:    None,
        observers:            None,
        custom_scalars:       None,
        observers_config:     None,
        subscriptions_config: None,
        validation_config:    None,
        federation_config:    None,
        debug_config:         None,
        mcp_config:           None,
        rest_config:          None,
        query_defaults:       None,
        naming_convention:    NamingConvention::default(),
        session_variables:    None,
        hierarchies_config:   None,
    };

    let compiled = SchemaConverter::convert(intermediate).expect("Compilation should succeed");

    let email_where = compiled
        .input_types
        .iter()
        .find(|t| t.name == "EmailAddressWhereInput")
        .expect("EmailAddressWhereInput should exist");

    // Check standard operators present
    let field_names: Vec<_> = email_where.fields.iter().map(|f| &f.name).collect();
    assert!(field_names.contains(&&"eq".to_string()), "Should have eq operator");
    assert!(field_names.contains(&&"neq".to_string()), "Should have neq operator");
    assert!(field_names.contains(&&"contains".to_string()), "Should have contains operator");
    assert!(field_names.contains(&&"isnull".to_string()), "Should have isnull operator");

    // Check rich operators for email
    assert!(field_names.contains(&&"domainEq".to_string()), "Should have domainEq operator");
    assert!(field_names.contains(&&"domainIn".to_string()), "Should have domainIn operator");
    assert!(
        field_names.contains(&&"domainEndswith".to_string()),
        "Should have domainEndswith operator"
    );

    // Total fields should be reasonable (6 standard + 4 email specific)
    assert!(email_where.fields.len() >= 10, "Should have at least 10 fields");
}

/// Test: SQL templates have correct database coverage
#[test]
fn test_sql_templates_cover_all_databases() {
    let intermediate = IntermediateSchema {
        security:             None,
        version:              "2.0.0".to_string(),
        types:                vec![],
        enums:                vec![],
        input_types:          vec![],
        interfaces:           vec![],
        unions:               vec![],
        queries:              vec![],
        mutations:            vec![],
        subscriptions:        vec![],
        fragments:            None,
        directives:           None,
        fact_tables:          None,
        aggregate_queries:    None,
        observers:            None,
        custom_scalars:       None,
        observers_config:     None,
        subscriptions_config: None,
        validation_config:    None,
        federation_config:    None,
        debug_config:         None,
        mcp_config:           None,
        rest_config:          None,
        query_defaults:       None,
        naming_convention:    NamingConvention::default(),
        session_variables:    None,
        hierarchies_config:   None,
    };

    let compiled = SchemaConverter::convert(intermediate).expect("Compilation should succeed");

    // Check multiple types for database coverage
    let types_to_check = vec![
        "EmailAddressWhereInput",
        "VINWhereInput",
        "CoordinatesWhereInput",
    ];

    for type_name in types_to_check {
        let where_input = compiled
            .input_types
            .iter()
            .find(|t| t.name == type_name)
            .unwrap_or_else(|| panic!("{} should exist", type_name));

        let metadata = where_input
            .metadata
            .as_ref()
            .unwrap_or_else(|| panic!("{} should have metadata", type_name));
        let operators = metadata["operators"].as_object().unwrap();

        // Each operator should have templates for all 4 databases
        for (op_name, templates) in operators {
            let db_templates = templates.as_object().unwrap();
            for db in &["postgres", "mysql", "sqlite", "sqlserver"] {
                assert!(
                    db_templates.contains_key(*db),
                    "{}/{} should have {} template",
                    type_name,
                    op_name,
                    db
                );
            }
        }
    }
}

/// Test: Lookup data integrity
#[test]
fn test_lookup_data_integrity() {
    let intermediate = IntermediateSchema {
        security:             None,
        version:              "2.0.0".to_string(),
        types:                vec![],
        enums:                vec![],
        input_types:          vec![],
        interfaces:           vec![],
        unions:               vec![],
        queries:              vec![],
        mutations:            vec![],
        subscriptions:        vec![],
        fragments:            None,
        directives:           None,
        fact_tables:          None,
        aggregate_queries:    None,
        observers:            None,
        custom_scalars:       None,
        observers_config:     None,
        subscriptions_config: None,
        validation_config:    None,
        federation_config:    None,
        debug_config:         None,
        mcp_config:           None,
        rest_config:          None,
        query_defaults:       None,
        naming_convention:    NamingConvention::default(),
        session_variables:    None,
        hierarchies_config:   None,
    };

    let compiled = SchemaConverter::convert(intermediate).expect("Compilation should succeed");

    let security = compiled.security.as_ref().expect("Security section should exist");
    let lookup = security.additional["lookup_data"]
        .as_object()
        .expect("Lookup data should be an object");

    // Verify countries
    let countries = lookup["countries"].as_object().expect("Countries should exist");
    assert!(countries.len() >= 10, "Should have at least 10 countries");

    // Check sample countries have required fields
    if let Some(us) = countries.get("US") {
        let us_obj = us.as_object().unwrap();
        assert!(us_obj.contains_key("continent"), "US should have continent");
        assert!(us_obj.contains_key("in_eu"), "US should have in_eu");
        assert!(us_obj.contains_key("in_schengen"), "US should have in_schengen");
    }

    // Verify currencies
    let currencies = lookup["currencies"].as_object().expect("Currencies should exist");
    assert!(currencies.len() >= 5, "Should have at least 5 currencies");

    // Check sample currency has required fields
    if let Some(usd) = currencies.get("USD") {
        let usd_obj = usd.as_object().unwrap();
        assert!(usd_obj.contains_key("symbol"), "USD should have symbol");
        assert!(usd_obj.contains_key("decimal_places"), "USD should have decimal_places");
    }

    // Verify timezones
    let timezones = lookup["timezones"].as_object().expect("Timezones should exist");
    assert!(timezones.len() >= 5, "Should have at least 5 timezones");

    // Verify languages
    let languages = lookup["languages"].as_object().expect("Languages should exist");
    assert!(languages.len() >= 5, "Should have at least 5 languages");
}

/// Test: Schema is valid after compilation
#[test]
fn test_compiled_schema_is_valid() {
    let intermediate = IntermediateSchema {
        security:             None,
        version:              "2.0.0".to_string(),
        types:                vec![],
        enums:                vec![],
        input_types:          vec![],
        interfaces:           vec![],
        unions:               vec![],
        queries:              vec![],
        mutations:            vec![],
        subscriptions:        vec![],
        fragments:            None,
        directives:           None,
        fact_tables:          None,
        aggregate_queries:    None,
        observers:            None,
        custom_scalars:       None,
        observers_config:     None,
        subscriptions_config: None,
        validation_config:    None,
        federation_config:    None,
        debug_config:         None,
        mcp_config:           None,
        rest_config:          None,
        query_defaults:       None,
        naming_convention:    NamingConvention::default(),
        session_variables:    None,
        hierarchies_config:   None,
    };

    let compiled = SchemaConverter::convert(intermediate).expect("Compilation should succeed");

    // Verify basic structure
    assert!(
        !compiled.types.is_empty() || !compiled.input_types.is_empty(),
        "Compiled schema should have types or input types"
    );
    assert!(!compiled.input_types.is_empty(), "Should have input types");

    // Verify all WhereInput types have valid structure
    for where_input in &compiled.input_types {
        assert!(!where_input.name.is_empty(), "Type name should not be empty");
        assert!(
            where_input.name.ends_with("WhereInput"),
            "Type {} should end with 'WhereInput'",
            where_input.name
        );
        assert!(!where_input.fields.is_empty(), "Type {} should have fields", where_input.name);

        // All fields should have valid names and types
        for field in &where_input.fields {
            assert!(!field.name.is_empty(), "Field name should not be empty");
            assert!(!field.field_type.is_empty(), "Field type should not be empty");
        }
    }
}