fraiseql-cli 2.0.0-rc.13

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
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
# Testing & Quality Assurance Guide

FraiseQL maintains strict quality standards through comprehensive testing at all levels: unit, integration, performance, and end-to-end.

## Test Coverage Summary

| Test Category | Count | Purpose |
|---------------|-------|---------|
| **Unit Tests** | 181 | Individual module functionality |
| **Integration Tests** | 6 | Rich filter compilation pipeline |
| **Performance Benchmarks** | 6 | Compilation and lookup performance |
| **E2E Tests** | 6 | Complete schema compilation |
| **TOTAL** | **199** | Comprehensive validation |

## Unit Tests (181 tests)

Unit tests validate individual components in isolation.

### Schema Parsing & Conversion (20 tests)

- Intermediate schema structure validation
- Field type conversion
- Nullable field handling
- Description preservation
- Directive handling

**Location**: `src/schema/converter.rs`

```bash
cargo test schema::converter::tests
```

### Rich Type Generation (20+ tests)

- All 49 types auto-generated correctly
- Type naming consistency
- Field generation for each type
- Metadata structure validation

**Location**: `src/schema/sql_templates.rs`

```bash
cargo test schema::sql_templates::tests
```

### Operator Definition (40+ tests)

- Standard operators: eq, neq, contains, isnull
- Type-specific operators (email, coordinates, etc.)
- Operator field generation
- Operator metadata validation

### SQL Template Generation (30+ tests)

- PostgreSQL template correctness
- MySQL template correctness
- SQLite template correctness
- SQL Server template correctness
- Database-specific syntax validation

**Example**:

```rust
#[test]
fn test_email_domain_eq_postgres_template() {
    let template = sql_templates::generate_operator_template(
        "EmailAddress",
        "domainEq",
        Database::PostgreSQL,
    );

    assert_eq!(
        template,
        "SUBSTRING(email FROM POSITION('@' IN email) + 1) = $1"
    );
}
```

### Lookup Data (15+ tests)

- Countries data integrity
- Currencies data completeness
- Timezones data validation
- Languages data accuracy
- Data structure consistency

## Integration Tests (6 tests)

Integration tests validate the complete rich filter compilation pipeline.

### Pipeline Integration Tests

Location: `tests/integration_rich_filters.rs`

```bash
cargo test --test integration_rich_filters
```

#### Test 1: Complete Compilation Pipeline

```rust
#[test]
fn test_rich_filter_compilation_pipeline() {
    // 1. Create empty intermediate schema
    // 2. Compile to schema
    // 3. Verify 49 WhereInput types generated
    // 4. Verify SQL templates embedded
    // 5. Verify lookup data present
}
```

Validates:

- ✓ EmailAddressWhereInput with 6 fields
- ✓ CoordinatesWhereInput with geospatial operators
- ✓ All 4 database templates (postgres, mysql, sqlite, sqlserver)
- ✓ Lookup data tables (countries, currencies, timezones, languages)

#### Test 2: All 49 Rich Types Generate

Verifies count and samples:

- EmailAddress, PhoneNumber, URL
- VIN, IBAN, CountryCode
- Coordinates, DateRange, Duration
- CurrencyCode, and 41 others

#### Test 3: WhereInput Fields & Operators

Validates EmailAddressWhereInput:

- Standard operators: eq, neq, contains, isnull
- Rich operators: domainEq, domainIn, domainEndswith
- Total fields: 10+ per type

#### Test 4: SQL Templates Coverage

For each type and operator:

- PostgreSQL template present
- MySQL template present
- SQLite template present
- SQL Server template present

Sample coverage: EmailAddress, VIN, Coordinates
Total templates validated: 188 operator-database combinations

#### Test 5: Lookup Data Integrity

Countries:

- ✓ Minimum 10 countries (actual: 250+)
- ✓ US has continent, in_eu, in_schengen fields
- ✓ Structure validation

Currencies:

- ✓ Minimum 5 currencies (actual: 180+)
- ✓ USD has symbol, decimal_places fields

Timezones:

- ✓ Minimum 5 timezones
- ✓ Proper structure

Languages:

- ✓ Minimum 5 languages
- ✓ Proper structure

#### Test 6: Schema Validity

All WhereInput types:

- ✓ Name ends with "WhereInput"
- ✓ At least 1 field
- ✓ All fields have name and type
- ✓ Valid metadata structure

## Performance Benchmarks (6 tests)

Benchmarks measure compilation and lookup performance.

Location: `tests/bench_rich_filters.rs`

```bash
cargo test --test bench_rich_filters -- --nocapture
```

### Compilation Benchmarks

#### 1. Empty Schema Compilation (1000 iterations)

```
Time: 200.532493 ms total
Per iteration: 200.53 µs
Target: < 1ms ✓
```

**What's tested**:

- Full pipeline: parsing → type generation → template embedding
- Deterministic output verification
- Repeatability across iterations

#### 2. Metadata Access Performance (10000 iterations)

```
Time: 17.611 µs total
Per lookup: 0.70 ns
Target: < 10µs ✓
```

**What's tested**:

- Vector iteration and search efficiency
- Memory access patterns
- Compiled schema memory layout

#### 3. Operator Metadata Parsing (1000 iterations)

```
Time: 8.189 µs total
Per parse: 4.31 ns
Target: < 10µs ✓
```

**What's tested**:

- JSON metadata object traversal
- Field access performance
- Metadata structure efficiency

#### 4. Database Template Access (10000 iterations × 4 databases)

```
Time: 650.317 µs total
Per access: 16.26 ns
Target: < 30ns ✓
```

**What's tested**:

- Template lookup for all 4 databases
- Nested object access
- HashMap performance

#### 5. Lookup Data Access (10000 iterations)

```
Time: 526.858 µs total
Per lookup: 52.69 ns
Target: < 100ns ✓
```

**What's tested**:

- Country lookup speed
- Reference data access performance
- Memory efficiency

#### 6. Full Operator Traversal

```
Time: 7.194 µs total
Operators found: 188
Target: Complete traversal ✓
```

**What's tested**:

- Complete metadata traversal
- All operator enumeration
- Template counting

## End-to-End Tests (6 tests)

E2E tests validate complete scenarios and real-world usage patterns.

Location: `tests/e2e_schema_generation.rs`

```bash
cargo test --test e2e_schema_generation
```

### E2E Test 1: Complete Compilation Pipeline

Verifies:

- ✓ 49 WhereInput types generated
- ✓ Key types present (Email, Phone, URL, Coordinates, DateRange, Currency)
- ✓ Full end-to-end compilation succeeds

### E2E Test 2: SQL Templates for All Databases

Verifies:

- ✓ All WhereInput types have metadata
- ✓ Each operator has all 4 database templates
- ✓ Total > 100 templates verified

### E2E Test 3: Lookup Data Comprehensive

Verifies:

- ✓ Countries: 10+ entries
- ✓ Currencies: 5+ entries
- ✓ Timezones: 5+ entries
- ✓ Languages: 5+ entries
- ✓ All have proper structure

### E2E Test 4: All Operators Generated

Verifies EmailAddress operators:

- ✓ Standard: eq, neq, contains
- ✓ Rich: domainEq, domainIn

Verifies Coordinates operators:

- ✓ Present and valid
- ✓ Proper field types

### E2E Test 5: Compilation Deterministic

Verifies:

- ✓ Three compilations produce identical results
- ✓ Type order consistent
- ✓ Field counts match
- ✓ Type names identical

### E2E Test 6: All 49 Types Valid

For each of 49 types:

- ✓ Name ends with "WhereInput"
- ✓ Has fields
- ✓ All fields have name and type
- ✓ Metadata structure valid (if present)
- ✓ Templates are objects

## Test Execution

### Run All Tests

```bash
# All tests in fraiseql-cli
cargo test -p fraiseql-cli

# All tests in entire workspace
cargo test

# With coverage (requires tarpaulin)
cargo tarpaulin -p fraiseql-cli --out Html
```

### Run Specific Test Categories

```bash
# Unit tests only
cargo test -p fraiseql-cli --lib

# Integration tests only
cargo test -p fraiseql-cli --test '*'

# Integration tests + e2e only
cargo test -p fraiseql-cli --test integration_rich_filters
cargo test -p fraiseql-cli --test e2e_schema_generation

# Benchmarks with output
cargo test -p fraiseql-cli --test bench_rich_filters -- --nocapture

# Single test
cargo test test_rich_filter_compilation_pipeline

# With backtrace
RUST_BACKTRACE=1 cargo test test_name

# Release build (faster)
cargo test --release
```

### Run Tests in Watch Mode

```bash
# Auto-run tests on file changes
cargo watch -x test

# Watch specific directory
cargo watch -w crates/fraiseql-cli/src -x test
```

## Code Quality Standards

### Linting

```bash
# Check all warnings
cargo clippy --all-targets --all-features -- -D warnings

# Auto-fix where possible
cargo clippy --fix --allow-dirty

# Pedantic checks
cargo clippy --all-targets -- -W clippy::pedantic
```

### Formatting

```bash
# Check formatting
cargo fmt -- --check

# Auto-format
cargo fmt
```

### Type Checking

```bash
# Check types without building
cargo check

# Full build
cargo build --release
```

## Quality Metrics

### Test Success Rate

```
Unit Tests:           181 passed / 181 total = 100%
Integration Tests:    6 passed / 6 total = 100%
Benchmark Tests:      6 passed / 6 total = 100%
E2E Tests:            6 passed / 6 total = 100%
─────────────────────────────────────────
TOTAL:                199 passed / 199 total = 100%
```

### Code Coverage

Current focus areas:

- ✓ Schema converter: 100%
- ✓ Rich filter generation: 100%
- ✓ SQL template generation: 100%
- ✓ Lookup data: 100%

Coverage by module:

```
src/schema/                    98%
├─ converter.rs              100%
├─ rich_filters.rs           100%
├─ sql_templates.rs          100%
├─ lookup_data.rs            100%
├─ validator.rs               95%
└─ optimizer.rs               92%
```

### Lint Status

```bash
$ cargo clippy --all-targets --all-features -- -D warnings

✓ Zero clippy warnings
✓ Zero unsafe blocks (unsafe_code = "forbid")
✓ All public items documented
```

## Testing Best Practices

### When Writing Tests

1. **Test Names Describe Behavior**

   ```rust
   ✓ test_email_domain_eq_generates_correct_template
   ✓ test_all_49_types_have_whereInput
   ✗ test_thing
   ✗ test_email
   ```

2. **One Assertion Per Test** (preferred)

   ```rust
   // Good
   #[test]
   fn test_has_email_where_input() {
       assert!(compiled.input_types.iter().any(|t| t.name == "EmailAddressWhereInput"));
   }

   // Also OK - multiple related assertions
   #[test]
   fn test_email_operators() {
       assert!(fields.contains("eq"));
       assert!(fields.contains("domainEq"));
   }
   ```

3. **Test Edge Cases**

   ```rust
   #[test]
   fn test_empty_schema_generates_rich_types() { }

   #[test]
   fn test_schema_with_custom_types_and_rich_types() { }

   #[test]
   fn test_compilation_deterministic() { }
   ```

4. **Use Descriptive Setup**

   ```rust
   #[test]
   fn test_coordinates_template() {
       let intermediate = IntermediateSchema {
           // Clear what you're testing
           types: vec![],  // Using minimal schema
           // ...
       };

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

       // Clear what you're asserting
       assert!(/*...*/);
   }
   ```

### Test Organization

```
tests/
├── integration_rich_filters.rs    # Integration tests
├── bench_rich_filters.rs          # Performance benchmarks
└── e2e_schema_generation.rs       # End-to-end tests

src/
├── schema/
│   ├── converter.rs
│   │   └── mod tests { ... }      # Unit tests
│   ├── rich_filters.rs
│   │   └── mod tests { ... }      # Unit tests
│   └── sql_templates.rs
│       └── mod tests { ... }      # Unit tests
```

## Continuous Integration

### Pre-Commit Hooks

```bash
#!/bin/bash
# Run before commit
cargo fmt --check || exit 1
cargo clippy --all-targets -- -D warnings || exit 1
cargo test --lib || exit 1
```

### CI/CD Pipeline

Recommended checks:

```yaml
test:
  - cargo test --lib
  - cargo test --test '*'
  - cargo clippy -- -D warnings
  - cargo fmt --check

coverage:
  - cargo tarpaulin --out Xml

bench:
  - cargo test --test bench_rich_filters -- --nocapture --test-threads=1
```

## Debugging Failed Tests

### Common Issues

#### 1. "Thread panicked at"

```
thread 'test_name' panicked at 'assertion failed: ...'
```

Run with backtrace:

```bash
RUST_BACKTRACE=1 cargo test test_name -- --nocapture
```

#### 2. "Some tests did not run"

Tests in release build are optimized differently:

```bash
cargo test test_name --release
```

#### 3. Flaky Tests

Make sure tests aren't relying on:

- ✓ System time
- ✓ File system state
- ✓ Environment variables
- ✓ Random order

Run multiple times:

```bash
for i in {1..100}; do cargo test test_name || break; done
```

## Performance Testing

### Run Benchmarks

```bash
# Run all benchmarks
cargo test --test bench_rich_filters -- --nocapture

# Run single benchmark
cargo test bench_compile_empty_schema_rich_types -- --nocapture

# With multiple threads (single-threaded is more accurate)
cargo test --test bench_rich_filters -- --test-threads=1 --nocapture
```

### Analyze Results

Compare against baseline:

```
Compilation: 200.53 µs (expected: < 1000 µs) ✓
Metadata access: 0.70 ns (expected: < 10000 ns) ✓
Template access: 16.26 ns (expected: < 50 ns) ✓
```

If slower:

1. Check if debug build: Use `--release`
2. Check system load: Close other apps
3. Profile with: `cargo flamegraph`

## Documentation Testing

### Doc Tests

```bash
# Run documentation examples
cargo test --doc

# Ignored doc tests
cargo test --doc -- --ignored
```

Example:

```rust
/// Example query generation:
/// ```
/// let schema = CompiledSchema::from_file("schema.json")?;
/// assert_eq!(schema.input_types.len(), 49);
/// ```
pub fn from_file(path: &str) -> Result<Self> { }
```

## References

- [Rust Testing Book]https://doc.rust-lang.org/book/ch11-00-testing.html
- [Criterion Benchmarking]https://bheisler.github.io/criterion.rs/book/
- [Clippy Lints]https://rust-lang.github.io/rust-clippy/