Download tests/reader/backend/test_reader_identity_contract.py from VoiceOfML/Search: direct link, hf CLI and curl.
- Browser
- Download file 3.19 kB
-
https://huggingface.co/spaces/VoiceOfML/Search/resolve/main/tests/reader/backend/test_reader_identity_contract.py
- Command line
-
hf download hf://spaces/VoiceOfML/Search/tests/reader/backend/test_reader_identity_contract.py
-
curl -L -o test_reader_identity_contract.py https://huggingface.co/spaces/VoiceOfML/Search/resolve/main/tests/reader/backend/test_reader_identity_contract.py
3.19 kB
| """Reader source identity: path byte normalization and stable short IDs.""" | |
| import json | |
| from pathlib import Path | |
| import random | |
| import re | |
| import unittest | |
| from urllib.parse import quote | |
| from unittest.mock import patch | |
| from backend.reader.query.domain import identity | |
| class ReaderIdentityTests(unittest.TestCase): | |
| def test_dynamic_domain_hooks(self): | |
| self.assertEqual(identity.reader_short_id.__module__, identity.__name__) | |
| with patch.object(identity, "canonical_reader_source_url", return_value="replacement") as canonical: | |
| with patch.object(identity, "_base36", return_value="hook") as base36: | |
| self.assertEqual(identity.reader_short_id("original"), "000000000hook") | |
| canonical.assert_called_once_with("original") | |
| base36.assert_called_once() | |
| def test_canonicalization_matches_independent_encoder(self): | |
| prefix = "https://huggingface.co/datasets/VoiceOfML/Test/resolve/main/" | |
| def reference(path): | |
| def replace(match): | |
| token = match.group() | |
| if re.fullmatch(r"%[0-9a-fA-F]{2}", token): | |
| char = chr(int(token[1:], 16)) | |
| return char if re.fullmatch(r"[A-Za-z0-9._~-]", char) else token.upper() | |
| return quote(token, safe="") | |
| return prefix + re.sub(r"%[0-9a-fA-F]{2}|[^A-Za-z0-9._~/-]", replace, path) | |
| rng = random.Random(20260927) | |
| tokens = [chr(i) for i in range(128) if chr(i) not in "?#"] | |
| tokens += ["书", "é", "😀", "%2f", "%25", "%41", "%7e", "%G0", "%"] | |
| paths = [f"%{value:02x}" for value in range(256)] | |
| paths += [f"%{value:02X}" for value in range(256)] | |
| paths += ["".join(rng.choices(tokens, k=40)) for _ in range(256)] | |
| for path in paths: | |
| with self.subTest(path=path): | |
| self.assertEqual(identity.canonical_reader_source_url(prefix + path), reference(path)) | |
| for value in ("https://example.com/%ab", prefix + "a?raw=%ab#fragment"): | |
| self.assertEqual(identity.canonical_reader_source_url(value), value) | |
| def test_cross_site_vectors_and_base36(self): | |
| for value, expected in ((0, "0"), (35, "z"), (36, "10"), (72, "20"), (1296, "100"), | |
| (2**64 - 1, "3w5e11264sgsf")): | |
| self.assertEqual(identity._base36(value), expected) | |
| vectors = json.loads((Path(__file__).resolve().parents[2] / "support/reader-contract-vectors.json").read_text()) | |
| for value, expected in vectors["ids"]: | |
| self.assertEqual(identity.reader_short_id(value), expected) | |
| prefix = "https://huggingface.co/datasets/VoiceOfML/VOMEBOOK/resolve/main/" | |
| for value, expected in vectors["canonicalPaths"]: | |
| self.assertEqual(identity.canonical_reader_source_url(prefix + value), prefix + expected) | |
| self.assertEqual(identity.reader_short_id(prefix + value), identity.reader_short_id(prefix + expected)) | |
| self.assertEqual(identity.reader_short_id(prefix + vectors["reportedBook"]["path"]), | |
| vectors["reportedBook"]["id"]) | |
| if __name__ == "__main__": | |
| unittest.main() | |