Skip to content

Commit

Permalink
tests.models_file - use all files, account for special reidentify and…
Browse files Browse the repository at this point in the history
… "ignore if" cases
  • Loading branch information
MatteoCampinoti94 committed Oct 24, 2024
1 parent eb2e221 commit ec08a23
Showing 1 changed file with 84 additions and 75 deletions.
159 changes: 84 additions & 75 deletions tests/test_models_file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from os import environ
from pathlib import Path
from random import random
from uuid import uuid4

import pytest
Expand All @@ -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()
Expand All @@ -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

0 comments on commit ec08a23

Please sign in to comment.