Skip to content

Commit 100c328

Browse files
committed
test: fix CI flakes (postgres migrations, tsquery errors, windows init)
- Stamp alembic head when Postgres schema is created via ORM in tests\n- Treat additional tsquery parse errors (e.g. 'no operand in tsquery') as safe empty results\n- Isolate ConfigManager home + extend timeout in subprocess init test for Windows Signed-off-by: phernandez <[email protected]>
1 parent 0aba2c4 commit 100c328

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/basic_memory/repository/postgres_search_repository.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ async def search(
337337
if (
338338
"syntax error in tsquery" in msg
339339
or "invalid input syntax for type tsquery" in msg
340-
): # pragma: no cover
340+
or "no operand in tsquery" in msg
341+
or "no operator in tsquery" in msg
342+
):
341343
logger.warning(f"tsquery syntax error for search term: {search_text}, error: {e}")
342344
return []
343345

tests/cli/test_cli_tool_exit.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
The fix ensures db.shutdown_db() is called before asyncio.run() returns.
1414
"""
1515

16+
import os
1617
import subprocess
1718
import sys
1819

@@ -55,7 +56,7 @@ def test_cli_command_exits_cleanly(self, command: list[str]):
5556
"This indicates database connections are not being cleaned up properly."
5657
)
5758

58-
def test_ensure_initialization_exits_cleanly(self):
59+
def test_ensure_initialization_exits_cleanly(self, tmp_path):
5960
"""Test that ensure_initialization doesn't cause process hang.
6061
6162
This test directly tests the initialization function that's called
@@ -71,11 +72,20 @@ def test_ensure_initialization_exits_cleanly(self):
7172
print("OK")
7273
"""
7374
try:
75+
# Ensure the subprocess uses an isolated home directory so ConfigManager doesn't
76+
# touch the real user profile/AppData (which can be slow/flaky on CI Windows).
77+
env = dict(os.environ)
78+
bm_home = tmp_path / "basic-memory-home"
79+
env["BASIC_MEMORY_HOME"] = str(bm_home)
80+
env["HOME"] = str(tmp_path)
81+
env["USERPROFILE"] = str(tmp_path)
82+
7483
result = subprocess.run(
7584
[sys.executable, "-c", code],
7685
capture_output=True,
7786
text=True,
78-
timeout=10.0,
87+
timeout=30.0,
88+
env=env,
7989
)
8090
assert "OK" in result.stdout, f"Unexpected output: {result.stdout}"
8191
except subprocess.TimeoutExpired:

tests/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import pytest
1111
import pytest_asyncio
12+
from alembic import command
13+
from alembic.config import Config
1214
from sqlalchemy import text
1315
from sqlalchemy.ext.asyncio import (
1416
AsyncEngine,
@@ -233,6 +235,23 @@ async def engine_factory(
233235
await conn.execute(CREATE_POSTGRES_SEARCH_INDEX_METADATA)
234236
await conn.execute(CREATE_POSTGRES_SEARCH_INDEX_PERMALINK)
235237

238+
# Mark migrations as already applied for this test-created schema.
239+
#
240+
# Some codepaths (e.g. ensure_initialization()) invoke Alembic migrations.
241+
# If we create tables via ORM directly, alembic_version is missing and migrations
242+
# will try to create tables again, causing DuplicateTableError.
243+
alembic_dir = Path(db.__file__).parent / "alembic"
244+
cfg = Config()
245+
cfg.set_main_option("script_location", str(alembic_dir))
246+
cfg.set_main_option(
247+
"file_template",
248+
"%%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s",
249+
)
250+
cfg.set_main_option("timezone", "UTC")
251+
cfg.set_main_option("revision_environment", "false")
252+
cfg.set_main_option("sqlalchemy.url", async_url)
253+
command.stamp(cfg, "head")
254+
236255
yield engine, session_maker
237256

238257
await engine.dispose()

0 commit comments

Comments
 (0)