sea-orm-spanner 0.1.0

Google Cloud Spanner backend for SeaORM
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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
mod common;
mod entity;

use {
    chrono::{Datelike, NaiveDate, Utc},
    common::setup_test_database,
    entity::all_types,
    sea_orm::{ActiveModelTrait, EntityTrait, Set},
    serde_json::json,
    serial_test::serial,
};

fn create_test_model(id: &str) -> all_types::ActiveModel {
    all_types::ActiveModel {
        id: Set(id.to_string()),
        string_val: Set("test string".to_string()),
        string_nullable: Set(Some("nullable string".to_string())),
        int64_val: Set(9223372036854775807i64),
        int64_nullable: Set(Some(-9223372036854775808i64)),
        int32_val: Set(2147483647_i64),
        int32_nullable: Set(Some(-2147483648_i64)),
        float64_val: Set(std::f64::consts::PI),
        float64_nullable: Set(Some(-1.7976931348623157e308)),
        float32_val: Set(std::f64::consts::PI),
        float32_nullable: Set(Some(-3.40282e38f64)),
        bool_val: Set(true),
        bool_nullable: Set(Some(false)),
        bytes_val: Set(vec![0x00, 0x01, 0xFF, 0xFE]),
        bytes_nullable: Set(Some(vec![0xDE, 0xAD, 0xBE, 0xEF])),
        timestamp_val: Set(Utc::now()),
        timestamp_nullable: Set(Some(Utc::now())),
        date_val: Set(NaiveDate::from_ymd_opt(2026, 1, 26).unwrap()),
        date_nullable: Set(Some(NaiveDate::from_ymd_opt(2025, 12, 25).unwrap())),
        json_val: Set(json!({"key": "value", "number": 42, "nested": {"a": 1}})),
        json_nullable: Set(Some(json!(["array", "of", "strings"]))),
    }
}

fn create_test_model_with_nulls(id: &str) -> all_types::ActiveModel {
    all_types::ActiveModel {
        id: Set(id.to_string()),
        string_val: Set("required string".to_string()),
        string_nullable: Set(None),
        int64_val: Set(i64::MIN),
        int64_nullable: Set(None),
        int32_val: Set(0_i64),
        int32_nullable: Set(None),
        float64_val: Set(0.0),
        float64_nullable: Set(None),
        float32_val: Set(0.0f64),
        float32_nullable: Set(None),
        bool_val: Set(false),
        bool_nullable: Set(None),
        bytes_val: Set(vec![0]),
        bytes_nullable: Set(None),
        timestamp_val: Set(Utc::now()),
        timestamp_nullable: Set(None),
        date_val: Set(NaiveDate::from_ymd_opt(2026, 1, 1).unwrap()),
        date_nullable: Set(None),
        json_val: Set(json!({})),
        json_nullable: Set(None),
    }
}

mod string_type_tests {
    use super::*;

    #[tokio::test]
    #[serial]
    async fn test_string_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            id: Set(id.clone()),
            string_val: Set("hello world".to_string()),
            string_nullable: Set(Some("optional".to_string())),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.string_val, "hello world");
        assert_eq!(inserted.string_nullable, Some("optional".to_string()));

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.string_val, "hello world");
        assert_eq!(selected.string_nullable, Some("optional".to_string()));
    }

    #[tokio::test]
    #[serial]
    async fn test_string_with_special_characters() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let special_string = "Hello, 世界! 🌍 'quotes' \"double\" \\backslash\\ newline\nend";
        let model = all_types::ActiveModel {
            string_val: Set(special_string.to_string()),
            string_nullable: Set(None),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.string_val, special_string);

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.string_val, special_string);
    }

    #[tokio::test]
    #[serial]
    async fn test_string_nullable_null() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            string_nullable: Set(None),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert!(inserted.string_nullable.is_none());

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert!(selected.string_nullable.is_none());
    }
}

mod integer_type_tests {
    use super::*;

    #[tokio::test]
    #[serial]
    async fn test_int64_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            int64_val: Set(i64::MAX),
            int64_nullable: Set(Some(i64::MIN)),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.int64_val, i64::MAX);
        assert_eq!(inserted.int64_nullable, Some(i64::MIN));

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.int64_val, i64::MAX);
        assert_eq!(selected.int64_nullable, Some(i64::MIN));
    }

    #[tokio::test]
    #[serial]
    async fn test_int32_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            int32_val: Set(i32::MAX as i64),
            int32_nullable: Set(Some(i32::MIN as i64)),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.int32_val, i32::MAX as i64);
        assert_eq!(inserted.int32_nullable, Some(i32::MIN as i64));

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.int32_val, i32::MAX as i64);
        assert_eq!(selected.int32_nullable, Some(i32::MIN as i64));
    }

    #[tokio::test]
    #[serial]
    async fn test_integer_zero_and_negative() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            int64_val: Set(i64::MIN),
            int64_nullable: Set(Some(i64::MAX)),
            int32_val: Set(-999_i64),
            int32_nullable: Set(Some(0_i64)),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.int64_val, i64::MIN);
        assert_eq!(inserted.int64_nullable, Some(i64::MAX));
        assert_eq!(inserted.int32_val, -999_i64);
        assert_eq!(inserted.int32_nullable, Some(0_i64));

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.int64_val, i64::MIN);
        assert_eq!(selected.int64_nullable, Some(i64::MAX));
        assert_eq!(selected.int32_val, -999_i64);
        assert_eq!(selected.int32_nullable, Some(0_i64));
    }

    #[tokio::test]
    #[serial]
    async fn test_integer_nullable_null() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            int64_nullable: Set(None),
            int32_nullable: Set(None),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert!(inserted.int64_nullable.is_none());
        assert!(inserted.int32_nullable.is_none());

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert!(selected.int64_nullable.is_none());
        assert!(selected.int32_nullable.is_none());
    }
}

mod float_type_tests {
    use super::*;

    #[tokio::test]
    #[serial]
    async fn test_float64_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            float64_val: Set(std::f64::consts::PI),
            float64_nullable: Set(Some(std::f64::consts::E)),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert!((inserted.float64_val - std::f64::consts::PI).abs() < f64::EPSILON);
        assert!((inserted.float64_nullable.unwrap() - std::f64::consts::E).abs() < f64::EPSILON);

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert!((selected.float64_val - std::f64::consts::PI).abs() < f64::EPSILON);
    }

    #[tokio::test]
    #[serial]
    async fn test_float32_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            float32_val: Set(std::f32::consts::PI as f64),
            float32_nullable: Set(Some(std::f32::consts::E as f64)),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert!((inserted.float32_val - std::f32::consts::PI as f64).abs() < f64::EPSILON);

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert!((selected.float32_val - std::f32::consts::PI as f64).abs() < f64::EPSILON);
    }

    #[tokio::test]
    #[serial]
    async fn test_float_special_values() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            float64_val: Set(0.0),
            float64_nullable: Set(Some(-0.0)),
            float32_val: Set(0.0f64),
            float32_nullable: Set(None),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.float64_val, 0.0);
        assert!(inserted.float32_nullable.is_none());

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.float64_val, 0.0);
    }
}

mod bool_type_tests {
    use super::*;

    #[tokio::test]
    #[serial]
    async fn test_bool_true_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            bool_val: Set(true),
            bool_nullable: Set(Some(true)),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert!(inserted.bool_val);
        assert_eq!(inserted.bool_nullable, Some(true));

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert!(selected.bool_val);
        assert_eq!(selected.bool_nullable, Some(true));
    }

    #[tokio::test]
    #[serial]
    async fn test_bool_false_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            bool_val: Set(false),
            bool_nullable: Set(Some(false)),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert!(!inserted.bool_val);
        assert_eq!(inserted.bool_nullable, Some(false));

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert!(!selected.bool_val);
        assert_eq!(selected.bool_nullable, Some(false));
    }

    #[tokio::test]
    #[serial]
    async fn test_bool_nullable_null() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            bool_nullable: Set(None),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert!(inserted.bool_nullable.is_none());

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert!(selected.bool_nullable.is_none());
    }
}

mod bytes_type_tests {
    use super::*;

    #[tokio::test]
    #[serial]
    async fn test_bytes_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let bytes_data = vec![0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD];
        let model = all_types::ActiveModel {
            bytes_val: Set(bytes_data.clone()),
            bytes_nullable: Set(Some(vec![0xDE, 0xAD, 0xBE, 0xEF])),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.bytes_val, bytes_data);
        assert_eq!(inserted.bytes_nullable, Some(vec![0xDE, 0xAD, 0xBE, 0xEF]));

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.bytes_val, bytes_data);
        assert_eq!(selected.bytes_nullable, Some(vec![0xDE, 0xAD, 0xBE, 0xEF]));
    }

    #[tokio::test]
    #[serial]
    async fn test_bytes_single_null() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            bytes_val: Set(vec![0]),
            bytes_nullable: Set(Some(vec![0, 0])),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.bytes_val, vec![0]);
        assert_eq!(inserted.bytes_nullable, Some(vec![0, 0]));

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.bytes_val, vec![0]);
        assert_eq!(selected.bytes_nullable, Some(vec![0, 0]));
    }

    #[tokio::test]
    #[serial]
    async fn test_bytes_nullable_null() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            bytes_nullable: Set(None),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert!(inserted.bytes_nullable.is_none());

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert!(selected.bytes_nullable.is_none());
    }
}

mod timestamp_type_tests {
    use {
        super::*,
        chrono::{TimeZone, Utc},
    };

    #[tokio::test]
    #[serial]
    async fn test_timestamp_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let now = Utc::now();
        let model = all_types::ActiveModel {
            timestamp_val: Set(now),
            timestamp_nullable: Set(Some(now)),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        let time_diff = (inserted.timestamp_val - now).num_seconds().abs();
        assert!(
            time_diff < 2,
            "Timestamp difference too large: {} seconds",
            time_diff
        );

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        let selected_diff = (selected.timestamp_val - now).num_seconds().abs();
        assert!(selected_diff < 2, "Selected timestamp difference too large");
    }

    /// Regression test: timestamp read should not return None when data exists
    /// Bug report: "2026-01-31T07:30:08.391502Z" stored but returns None
    #[tokio::test]
    #[serial]
    async fn test_timestamp_not_none_after_read() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let specific_time = Utc.with_ymd_and_hms(2026, 1, 31, 7, 30, 8).unwrap();
        let model = all_types::ActiveModel {
            timestamp_val: Set(specific_time),
            timestamp_nullable: Set(Some(specific_time)),
            ..create_test_model(&id)
        };

        let _inserted = model.insert(&db).await.expect("Insert failed");

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");

        assert!(
            selected.timestamp_val.year() >= 2024,
            "BUG: timestamp_val returned wrong year {}. Expected 2026, got {:?}",
            selected.timestamp_val.year(),
            selected.timestamp_val
        );

        assert!(
            selected.timestamp_nullable.is_some(),
            "BUG: timestamp_nullable returned None when it should have a value"
        );

        let nullable_val = selected.timestamp_nullable.unwrap();
        assert!(
            nullable_val.year() >= 2024,
            "BUG: timestamp_nullable returned wrong year {}. Expected 2026, got {:?}",
            nullable_val.year(),
            nullable_val
        );

        let diff = (selected.timestamp_val - specific_time).num_seconds().abs();
        assert!(
            diff < 2,
            "timestamp_val differs from inserted value by {} seconds",
            diff
        );
    }

    #[tokio::test]
    #[serial]
    async fn test_timestamp_specific_date() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let specific_time = Utc.with_ymd_and_hms(2024, 6, 15, 12, 30, 45).unwrap();
        let model = all_types::ActiveModel {
            timestamp_val: Set(specific_time),
            timestamp_nullable: Set(Some(specific_time)),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(
            inserted.timestamp_val.date_naive(),
            specific_time.date_naive()
        );

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(
            selected.timestamp_val.date_naive(),
            specific_time.date_naive()
        );
    }

    #[tokio::test]
    #[serial]
    async fn test_timestamp_nullable_null() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            timestamp_nullable: Set(None),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert!(inserted.timestamp_nullable.is_none());

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert!(selected.timestamp_nullable.is_none());
    }
}

mod date_type_tests {
    use super::*;

    #[tokio::test]
    #[serial]
    async fn test_date_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let date_val = NaiveDate::from_ymd_opt(2026, 1, 26).unwrap();
        let date_nullable = NaiveDate::from_ymd_opt(2025, 12, 25).unwrap();

        let model = all_types::ActiveModel {
            date_val: Set(date_val),
            date_nullable: Set(Some(date_nullable)),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.date_val, date_val);
        assert_eq!(inserted.date_nullable, Some(date_nullable));

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.date_val, date_val);
        assert_eq!(selected.date_nullable, Some(date_nullable));
    }

    #[tokio::test]
    #[serial]
    async fn test_date_nullable_null() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            date_nullable: Set(None),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert!(inserted.date_nullable.is_none());

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert!(selected.date_nullable.is_none());
    }

    #[tokio::test]
    #[serial]
    async fn test_date_edge_cases() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let min_date = NaiveDate::from_ymd_opt(1, 1, 1).unwrap();
        let model = all_types::ActiveModel {
            date_val: Set(min_date),
            date_nullable: Set(None),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.date_val, min_date);

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.date_val, min_date);
    }
}

mod json_type_tests {
    use super::*;

    #[tokio::test]
    #[serial]
    async fn test_json_object_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let json_obj = json!({
            "name": "test",
            "count": 42,
            "active": true,
            "tags": ["a", "b", "c"]
        });

        let model = all_types::ActiveModel {
            json_val: Set(json_obj.clone()),
            json_nullable: Set(Some(json_obj.clone())),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.json_val, json_obj);
        assert_eq!(inserted.json_nullable, Some(json_obj.clone()));

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.json_val, json_obj);
        assert_eq!(selected.json_nullable, Some(json_obj));
    }

    #[tokio::test]
    #[serial]
    async fn test_json_array_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let json_arr = json!([1, 2, 3, "four", true, null]);

        let model = all_types::ActiveModel {
            json_val: Set(json_arr.clone()),
            json_nullable: Set(Some(json_arr.clone())),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.json_val, json_arr);

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.json_val, json_arr);
    }

    #[tokio::test]
    #[serial]
    async fn test_json_nested_insert_and_select() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let nested_json = json!({
            "level1": {
                "level2": {
                    "level3": {
                        "value": "deep"
                    }
                }
            },
            "array_of_objects": [
                {"id": 1, "name": "first"},
                {"id": 2, "name": "second"}
            ]
        });

        let model = all_types::ActiveModel {
            json_val: Set(nested_json.clone()),
            json_nullable: Set(None),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.json_val, nested_json);

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.json_val, nested_json);
    }

    #[tokio::test]
    #[serial]
    async fn test_json_primitive_values() {
        let db = setup_test_database().await;

        let test_cases = vec![json!(null), json!(true), json!(false), json!("a string")];

        for (i, json_val) in test_cases.into_iter().enumerate() {
            let id = uuid::Uuid::new_v4().to_string();

            let model = all_types::ActiveModel {
                json_val: Set(json_val.clone()),
                json_nullable: Set(Some(json_val.clone())),
                ..create_test_model(&id)
            };

            let inserted = model.insert(&db).await.expect("Insert failed");
            assert_eq!(inserted.json_val, json_val, "Failed for case {}", i);

            let selected = all_types::Entity::find_by_id(&id)
                .one(&db)
                .await
                .expect("Select failed")
                .expect("Entity not found");
            assert_eq!(selected.json_val, json_val, "Select failed for case {}", i);
        }
    }

    #[tokio::test]
    #[serial]
    async fn test_json_empty_structures() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let empty_obj = json!({});
        let model = all_types::ActiveModel {
            json_val: Set(empty_obj.clone()),
            json_nullable: Set(Some(json!([]))),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.json_val, empty_obj);
        assert_eq!(inserted.json_nullable, Some(json!([])));

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.json_val, empty_obj);
        assert_eq!(selected.json_nullable, Some(json!([])));
    }

    #[tokio::test]
    #[serial]
    async fn test_json_with_special_characters() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let special_json = json!({
            "unicode": "日本語 🎉 émojis",
            "quotes": "He said \"hello\"",
            "backslash": "path\\to\\file",
            "newlines": "line1\nline2\ttab"
        });

        let model = all_types::ActiveModel {
            json_val: Set(special_json.clone()),
            json_nullable: Set(None),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.json_val, special_json);

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.json_val, special_json);
    }

    #[tokio::test]
    #[serial]
    async fn test_json_nullable_null() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = all_types::ActiveModel {
            json_nullable: Set(None),
            ..create_test_model(&id)
        };

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert!(inserted.json_nullable.is_none());

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert!(selected.json_nullable.is_none());
    }
}

mod all_nulls_test {
    use super::*;

    #[tokio::test]
    #[serial]
    async fn test_all_nullable_fields_as_null() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = create_test_model_with_nulls(&id);

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert!(inserted.string_nullable.is_none());
        assert!(inserted.int64_nullable.is_none());
        assert!(inserted.int32_nullable.is_none());
        assert!(inserted.float64_nullable.is_none());
        assert!(inserted.float32_nullable.is_none());
        assert!(inserted.bool_nullable.is_none());
        assert!(inserted.bytes_nullable.is_none());
        assert!(inserted.timestamp_nullable.is_none());
        assert!(inserted.json_nullable.is_none());

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert!(selected.string_nullable.is_none());
        assert!(selected.int64_nullable.is_none());
        assert!(selected.int32_nullable.is_none());
        assert!(selected.float64_nullable.is_none());
        assert!(selected.float32_nullable.is_none());
        assert!(selected.bool_nullable.is_none());
        assert!(selected.bytes_nullable.is_none());
        assert!(selected.timestamp_nullable.is_none());
        assert!(selected.json_nullable.is_none());
    }
}

mod all_values_test {
    use super::*;

    #[tokio::test]
    #[serial]
    async fn test_all_fields_with_values() {
        let db = setup_test_database().await;
        let id = uuid::Uuid::new_v4().to_string();

        let model = create_test_model(&id);

        let inserted = model.insert(&db).await.expect("Insert failed");
        assert_eq!(inserted.string_val, "test string");
        assert!(inserted.string_nullable.is_some());
        assert_eq!(inserted.int64_val, 9223372036854775807i64);
        assert!(inserted.int64_nullable.is_some());
        assert!(inserted.bool_val);
        assert!(inserted.bool_nullable.is_some());
        assert!(!inserted.bytes_val.is_empty());
        assert!(inserted.bytes_nullable.is_some());
        assert!(inserted.json_nullable.is_some());

        let selected = all_types::Entity::find_by_id(&id)
            .one(&db)
            .await
            .expect("Select failed")
            .expect("Entity not found");
        assert_eq!(selected.string_val, "test string");
        assert!(selected.string_nullable.is_some());
        assert_eq!(selected.int64_val, 9223372036854775807i64);
    }
}