sqlx-data-macros 0.1.0

Procedural macros for sqlx-data - #[repo] and #[dml] derive macros with automatic SQL generation, repository pattern, data access layer, and compile-time query validation
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
use crate::dml::{DmlMethod, DmlParameter};
use crate::type_analyzer::TypeAnalyzer;
use crate::type_system::{QueryType, ReturnType};
use quote::format_ident;
use sqlx_data_parser::SqlStatementType;
/// Comprehensive testing framework for sqlx-data macros
///
/// This module provides structured testing utilities for validating
/// macro functionality, type analysis, and code generation.
use syn::parse_quote;

/// Test framework for macro functionality
pub struct MacroTestFramework;

impl MacroTestFramework {
    /// Run all comprehensive tests
    pub fn run_all_tests() -> Result<(), TestError> {
        Self::test_type_analysis()?;
        Self::test_query_strategies()?;
        Self::test_code_generation()?;
        Self::test_edge_cases()?;
        println!("โœ… All macro tests passed!");
        Ok(())
    }

    /// Test type analysis functionality
    fn test_type_analysis() -> Result<(), TestError> {
        println!("๐Ÿงช Testing type analysis...");

        // Test scalar types
        TypeAnalysisTests::test_scalar_types()?;

        // Test Result type alias
        TypeAnalysisTests::test_result_type_alias()?;

        // Test complex types
        TypeAnalysisTests::test_complex_types()?;

        // Test Option types
        TypeAnalysisTests::test_option_types()?;

        // Test Vec types
        TypeAnalysisTests::test_vec_types()?;

        // Test tuple types
        TypeAnalysisTests::test_tuple_types()?;

        println!("โœ… Type analysis tests passed!");
        Ok(())
    }

    /// Test query strategy determination
    fn test_query_strategies() -> Result<(), TestError> {
        println!("๐Ÿงช Testing query strategies...");

        QueryStrategyTests::test_scalar_strategies()?;
        QueryStrategyTests::test_struct_strategies()?;
        QueryStrategyTests::test_tuple_strategies()?;
        QueryStrategyTests::test_collection_strategies()?;

        println!("โœ… Query strategy tests passed!");
        Ok(())
    }

    /// Test code generation
    fn test_code_generation() -> Result<(), TestError> {
        println!("๐Ÿงช Testing code generation...");

        CodeGenerationTests::test_basic_scalar_generation()?;
        CodeGenerationTests::test_struct_generation()?;
        CodeGenerationTests::test_tuple_generation()?;
        CodeGenerationTests::test_parameter_handling()?;

        println!("โœ… Code generation tests passed!");
        Ok(())
    }

    /// Test edge cases and error conditions
    fn test_edge_cases() -> Result<(), TestError> {
        println!("๐Ÿงช Testing edge cases...");

        EdgeCaseTests::test_empty_parameters()?;
        EdgeCaseTests::test_complex_nested_types()?;
        EdgeCaseTests::test_invalid_sql_handling()?;
        EdgeCaseTests::test_type_mismatch_detection()?;

        println!("โœ… Edge case tests passed!");
        Ok(())
    }
}

/// Type analysis specific tests
pub struct TypeAnalysisTests;

impl TypeAnalysisTests {
    pub fn test_scalar_types() -> Result<(), TestError> {
        // Test primitive scalar types
        let test_cases = [
            (
                "i32",
                ReturnType::Scalar {
                    name: format_ident!("i32"),
                },
            ),
            (
                "i64",
                ReturnType::Scalar {
                    name: format_ident!("i64"),
                },
            ),
            (
                "String",
                ReturnType::Scalar {
                    name: format_ident!("String"),
                },
            ),
            (
                "bool",
                ReturnType::Scalar {
                    name: format_ident!("bool"),
                },
            ),
            (
                "f64",
                ReturnType::Scalar {
                    name: format_ident!("f64"),
                },
            ),
        ];

        for (type_str, expected) in test_cases {
            let ty: syn::Type = syn::parse_str(type_str).map_err(|e| {
                TestError::ParseError(format!("Failed to parse {}: {}", type_str, e))
            })?;

            let analyzed = TypeAnalyzer::analyze_type(&ty).map_err(|e| {
                TestError::ParseError(format!("Failed to analyze {}: {}", type_str, e))
            })?;

            if !matches_return_type(&analyzed, &expected) {
                return Err(TestError::AssertionError(format!(
                    "Type analysis mismatch for {}: expected {:?}, got {:?}",
                    type_str, expected, analyzed
                )));
            }
        }

        Ok(())
    }

    pub fn test_result_type_alias() -> Result<(), TestError> {
        // Test Result<T> type alias (single parameter)
        let ty: syn::Type = parse_quote!(Result<i32>);
        let analyzed = TypeAnalyzer::analyze_type(&ty)
            .map_err(|e| TestError::ParseError(format!("Failed to analyze Result<i32>: {}", e)))?;

        match analyzed {
            ReturnType::Result { ok_type, err_type } => {
                match ok_type.as_ref() {
                    ReturnType::Scalar { name } if name.to_string() == "i32" => (),
                    _ => {
                        return Err(TestError::AssertionError(
                            "Result<i32> should have i32 as ok_type".to_string(),
                        ));
                    }
                }
                match err_type.as_ref() {
                    ReturnType::Unknown { name } if name == "sqlx_data::Error" => (),
                    _ => {
                        return Err(TestError::AssertionError(format!(
                            "Result<T> should have sqlx_data::Error as err_type, but got: {:?}",
                            err_type
                        )));
                    }
                }
            }
            _ => {
                return Err(TestError::AssertionError(
                    "Result<i32> should be analyzed as Result type".to_string(),
                ));
            }
        }

        Ok(())
    }

    pub fn test_complex_types() -> Result<(), TestError> {
        // Test Result<Option<Vec<User>>>
        let ty: syn::Type = parse_quote!(Result<Option<Vec<User>>>);
        let analyzed = TypeAnalyzer::analyze_type(&ty)
            .map_err(|e| TestError::ParseError(format!("Failed to analyze complex type: {}", e)))?;

        match analyzed {
            ReturnType::Result { ok_type, .. } => match ok_type.as_ref() {
                ReturnType::Option { inner_type } => match inner_type.as_ref() {
                    ReturnType::Vec { element_type } => match element_type.as_ref() {
                        ReturnType::Struct { name } if name.to_string() == "User" => (),
                        _ => {
                            return Err(TestError::AssertionError(
                                "Expected User struct in Vec".to_string(),
                            ));
                        }
                    },
                    _ => {
                        return Err(TestError::AssertionError(
                            "Expected Vec in Option".to_string(),
                        ));
                    }
                },
                _ => {
                    return Err(TestError::AssertionError(
                        "Expected Option in Result".to_string(),
                    ));
                }
            },
            _ => {
                return Err(TestError::AssertionError(
                    "Expected Result type".to_string(),
                ));
            }
        }

        Ok(())
    }

    pub fn test_option_types() -> Result<(), TestError> {
        let ty: syn::Type = parse_quote!(Option<String>);
        let analyzed = TypeAnalyzer::analyze_type(&ty)
            .map_err(|e| TestError::ParseError(format!("Failed to analyze Option type: {}", e)))?;

        match analyzed {
            ReturnType::Option { inner_type } => match inner_type.as_ref() {
                ReturnType::Scalar { name } if name.to_string() == "String" => (),
                _ => {
                    return Err(TestError::AssertionError(
                        "Option<String> should contain String scalar".to_string(),
                    ));
                }
            },
            _ => {
                return Err(TestError::AssertionError(
                    "Option<String> should be analyzed as Option type".to_string(),
                ));
            }
        }

        Ok(())
    }

    pub fn test_vec_types() -> Result<(), TestError> {
        let ty: syn::Type = parse_quote!(Vec<User>);
        let analyzed = TypeAnalyzer::analyze_type(&ty)
            .map_err(|e| TestError::ParseError(format!("Failed to analyze Vec type: {}", e)))?;

        match analyzed {
            ReturnType::Vec { element_type } => match element_type.as_ref() {
                ReturnType::Struct { name } if name.to_string() == "User" => (),
                _ => {
                    return Err(TestError::AssertionError(
                        "Vec<User> should contain User struct".to_string(),
                    ));
                }
            },
            _ => {
                return Err(TestError::AssertionError(
                    "Vec<User> should be analyzed as Vec type".to_string(),
                ));
            }
        }

        Ok(())
    }

    pub fn test_tuple_types() -> Result<(), TestError> {
        let ty: syn::Type = parse_quote!((i32, String, bool));
        let analyzed = TypeAnalyzer::analyze_type(&ty)
            .map_err(|e| TestError::ParseError(format!("Failed to analyze Tuple type: {}", e)))?;

        match analyzed {
            ReturnType::Tuple { elements } => {
                if elements.len() != 3 {
                    return Err(TestError::AssertionError(
                        "Tuple should have 3 elements".to_string(),
                    ));
                }

                // Check each element
                match (&elements[0], &elements[1], &elements[2]) {
                    (
                        ReturnType::Scalar { name: name1 },
                        ReturnType::Scalar { name: name2 },
                        ReturnType::Scalar { name: name3 },
                    ) if name1 == "i32" && name2 == "String" && name3 == "bool" => (),
                    _ => {
                        return Err(TestError::AssertionError(
                            "Tuple elements don't match expected types".to_string(),
                        ));
                    }
                }
            }
            _ => {
                return Err(TestError::AssertionError(
                    "(i32, String, bool) should be analyzed as Tuple type".to_string(),
                ));
            }
        }

        Ok(())
    }
}

/// Query strategy specific tests
pub struct QueryStrategyTests;

impl QueryStrategyTests {
    pub fn test_scalar_strategies() -> Result<(), TestError> {
        // Test scalar types result in QueryScalar
        let ty: syn::Type = parse_quote!(Result<i32>);
        let analyzed = TypeAnalyzer::analyze_type(&ty)
            .map_err(|e| TestError::ParseError(format!("Failed to analyze type: {}", e)))?;
        let strategy = TypeAnalyzer::determine_query_strategy(&analyzed);

        match strategy {
            Ok(QueryType::QueryScalar) => {
                // Test passed - scalar types correctly result in QueryScalar
            }
            _ => {
                return Err(TestError::AssertionError(
                    "Result<i32> should use QueryScalar strategy".to_string(),
                ));
            }
        }

        Ok(())
    }

    pub fn test_struct_strategies() -> Result<(), TestError> {
        let ty: syn::Type = parse_quote!(Result<User>);
        let analyzed = TypeAnalyzer::analyze_type(&ty)
            .map_err(|e| TestError::ParseError(format!("Failed to analyze type: {}", e)))?;
        let strategy = TypeAnalyzer::determine_query_strategy(&analyzed);

        match strategy {
            Ok(QueryType::QueryAs) => {
                // Test passed - struct types correctly result in QueryAs
            }
            _ => {
                return Err(TestError::AssertionError(
                    "Result<User> should use QueryAs strategy".to_string(),
                ));
            }
        }

        Ok(())
    }

    pub fn test_tuple_strategies() -> Result<(), TestError> {
        let ty: syn::Type = parse_quote!(Result<(i32, String)>);
        let analyzed = TypeAnalyzer::analyze_type(&ty)
            .map_err(|e| TestError::ParseError(format!("Failed to analyze type: {}", e)))?;
        let strategy = TypeAnalyzer::determine_query_strategy(&analyzed);

        match strategy {
            Ok(QueryType::QueryAs) => {
                // Test passed - tuple types correctly result in QueryAs
            }
            _ => {
                return Err(TestError::AssertionError(
                    "Result<(i32, String)> should use QueryAs strategy".to_string(),
                ));
            }
        }

        Ok(())
    }

    pub fn test_collection_strategies() -> Result<(), TestError> {
        // Test Vec<T> uses fetch_all
        let ty: syn::Type = parse_quote!(Result<Vec<User>>);
        let analyzed = TypeAnalyzer::analyze_type(&ty)
            .map_err(|e| TestError::ParseError(format!("Failed to analyze type: {}", e)))?;
        let strategy = TypeAnalyzer::determine_query_strategy(&analyzed);

        match strategy {
            Ok(QueryType::QueryAs) => {
                // Test passed - Vec<T> correctly results in QueryAs
            }
            _ => {
                return Err(TestError::AssertionError(
                    "Result<Vec<User>> should use QueryAs strategy with fetch_all".to_string(),
                ));
            }
        }

        // Test Option<T> uses fetch_optional
        let ty: syn::Type = parse_quote!(Result<Option<User>>);
        let analyzed = TypeAnalyzer::analyze_type(&ty)
            .map_err(|e| TestError::ParseError(format!("Failed to analyze type: {}", e)))?;
        let strategy = TypeAnalyzer::determine_query_strategy(&analyzed);

        match strategy {
            Ok(QueryType::QueryAs) => {
                // Test passed - Option<T> correctly results in QueryAs
            }
            _ => {
                return Err(TestError::AssertionError(
                    "Result<Option<User>> should use QueryAs strategy with fetch_optional"
                        .to_string(),
                ));
            }
        }

        Ok(())
    }
}

/// Code generation specific tests
pub struct CodeGenerationTests;

impl CodeGenerationTests {
    pub fn test_basic_scalar_generation() -> Result<(), TestError> {
        // Test basic scalar DML method generation
        let method = create_test_dml_method(
            "get_count",
            "SELECT COUNT(*) as count FROM users",
            vec![],
            parse_quote!(Result<i64>),
            SqlStatementType::Select,
        );

        let result = crate::code_generator::CodeGenerator::generate_dml_methods(&method);

        match result {
            Ok(generated) => {
                let code_str = generated.to_string();
                // Basic checks for generated code
                if !code_str.contains("get_count_query") {
                    return Err(TestError::AssertionError(
                        "Generated code should contain query method".to_string(),
                    ));
                }
                if !code_str.contains("get_count") {
                    return Err(TestError::AssertionError(
                        "Generated code should contain main method".to_string(),
                    ));
                }
            }
            Err(e) => {
                return Err(TestError::CodeGenerationError(format!(
                    "Failed to generate scalar method: {}",
                    e
                )));
            }
        }

        Ok(())
    }

    pub fn test_struct_generation() -> Result<(), TestError> {
        let method = create_test_dml_method(
            "find_user",
            "SELECT id, name, email FROM users WHERE id = $1",
            vec![DmlParameter {
                name: "id".to_string(),
                type_: parse_quote!(i64),
                is_pool: false,
                is_dynamic_param: false,
                is_generic: false,
            }],
            parse_quote!(Result<User>),
            SqlStatementType::Select,
        );

        let result = crate::code_generator::CodeGenerator::generate_dml_methods(&method);

        match result {
            Ok(generated) => {
                let code_str = generated.to_string();
                if !code_str.contains("find_user_query") || !code_str.contains("find_user") {
                    return Err(TestError::AssertionError(
                        "Generated code missing expected methods".to_string(),
                    ));
                }
            }
            Err(e) => {
                return Err(TestError::CodeGenerationError(format!(
                    "Failed to generate struct method: {}",
                    e
                )));
            }
        }

        Ok(())
    }

    pub fn test_tuple_generation() -> Result<(), TestError> {
        let method = create_test_dml_method(
            "get_stats",
            "SELECT COUNT(*) as count, AVG(age) as avg_age FROM users",
            vec![],
            parse_quote!(Result<(i64, f64)>),
            SqlStatementType::Select,
        );

        let result = crate::code_generator::CodeGenerator::generate_dml_methods(&method);

        match result {
            Ok(generated) => {
                let code_str = generated.to_string();
                if !code_str.contains("get_stats_query") {
                    return Err(TestError::AssertionError(
                        "Generated code should contain query method for tuple".to_string(),
                    ));
                }
            }
            Err(e) => {
                return Err(TestError::CodeGenerationError(format!(
                    "Failed to generate tuple method: {}",
                    e
                )));
            }
        }

        Ok(())
    }

    #[cfg(feature = "sqlite")]
    pub fn test_tuple_casting() -> Result<(), TestError> {
        let method = create_test_dml_method(
            "avg_stats",
            "SELECT birth_year, AVG(age) as avg_age FROM users WHERE birth_year IS NOT NULL GROUP BY birth_year HAVING AVG(age) >$1",
            vec![],
            parse_quote!(Result<Vec<(Option<u16>, Option<f32>)>>),
            SqlStatementType::Select,
        );

        let result = crate::code_generator::CodeGenerator::generate_dml_methods(&method);

        match result {
            Ok(generated) => {
                let code_str = generated.to_string();
                // Check that f64 -> f32 casting is generated correctly (SQLite returns f64 for AVG)
                if !code_str.contains("as f32") {
                    return Err(TestError::AssertionError(
                        "Generated code should contain f32 casting".to_string(),
                    ));
                }
                // Check that i64 -> u16 casting is present (SQLite uses i64 for all integers)
                if !code_str.contains("as u16") {
                    return Err(TestError::AssertionError(
                        "Generated code should contain u16 casting".to_string(),
                    ));
                }
            }
            Err(e) => {
                return Err(TestError::CodeGenerationError(format!(
                    "Failed to generate tuple casting: {}",
                    e
                )));
            }
        }

        Ok(())
    }

    pub fn test_parameter_handling() -> Result<(), TestError> {
        let method = create_test_dml_method(
            "find_by_age_range",
            "SELECT id, name, age FROM users WHERE age BETWEEN $1 AND $2",
            vec![
                DmlParameter {
                    name: "min_age".to_string(),
                    type_: parse_quote!(u8),
                    is_pool: false,
                    is_dynamic_param: false,
                    is_generic: false,
                },
                DmlParameter {
                    name: "max_age".to_string(),
                    type_: parse_quote!(u8),
                    is_pool: false,
                    is_dynamic_param: false,
                    is_generic: false,
                },
            ],
            parse_quote!(Result<Vec<User>>),
            SqlStatementType::Select,
        );

        let result = crate::code_generator::CodeGenerator::generate_dml_methods(&method);

        match result {
            Ok(generated) => {
                let code_str = generated.to_string();
                if !code_str.contains("min_age") || !code_str.contains("max_age") {
                    return Err(TestError::AssertionError(
                        "Generated code should contain parameter names".to_string(),
                    ));
                }
            }
            Err(e) => {
                return Err(TestError::CodeGenerationError(format!(
                    "Failed to generate method with parameters: {}",
                    e
                )));
            }
        }

        Ok(())
    }
}

/// Edge case specific tests
pub struct EdgeCaseTests;

impl EdgeCaseTests {
    pub fn test_empty_parameters() -> Result<(), TestError> {
        let method = create_test_dml_method(
            "get_all",
            "SELECT id, name, email FROM users",
            vec![],
            parse_quote!(Result<Vec<User>>),
            SqlStatementType::Select,
        );

        let result = crate::code_generator::CodeGenerator::generate_dml_methods(&method);

        if result.is_err() {
            return Err(TestError::CodeGenerationError(
                "Should handle empty parameters".to_string(),
            ));
        }

        Ok(())
    }

    pub fn test_complex_nested_types() -> Result<(), TestError> {
        let ty: syn::Type = parse_quote!(Result<Option<Vec<(i32, Option<String>)>>>);
        let analyzed = TypeAnalyzer::analyze_type(&ty).map_err(|e| {
            TestError::ParseError(format!("Failed to analyze complex nested type: {}", e))
        })?;

        // Should not panic or fail catastrophically
        let _strategy = TypeAnalyzer::determine_query_strategy(&analyzed);

        Ok(())
    }

    pub fn test_invalid_sql_handling() -> Result<(), TestError> {
        // This should be handled gracefully by the SQL parser
        let method = create_test_dml_method(
            "invalid_sql",
            "INVALID SQL SYNTAX",
            vec![],
            parse_quote!(Result<i32>),
            SqlStatementType::Select,
        );

        let result = crate::code_generator::CodeGenerator::generate_dml_methods(&method);

        // Should either succeed (if SQL parser is lenient) or provide meaningful error
        match result {
            Ok(_) => (), // SQL parser was lenient
            Err(e) => {
                // Should have meaningful error message
                if e.to_string().is_empty() {
                    return Err(TestError::AssertionError(
                        "Error message should not be empty".to_string(),
                    ));
                }
            }
        }

        Ok(())
    }

    pub fn test_type_mismatch_detection() -> Result<(), TestError> {
        // This tests the type system's ability to handle edge cases
        let ty: syn::Type = parse_quote!(Result<UnknownType>);
        let analyzed = TypeAnalyzer::analyze_type(&ty)
            .map_err(|e| TestError::ParseError(format!("Failed to analyze unknown type: {}", e)))?;

        match analyzed {
            ReturnType::Result { ok_type, .. } => match ok_type.as_ref() {
                ReturnType::Struct { name } if name.to_string() == "UnknownType" => (),
                _ => {
                    return Err(TestError::AssertionError(
                        "Unknown type should be analyzed as struct".to_string(),
                    ));
                }
            },
            _ => {
                return Err(TestError::AssertionError(
                    "Should handle unknown types gracefully".to_string(),
                ));
            }
        }

        Ok(())
    }
}

/// Helper functions for tests
fn create_test_dml_method(
    name: &str,
    sql: &str,
    parameters: Vec<DmlParameter>,
    return_type: syn::Type,
    kind: SqlStatementType,
) -> DmlMethod {
    use syn::{FnArg, Pat, PatIdent, PatType, Signature, TraitItemFn};

    // Create function signature
    let mut inputs = syn::punctuated::Punctuated::new();

    // Add self parameter
    inputs.push(FnArg::Receiver(syn::Receiver {
        attrs: vec![],
        reference: Some((syn::Token![&](proc_macro2::Span::call_site()), None)),
        mutability: None,
        self_token: syn::Token![self](proc_macro2::Span::call_site()),
        colon_token: None,
        ty: Box::new(syn::parse_quote! { Self }),
    }));

    // Add other parameters
    for param in &parameters {
        let pat = PatIdent {
            attrs: vec![],
            by_ref: None,
            mutability: None,
            ident: syn::Ident::new(&param.name, proc_macro2::Span::call_site()),
            subpat: None,
        };

        inputs.push(FnArg::Typed(PatType {
            attrs: vec![],
            pat: Box::new(Pat::Ident(pat)),
            colon_token: syn::Token![:](proc_macro2::Span::call_site()),
            ty: Box::new(param.type_.clone()),
        }));
    }

    let sig = Signature {
        constness: None,
        asyncness: Some(syn::Token![async](proc_macro2::Span::call_site())),
        unsafety: None,
        abi: None,
        fn_token: syn::Token![fn](proc_macro2::Span::call_site()),
        ident: syn::Ident::new(name, proc_macro2::Span::call_site()),
        generics: syn::Generics::default(),
        paren_token: syn::token::Paren::default(),
        inputs,
        variadic: None,
        output: syn::ReturnType::Type(
            syn::Token![->](proc_macro2::Span::call_site()),
            Box::new(return_type),
        ),
    };

    let trait_method = TraitItemFn {
        attrs: vec![],
        sig,
        default: None,
        semi_token: Some(syn::Token![;](proc_macro2::Span::call_site())),
    };

    DmlMethod {
        method: trait_method,
        sql_content: sql.to_string(),
        parameters,
        statement: sqlx_data_parser::parse_sql(sql).unwrap(),
        kind,
        is_json_query: false,
        is_multi_insert: false,
        is_unchecked: false,
        has_explicit_instrument: false,
        trait_instrument: false,
        return_info_cache: std::sync::OnceLock::new(),
    }
}

fn matches_return_type(actual: &ReturnType, expected: &ReturnType) -> bool {
    match (actual, expected) {
        (ReturnType::Scalar { name: n1 }, ReturnType::Scalar { name: n2 }) => n1 == n2,
        (ReturnType::Struct { name: n1 }, ReturnType::Struct { name: n2 }) => n1 == n2,
        _ => false, // Simplified comparison for basic tests
    }
}

/// Test error types
#[derive(Debug)]
pub enum TestError {
    ParseError(String),
    AssertionError(String),
    CodeGenerationError(String),
}

impl std::fmt::Display for TestError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TestError::ParseError(msg) => write!(f, "Parse error: {}", msg),
            TestError::AssertionError(msg) => write!(f, "Assertion failed: {}", msg),
            TestError::CodeGenerationError(msg) => write!(f, "Code generation error: {}", msg),
        }
    }
}

impl std::error::Error for TestError {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_framework_comprehensive() {
        // Run all comprehensive tests
        match MacroTestFramework::run_all_tests() {
            Ok(()) => println!("๐ŸŽ‰ All comprehensive tests passed!"),
            Err(e) => panic!("Test failed: {}", e),
        }
    }

    #[test]
    fn test_individual_type_analysis() {
        TypeAnalysisTests::test_scalar_types().unwrap();
        TypeAnalysisTests::test_result_type_alias().unwrap();
        TypeAnalysisTests::test_complex_types().unwrap();
    }

    #[test]
    fn test_individual_query_strategies() {
        QueryStrategyTests::test_scalar_strategies().unwrap();
        QueryStrategyTests::test_struct_strategies().unwrap();
        QueryStrategyTests::test_collection_strategies().unwrap();
    }

    #[test]
    fn test_individual_code_generation() {
        CodeGenerationTests::test_basic_scalar_generation().unwrap();
        CodeGenerationTests::test_struct_generation().unwrap();
        #[cfg(feature = "sqlite")]
        CodeGenerationTests::test_tuple_casting().unwrap();
        CodeGenerationTests::test_parameter_handling().unwrap();
    }
}