Test newspaper SQLite preview and normalized fixtures
Browse filesFiles changed (2)
4 unmodified lines56789103 unmodified lines1415161718198 unmodified lines28293031323334353637384 unmodified linesimport tempfileimport unittestfrom pathlib import Pathimport appimport build_fulltext_db3 unmodified linesclass CorpusReaderTests(unittest.TestCase):def test_search_postings_match_independent_complete_fixture_oracle(self):rows = [(1, "手机资料"), (2, "手。机 文化 革命"), (3, "ABC 文化革命"), (4, "无关正文")]matrix = [("手机", True), ("手?机", True), ("手*机", True),8 unmodified lines(root / "CCRD" / filename).write_text(text, encoding="utf-8")records.append({"doc_id": f"CCRD:{number}", "source": "CCRD", "storage_root": "CCRD", "storage_rel_path": filename})create_fts_database(root / "CCRD.sqlite3", rows)# Match the production builder's stored normalized FTS text. The# oracle below still reads and normalizes independent originals.with sqlite3.connect(root / "CCRD.sqlite3") as connection:for number, text in rows:connection.execute("UPDATE content_fts SET content=? WHERE rowid=?", (build_fulltext_db.fts_content(text), number))expected = {query: set() for query in matrix}with CorpusReader(root, root) as reader:for record in records:4 unmodified lines56789101112133 unmodified lines17181920212223242526272829303132333435363738394041424344454647484950518 unmodified lines60616263646566674 unmodified linesimport tempfileimport unittestfrom pathlib import Pathfrom unittest.mock import patchfrom fastapi.testclient import TestClientimport appimport build_fulltext_db3 unmodified linesclass CorpusReaderTests(unittest.TestCase):def test_sqlite_newspaper_original_matches_actual_preview_endpoint(self):text = "报纸原文😀:文化革命和手机。"raw = text.encode("utf-8")record = {"doc_id":"RMRB:1", "source":"RMRB", "source_name":"人民日报", "display_name":"报纸样本","display_dirs":[], "display_rel_path":"报纸样本.txt", "storage_root":"RMRB", "storage_rel_path":"1.txt","extension":"txt", "size":len(raw), "has_preview":True, "has_fulltext":True,"text_storage":"sqlite", "text_sha256":hashlib.sha256(raw).hexdigest()}with tempfile.TemporaryDirectory() as directory:root = Path(directory)path = root / "RMRB.sqlite3"create_fts_database(path, [(1, text)])with sqlite3.connect(path) as connection:connection.execute("UPDATE documents SET doc_id=? WHERE doc_id=?", ("RMRB:1", "CCRD:1"))connection.execute("UPDATE content_fts SET content=? WHERE rowid=1", (build_fulltext_db.fts_content(text),))connection.execute("CREATE TABLE texts (doc_id TEXT PRIMARY KEY, body TEXT NOT NULL)")connection.execute("INSERT INTO texts VALUES (?, ?)", ("RMRB:1", text))connection.execute("INSERT INTO metadata VALUES ('index_profile', ?)", (app.NEWSPAPER_INDEX_PROFILE,))connection.execute("INSERT INTO metadata VALUES ('build_complete', 'true')")database = app.FulltextDatabases(root)try:with CorpusReader(root, root) as reader, patch.object(app, "record_map", {"RMRB:1": record}), \patch.object(app, "fulltext_databases", database):response = TestClient(app.app).get("/api/preview/RMRB:1")self.assertEqual(response.status_code, 200)self.assertEqual(response.json()["text"], reader.read_text(record))self.assertEqual(database.search_source("RMRB", "手机", True), {1})finally:database.close()def test_search_postings_match_independent_complete_fixture_oracle(self):rows = [(1, "手机资料"), (2, "手。机 文化 革命"), (3, "ABC 文化革命"), (4, "无关正文")]matrix = [("手机", True), ("手?机", True), ("手*机", True),8 unmodified lines(root / "CCRD" / filename).write_text(text, encoding="utf-8")records.append({"doc_id": f"CCRD:{number}", "source": "CCRD", "storage_root": "CCRD", "storage_rel_path": filename})create_fts_database(root / "CCRD.sqlite3", rows)# The fixture helper stores the same normalized FTS text as the# production builder; the oracle reads independent originals.expected = {query: set() for query in matrix}with CorpusReader(root, root) as reader:for record in records:
44 unmodified lines4546474849505114 unmodified lines66676869707144 unmodified linesconnection.execute("CREATE TABLE snippet_anchors (token_hash BLOB NOT NULL, doc_number INTEGER NOT NULL, char_offset INTEGER NOT NULL, PRIMARY KEY (token_hash, doc_number)) WITHOUT ROWID")for number, content in rows:connection.execute("INSERT INTO documents VALUES (?, ?)", (f"CCRD:{number}", content[:700]))connection.execute("INSERT INTO content_fts(rowid, content) VALUES (?, ?)", (number, content))connection.executemany("INSERT INTO snippet_anchors VALUES (?, ?, ?)",((token_hash, number, char_offset) for token_hash, char_offset in build_fulltext_db.snippet_anchors(content).items()),14 unmodified linesclass FulltextIntegrationTests(unittest.TestCase):def test_newspaper_text_integrity_preview_and_missing_index(self):text = "报纸原文:手机与文化。"payload = text.encode("utf-8")44 unmodified lines4546474849505114 unmodified lines6667686970717273747576777879808144 unmodified linesconnection.execute("CREATE TABLE snippet_anchors (token_hash BLOB NOT NULL, doc_number INTEGER NOT NULL, char_offset INTEGER NOT NULL, PRIMARY KEY (token_hash, doc_number)) WITHOUT ROWID")for number, content in rows:connection.execute("INSERT INTO documents VALUES (?, ?)", (f"CCRD:{number}", content[:700]))connection.execute("INSERT INTO content_fts(rowid, content) VALUES (?, ?)", (number, build_fulltext_db.fts_content(content)))connection.executemany("INSERT INTO snippet_anchors VALUES (?, ?, ?)",((token_hash, number, char_offset) for token_hash, char_offset in build_fulltext_db.snippet_anchors(content).items()),14 unmodified linesclass FulltextIntegrationTests(unittest.TestCase):def test_fixture_fts_matches_production_normalization(self):text = "ABC\x00文化革命"with tempfile.TemporaryDirectory() as directory:path = Path(directory) / "CCRD.sqlite3"create_fts_database(path, [(1, text)])with sqlite3.connect(path) as connection:value = connection.execute("SELECT content FROM content_fts WHERE rowid=1").fetchone()[0]self.assertEqual(value, build_fulltext_db.fts_content(text))self.assertEqual(value, "abc 文化革命")def test_newspaper_text_integrity_preview_and_missing_index(self):text = "报纸原文:手机与文化。"payload = text.encode("utf-8")
tests/test_corpus_reader.py+34-5
tests/test_integration.py+11-1