|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +from sqlalchemy import text |
| 6 | +from sqlalchemy.ext.asyncio import create_async_engine |
| 7 | + |
| 8 | +from crawlee.configuration import Configuration |
| 9 | +from crawlee.storage_clients import SqlStorageClient |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from pathlib import Path |
| 13 | + |
| 14 | + |
| 15 | +async def test_sqlite_wal_mode_with_default_connection(tmp_path: Path) -> None: |
| 16 | + """Test that WAL mode is applied for the default SQLite connection.""" |
| 17 | + configuration = Configuration(storage_dir=str(tmp_path)) |
| 18 | + |
| 19 | + async with SqlStorageClient() as storage_client: |
| 20 | + await storage_client.initialize(configuration) |
| 21 | + |
| 22 | + async with storage_client.engine.begin() as conn: |
| 23 | + result = await conn.execute(text('PRAGMA journal_mode')) |
| 24 | + assert result.scalar() == 'wal' |
| 25 | + |
| 26 | + |
| 27 | +async def test_sqlite_wal_mode_with_connection_string(tmp_path: Path) -> None: |
| 28 | + """Test that WAL mode is applied when using a custom SQLite connection string.""" |
| 29 | + db_path = tmp_path / 'test.db' |
| 30 | + configuration = Configuration(storage_dir=str(tmp_path)) |
| 31 | + |
| 32 | + async with SqlStorageClient(connection_string=f'sqlite+aiosqlite:///{db_path}') as storage_client: |
| 33 | + await storage_client.initialize(configuration) |
| 34 | + |
| 35 | + async with storage_client.engine.begin() as conn: |
| 36 | + result = await conn.execute(text('PRAGMA journal_mode')) |
| 37 | + assert result.scalar() == 'wal' |
| 38 | + |
| 39 | + |
| 40 | +async def test_sqlite_wal_mode_not_applied_with_custom_engine(tmp_path: Path) -> None: |
| 41 | + """Test that WAL mode is not applied when using a user-provided engine.""" |
| 42 | + db_path = tmp_path / 'test.db' |
| 43 | + configuration = Configuration(storage_dir=str(tmp_path)) |
| 44 | + engine = create_async_engine(f'sqlite+aiosqlite:///{db_path}', future=True) |
| 45 | + |
| 46 | + async with SqlStorageClient(engine=engine) as storage_client: |
| 47 | + await storage_client.initialize(configuration) |
| 48 | + |
| 49 | + async with engine.begin() as conn: |
| 50 | + result = await conn.execute(text('PRAGMA journal_mode')) |
| 51 | + assert result.scalar() != 'wal' |
0 commit comments