Download tests/reader/backend/test_reader_index_contract.py from VoiceOfML/Search: direct link, hf CLI and curl.
- Browser
- Download file 8.65 kB
-
https://huggingface.co/spaces/VoiceOfML/Search/resolve/main/tests/reader/backend/test_reader_index_contract.py
- Command line
-
hf download hf://spaces/VoiceOfML/Search/tests/reader/backend/test_reader_index_contract.py
-
curl -L -o test_reader_index_contract.py https://huggingface.co/spaces/VoiceOfML/Search/resolve/main/tests/reader/backend/test_reader_index_contract.py
8.65 kB
| """Pure incremental Reader index: snapshot ownership, aliases and budgets.""" | |
| import unittest | |
| from backend.reader_index import advance_build, new_build | |
| class ReaderIndexTests(unittest.TestCase): | |
| def options(clock=None): | |
| if clock is None: | |
| # Preserve a fixed, pre-deadline time while bounding accidental | |
| # non-advancing loops; each valid fixture has at most five rows. | |
| calls = iter(range(16)) | |
| def clock(): | |
| next(calls) | |
| return 0 | |
| return {"now": clock, "source_id": lambda url: "id:" + url, | |
| "asset_key": lambda rec: rec["Repo"] + ":" + rec["File"], | |
| "record_link": lambda rec: "canonical:" + rec["File"] + "." + rec["Extension"], | |
| "dataset_url": lambda repo, path: repo + "/" + path, | |
| "reader_extensions": frozenset({"pdf", "txt"})} | |
| def test_batches_keep_snapshot_keys_ids_and_only_reader_repos(self): | |
| records = [{"Repo": "VoiceOfML/A", "File": "book", "Extension": "pdf", "Folder": [], "_ext_lower": "pdf"}, | |
| {"Repo": "VoiceOfML/A", "File": "photo.jpg", "Extension": "jpg", "Folder": ["docs"], | |
| "Link": "custom:image", "_ext_lower": "jpg"}, | |
| {"Repo": "VoiceOfML/B", "File": "note", "Extension": "txt", "Folder": [], "_ext_lower": "txt"}] | |
| build = new_build(7, records) | |
| self.assertIs(build["records"], records) | |
| self.assertFalse(advance_build(build, 2, 10, **self.options())) | |
| self.assertEqual(build["next"], 2) | |
| self.assertEqual(build["indices"], [0]) | |
| self.assertEqual(build["repos"], {"VoiceOfML/A": [0]}) | |
| self.assertEqual(build["ids"], {"id:canonical:book.pdf": [0], "id:custom:image": [1]}) | |
| self.assertEqual(build["legacy_ids"], {"id:VoiceOfML/A/docs/photo.jpg": [1]}) | |
| self.assertTrue(advance_build(build, 2, 10, **self.options())) | |
| self.assertEqual(build["indices"], [0, 2]) | |
| self.assertEqual(build["repos"], {"VoiceOfML/A": [0], "VoiceOfML/B": [2]}) | |
| self.assertEqual(build["keys"], {"VoiceOfML/A:book": 0, "VoiceOfML/A:photo.jpg": 1, "VoiceOfML/B:note": 2}) | |
| self.assertEqual(build["generation"], 7) | |
| def test_tiff_originals_keep_asset_lookup_but_leave_native_pool(self): | |
| records = [{"Repo": "VoiceOfML/A", "File": ext, "Extension": ext, "_ext_lower": ext} | |
| for ext in ("tif", "tiff", "pdf")] | |
| options = self.options() | |
| options["reader_extensions"] = frozenset({"pdf"}) | |
| build = new_build(1, records) | |
| self.assertTrue(advance_build(build, 3, 10, **options)) | |
| self.assertEqual(build["indices"], [2]) | |
| self.assertEqual(build["keys"]["VoiceOfML/A:tif"], 0) | |
| self.assertEqual(build["keys"]["VoiceOfML/A:tiff"], 1) | |
| def test_repeated_batches_are_bounded_and_move_forward(self): | |
| records = [{"Repo": "VoiceOfML/A", "File": str(i), "Extension": "pdf", "_ext_lower": "pdf"} | |
| for i in range(3)] | |
| build = new_build(1, records) | |
| # A clock that advances every call bounds a broken loop without waiting | |
| # for the mutation runner's timeout. | |
| tick = iter(range(20)) | |
| self.assertFalse(advance_build(build, 2, 2, **self.options(lambda: next(tick)))) | |
| self.assertEqual(build["next"], 2) | |
| self.assertEqual(build["indices"], [0, 1]) | |
| self.assertEqual(build["repos"], {"VoiceOfML/A": [0, 1]}) | |
| def test_missing_metadata_uses_empty_defaults_without_creating_aliases(self): | |
| records = [{"File": "book", "Extension": "pdf", "_ext_lower": "pdf"}, | |
| {"Repo": "VoiceOfML/A", "Extension": "pdf", "_ext_lower": "pdf"}, | |
| {"Repo": "VoiceOfML/A", "File": "book", "_ext_lower": "pdf"}, | |
| {"Repo": "VoiceOfML/A", "File": "photo.jpg", "Extension": "jpg", "_ext_lower": "jpg"}, | |
| {"Repo": "VoiceOfML/A", "File": "other", "Extension": "pdf"}] | |
| options = self.options() | |
| options.update(asset_key=lambda rec: repr(sorted(rec.items())), | |
| record_link=lambda rec: "url:" + rec.get("File", "") + "." + rec.get("Extension", ""), | |
| reader_extensions=frozenset({"pdf", "XXXX"})) | |
| build = new_build(0, records) | |
| self.assertTrue(advance_build(build, len(records), 10, **options)) | |
| self.assertEqual(build["repos"][""], [0]) | |
| self.assertEqual(build["indices"], [0, 1, 2]) | |
| self.assertEqual(build["legacy_ids"], {"id:VoiceOfML/A/photo.jpg": [3]}) | |
| def test_legacy_alias_uses_original_record_for_computed_source(self): | |
| record = {"Repo": "VoiceOfML/A", "File": "x.pdf", "Extension": "pdf", "Folder": []} | |
| calls = [] | |
| options = self.options() | |
| options["record_link"] = lambda rec: calls.append(rec) or "canonical" | |
| build = new_build(0, [record]) | |
| self.assertTrue(advance_build(build, 1, 10, **options)) | |
| self.assertEqual(calls, [record, record]) | |
| self.assertEqual(build["legacy_ids"], {"id:VoiceOfML/A/x.pdf": [0]}) | |
| def test_alias_equality_limits_and_time_budget(self): | |
| same = "VoiceOfML/A/docs/photo.jpg" | |
| records = [{"Repo": "VoiceOfML/A", "File": "photo.jpg", "Extension": "jpg", "Folder": ["docs"], | |
| "Link": same, "_ext_lower": "jpg"}, | |
| {"Repo": "VoiceOfML/A", "File": "x", "Extension": "pdf", "Folder": [], "_ext_lower": "pdf"}] | |
| build = new_build(2, records) | |
| self.assertFalse(advance_build(build, 0, 10, **self.options())) | |
| self.assertEqual(build["next"], 0) | |
| self.assertFalse(advance_build(build, 5, 10, **self.options(lambda: 10))) | |
| self.assertEqual(build["next"], 1) | |
| self.assertEqual(build["legacy_ids"], {}) | |
| self.assertTrue(advance_build(build, 5, 10, **self.options())) | |
| self.assertEqual(build["indices"], [1]) | |
| def test_last_record_completes_even_when_deadline_is_reached(self): | |
| record = {"Repo": "VoiceOfML/A", "File": "x", "Extension": "pdf", "_ext_lower": "pdf"} | |
| build = new_build(5, [record]) | |
| self.assertTrue(advance_build(build, 1, 10, **self.options(lambda: 10))) | |
| self.assertEqual(build["next"], 1) | |
| self.assertEqual(build["indices"], [0]) | |
| def test_mapping_defaults_are_explicit_even_with_custom_fallbacks(self): | |
| class FallbackRecord(dict): | |
| def get(self, key, default=None): | |
| if key not in self and default is None: | |
| return {"File": "other.pdf", "Extension": "pdf", "_ext_lower": "pdf"}.get(key) | |
| return super().get(key, default) | |
| records = [FallbackRecord({"Repo": "VoiceOfML/A", "Extension": "pdf"}), | |
| FallbackRecord({"Repo": "VoiceOfML/A", "File": "other.pdf"})] | |
| options = self.options() | |
| options.update(asset_key=lambda rec: str(len(rec)), record_link=lambda rec: "original", | |
| reader_extensions=frozenset({"pdf"})) | |
| build = new_build(0, records) | |
| self.assertTrue(advance_build(build, 2, 10, **options)) | |
| self.assertEqual(build["indices"], []) | |
| self.assertEqual(build["legacy_ids"], {}) | |
| def test_missing_file_and_extension_cannot_make_invented_legacy_alias(self): | |
| records = [{"Repo": "VoiceOfML/A", "Extension": "XXXX"}, | |
| {"Repo": "VoiceOfML/A", "File": "file.XXXX"}] | |
| options = self.options() | |
| options.update(asset_key=lambda rec: repr(rec), record_link=lambda rec: "canonical") | |
| build = new_build(0, records) | |
| self.assertTrue(advance_build(build, 2, 10, **options)) | |
| self.assertEqual(build["legacy_ids"], {}) | |
| self.assertEqual(build["ids"], {"id:canonical": [0, 1]}) | |
| def test_mapping_cannot_invent_missing_filename_from_a_default(self): | |
| class DefaultAwareRecord(dict): | |
| def get(self, key, default=None): | |
| if key == "File" and key not in self: | |
| # An adapter may decorate a nonempty fallback filename. | |
| return f"{default}.pdf" if default else default | |
| return super().get(key, default) | |
| record = DefaultAwareRecord({"Repo": "VoiceOfML/A", "Extension": "pdf"}) | |
| options = self.options() | |
| options.update(asset_key=lambda _: "key", record_link=lambda _: "canonical") | |
| build = new_build(0, [record]) | |
| self.assertTrue(advance_build(build, 1, 10, **options)) | |
| self.assertEqual(build["ids"], {"id:canonical": [0]}) | |
| self.assertEqual(build["legacy_ids"], {}) | |
| if __name__ == "__main__": | |
| unittest.main() | |