|
| 1 | +import pytest |
| 2 | + |
| 3 | +from mysql_to_sqlite3.transporter import MySQLtoSQLite |
| 4 | + |
| 5 | + |
| 6 | +class TestSqlglotAugmentedTypeTranslation: |
| 7 | + @pytest.mark.parametrize("mysql_type", ["double precision", "DOUBLE PRECISION", "DoUbLe PrEcIsIoN"]) |
| 8 | + def test_double_precision_maps_to_numeric_type(self, mysql_type: str) -> None: |
| 9 | + # Prior mapper would resolve this to TEXT; sqlglot fallback should improve it |
| 10 | + out = MySQLtoSQLite._translate_type_from_mysql_to_sqlite(mysql_type) |
| 11 | + assert out in {"DOUBLE", "REAL"} |
| 12 | + |
| 13 | + def test_fixed_maps_to_decimal(self) -> None: |
| 14 | + out = MySQLtoSQLite._translate_type_from_mysql_to_sqlite("fixed(10,2)") |
| 15 | + # Normalize to DECIMAL (without length) to match existing style |
| 16 | + assert out == "DECIMAL" |
| 17 | + |
| 18 | + def test_character_varying_keeps_length_as_varchar(self) -> None: |
| 19 | + out = MySQLtoSQLite._translate_type_from_mysql_to_sqlite("character varying(20)") |
| 20 | + assert out == "VARCHAR(20)" |
| 21 | + |
| 22 | + def test_char_varying_keeps_length_as_varchar(self) -> None: |
| 23 | + out = MySQLtoSQLite._translate_type_from_mysql_to_sqlite("char varying(12)") |
| 24 | + assert out == "VARCHAR(12)" |
| 25 | + |
| 26 | + def test_national_character_varying_maps_to_nvarchar(self) -> None: |
| 27 | + out = MySQLtoSQLite._translate_type_from_mysql_to_sqlite("national character varying(15)") |
| 28 | + assert out == "NVARCHAR(15)" |
| 29 | + |
| 30 | + def test_national_character_maps_to_nchar(self) -> None: |
| 31 | + out = MySQLtoSQLite._translate_type_from_mysql_to_sqlite("national character(5)") |
| 32 | + assert out == "NCHAR(5)" |
| 33 | + |
| 34 | + @pytest.mark.parametrize( |
| 35 | + "mysql_type,expected", |
| 36 | + [ |
| 37 | + ("int unsigned", "INTEGER"), |
| 38 | + ("mediumint unsigned", "MEDIUMINT"), |
| 39 | + ("smallint unsigned", "SMALLINT"), |
| 40 | + ("tinyint unsigned", "TINYINT"), |
| 41 | + ("bigint unsigned", "BIGINT"), |
| 42 | + ], |
| 43 | + ) |
| 44 | + def test_unsigned_variants_strip_unsigned(self, mysql_type: str, expected: str) -> None: |
| 45 | + out = MySQLtoSQLite._translate_type_from_mysql_to_sqlite(mysql_type) |
| 46 | + assert out == expected |
| 47 | + |
| 48 | + def test_timestamp_maps_to_datetime(self) -> None: |
| 49 | + out = MySQLtoSQLite._translate_type_from_mysql_to_sqlite("timestamp") |
| 50 | + assert out == "DATETIME" |
| 51 | + |
| 52 | + def test_varbinary_and_blobs_map_to_blob(self) -> None: |
| 53 | + assert MySQLtoSQLite._translate_type_from_mysql_to_sqlite("varbinary(16)") == "BLOB" |
| 54 | + assert MySQLtoSQLite._translate_type_from_mysql_to_sqlite("mediumblob") == "BLOB" |
| 55 | + |
| 56 | + def test_char_maps_to_character_with_length(self) -> None: |
| 57 | + out = MySQLtoSQLite._translate_type_from_mysql_to_sqlite("char(3)") |
| 58 | + assert out == "CHARACTER(3)" |
| 59 | + |
| 60 | + def test_json_mapping_respects_json1(self) -> None: |
| 61 | + assert ( |
| 62 | + MySQLtoSQLite._translate_type_from_mysql_to_sqlite("json", sqlite_json1_extension_enabled=False) == "TEXT" |
| 63 | + ) |
| 64 | + assert MySQLtoSQLite._translate_type_from_mysql_to_sqlite("json", sqlite_json1_extension_enabled=True) == "JSON" |
| 65 | + |
| 66 | + def test_fallback_to_text_on_unknown_type(self) -> None: |
| 67 | + out = MySQLtoSQLite._translate_type_from_mysql_to_sqlite("geography") |
| 68 | + assert out == "TEXT" |
| 69 | + |
| 70 | + def test_enum_remains_text(self) -> None: |
| 71 | + out = MySQLtoSQLite._translate_type_from_mysql_to_sqlite("enum('a','b')") |
| 72 | + assert out == "TEXT" |
0 commit comments