Download tests/test_data_decoding.py from VoiceOfML/Search: direct link, hf CLI and curl.
- Browser
- Download file 2.62 kB
-
https://huggingface.co/spaces/VoiceOfML/Search/resolve/main/tests/test_data_decoding.py
- Command line
-
hf download hf://spaces/VoiceOfML/Search/tests/test_data_decoding.py
-
curl -L -o test_data_decoding.py https://huggingface.co/spaces/VoiceOfML/Search/resolve/main/tests/test_data_decoding.py
2.62 kB
| from __future__ import annotations | |
| import unittest | |
| IMPORT_ERROR = None | |
| try: | |
| import app | |
| except ModuleNotFoundError as exc: # pragma: no cover - environment dependent | |
| app = None | |
| IMPORT_ERROR = exc | |
| class DataDecodingTests(unittest.TestCase): | |
| def test_compact_records_decode_and_invalid_indexes_are_safe(self): | |
| data = {"rp": ["Repo/A"], "fd": [["books"]], "rc": [[0, "novel", "pdf", 0, 12, 1], [4, "bad", "txt", 9, 0, 0], [0, "short"]]} | |
| self.assertEqual(app.decode_search_payload(data), [{ | |
| "Repo": "Repo/A", "File": "novel", "Extension": "pdf", | |
| "Folder": ["books"], "Size": 12, "HasTxt": True, | |
| }, { | |
| "Repo": "", "File": "bad", "Extension": "txt", | |
| "Folder": [], "Size": 0, "HasTxt": False, | |
| }]) | |
| def test_tree_decoding_derives_paths_and_toggle_semantics(self): | |
| result = app.decode_folder_tree_data({"Repo/A": [{"n": "root", "df": True, "ch": [{"n": "child", "df": True}]}]})["Repo/A"][0] | |
| self.assertTrue(result["isRoot"]) | |
| self.assertEqual(result["path"], "") | |
| child = result["children"][0] | |
| self.assertEqual(child["path"], "child") | |
| self.assertFalse(child["showSelfToggle"]) | |
| def test_legacy_long_tree_fields_are_not_decoded(self): | |
| result = app.decode_folder_tree_data({"Repo/A": [{"name": "legacy", "children": []}]})["Repo/A"][0] | |
| self.assertEqual(result["name"], "") | |
| def test_browser_decoding_preserves_folder_semantics_and_file_size(self): | |
| result = app.decode_folder_browser_data({"Repo/A": {"books": {"d": [{"n": "part", "c": 2}], "f": [{"n": "book", "e": "pdf", "s": 0}]}}})["Repo/A"]["books"] | |
| self.assertEqual(result["current_path"], "books") | |
| self.assertEqual(result["folders"][0]["path"], "books/part") | |
| self.assertEqual(result["files"][0]["size"], 0) | |
| def test_legacy_long_browser_fields_are_not_decoded(self): | |
| result = app.decode_folder_browser_data({"Repo/A": {"": {"folders": [{"name": "legacy"}], "files": []}}})["Repo/A"][""] | |
| self.assertEqual(result["folders"], []) | |
| def test_record_and_browser_names_do_not_duplicate_extensions(self): | |
| self.assertEqual(app.build_browser_file_name({"name": "book.PDF", "ext": "pdf"}), "book.PDF") | |
| self.assertEqual(app.build_browser_file_name({"name": "book", "ext": "pdf"}), "book.pdf") | |
| self.assertEqual(app.build_txt_relative_path({"File": "book", "Extension": "pdf", "Folder": ["a"]}), "a/book.txt") | |
| if __name__ == "__main__": | |
| unittest.main() | |