data-modelling-core 2.4.0

Core SDK library for model operations across platforms
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
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
//! Integration tests for round-trip import/export

use data_modelling_core::convert::migrate_dataflow::migrate_dataflow_to_domain;
use data_modelling_core::export::json_schema::JSONSchemaExporter;
use data_modelling_core::export::odcl::ODCLExporter;
use data_modelling_core::export::odcs::ODCSExporter;
use data_modelling_core::export::sql::SQLExporter;
use data_modelling_core::import::json_schema::JSONSchemaImporter;
use data_modelling_core::import::odcs::ODCSImporter;
use data_modelling_core::import::sql::SQLImporter;
use data_modelling_core::models::enums::InfrastructureType;
use data_modelling_core::models::{Column, Table};
use serde_json::json;

fn create_table_from_import_result(
    result: &data_modelling_core::import::ImportResult,
) -> Vec<Table> {
    result
        .tables
        .iter()
        .map(|t| Table {
            id: uuid::Uuid::new_v4(),
            name: t.name.clone().unwrap_or_default(),
            columns: t
                .columns
                .iter()
                .map(|c| Column {
                    name: c.name.clone(),
                    data_type: c.data_type.clone(),
                    physical_type: c.physical_type.clone(),
                    nullable: c.nullable,
                    primary_key: c.primary_key,
                    description: c.description.clone().unwrap_or_default(),
                    quality: c.quality.clone().unwrap_or_default(),
                    relationships: c.relationships.clone(),
                    enum_values: c.enum_values.clone().unwrap_or_default(),
                    ..Default::default()
                })
                .collect(),
            database_type: None,
            catalog_name: None,
            schema_name: None,
            medallion_layers: Vec::new(),
            scd_pattern: None,
            data_vault_classification: None,
            modeling_level: None,
            tags: Vec::new(),
            odcl_metadata: std::collections::HashMap::new(),
            owner: None,
            sla: None,
            contact_details: None,
            infrastructure_type: None,
            notes: None,
            position: None,
            yaml_file_path: None,
            drawio_cell_id: None,
            quality: Vec::new(),
            errors: Vec::new(),
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
        })
        .collect()
}

mod sql_roundtrip_tests {
    use super::*;

    #[test]
    fn test_sql_import_export_roundtrip() {
        // Import SQL
        let original_sql =
            "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, email TEXT);";
        let importer = SQLImporter::new("postgres");
        let import_result = importer.parse(original_sql).unwrap();

        assert_eq!(import_result.tables.len(), 1);
        let original_table = &import_result.tables[0];
        assert_eq!(original_table.columns.len(), 3);

        // Convert to Table objects
        let tables = create_table_from_import_result(&import_result);

        // Export back to SQL
        let exporter = SQLExporter;
        let export_result = exporter.export(&tables, Some("postgres")).unwrap();

        // Re-import the exported SQL
        let reimport_result = importer.parse(&export_result.content).unwrap();

        // Verify structure is preserved
        assert_eq!(reimport_result.tables.len(), 1);
        let reimported_table = &reimport_result.tables[0];

        assert_eq!(original_table.name, reimported_table.name);
        assert_eq!(original_table.columns.len(), reimported_table.columns.len());

        // Verify each column
        for (orig, reimp) in original_table
            .columns
            .iter()
            .zip(reimported_table.columns.iter())
        {
            assert_eq!(orig.name, reimp.name);
            assert_eq!(orig.nullable, reimp.nullable);
            assert_eq!(orig.primary_key, reimp.primary_key);
        }
    }

    #[test]
    fn test_multiple_tables_roundtrip() {
        let original_sql = r#"
            CREATE TABLE users (id INT PRIMARY KEY, name TEXT NOT NULL);
            CREATE TABLE orders (id INT PRIMARY KEY, user_id INT, total DECIMAL);
        "#;
        let importer = SQLImporter::new("postgres");
        let import_result = importer.parse(original_sql).unwrap();

        assert_eq!(import_result.tables.len(), 2);

        let tables = create_table_from_import_result(&import_result);
        let exporter = SQLExporter;
        let export_result = exporter.export(&tables, Some("postgres")).unwrap();

        let reimport_result = importer.parse(&export_result.content).unwrap();

        assert_eq!(reimport_result.tables.len(), 2);
        assert_eq!(import_result.tables[0].name, reimport_result.tables[0].name);
        assert_eq!(import_result.tables[1].name, reimport_result.tables[1].name);
    }
}

mod json_schema_roundtrip_tests {
    use super::*;

    #[test]
    fn test_json_schema_import_export_roundtrip() {
        let original_schema = r#"
        {
            "title": "User",
            "type": "object",
            "properties": {
                "id": { "type": "integer" },
                "name": { "type": "string" },
                "active": { "type": "boolean" }
            },
            "required": ["id", "name"]
        }
        "#;

        let importer = JSONSchemaImporter::new();
        let import_result = importer.import(original_schema).unwrap();

        assert_eq!(import_result.tables.len(), 1);
        assert_eq!(import_result.tables[0].columns.len(), 3);

        let tables = create_table_from_import_result(&import_result);

        let exporter = JSONSchemaExporter;
        let export_result = exporter.export(&tables).unwrap();

        // Parse the exported JSON Schema
        let exported_schema: serde_json::Value =
            serde_json::from_str(&export_result.content).unwrap();

        // Verify structure
        let definitions = exported_schema
            .get("definitions")
            .unwrap()
            .as_object()
            .unwrap();
        assert!(definitions.contains_key("User"));

        let user_schema = definitions.get("User").unwrap();
        let properties = user_schema.get("properties").unwrap().as_object().unwrap();
        assert!(properties.contains_key("id"));
        assert!(properties.contains_key("name"));
        assert!(properties.contains_key("active"));
    }

    #[test]
    fn test_json_schema_validation_conditions_roundtrip() {
        let schema_with_validations = r#"
        {
            "title": "Product",
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 1000000
                },
                "name": {
                    "type": "string",
                    "minLength": 3,
                    "maxLength": 100,
                    "pattern": "^[A-Z][a-zA-Z0-9 ]*$"
                },
                "price": {
                    "type": "number",
                    "minimum": 0,
                    "exclusiveMinimum": false,
                    "multipleOf": 0.01
                },
                "status": {
                    "type": "string",
                    "enum": ["active", "inactive", "pending"]
                },
                "tags": {
                    "type": "array",
                    "items": { "type": "string" },
                    "minItems": 1,
                    "maxItems": 10,
                    "uniqueItems": true
                }
            },
            "required": ["id", "name"]
        }
        "#;

        let importer = JSONSchemaImporter::new();
        let import_result = importer.import(schema_with_validations).unwrap();

        assert_eq!(import_result.tables.len(), 1);
        let table = &import_result.tables[0];
        assert_eq!(table.name.as_deref(), Some("Product"));

        // Verify validation conditions were extracted
        let id_col = table.columns.iter().find(|c| c.name == "id").unwrap();
        assert!(
            id_col.quality.is_some() && !id_col.quality.as_ref().unwrap().is_empty(),
            "id column should have quality rules"
        );

        let name_col = table.columns.iter().find(|c| c.name == "name").unwrap();
        assert!(
            name_col.quality.is_some() && !name_col.quality.as_ref().unwrap().is_empty(),
            "name column should have quality rules"
        );

        let status_col = table.columns.iter().find(|c| c.name == "status").unwrap();
        assert!(
            status_col.enum_values.is_some()
                && !status_col.enum_values.as_ref().unwrap().is_empty(),
            "status column should have enum values"
        );
        assert_eq!(status_col.enum_values.as_ref().unwrap().len(), 3);

        // Export back to JSON Schema
        let tables = create_table_from_import_result(&import_result);
        let exporter = JSONSchemaExporter;
        let export_result = exporter.export(&tables).unwrap();

        // Parse exported schema
        let exported_schema: serde_json::Value =
            serde_json::from_str(&export_result.content).unwrap();
        let definitions = exported_schema
            .get("definitions")
            .unwrap()
            .as_object()
            .unwrap();
        let product_schema = definitions.get("Product").unwrap();
        let properties = product_schema
            .get("properties")
            .unwrap()
            .as_object()
            .unwrap();

        // Verify validations were exported
        let id_prop = properties.get("id").unwrap().as_object().unwrap();
        assert!(id_prop.contains_key("minimum"));
        assert!(id_prop.contains_key("maximum"));

        let name_prop = properties.get("name").unwrap().as_object().unwrap();
        assert!(name_prop.contains_key("minLength"));
        assert!(name_prop.contains_key("maxLength"));
        assert!(name_prop.contains_key("pattern"));

        let status_prop = properties.get("status").unwrap().as_object().unwrap();
        assert!(status_prop.contains_key("enum"));
        let enum_vals = status_prop.get("enum").unwrap().as_array().unwrap();
        assert_eq!(enum_vals.len(), 3);

        let tags_prop = properties.get("tags").unwrap().as_object().unwrap();
        assert!(tags_prop.contains_key("minItems"));
        assert!(tags_prop.contains_key("maxItems"));
        assert!(tags_prop.contains_key("uniqueItems"));
    }
}

mod validation_integration_tests {
    use data_modelling_core::validation::{
        validate_column_name, validate_data_type, validate_table_name,
    };

    #[test]
    fn test_validation_with_imported_data() {
        use super::*;

        let sql = "CREATE TABLE valid_table (id INT PRIMARY KEY, user_name TEXT);";
        let importer = SQLImporter::new("postgres");
        let result = importer.parse(sql).unwrap();

        let table = &result.tables[0];

        // Table name should be valid
        assert!(validate_table_name(table.name.as_deref().unwrap()).is_ok());

        // Column names should be valid
        for col in &table.columns {
            assert!(validate_column_name(&col.name).is_ok());
            assert!(validate_data_type(&col.data_type).is_ok());
        }
    }

    #[test]
    fn test_validation_catches_issues() {
        // Empty table name
        assert!(validate_table_name("").is_err());

        // Table name with invalid character
        assert!(validate_table_name("user;table").is_err());

        // Reserved word
        assert!(validate_table_name("SELECT").is_err());

        // Column starting with number
        assert!(validate_column_name("123column").is_err());

        // SQL injection in data type
        assert!(validate_data_type("INT; DROP TABLE users;--").is_err());
    }
}

mod cross_format_tests {
    use super::*;
    use data_modelling_core::import::avro::AvroImporter;
    use data_modelling_core::import::protobuf::ProtobufImporter;

    #[test]
    fn test_sql_to_json_schema() {
        // Import from SQL
        let sql =
            "CREATE TABLE users (id BIGINT PRIMARY KEY, name VARCHAR(100) NOT NULL, age INT);";
        let sql_importer = SQLImporter::new("postgres");
        let import_result = sql_importer.parse(sql).unwrap();

        let tables = create_table_from_import_result(&import_result);

        // Export to JSON Schema
        let json_exporter = JSONSchemaExporter;
        let export_result = json_exporter.export(&tables).unwrap();

        let schema: serde_json::Value = serde_json::from_str(&export_result.content).unwrap();
        let definitions = schema.get("definitions").unwrap().as_object().unwrap();

        let users = definitions.get("users").unwrap();
        let properties = users.get("properties").unwrap().as_object().unwrap();

        assert!(properties.contains_key("id"));
        assert!(properties.contains_key("name"));
        assert!(properties.contains_key("age"));
    }

    #[test]
    fn test_avro_to_sql() {
        let avro = r#"
        {
            "type": "record",
            "name": "Event",
            "fields": [
                { "name": "id", "type": "long" },
                { "name": "timestamp", "type": "long" },
                { "name": "payload", "type": "string" }
            ]
        }
        "#;

        let avro_importer = AvroImporter::new();
        let import_result = avro_importer.import(avro).unwrap();

        let tables = create_table_from_import_result(&import_result);

        let sql_exporter = SQLExporter;
        let export_result = sql_exporter.export(&tables, Some("postgres")).unwrap();

        assert!(export_result.content.contains("\"Event\""));
        assert!(export_result.content.contains("\"id\""));
        assert!(export_result.content.contains("\"timestamp\""));
        assert!(export_result.content.contains("\"payload\""));
    }

    #[test]
    fn test_protobuf_to_sql() {
        let proto = r#"
            syntax = "proto3";

            message Product {
                int64 id = 1;
                string name = 2;
                double price = 3;
            }
        "#;

        let proto_importer = ProtobufImporter::new();
        let import_result = proto_importer.import(proto).unwrap();

        let tables = create_table_from_import_result(&import_result);

        let sql_exporter = SQLExporter;
        let export_result = sql_exporter.export(&tables, Some("postgres")).unwrap();

        assert!(export_result.content.contains("\"Product\""));
        assert!(export_result.content.contains("\"id\""));
        assert!(export_result.content.contains("\"name\""));
        assert!(export_result.content.contains("\"price\""));
    }

    #[test]
    fn test_odcl_round_trip_preserves_all_fields() {
        let original_yaml = r#"
dataContractSpecification: 1.2.1
id: test-contract
info:
  title: Test Contract
  version: 1.0.0
models:
  test_table:
    type: table
    fields:
      complete_column:
        $ref: '#/definitions/order_id'
        description: This column has all three field types
        type: text
        required: true
        quality:
          - type: sql
            description: Validation rule
            query: SELECT COUNT(*) FROM test_table
            mustBeGreaterThan: 0
definitions:
  order_id:
    type: text
    format: uuid
    description: An internal ID
"#;

        // Import
        let mut importer = ODCSImporter::new();
        let import_result = importer.import(original_yaml).unwrap();
        assert_eq!(import_result.tables.len(), 1);

        let _table_data = &import_result.tables[0];
        let table = create_table_from_import_result(&import_result)[0].clone();

        // Verify fields were imported correctly
        let column = table
            .columns
            .iter()
            .find(|c| c.name == "complete_column")
            .expect("Should find complete_column");

        assert_eq!(column.description, "This column has all three field types");
        // ref_path is now stored as relationships
        assert!(
            !column.relationships.is_empty(),
            "Column should have relationships from $ref"
        );
        assert!(!column.quality.is_empty());

        // Export back to ODCL
        let exported_yaml = ODCLExporter::export_table(&table, "odcl");

        // Import the exported YAML
        let mut importer2 = ODCSImporter::new();
        let round_trip_result = importer2.import(&exported_yaml).unwrap();
        assert_eq!(round_trip_result.tables.len(), 1);

        let round_trip_table = create_table_from_import_result(&round_trip_result)[0].clone();
        let round_trip_column = round_trip_table
            .columns
            .iter()
            .find(|c| c.name == "complete_column")
            .expect("Should find complete_column after round-trip");

        // Verify critical fields are preserved (description and $ref are most important)
        assert_eq!(
            round_trip_column.description, column.description,
            "Description should be preserved"
        );
        assert_eq!(
            round_trip_column.relationships.len(),
            column.relationships.len(),
            "relationships (from $ref) should be preserved"
        );

        // Note: Quality preservation may vary depending on format conversion
        // The exported YAML contains quality, but format conversion (ODCL -> ODCS v3.1.0)
        // may affect how quality is parsed. Description and $ref are the critical fields
        // for this user story.
    }

    #[test]
    fn test_odcs_v3_1_0_round_trip_preserves_all_fields() {
        let original_yaml = r#"
apiVersion: v3.1.0
kind: DataContract
id: test-contract-id
version: 1.0.0
schema:
  - id: test_schema
    name: test_table
    properties:
      - id: col1_prop
        name: complete_column
        logicalType: string
        physicalType: varchar(100)
        required: true
        description: This column has all three field types
        $ref: '#/definitions/order_id'
        quality:
          - metric: nullValues
            mustBe: 0
            description: column should not contain null values
            dimension: completeness
            type: library
            severity: error
definitions:
  order_id:
    logicalType: string
    physicalType: uuid
    description: An internal ID
"#;

        // Import
        let mut importer = ODCSImporter::new();
        let import_result = importer.import(original_yaml).unwrap();
        assert_eq!(import_result.tables.len(), 1);

        let table = create_table_from_import_result(&import_result)[0].clone();

        // Verify fields were imported correctly
        let column = table
            .columns
            .iter()
            .find(|c| c.name == "complete_column")
            .expect("Should find complete_column");

        assert_eq!(column.description, "This column has all three field types");
        // $ref is now converted to relationships on import
        assert!(
            !column.relationships.is_empty(),
            "Should have relationships from $ref"
        );
        assert_eq!(
            column.relationships[0].to, "definitions/order_id",
            "Relationship should point to definitions/order_id"
        );
        assert!(!column.quality.is_empty());

        // Export back to ODCS v3.1.0
        let exported_yaml = ODCSExporter::export_table(&table, "odcs_v3_1_0");

        // Import the exported YAML
        let mut importer2 = ODCSImporter::new();
        let round_trip_result = importer2.import(&exported_yaml).unwrap();
        assert_eq!(round_trip_result.tables.len(), 1);

        let round_trip_table = create_table_from_import_result(&round_trip_result)[0].clone();
        let round_trip_column = round_trip_table
            .columns
            .iter()
            .find(|c| c.name == "complete_column")
            .expect("Should find complete_column after round-trip");

        // Verify critical fields are preserved (description and relationships are most important)
        assert_eq!(
            round_trip_column.description, column.description,
            "Description should be preserved"
        );
        assert_eq!(
            round_trip_column.relationships.len(),
            column.relationships.len(),
            "Relationships should be preserved"
        );
        if !column.relationships.is_empty() {
            assert_eq!(
                round_trip_column.relationships[0].to, column.relationships[0].to,
                "Relationship target should be preserved"
            );
        }

        // Note: Quality preservation may vary depending on format conversion
        // The exported YAML contains quality, but format conversion may affect parsing.
        // Description and relationships are the critical fields for this user story.
    }
}

mod dataflow_migration_tests {
    use super::*;

    #[test]
    fn test_dataflow_to_domain_migration() {
        let dataflow_yaml = r#"
nodes:
  - id: 550e8400-e29b-41d4-a716-446655440000
    name: kafka-cluster
    metadata:
      owner: "Data Engineering Team"
      infrastructure_type: "Kafka"
      sla:
        - property: availability
          value: 99.9
          unit: percent
          driver: operational
      contact_details:
        email: data-eng@example.com
        name: Data Engineering Team
        role: System Owner
      notes: Primary Kafka cluster for customer events
  - id: 660e8400-e29b-41d4-a716-446655440001
    name: postgres-db
    metadata:
      owner: "Database Team"
      infrastructure_type: "PostgreSQL"
relationships:
  - id: 770e8400-e29b-41d4-a716-446655440002
    source_node_id: 550e8400-e29b-41d4-a716-446655440000
    target_node_id: 660e8400-e29b-41d4-a716-446655440001
    metadata:
      notes: Data flows from Kafka to Postgres
"#;

        let domain = migrate_dataflow_to_domain(dataflow_yaml, Some("customer-service")).unwrap();

        // Verify domain
        assert_eq!(domain.name, "customer-service");
        assert_eq!(domain.systems.len(), 2);
        assert_eq!(domain.system_connections.len(), 1);

        // Verify first system (Kafka)
        let kafka_system = domain
            .systems
            .iter()
            .find(|s| s.name == "kafka-cluster")
            .unwrap();
        assert_eq!(kafka_system.infrastructure_type, InfrastructureType::Kafka);
        assert_eq!(
            kafka_system.owner,
            Some("Data Engineering Team".to_string())
        );
        assert!(kafka_system.sla.is_some());
        assert_eq!(kafka_system.sla.as_ref().unwrap().len(), 1);
        assert_eq!(
            kafka_system.sla.as_ref().unwrap()[0].property,
            "availability"
        );
        assert!(kafka_system.contact_details.is_some());
        assert_eq!(
            kafka_system.contact_details.as_ref().unwrap().email,
            Some("data-eng@example.com".to_string())
        );
        assert_eq!(
            kafka_system.notes,
            Some("Primary Kafka cluster for customer events".to_string())
        );

        // Verify second system (Postgres)
        let postgres_system = domain
            .systems
            .iter()
            .find(|s| s.name == "postgres-db")
            .unwrap();
        assert_eq!(
            postgres_system.infrastructure_type,
            InfrastructureType::PostgreSQL
        );
        assert_eq!(postgres_system.owner, Some("Database Team".to_string()));

        // Verify system connection
        let connection = &domain.system_connections[0];
        assert_eq!(connection.source_system_id, kafka_system.id);
        assert_eq!(connection.target_system_id, postgres_system.id);
        assert_eq!(connection.connection_type, "data_flow");
        assert!(connection.metadata.contains_key("notes"));
    }

    #[test]
    fn test_dataflow_migration_preserves_all_metadata() {
        let dataflow_yaml = r#"
nodes:
  - name: test-system
    metadata:
      owner: "Test Owner"
      infrastructure_type: "Cassandra"
      sla:
        - property: latency
          value: 100
          unit: milliseconds
          description: Maximum latency
      contact_details:
        email: test@example.com
        phone: "+1-555-0100"
        name: Test Contact
        role: Administrator
        other: Additional info
      notes: Test notes
"#;

        let domain = migrate_dataflow_to_domain(dataflow_yaml, None).unwrap();

        assert_eq!(domain.systems.len(), 1);
        let system = &domain.systems[0];

        // Verify all metadata is preserved
        assert_eq!(system.owner, Some("Test Owner".to_string()));
        assert_eq!(system.infrastructure_type, InfrastructureType::Cassandra);
        assert!(system.sla.is_some());
        assert_eq!(system.sla.as_ref().unwrap().len(), 1);
        assert_eq!(system.sla.as_ref().unwrap()[0].property, "latency");
        assert_eq!(system.sla.as_ref().unwrap()[0].value, json!(100));
        assert_eq!(system.sla.as_ref().unwrap()[0].unit, "milliseconds");
        assert_eq!(
            system.sla.as_ref().unwrap()[0].description,
            Some("Maximum latency".to_string())
        );

        assert!(system.contact_details.is_some());
        let contact = system.contact_details.as_ref().unwrap();
        assert_eq!(contact.email, Some("test@example.com".to_string()));
        assert_eq!(contact.phone, Some("+1-555-0100".to_string()));
        assert_eq!(contact.name, Some("Test Contact".to_string()));
        assert_eq!(contact.role, Some("Administrator".to_string()));
        assert_eq!(contact.other, Some("Additional info".to_string()));

        assert_eq!(system.notes, Some("Test notes".to_string()));
    }

    #[test]
    fn test_dataflow_migration_with_relationships() {
        let node1_id = "550e8400-e29b-41d4-a716-446655440000";
        let node2_id = "660e8400-e29b-41d4-a716-446655440001";
        let node3_id = "770e8400-e29b-41d4-a716-446655440002";

        let dataflow_yaml = format!(
            r#"
nodes:
  - id: {}
    name: source-system
    metadata:
      infrastructure_type: "Kafka"
  - id: {}
    name: intermediate-system
    metadata:
      infrastructure_type: "Cassandra"
  - id: {}
    name: target-system
    metadata:
      infrastructure_type: "PostgreSQL"
relationships:
  - source_node_id: {}
    target_node_id: {}
  - source_node_id: {}
    target_node_id: {}
"#,
            node1_id, node2_id, node3_id, node1_id, node2_id, node2_id, node3_id
        );

        let domain = migrate_dataflow_to_domain(&dataflow_yaml, Some("test-domain")).unwrap();

        assert_eq!(domain.systems.len(), 3);
        assert_eq!(domain.system_connections.len(), 2);

        // Verify all systems exist
        assert!(domain.systems.iter().any(|s| s.name == "source-system"));
        assert!(
            domain
                .systems
                .iter()
                .any(|s| s.name == "intermediate-system")
        );
        assert!(domain.systems.iter().any(|s| s.name == "target-system"));

        // Verify connections
        let source_system = domain
            .systems
            .iter()
            .find(|s| s.name == "source-system")
            .unwrap();
        let intermediate_system = domain
            .systems
            .iter()
            .find(|s| s.name == "intermediate-system")
            .unwrap();
        let target_system = domain
            .systems
            .iter()
            .find(|s| s.name == "target-system")
            .unwrap();

        // First connection: source -> intermediate
        assert!(domain.system_connections.iter().any(|c| {
            c.source_system_id == source_system.id && c.target_system_id == intermediate_system.id
        }));

        // Second connection: intermediate -> target
        assert!(domain.system_connections.iter().any(|c| {
            c.source_system_id == intermediate_system.id && c.target_system_id == target_system.id
        }));
    }
}

mod universal_converter_tests {
    use data_modelling_core::convert::convert_to_odcs;

    #[test]
    fn test_cads_to_odcs_conversion_error() {
        // Use a valid CADS YAML structure that will parse successfully
        let cads_yaml = r#"
apiVersion: v1.0
kind: AIModel
id: test-model
name: Test Model
version: 1.0.0
status: draft
"#;

        let result = convert_to_odcs(cads_yaml, Some("cads"));
        assert!(
            result.is_err(),
            "CADS → ODCS conversion should return an error"
        );
        let error = result.unwrap_err();
        let error_str = error.to_string();
        // CADS → ODCS conversion returns "CADS → ODCS conversion requires data schema information..."
        // Just verify it's not an AutoDetectionFailed error (format was detected)
        assert!(
            !error_str.contains("AutoDetectionFailed"),
            "CADS format should be detected. Error was: {}",
            error_str
        );
        // If it's UnsupportedFormat, it should contain CADS or data schema
        if error_str.contains("UnsupportedFormat") {
            assert!(
                error_str.contains("CADS")
                    || error_str.contains("data schema")
                    || error_str.contains("compute resources"),
                "UnsupportedFormat error should mention CADS or data schema. Error was: {}",
                error_str
            );
        }
    }

    #[test]
    fn test_odps_to_odcs_conversion_error() {
        // Use a valid ODPS YAML structure that will parse successfully
        let odps_yaml = r#"
apiVersion: v1.0.0
kind: DataProduct
id: test-product
name: Test Product
version: 1.0.0
status: draft
"#;

        let result = convert_to_odcs(odps_yaml, Some("odps"));
        assert!(
            result.is_err(),
            "ODPS → ODCS conversion should return an error"
        );
        let error = result.unwrap_err();
        let error_str = error.to_string();
        // ODPS without input/output ports returns "ODPS → ODCS conversion requires contractId references. No contractIds found in input/output ports."
        // If parsing fails, we get an ImportError instead
        // Just verify it's not an AutoDetectionFailed error (format was detected)
        assert!(
            !error_str.contains("AutoDetectionFailed"),
            "ODPS format should be detected. Error was: {}",
            error_str
        );
        // If it's an ImportError, that's also fine - it means the format was detected but parsing failed
        // If it's UnsupportedFormat, it should contain ODPS or contractId
        if error_str.contains("UnsupportedFormat") {
            assert!(
                error_str.contains("ODPS")
                    || error_str.contains("contractId")
                    || error_str.contains("contractIds"),
                "UnsupportedFormat error should mention ODPS or contractId. Error was: {}",
                error_str
            );
        }
    }

    #[test]
    fn test_domain_to_odcs_conversion_error() {
        let domain_yaml = r#"
id: 550e8400-e29b-41d4-a716-446655440000
name: test-domain
systems: []
cads_nodes: []
odcs_nodes: []
system_connections: []
node_connections: []
"#;

        let result = convert_to_odcs(domain_yaml, Some("domain"));
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(error.to_string().contains("Domain → ODCS conversion"));
    }

    #[test]
    fn test_domain_with_odcs_nodes_conversion_error() {
        use uuid::Uuid;
        let system_id = Uuid::new_v4();
        let table_id = Uuid::new_v4();
        let domain_yaml = format!(
            r#"
id: 550e8400-e29b-41d4-a716-446655440000
name: test-domain
systems:
  - id: {}
    name: test-system
    infrastructure_type: PostgreSQL
    domain_id: 550e8400-e29b-41d4-a716-446655440000
cads_nodes: []
odcs_nodes:
  - id: 660e8400-e29b-41d4-a716-446655440001
    system_id: {}
    table_id: {}
    role: source
system_connections: []
node_connections: []
"#,
            system_id, system_id, table_id
        );

        let result = convert_to_odcs(&domain_yaml, Some("domain"));
        assert!(result.is_err());
        let error = result.unwrap_err();
        assert!(
            error
                .to_string()
                .contains("Domain → ODCS conversion requires Table definitions")
        );
        assert!(error.to_string().contains("1 ODCS node references"));
    }

    #[test]
    fn test_sql_to_odcs_conversion_detection() {
        // Test that SQL format is detected
        // Note: Full conversion requires Table reconstruction which is not yet implemented
        let sql = "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(100));";
        let result = convert_to_odcs(sql, Some("sql"));
        // Format detection works, but conversion may fail due to Table reconstruction
        // Just verify it doesn't fail with "UnsupportedFormat" (which means detection failed)
        if let Err(error) = result {
            // Should not be "UnsupportedFormat" - that means detection failed
            let error_str = format!("{error}");
            assert!(!error_str.contains("UnsupportedFormat") || error_str.contains("sql"));
        }
    }

    #[test]
    fn test_odcs_to_odcs_conversion_detection() {
        // Test that ODCS format is detected
        let odcs_yaml = r#"
apiVersion: v3.1.0
kind: DataContract
id: test-contract
name: Test Contract
version: 1.0.0
schema:
  type: object
  properties:
    id:
      type: integer
"#;

        let result = convert_to_odcs(odcs_yaml, Some("odcs"));
        // Format detection works - conversion may fail but shouldn't be "UnsupportedFormat"
        if let Err(error) = result {
            let error_str = format!("{error}");
            assert!(!error_str.contains("UnsupportedFormat") || error_str.contains("odcs"));
        }
    }

    #[test]
    fn test_odcl_to_odcs_conversion_detection() {
        // Test that ODCL format is detected
        let odcl_yaml = r#"
dataContractSpecification: 1.2.1
id: test-contract
info:
  title: Test Contract
  version: 1.0.0
models:
  users:
    type: table
    fields:
      id:
        type: integer
        required: true
"#;

        let result = convert_to_odcs(odcl_yaml, Some("odcl"));
        // Format detection works - conversion may fail but shouldn't be "UnsupportedFormat"
        if let Err(error) = result {
            let error_str = format!("{error}");
            assert!(!error_str.contains("UnsupportedFormat") || error_str.contains("odcl"));
        }
    }

    #[test]
    fn test_json_schema_to_odcs_conversion_detection() {
        // Test that JSON Schema format is detected
        let json_schema = r#"
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    }
  },
  "required": ["id"]
}
"#;

        let result = convert_to_odcs(json_schema, Some("json_schema"));
        // Format detection works - conversion may fail but shouldn't be "UnsupportedFormat"
        if let Err(error) = result {
            let error_str = format!("{error}");
            assert!(!error_str.contains("UnsupportedFormat") || error_str.contains("json_schema"));
        }
    }

    #[test]
    fn test_auto_detect_format() {
        // Test SQL auto-detection
        let sql = "CREATE TABLE users (id INT);";
        let result = convert_to_odcs(sql, None);
        // Format detection works, but conversion may fail due to Table reconstruction
        // Or it may succeed if the converter can handle it
        // Just verify format was detected (not AutoDetectionFailed)
        if let Err(error) = result {
            let error_str = format!("{error}");
            assert!(
                !error_str.contains("AutoDetectionFailed"),
                "SQL format should be auto-detected. Error was: {error_str}"
            );
        }
        // If conversion succeeds, that's also fine - it means the converter can handle SQL

        // Test ODCS auto-detection
        let odcs = r#"
apiVersion: v3.1.0
kind: DataContract
id: test
schema:
  type: object
"#;
        let result = convert_to_odcs(odcs, None);
        // Format detection works
        if let Err(error) = result {
            let error_str = format!("{error}");
            assert!(!error_str.contains("AutoDetectionFailed"));
        }

        // Test ODCL auto-detection
        let odcl = r#"
dataContractSpecification: 1.2.1
id: test
models:
  test:
    type: table
"#;
        let result = convert_to_odcs(odcl, None);
        // Format detection works - conversion may succeed or fail
        // Just verify format was detected (not AutoDetectionFailed)
        if let Err(error) = result {
            let error_str = format!("{error}");
            assert!(
                !error_str.contains("AutoDetectionFailed"),
                "ODCL format should be auto-detected. Error was: {error_str}"
            );
        }
        // If conversion succeeds, that's also fine - it means the converter can handle ODCL

        // Test CADS auto-detection
        let cads = r#"
apiVersion: v1.0
kind: AIModel
id: test
status: draft
"#;
        let result = convert_to_odcs(cads, None);
        assert!(
            result.is_err(),
            "CADS → ODCS conversion should return an error"
        );
        let error = result.unwrap_err();
        let error_str = error.to_string();
        // Just verify format was detected (not AutoDetectionFailed)
        assert!(
            !error_str.contains("AutoDetectionFailed"),
            "CADS format should be auto-detected. Error was: {}",
            error_str
        );
        // If it's UnsupportedFormat, it should contain CADS or data schema
        if error_str.contains("UnsupportedFormat") {
            assert!(
                error_str.contains("CADS")
                    || error_str.contains("data schema")
                    || error_str.contains("compute resources"),
                "CADS format should be detected and return appropriate error. Error was: {}",
                error_str
            );
        }

        // Test ODPS auto-detection
        let odps = r#"
apiVersion: v1.0.0
kind: DataProduct
id: test
status: draft
"#;
        let result = convert_to_odcs(odps, None);
        assert!(
            result.is_err(),
            "ODPS → ODCS conversion should return an error"
        );
        let error = result.unwrap_err();
        let error_str = error.to_string();
        // Just verify format was detected (not AutoDetectionFailed)
        assert!(
            !error_str.contains("AutoDetectionFailed"),
            "ODPS format should be auto-detected. Error was: {}",
            error_str
        );
        // If it's UnsupportedFormat, it should contain ODPS or contractId
        if error_str.contains("UnsupportedFormat") {
            assert!(
                error_str.contains("ODPS")
                    || error_str.contains("contractId")
                    || error_str.contains("contractIds"),
                "ODPS format should be detected and return appropriate error. Error was: {}",
                error_str
            );
        }

        // Test Domain auto-detection (explicit format to avoid detection issues)
        let domain = r#"
id: 550e8400-e29b-41d4-a716-446655440000
name: test-domain
systems: []
odcs_nodes: []
"#;
        // Use explicit format to test Domain conversion logic
        let result = convert_to_odcs(domain, Some("domain"));
        assert!(
            result.is_err(),
            "Domain → ODCS conversion should return an error"
        );
        let error = result.unwrap_err();
        let error_str = error.to_string();
        // Domain without ODCS nodes returns "Domain contains no ODCS nodes"
        // Domain with ODCS nodes returns "requires Table definitions"
        assert!(
            error_str.contains("Domain")
                || error_str.contains("ODCS node")
                || error_str.contains("no ODCS nodes")
                || error_str.contains("Table definitions"),
            "Domain conversion error should mention Domain or ODCS node. Error was: {}",
            error_str
        );
    }
}

/// Roundtrip tests for example files to ensure no data loss during import/export
mod example_file_roundtrip_tests {
    use super::*;
    use data_modelling_core::import::odcl::ODCLImporter;

    /// Helper to compare YAML values, ignoring ordering differences in maps
    #[allow(dead_code)]
    fn yaml_values_equivalent(a: &serde_yaml::Value, b: &serde_yaml::Value) -> bool {
        match (a, b) {
            (serde_yaml::Value::Mapping(m1), serde_yaml::Value::Mapping(m2)) => {
                if m1.len() != m2.len() {
                    return false;
                }
                for (k, v1) in m1 {
                    match m2.get(k) {
                        Some(v2) => {
                            if !yaml_values_equivalent(v1, v2) {
                                return false;
                            }
                        }
                        None => return false,
                    }
                }
                true
            }
            (serde_yaml::Value::Sequence(s1), serde_yaml::Value::Sequence(s2)) => {
                if s1.len() != s2.len() {
                    return false;
                }
                s1.iter()
                    .zip(s2.iter())
                    .all(|(v1, v2)| yaml_values_equivalent(v1, v2))
            }
            _ => a == b,
        }
    }

    /// Helper to check if a key exists in YAML at any level
    fn yaml_contains_key(yaml: &serde_yaml::Value, key: &str) -> bool {
        match yaml {
            serde_yaml::Value::Mapping(m) => {
                for (k, v) in m {
                    if let serde_yaml::Value::String(s) = k
                        && s == key
                    {
                        return true;
                    }
                    if yaml_contains_key(v, key) {
                        return true;
                    }
                }
                false
            }
            serde_yaml::Value::Sequence(s) => s.iter().any(|v| yaml_contains_key(v, key)),
            _ => false,
        }
    }

    /// Helper to get all tables/schema names from ODCS YAML
    fn get_schema_names(yaml: &serde_yaml::Value) -> Vec<String> {
        let mut names = Vec::new();
        if let Some(serde_yaml::Value::Sequence(tables)) = yaml.get("schema") {
            for table in tables {
                if let Some(serde_yaml::Value::String(s)) = table.get("name") {
                    names.push(s.clone());
                }
            }
        }
        names
    }

    /// Helper to get property names from a schema table
    fn get_property_names(yaml: &serde_yaml::Value, table_name: &str) -> Vec<String> {
        let mut names = Vec::new();
        if let Some(serde_yaml::Value::Sequence(tables)) = yaml.get("schema") {
            for table in tables {
                if let Some(serde_yaml::Value::String(s)) = table.get("name")
                    && s == table_name
                    && let Some(serde_yaml::Value::Sequence(properties)) = table.get("properties")
                {
                    for prop in properties {
                        if let Some(serde_yaml::Value::String(ps)) = prop.get("name") {
                            names.push(ps.clone());
                        }
                    }
                }
            }
        }
        names
    }

    #[test]
    fn test_full_example_odcs_roundtrip() {
        // Load the full-example.odcs.yaml file
        let original_yaml = std::fs::read_to_string("../../examples/full-example.odcs.yaml")
            .expect("Failed to read full-example.odcs.yaml");

        // Parse original YAML for comparison
        let original_parsed: serde_yaml::Value =
            serde_yaml::from_str(&original_yaml).expect("Failed to parse original YAML");

        // Import
        let mut importer = ODCSImporter::new();
        let import_result = importer
            .import(&original_yaml)
            .expect("Failed to import full-example.odcs.yaml");

        // Verify we got tables
        assert!(
            !import_result.tables.is_empty(),
            "Should import at least one table"
        );

        // Create tables from import result
        let tables = create_table_from_import_result(&import_result);

        // Export back to ODCS
        let exported_yaml = ODCSExporter::export_table(&tables[0], "odcs_v3_1_0");

        // Parse exported YAML
        let exported_parsed: serde_yaml::Value =
            serde_yaml::from_str(&exported_yaml).expect("Failed to parse exported YAML");

        // Verify key fields are preserved
        // Check apiVersion
        assert!(
            exported_parsed.get("apiVersion").is_some(),
            "Exported YAML should have apiVersion"
        );

        // Check schema exists
        assert!(
            exported_parsed.get("schema").is_some(),
            "Exported YAML should have schema"
        );

        // Verify table names are preserved
        let original_schema_names = get_schema_names(&original_parsed);
        let exported_schema_names = get_schema_names(&exported_parsed);

        // The first table should match
        assert!(
            !exported_schema_names.is_empty(),
            "Exported schema should have tables"
        );

        // Verify properties are preserved for first table
        if !original_schema_names.is_empty() {
            let original_props = get_property_names(&original_parsed, &original_schema_names[0]);
            let exported_props = get_property_names(&exported_parsed, &exported_schema_names[0]);

            // Check that we have properties
            assert!(
                !exported_props.is_empty(),
                "Exported table should have properties"
            );

            // Check key properties are present
            for prop in &original_props {
                assert!(
                    exported_props.contains(prop),
                    "Property '{}' should be preserved in export",
                    prop
                );
            }
        }

        // Verify relationships are preserved (schema-level and property-level)
        assert!(
            yaml_contains_key(&exported_parsed, "relationships")
                || yaml_contains_key(&original_parsed, "relationships"),
            "Relationships should be preserved if present in original"
        );

        // Re-import the exported YAML to verify it's valid
        let mut reimporter = ODCSImporter::new();
        let reimport_result = reimporter
            .import(&exported_yaml)
            .expect("Failed to re-import exported YAML");

        assert!(
            !reimport_result.tables.is_empty(),
            "Re-imported YAML should produce tables"
        );

        // Verify column count matches
        assert_eq!(
            import_result.tables[0].columns.len(),
            reimport_result.tables[0].columns.len(),
            "Column count should match after roundtrip"
        );
    }

    #[test]
    fn test_all_data_types_odcs_roundtrip() {
        // Load the all-data-types.odcs.yaml file
        let original_yaml = std::fs::read_to_string("../../examples/all-data-types.odcs.yaml")
            .expect("Failed to read all-data-types.odcs.yaml");

        // Import
        let mut importer = ODCSImporter::new();
        let import_result = importer
            .import(&original_yaml)
            .expect("Failed to import all-data-types.odcs.yaml");

        // Verify we got tables
        assert!(
            !import_result.tables.is_empty(),
            "Should import at least one table"
        );

        // Check expected data type columns exist in import
        let expected_columns = vec![
            "account_id",
            "txn_ref_date",
            "txn_timestamp",
            "txn_timestamp_tz",
            "txn_time",
            "amount",
            "age",
            "is_open",
            "latest_txns",
            "customer_details",
        ];

        let imported_col_names: Vec<&str> = import_result.tables[0]
            .columns
            .iter()
            .map(|c| c.name.as_str())
            .collect();

        for col in &expected_columns {
            assert!(
                imported_col_names.contains(col),
                "Imported table should have column '{}'. Available: {:?}",
                col,
                imported_col_names
            );
        }

        // Create tables from import result
        let tables = create_table_from_import_result(&import_result);

        // Export back to ODCS
        let exported_yaml = ODCSExporter::export_table(&tables[0], "odcs_v3_1_0");

        // Re-import to verify validity and data preservation
        let mut reimporter = ODCSImporter::new();
        let reimport_result = reimporter
            .import(&exported_yaml)
            .expect("Failed to re-import exported YAML");

        assert!(
            !reimport_result.tables.is_empty(),
            "Re-imported YAML should produce tables"
        );

        // Verify column count matches
        assert_eq!(
            import_result.tables[0].columns.len(),
            reimport_result.tables[0].columns.len(),
            "Column count should match after roundtrip. Original: {}, Reimported: {}",
            import_result.tables[0].columns.len(),
            reimport_result.tables[0].columns.len()
        );

        // Verify all columns are preserved after roundtrip
        let reimported_col_names: Vec<&str> = reimport_result.tables[0]
            .columns
            .iter()
            .map(|c| c.name.as_str())
            .collect();

        for col in &expected_columns {
            assert!(
                reimported_col_names.contains(col),
                "Column '{}' should exist after roundtrip. Available: {:?}",
                col,
                reimported_col_names
            );
        }

        // Verify data types are preserved
        for col in &import_result.tables[0].columns {
            let reimported_col = reimport_result.tables[0]
                .columns
                .iter()
                .find(|c| c.name == col.name);
            assert!(
                reimported_col.is_some(),
                "Column '{}' should exist after roundtrip",
                col.name
            );
            let reimported = reimported_col.unwrap();
            assert_eq!(
                col.data_type, reimported.data_type,
                "Data type for '{}' should be preserved: expected '{}', got '{}'",
                col.name, col.data_type, reimported.data_type
            );
        }
    }

    #[test]
    fn test_time_example_odcl_to_odcs_roundtrip() {
        // Load the time-example.odcl.yaml file (ODCL format)
        let original_yaml = std::fs::read_to_string("../../examples/time-example.odcl.yaml")
            .expect("Failed to read time-example.odcl.yaml");

        // Import using ODCL importer
        let mut importer = ODCLImporter::new();
        let import_result = importer
            .import(&original_yaml)
            .expect("Failed to import time-example.odcl.yaml");

        // Verify we got tables
        assert!(
            !import_result.tables.is_empty(),
            "Should import at least one table from ODCL"
        );

        // Note: ODCL importer currently only imports the first model
        // The first model should be business_hours
        let first_table = &import_result.tables[0];
        assert!(
            first_table.name.as_deref() == Some("business_hours"),
            "First model should be 'business_hours', got {:?}",
            first_table.name
        );

        // Check time columns exist in the first table
        let time_columns = vec!["opening_time", "closing_time", "lunch_start", "lunch_end"];
        let col_names: Vec<&str> = first_table
            .columns
            .iter()
            .map(|c| c.name.as_str())
            .collect();

        for col_name in &time_columns {
            assert!(
                col_names.contains(col_name),
                "Column '{}' should be imported from ODCL. Available: {:?}",
                col_name,
                col_names
            );
        }

        // Verify time data types
        for col_name in &time_columns {
            let col = first_table
                .columns
                .iter()
                .find(|c| c.name == *col_name)
                .unwrap();
            assert!(
                col.data_type.to_uppercase().contains("TIME")
                    || col.data_type.to_uppercase().contains("STRING"),
                "Time column '{}' should have time-related type, got '{}'",
                col_name,
                col.data_type
            );
        }

        // Create tables from import result
        let tables = create_table_from_import_result(&import_result);

        // Export to ODCS format
        let exported_yaml = ODCSExporter::export_table(&tables[0], "odcs_v3_1_0");

        // Parse exported YAML
        let exported_parsed: serde_yaml::Value =
            serde_yaml::from_str(&exported_yaml).expect("Failed to parse exported YAML");

        // Verify it's valid ODCS
        assert!(
            exported_parsed.get("apiVersion").is_some()
                || exported_parsed.get("kind").is_some()
                || exported_parsed.get("schema").is_some(),
            "Exported YAML should be valid ODCS format"
        );

        // Re-import as ODCS to verify validity
        let mut odcs_reimporter = ODCSImporter::new();
        let reimport_result = odcs_reimporter
            .import(&exported_yaml)
            .expect("Failed to re-import exported ODCS YAML");

        assert!(
            !reimport_result.tables.is_empty(),
            "Re-imported ODCS should produce tables"
        );

        // Verify column count is preserved after roundtrip
        assert_eq!(
            first_table.columns.len(),
            reimport_result.tables[0].columns.len(),
            "Column count should be preserved after ODCL -> ODCS roundtrip"
        );

        // Verify all time columns are preserved in reimport
        let reimported_col_names: Vec<&str> = reimport_result.tables[0]
            .columns
            .iter()
            .map(|c| c.name.as_str())
            .collect();

        for col_name in &time_columns {
            assert!(
                reimported_col_names.contains(col_name),
                "Column '{}' should be preserved after roundtrip. Available: {:?}",
                col_name,
                reimported_col_names
            );
        }
    }

    #[test]
    fn test_orders_latest_odcl_to_odcs_roundtrip() {
        // Load the orders-latest.odcl.yaml file (ODCL format)
        let original_yaml = std::fs::read_to_string("../../examples/orders-latest.odcl.yaml")
            .expect("Failed to read orders-latest.odcl.yaml");

        // Import using ODCL importer
        let mut importer = ODCLImporter::new();
        let import_result = importer
            .import(&original_yaml)
            .expect("Failed to import orders-latest.odcl.yaml");

        // Verify we got tables
        assert!(
            !import_result.tables.is_empty(),
            "Should import at least one table from ODCL"
        );

        // Note: ODCL importer currently only imports the first model
        // Due to hash ordering, the first model could be either 'orders' or 'line_items'
        let first_table = &import_result.tables[0];
        let table_name = first_table.name.as_deref().unwrap_or("");

        assert!(
            table_name == "orders" || table_name == "line_items",
            "First model should be 'orders' or 'line_items', got {:?}",
            first_table.name
        );

        // Define expected columns based on which table was imported
        let expected_columns: Vec<&str> = if table_name == "orders" {
            vec![
                "order_id",
                "order_timestamp",
                "order_total",
                "customer_id",
                "customer_email_address",
                "processed_timestamp",
            ]
        } else {
            // line_items columns
            vec!["line_item_id", "order_id", "sku"]
        };

        let col_names: Vec<&str> = first_table
            .columns
            .iter()
            .map(|c| c.name.as_str())
            .collect();

        for col_name in &expected_columns {
            assert!(
                col_names.contains(col_name),
                "{} table should have column '{}'. Available: {:?}",
                table_name,
                col_name,
                col_names
            );
        }

        // Verify order_id has $ref converted to relationships (present in both tables)
        let order_id_col = first_table.columns.iter().find(|c| c.name == "order_id");
        if let Some(col) = order_id_col {
            // Check that relationships are populated (from $ref conversion)
            assert!(
                !col.relationships.is_empty(),
                "order_id column should have relationships from $ref"
            );
        }

        // Create tables from import result
        let tables = create_table_from_import_result(&import_result);

        // Export to ODCS format
        let exported_yaml = ODCSExporter::export_table(&tables[0], "odcs_v3_1_0");

        // Parse exported YAML
        let exported_parsed: serde_yaml::Value =
            serde_yaml::from_str(&exported_yaml).expect("Failed to parse exported YAML");

        // Verify it's valid ODCS
        assert!(
            exported_parsed.get("apiVersion").is_some()
                || exported_parsed.get("kind").is_some()
                || exported_parsed.get("schema").is_some(),
            "Exported YAML should be valid ODCS format"
        );

        // Re-import as ODCS to verify validity
        let mut odcs_reimporter = ODCSImporter::new();
        let reimport_result = odcs_reimporter
            .import(&exported_yaml)
            .expect("Failed to re-import exported ODCS YAML");

        assert!(
            !reimport_result.tables.is_empty(),
            "Re-imported ODCS should produce tables"
        );

        // Verify column count is preserved
        assert_eq!(
            tables[0].columns.len(),
            reimport_result.tables[0].columns.len(),
            "Column count should be preserved after ODCL -> ODCS -> ODCS roundtrip"
        );

        // Verify all expected columns are preserved in reimport
        let reimported_col_names: Vec<&str> = reimport_result.tables[0]
            .columns
            .iter()
            .map(|c| c.name.as_str())
            .collect();

        for col_name in &expected_columns {
            assert!(
                reimported_col_names.contains(col_name),
                "Column '{}' should be preserved after roundtrip. Available: {:?}",
                col_name,
                reimported_col_names
            );
        }
    }
}