Create a temporary SQLite database for testing.
()
| 41 | |
| 42 | @pytest.fixture |
| 43 | def sqlite_engine() -> sa.Engine: |
| 44 | """Create a temporary SQLite database for testing.""" |
| 45 | |
| 46 | import sqlalchemy as sa |
| 47 | from sqlalchemy import text |
| 48 | |
| 49 | engine = sa.create_engine("sqlite:///:memory:") |
| 50 | |
| 51 | # Test if standard syntax works |
| 52 | with engine.begin() as conn: |
| 53 | conn.execute( |
| 54 | text( |
| 55 | """ |
| 56 | CREATE TABLE test ( |
| 57 | id INTEGER PRIMARY KEY, |
| 58 | name TEXT |
| 59 | ) |
| 60 | """ |
| 61 | ) |
| 62 | ) |
| 63 | conn.execute( |
| 64 | text( |
| 65 | """ |
| 66 | INSERT INTO test (id, name) VALUES |
| 67 | (1, 'Alice'), |
| 68 | (2, 'Bob'), |
| 69 | (3, 'Charlie') |
| 70 | """ |
| 71 | ) |
| 72 | ) |
| 73 | # Add another table in another schema |
| 74 | conn.execute(text("ATTACH ':memory:' AS my_schema")) |
| 75 | conn.execute( |
| 76 | text( |
| 77 | """ |
| 78 | CREATE TABLE my_schema.test2 ( |
| 79 | id INTEGER PRIMARY KEY, |
| 80 | name TEXT |
| 81 | ) |
| 82 | """ |
| 83 | ) |
| 84 | ) |
| 85 | |
| 86 | # Test if mo.sql works |
| 87 | sql("INSERT INTO test (id, name) VALUES (4, 'Rose')", engine=engine) |
| 88 | return engine |
| 89 | |
| 90 | |
| 91 | @pytest.fixture |