~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Tests for migration 0030: default search.time_period 'y' → 'all'.
Pins the upgrade/downgrade semantics:
- Rows with the legacy default ``"y"`` get flipped.
- Rows users explicitly chose (``d``, ``w``, ``m``, ``all``) are left
untouched.
- Other settings keys with value ``"y"`` are not affected.
- Idempotency: a second upgrade is a no-op once values are migrated.
The on-disk encoding is JSON-text (``"y"`` with the surrounding quotes), so
the test inserts through SQLAlchemy's JSON column type to match production
storage exactly.
"""
import json
import shutil
import pytest
from alembic import command
from sqlalchemy import create_engine, text
from local_deep_research.database.alembic_runner import (
get_alembic_config,
)
def _run_upgrade_to(engine, revision):
config = get_alembic_config(engine)
with engine.begin() as conn:
config.attributes["connection"] = conn
command.upgrade(config, revision)
def _run_downgrade_to(engine, revision):
config = get_alembic_config(engine)
with engine.begin() as conn:
config.attributes["connection"] = conn
command.downgrade(config, revision)
def _seed_setting(engine, key, value):
"""Insert a setting matching production's JSON-text storage.
SQLAlchemy's JSON column writes ``json.dumps(value)``; we mirror that
explicitly so raw SQL produces the same on-disk bytes the migration
expects to match in its WHERE clause.
"""
with engine.begin() as conn:
conn.execute(
text(
"INSERT INTO settings "
"(key, value, type, name, ui_element, visible, editable) "
"VALUES (:key, :value, 'search', :name, 'select', 1, 1)"
),
{"key": key, "value": json.dumps(value), "name": key},
)
def _read_setting(engine, key):
with engine.begin() as conn:
row = conn.execute(
text("SELECT value FROM settings WHERE key = :key"),
{"key": key},
).fetchone()
if row is None:
return None
raw = row[0]
# JSON column round-trips through json.loads on read, but raw text()
# bypasses the column type — so decode manually.
return json.loads(raw) if isinstance(raw, str) else raw
@pytest.fixture(scope="module")
def _migrated_to_0029_template(tmp_path_factory):
"""Template DB fully migrated through 0029, built once per module.
Rebuilding the full 0001->0029 Alembic chain is the expensive part of
this file (dominates its runtime across ~16 tests); building it once
and letting each test start from a copy keeps per-test isolation while
paying the migration cost only once.
"""
template_dir = tmp_path_factory.mktemp("migration_0030_template")
db_path = template_dir / "template_0029.db"
engine = create_engine(f"sqlite:///{db_path}")
_run_upgrade_to(engine, "0029")
engine.dispose()
return db_path
@pytest.fixture
def migrated_to_0029_engine(tmp_path, _migrated_to_0029_template):
"""Database fully migrated through 0029 (the revision before 0030).
Copies the module-scoped template built by ``_migrated_to_0029_template``
into a fresh per-test file, so every test still gets its own isolated
database without re-running the Alembic chain from scratch.
"""
db_path = tmp_path / "test_0030.db"
shutil.copy2(_migrated_to_0029_template, db_path)
engine = create_engine(f"sqlite:///{db_path}")
yield engine
engine.dispose()
class TestMigration0030Upgrade:
def test_y_value_is_migrated_to_all(self, migrated_to_0029_engine):
engine = migrated_to_0029_engine
with engine.begin() as conn:
conn.execute(
text("DELETE FROM settings WHERE key = 'search.time_period'")
)
_seed_setting(engine, "search.time_period", "y")
_run_upgrade_to(engine, "0030")
assert _read_setting(engine, "search.time_period") == "all"
@pytest.mark.parametrize("explicit_value", ["d", "w", "m", "all"])
def test_explicit_non_y_choices_are_preserved(
self, migrated_to_0029_engine, explicit_value
):
engine = migrated_to_0029_engine
with engine.begin() as conn:
conn.execute(
text("DELETE FROM settings WHERE key = 'search.time_period'")
)
_seed_setting(engine, "search.time_period", explicit_value)
_run_upgrade_to(engine, "0030")
assert _read_setting(engine, "search.time_period") == explicit_value
def test_other_keys_with_y_value_are_not_touched(
self, migrated_to_0029_engine
):
engine = migrated_to_0029_engine
# An unrelated key that happens to hold 'y' must not be flipped.
_seed_setting(engine, "test.unrelated.period", "y")
_run_upgrade_to(engine, "0030")
assert _read_setting(engine, "test.unrelated.period") == "y"
def test_upgrade_is_idempotent(self, migrated_to_0029_engine):
"""Running the upgrade a second time is a no-op (already at head)."""
engine = migrated_to_0029_engine
with engine.begin() as conn:
conn.execute(
text("DELETE FROM settings WHERE key = 'search.time_period'")
)
_seed_setting(engine, "search.time_period", "y")
_run_upgrade_to(engine, "0030")
# Second invocation: nothing left to do; alembic short-circuits.
_run_upgrade_to(engine, "0030")
assert _read_setting(engine, "search.time_period") == "all"
def test_no_settings_row_does_not_error(self, migrated_to_0029_engine):
"""If the row never existed, the migration is a clean no-op."""
engine = migrated_to_0029_engine
with engine.begin() as conn:
conn.execute(
text("DELETE FROM settings WHERE key = 'search.time_period'")
)
_run_upgrade_to(engine, "0030")
assert _read_setting(engine, "search.time_period") is None
@pytest.mark.parametrize(
"raw_value",
[
"y", # bare unquoted — legacy bug / hand-edited DB
'"y', # truncated JSON
'y"', # truncated JSON the other way
json.dumps(json.dumps("y")), # double-encoded
],
)
def test_malformed_legacy_y_value_is_left_alone(
self, migrated_to_0029_engine, raw_value
):
"""If the on-disk bytes don't exactly match the JSON-encoded '"y"'
the migration docstring worries about, the WHERE clause must NOT
match — leaving the row alone rather than corrupting it. Safe-no-op
failure mode: future hand-edit or migration bug that writes bare
'y' will not be silently rewritten.
Parametrized over the legacy forms the docstring calls out as
potential on-disk shapes from older code paths.
"""
engine = migrated_to_0029_engine
with engine.begin() as conn:
conn.execute(
text("DELETE FROM settings WHERE key = 'search.time_period'")
)
# Insert the raw bytes verbatim — bypassing json.dumps so we
# control exactly what's on disk.
conn.execute(
text(
"INSERT INTO settings "
"(key, value, type, name, ui_element, visible, editable) "
"VALUES ('search.time_period', :value, "
"'search', 'period', 'select', 1, 1)"
),
{"value": raw_value},
)
_run_upgrade_to(engine, "0030")
with engine.begin() as conn:
row = conn.execute(
text(
"SELECT value FROM settings "
"WHERE key = 'search.time_period'"
)
).fetchone()
# The malformed value is left exactly as-is on disk.
assert row[0] == raw_value
class TestMigration0030Downgrade:
def test_downgrade_reverts_all_to_y(self, migrated_to_0029_engine):
engine = migrated_to_0029_engine
with engine.begin() as conn:
conn.execute(
text("DELETE FROM settings WHERE key = 'search.time_period'")
)
_seed_setting(engine, "search.time_period", "y")
_run_upgrade_to(engine, "0030")
_run_downgrade_to(engine, "0029")
assert _read_setting(engine, "search.time_period") == "y"
@pytest.mark.parametrize("preserved_value", ["d", "w", "m"])
def test_downgrade_preserves_other_explicit_choices(
self, migrated_to_0029_engine, preserved_value
):
engine = migrated_to_0029_engine
with engine.begin() as conn:
conn.execute(
text("DELETE FROM settings WHERE key = 'search.time_period'")
)
_seed_setting(engine, "search.time_period", preserved_value)
_run_upgrade_to(engine, "0030")
_run_downgrade_to(engine, "0029")
assert _read_setting(engine, "search.time_period") == preserved_value
class TestMigration0030HeadAlignment:
"""Head-alignment guard.
Deliberately does NOT pin ``get_head_revision() == "0030"``: this repo
was burned once already (see test_migration_0009_default_fetch_mode.py)
by a literal head pin that broke the moment a later migration chained
on top. The substantive invariant — that 0030 is correctly anchored
in the chain — is covered by asserting ``down_revision`` instead.
Global head-uniqueness / "head matches latest migration file" coverage
lives in the dynamic checks in
tests/database/test_migration_0022_note_references.py, which need no
update when a new migration (like this one) lands on top.
"""
def test_0030_chains_correctly_to_0029(self):
"""0030 (default_time_period_all) chains directly off 0029."""
from alembic.config import Config
from alembic.script import ScriptDirectory
from local_deep_research.database.alembic_runner import (
get_migrations_dir,
)
config = Config()
config.set_main_option("script_location", str(get_migrations_dir()))
script = ScriptDirectory.from_config(config)
rev_0030 = script.get_revision("0030")
assert rev_0030.down_revision == "0029"