|
| 1 | +import pytest |
| 2 | +from pathlib import Path |
| 3 | +from linkml_runtime.linkml_model import SchemaDefinition, ClassDefinition, SlotDefinition, EnumDefinition |
| 4 | +from schema_automator.importers.dbml_import_engine import DbmlImportEngine # Replace with actual module name |
| 5 | + |
| 6 | +# Sample DBML content for testing |
| 7 | +DBML_SAMPLE = """ |
| 8 | +Table Users { |
| 9 | + id int [primary key, not null] |
| 10 | + email varchar [unique, not null] |
| 11 | + username varchar |
| 12 | +} |
| 13 | +
|
| 14 | +Table Orders { |
| 15 | + order_id int [not null] |
| 16 | + user_id int [not null] |
| 17 | + product_id int [not null] |
| 18 | + quantity int |
| 19 | + index [unique, order_id, user_id] |
| 20 | +} |
| 21 | +
|
| 22 | +Table Countries { |
| 23 | + code varchar [primary key, not null] |
| 24 | + name varchar [not null] |
| 25 | +} |
| 26 | +""" |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def dbml_file(tmp_path): |
| 30 | + """ |
| 31 | + Fixture to create a temporary DBML file. |
| 32 | + """ |
| 33 | + dbml_path = tmp_path / "test.dbml" |
| 34 | + dbml_path.write_text(DBML_SAMPLE) |
| 35 | + return dbml_path |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def importer(): |
| 39 | + """ |
| 40 | + Fixture to initialize the DbmlImportEngine. |
| 41 | + """ |
| 42 | + return DbmlImportEngine() |
| 43 | + |
| 44 | +def test_dbml_to_linkml_conversion(dbml_file, importer): |
| 45 | + """ |
| 46 | + Test the basic conversion of DBML to a LinkML schema. |
| 47 | + """ |
| 48 | + schema = importer.convert(file=str(dbml_file), name="TestSchema") |
| 49 | + |
| 50 | + # Assert the schema object is created |
| 51 | + assert isinstance(schema, SchemaDefinition) |
| 52 | + |
| 53 | + # Check that expected classes are present |
| 54 | + assert "Users" in schema.classes |
| 55 | + assert "Orders" in schema.classes |
| 56 | + |
| 57 | + # Check that expected slots are present |
| 58 | + assert "id" in schema.slots |
| 59 | + assert schema.slots["id"].identifier |
| 60 | + assert schema.slots["id"].required |
| 61 | + |
| 62 | + # Check unique keys |
| 63 | + orders_class = schema.classes["Orders"] |
| 64 | + assert orders_class.unique_keys == [["order_id", "user_id"]] |
| 65 | + |
| 66 | +def test_controlled_vocabulary_detection(dbml_file, importer): |
| 67 | + """ |
| 68 | + Test that controlled vocabulary tables are converted to enumerations. |
| 69 | + """ |
| 70 | + schema = importer.convert(file=str(dbml_file), name="TestSchema") |
| 71 | + |
| 72 | + # Assert the enum is created for Countries |
| 73 | + assert "Countries" in schema.enums |
| 74 | + |
| 75 | + # Check the enum details |
| 76 | + countries_enum = schema.enums["Countries"] |
| 77 | + assert isinstance(countries_enum, EnumDefinition) |
| 78 | + assert "code" in countries_enum.permissible_values |
| 79 | + |
| 80 | +def test_primary_key_handling(dbml_file, importer): |
| 81 | + """ |
| 82 | + Test correct handling of primary keys and required attributes. |
| 83 | + """ |
| 84 | + schema = importer.convert(file=str(dbml_file), name="TestSchema") |
| 85 | + |
| 86 | + # Check that primary keys are marked as required and identifiers |
| 87 | + users_class = schema.classes["Users"] |
| 88 | + assert "id" in users_class.slots |
| 89 | + assert schema.slots["id"].identifier |
| 90 | + assert schema.slots["id"].required |
| 91 | + |
| 92 | +def test_multi_column_unique_key_handling(dbml_file, importer): |
| 93 | + """ |
| 94 | + Test correct handling of multi-column unique keys. |
| 95 | + """ |
| 96 | + schema = importer.convert(file=str(dbml_file), name="TestSchema") |
| 97 | + |
| 98 | + # Check multi-column unique keys in Orders |
| 99 | + orders_class = schema.classes["Orders"] |
| 100 | + assert orders_class.unique_keys == [["order_id", "user_id"]] |
0 commit comments