|
| 1 | +"""Tests for WAL mode and Windows-specific SQLite optimizations.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import pytest |
| 5 | +from pathlib import Path |
| 6 | +from unittest.mock import patch, MagicMock |
| 7 | +from sqlalchemy import text |
| 8 | + |
| 9 | +from basic_memory.db import DatabaseType, engine_session_factory |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.asyncio |
| 13 | +async def test_wal_mode_enabled(tmp_path): |
| 14 | + """Test that WAL mode is enabled on database connections.""" |
| 15 | + db_path = tmp_path / "test.db" |
| 16 | + |
| 17 | + # Use FILESYSTEM type since in-memory databases don't support WAL mode |
| 18 | + async with engine_session_factory(db_path, DatabaseType.FILESYSTEM) as (engine, _): |
| 19 | + # Execute a query to verify WAL mode is enabled |
| 20 | + async with engine.connect() as conn: |
| 21 | + result = await conn.execute(text("PRAGMA journal_mode")) |
| 22 | + journal_mode = result.fetchone()[0] |
| 23 | + |
| 24 | + # WAL mode should be enabled |
| 25 | + assert journal_mode.upper() == "WAL" |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.asyncio |
| 29 | +async def test_busy_timeout_configured(tmp_path): |
| 30 | + """Test that busy timeout is configured for database connections.""" |
| 31 | + db_path = tmp_path / "test.db" |
| 32 | + |
| 33 | + async with engine_session_factory(db_path, DatabaseType.MEMORY) as (engine, _): |
| 34 | + async with engine.connect() as conn: |
| 35 | + result = await conn.execute(text("PRAGMA busy_timeout")) |
| 36 | + busy_timeout = result.fetchone()[0] |
| 37 | + |
| 38 | + # Busy timeout should be 10 seconds (10000 milliseconds) |
| 39 | + assert busy_timeout == 10000 |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.asyncio |
| 43 | +async def test_synchronous_mode_configured(tmp_path): |
| 44 | + """Test that synchronous mode is set to NORMAL for performance.""" |
| 45 | + db_path = tmp_path / "test.db" |
| 46 | + |
| 47 | + async with engine_session_factory(db_path, DatabaseType.MEMORY) as (engine, _): |
| 48 | + async with engine.connect() as conn: |
| 49 | + result = await conn.execute(text("PRAGMA synchronous")) |
| 50 | + synchronous = result.fetchone()[0] |
| 51 | + |
| 52 | + # Synchronous should be NORMAL (1) |
| 53 | + assert synchronous == 1 |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_cache_size_configured(tmp_path): |
| 58 | + """Test that cache size is configured for performance.""" |
| 59 | + db_path = tmp_path / "test.db" |
| 60 | + |
| 61 | + async with engine_session_factory(db_path, DatabaseType.MEMORY) as (engine, _): |
| 62 | + async with engine.connect() as conn: |
| 63 | + result = await conn.execute(text("PRAGMA cache_size")) |
| 64 | + cache_size = result.fetchone()[0] |
| 65 | + |
| 66 | + # Cache size should be -64000 (64MB) |
| 67 | + assert cache_size == -64000 |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_temp_store_configured(tmp_path): |
| 72 | + """Test that temp_store is set to MEMORY.""" |
| 73 | + db_path = tmp_path / "test.db" |
| 74 | + |
| 75 | + async with engine_session_factory(db_path, DatabaseType.MEMORY) as (engine, _): |
| 76 | + async with engine.connect() as conn: |
| 77 | + result = await conn.execute(text("PRAGMA temp_store")) |
| 78 | + temp_store = result.fetchone()[0] |
| 79 | + |
| 80 | + # temp_store should be MEMORY (2) |
| 81 | + assert temp_store == 2 |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.asyncio |
| 85 | +async def test_windows_locking_mode_when_on_windows(tmp_path): |
| 86 | + """Test that Windows-specific locking mode is set when running on Windows.""" |
| 87 | + db_path = tmp_path / "test.db" |
| 88 | + |
| 89 | + with patch("os.name", "nt"): |
| 90 | + async with engine_session_factory(db_path, DatabaseType.MEMORY) as (engine, _): |
| 91 | + async with engine.connect() as conn: |
| 92 | + result = await conn.execute(text("PRAGMA locking_mode")) |
| 93 | + locking_mode = result.fetchone()[0] |
| 94 | + |
| 95 | + # Locking mode should be NORMAL on Windows |
| 96 | + assert locking_mode.upper() == "NORMAL" |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.asyncio |
| 100 | +async def test_windows_timeout_configured(tmp_path): |
| 101 | + """Test that Windows-specific timeout is configured.""" |
| 102 | + db_path = tmp_path / "test.db" |
| 103 | + |
| 104 | + # Note: We can't easily test the timeout parameter in connect_args |
| 105 | + # as it's passed to the underlying SQLite connection, but we can verify |
| 106 | + # the code path is exercised without errors |
| 107 | + with patch("os.name", "nt"): |
| 108 | + async with engine_session_factory(db_path, DatabaseType.MEMORY) as (engine, _): |
| 109 | + # If Windows-specific code path works, engine should be created successfully |
| 110 | + assert engine is not None |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.asyncio |
| 114 | +async def test_null_pool_on_windows(tmp_path): |
| 115 | + """Test that NullPool is used on Windows to avoid connection pooling issues.""" |
| 116 | + db_path = tmp_path / "test.db" |
| 117 | + |
| 118 | + with patch("os.name", "nt"): |
| 119 | + async with engine_session_factory(db_path, DatabaseType.MEMORY) as (engine, _): |
| 120 | + from sqlalchemy.pool import NullPool |
| 121 | + |
| 122 | + # Engine should be using NullPool on Windows |
| 123 | + assert isinstance(engine.pool, NullPool) |
| 124 | + |
| 125 | + |
| 126 | +@pytest.mark.asyncio |
| 127 | +async def test_regular_pool_on_non_windows(tmp_path): |
| 128 | + """Test that regular pooling is used on non-Windows platforms.""" |
| 129 | + db_path = tmp_path / "test.db" |
| 130 | + |
| 131 | + with patch("os.name", "posix"): |
| 132 | + async with engine_session_factory(db_path, DatabaseType.MEMORY) as (engine, _): |
| 133 | + from sqlalchemy.pool import NullPool |
| 134 | + |
| 135 | + # Engine should NOT be using NullPool on non-Windows |
| 136 | + assert not isinstance(engine.pool, NullPool) |
| 137 | + |
| 138 | + |
| 139 | +@pytest.mark.asyncio |
| 140 | +async def test_foreign_keys_enabled(tmp_path): |
| 141 | + """Test that foreign keys are enabled (from scoped_session context).""" |
| 142 | + db_path = tmp_path / "test.db" |
| 143 | + |
| 144 | + async with engine_session_factory(db_path, DatabaseType.MEMORY) as (engine, _): |
| 145 | + async with engine.connect() as conn: |
| 146 | + result = await conn.execute(text("PRAGMA foreign_keys")) |
| 147 | + foreign_keys = result.fetchone()[0] |
| 148 | + |
| 149 | + # Foreign keys should be enabled by default in our configuration |
| 150 | + # Note: This is set in scoped_session, not in the engine event listener |
| 151 | + # For this test we just verify the PRAGMA works |
| 152 | + assert foreign_keys is not None |
0 commit comments