Files
learningcircuit--local-deep…/tests/database/test_journal_rebuild_data_preservation.py
wehub-resource-sync 7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:08:55 +08:00

120 lines
4.2 KiB
Python

"""Data-preservation guarantees for the journals-table rebuild.
Adding ``name_lower`` and its index in the journal-quality migration
triggers a SQLite ``batch_alter_table`` rebuild (ALTER ADD COLUMN
+ index is rewritten as a full copy under the hood). This test
populates the table with 100 diverse rows *before* the migrations
touch it, runs the chain, and asserts every row survives with its
seeded columns preserved (``name``, ``quality_analysis_time``) and
``name_lower`` correctly backfilled — including diacritic, CJK, and
padded-whitespace name variants.
The ``batch_alter_table`` rebuild happens inside an Alembic
transaction, so SQLite's atomicity guarantees the table is either
fully rebuilt or untouched; a simulated mid-rebuild crash is
covered by SQLite's transaction rollback, not by our code. The
test therefore focuses on what the *output* of a successful rebuild
must look like: zero data loss, backfilled Unicode.
"""
from __future__ import annotations
import tempfile
import unicodedata
from pathlib import Path
from sqlalchemy import create_engine, inspect, text
from local_deep_research.database.alembic_runner import run_migrations
from local_deep_research.database.models import Base
def _expected_name_lower(name: str) -> str:
"""Mirror the migration's backfill expression so the test locks in
NFKC + lower + strip semantics, not bare str.lower(). Divergence
between writers produces silent cache misses — see
0006_journal_quality_system.py Step 1/3.
"""
return unicodedata.normalize("NFKC", name).lower().strip()
def _make_engine():
tmp = Path(tempfile.mkdtemp()) / "rebuild.db"
return create_engine(f"sqlite:///{tmp}")
def _seed(engine, n: int) -> list[tuple[str, int]]:
"""Insert ``n`` journal rows with a mix of ASCII and Unicode names."""
rows = []
with engine.begin() as conn:
conn.execute(text("DELETE FROM journals"))
for i in range(n):
# Mix: diacritics, Asian, cased, whitespace.
if i % 4 == 0:
name = f"Café Research {i}"
elif i % 4 == 1:
name = f"JOURNAL {i}"
elif i % 4 == 2:
name = f"日本の学術誌 {i}"
else:
name = f" Spaced Title {i} "
q_time = 1_700_000_000 + i
conn.execute(
text(
"INSERT INTO journals (name, quality_analysis_time) "
"VALUES (:n, :t)"
),
{"n": name, "t": q_time},
)
rows.append((name, q_time))
return rows
def test_all_rows_survive_the_chain_with_correct_backfill():
engine = _make_engine()
try:
Base.metadata.create_all(engine)
seed_rows = _seed(engine, 100)
# Wipe any name_lower the ORM default might have set so the backfill
# branch is the one under test.
with engine.begin() as conn:
conn.execute(text("UPDATE journals SET name_lower = NULL"))
run_migrations(engine)
# 100 rows still there; name / quality_analysis_time preserved;
# name_lower backfilled correctly.
with engine.begin() as conn:
actual = conn.execute(
text(
"SELECT name, name_lower, quality_analysis_time "
"FROM journals ORDER BY id"
)
).all()
assert len(actual) == len(seed_rows), (
f"Row count regression: {len(actual)} vs {len(seed_rows)}"
)
for (seed_name, seed_t), row in zip(seed_rows, actual):
assert row.name == seed_name
assert row.name_lower == _expected_name_lower(seed_name)
assert row.quality_analysis_time == seed_t
finally:
engine.dispose()
def test_no_orphan_tmp_table_after_migration():
"""Alembic's batch rebuild must not leave ``_alembic_tmp_journals``."""
engine = _make_engine()
try:
Base.metadata.create_all(engine)
_seed(engine, 10)
run_migrations(engine)
insp = inspect(engine)
table_names = set(insp.get_table_names())
orphans = {t for t in table_names if t.startswith("_alembic_tmp_")}
assert not orphans, f"Orphan rebuild tables remain: {orphans}"
finally:
engine.dispose()