|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +from ..utils import ensure_specified_sql_driver |
| 4 | + |
| 5 | + |
| 6 | +def test_ensure_specified_sql_driver(): |
| 7 | + # Postgres |
| 8 | + # Default driver is added if missing. |
| 9 | + assert ( |
| 10 | + ensure_specified_sql_driver( |
| 11 | + "postgresql://user:password@localhost:5432/database" |
| 12 | + ) |
| 13 | + == "postgresql+asyncpg://user:password@localhost:5432/database" |
| 14 | + ) |
| 15 | + # Default driver passes through if specified. |
| 16 | + assert ( |
| 17 | + ensure_specified_sql_driver( |
| 18 | + "postgresql+asyncpg://user:password@localhost:5432/database" |
| 19 | + ) |
| 20 | + == "postgresql+asyncpg://user:password@localhost:5432/database" |
| 21 | + ) |
| 22 | + # Do not override user-provided. |
| 23 | + assert ( |
| 24 | + ensure_specified_sql_driver( |
| 25 | + "postgresql+custom://user:password@localhost:5432/database" |
| 26 | + ) |
| 27 | + == "postgresql+custom://user:password@localhost:5432/database" |
| 28 | + ) |
| 29 | + |
| 30 | + # SQLite |
| 31 | + # Default driver is added if missing. |
| 32 | + assert ( |
| 33 | + ensure_specified_sql_driver("sqlite:////test.db") |
| 34 | + == "sqlite+aiosqlite:////test.db" |
| 35 | + ) |
| 36 | + # Default driver passes through if specified. |
| 37 | + assert ( |
| 38 | + ensure_specified_sql_driver("sqlite+aiosqlite:////test.db") |
| 39 | + == "sqlite+aiosqlite:////test.db" |
| 40 | + ) |
| 41 | + # Do not override user-provided. |
| 42 | + assert ( |
| 43 | + ensure_specified_sql_driver("sqlite+custom:////test.db") |
| 44 | + == "sqlite+custom:////test.db" |
| 45 | + ) |
| 46 | + # Handle SQLite :memory: URIs |
| 47 | + assert ( |
| 48 | + ensure_specified_sql_driver("sqlite+aiosqlite://:memory:") |
| 49 | + == "sqlite+aiosqlite://:memory:" |
| 50 | + ) |
| 51 | + assert ( |
| 52 | + ensure_specified_sql_driver("sqlite://:memory:") |
| 53 | + == "sqlite+aiosqlite://:memory:" |
| 54 | + ) |
| 55 | + # Handle SQLite relative URIs |
| 56 | + assert ( |
| 57 | + ensure_specified_sql_driver("sqlite+aiosqlite:///test.db") |
| 58 | + == "sqlite+aiosqlite:///test.db" |
| 59 | + ) |
| 60 | + assert ( |
| 61 | + ensure_specified_sql_driver("sqlite:///test.db") |
| 62 | + == "sqlite+aiosqlite:///test.db" |
| 63 | + ) |
| 64 | + # Filepaths are implicitly SQLite databases. |
| 65 | + # Relative path |
| 66 | + assert ensure_specified_sql_driver("test.db") == "sqlite+aiosqlite:///test.db" |
| 67 | + # Path object |
| 68 | + assert ensure_specified_sql_driver(Path("test.db")) == "sqlite+aiosqlite:///test.db" |
| 69 | + # Relative path anchored to . |
| 70 | + assert ensure_specified_sql_driver("./test.db") == "sqlite+aiosqlite:///test.db" |
| 71 | + # Absolute path |
| 72 | + assert ( |
| 73 | + ensure_specified_sql_driver(Path("/tmp/test.db")) |
| 74 | + == f"sqlite+aiosqlite:///{Path('/tmp/test.db')}" |
| 75 | + ) |
0 commit comments