From ec08a237344d79315b2b82f8cf2c6de04ce20cd0 Mon Sep 17 00:00:00 2001 From: Matteo Campinoti Date: Thu, 24 Oct 2024 13:03:15 +0200 Subject: [PATCH] tests.models_file - use all files, account for special reidentify and "ignore if" cases --- tests/test_models_file.py | 159 ++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 75 deletions(-) diff --git a/tests/test_models_file.py b/tests/test_models_file.py index ef0ebde..88710aa 100644 --- a/tests/test_models_file.py +++ b/tests/test_models_file.py @@ -1,6 +1,5 @@ from os import environ from pathlib import Path -from random import random from uuid import uuid4 import pytest @@ -22,12 +21,6 @@ def siegfried(siegfried_folder: Path) -> Siegfried: return Siegfried(Path(environ["GOPATH"], "bin", "sf"), "pronom.sig", siegfried_folder) -@pytest.fixture(scope="session") -def test_file(test_files: Path, test_files_data: dict[str, dict]) -> Path: - files = sorted((n for n, d in test_files_data.items() if d["binary"] and d["matches"]), key=lambda _: random()) - return test_files / files[0] - - @pytest.fixture(scope="session") def actions() -> dict[str, Action]: return get_actions() @@ -40,102 +33,118 @@ def custom_signatures() -> list[CustomSignature]: def test_base_file( test_folder: Path, - test_file: Path, + test_files: Path, test_files_data: dict[str, dict], siegfried: Siegfried, custom_signatures: list[CustomSignature], ): - uuid = uuid4() - file = BaseFile.from_file( - test_file, - test_folder, - siegfried, - custom_signatures, - uuid, - ) - assert file.relative_path == test_file.relative_to(test_folder) - assert file.root == test_folder - assert file.uuid == uuid - assert file.checksum == test_files_data[test_file.name]["checksum"] - assert file.is_binary == test_files_data[test_file.name]["binary"] - assert file.size == test_files_data[test_file.name]["filesize"] - assert file.puid == test_files_data[test_file.name]["matches"]["id"] - assert file.signature == test_files_data[test_file.name]["matches"]["format"] - assert set(file.warning or []) == set(test_files_data[test_file.name]["matches"]["warning"]) + for filename, filedata in test_files_data.items(): + filepath = test_files / filename + uuid = uuid4() + file = BaseFile.from_file( + test_files / filename, + test_folder, + siegfried, + custom_signatures, + uuid, + ) + assert file.relative_path == filepath.relative_to(test_folder) + assert file.root == test_folder + assert file.uuid == uuid + assert file.checksum == test_files_data[filepath.name]["checksum"] + assert file.is_binary == test_files_data[filepath.name]["binary"] + assert file.size == test_files_data[filepath.name]["filesize"] + assert file.puid == test_files_data[filepath.name]["matches"]["id"] + if file.puid: + assert file.signature == test_files_data[filepath.name]["matches"]["format"] + assert set(file.warning or []) == set(test_files_data[filepath.name]["matches"]["warning"]) + else: + assert file.signature is None + assert file.warning is None def test_original_file( test_folder: Path, - test_file: Path, + test_files: Path, test_files_data: dict[str, dict], siegfried: Siegfried, actions: dict[str, Action], custom_signatures: list[CustomSignature], ) -> None: - uuid = uuid4() - parent = uuid4() - processed = False - lock = True - file = OriginalFile.from_file( - test_file, - test_folder, - siegfried, - actions, - custom_signatures, - uuid, - parent, - processed, - lock, - ) - assert file.puid in actions - assert file.action == actions[file.puid].action - assert file.action_data == actions[file.puid].action_data - assert file.parent == parent - assert file.processed == processed - assert file.lock == lock - assert file.original_name == test_file.name + for filename, filedata in test_files_data.items(): + uuid = uuid4() + parent = uuid4() + processed = False + lock = True + file = OriginalFile.from_file( + test_files / filename, + test_folder, + siegfried, + actions, + custom_signatures, + uuid, + parent, + processed, + lock, + ) + assert file.parent == parent + assert file.processed == processed + assert file.lock == lock + assert file.original_name == filename + + action = actions.get(filedata["matches"]["id"]) + + if action and action.reidentify: + assert file.puid in (filedata["matches"]["id"], None) or file.puid in [cs.puid for cs in custom_signatures] + + if file.puid and (action := actions.get(file.puid)): + assert all(d == file.action_data.model_dump()[a] for a, d in action.action_data.model_dump().items() if d) + assert file.action == action.action or (action.ignore_if and file.action == "ignore") def test_converted_file( test_folder: Path, - test_file: Path, + test_files: Path, test_files_data: dict[str, dict], siegfried: Siegfried, custom_signatures: list[CustomSignature], ): - uuid = uuid4() - original_uuid = uuid4() - file = ConvertedFile.from_file( - test_file, - test_folder, - original_uuid, - siegfried, - custom_signatures, - uuid, - ) - assert file.original_uuid == original_uuid + for filename in test_files_data.keys(): + uuid = uuid4() + original_uuid = uuid4() + file = ConvertedFile.from_file( + test_files / filename, + test_folder, + original_uuid, + siegfried, + custom_signatures, + uuid, + ) + assert file.original_uuid == original_uuid def test_master_file( test_folder: Path, - test_file: Path, + test_files: Path, test_files_data: dict[str, dict], siegfried: Siegfried, actions: dict[str, Action], custom_signatures: list[CustomSignature], ) -> None: convert_actions: dict[str, ConvertAction] = {p: a.convert for p, a in actions.items() if a.convert} - uuid = uuid4() - original_uuid = uuid4() - file = MasterFile.from_file( - test_file, - test_folder, - original_uuid, - siegfried, - custom_signatures, - convert_actions, - {"": next(iter(convert_actions.values()))}, - uuid, - ) - assert file.convert_access == convert_actions.get(file.puid) - assert file.convert_statutory is None + convert_actions_empty: dict[str, ConvertAction] = {"": next(iter(convert_actions.values()))} + for filename, filedata in test_files_data.items(): + uuid = uuid4() + original_uuid = uuid4() + file = MasterFile.from_file( + test_files / filename, + test_folder, + original_uuid, + siegfried, + custom_signatures, + convert_actions, + convert_actions_empty, + uuid, + ) + assert file.convert_access == convert_actions.get(file.puid) + assert file.convert_statutory is None