File size: 8,652 Bytes
3c9cfda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d6c14d
 
 
 
e1833b0
6d6c14d
 
 
 
 
 
3c9cfda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Pure incremental Reader index: snapshot ownership, aliases and budgets."""
import unittest

from backend.reader_index import advance_build, new_build


class ReaderIndexTests(unittest.TestCase):
    @staticmethod
    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()