From 7853d243fca4050ce3da2cee424d3091fbcd9f5e Mon Sep 17 00:00:00 2001 From: Elijas <4084885+Elijas@users.noreply.github.com> Date: Tue, 19 Dec 2023 07:54:55 +0200 Subject: [PATCH] feat: add automated expected structure-and-text postprocessing --- .../test_structure_and_text.py | 16 ++++++++++++++++ tests/types.py | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/tests/accuracy/structure_and_text/test_structure_and_text.py b/tests/accuracy/structure_and_text/test_structure_and_text.py index 30b0775..19ad0ae 100644 --- a/tests/accuracy/structure_and_text/test_structure_and_text.py +++ b/tests/accuracy/structure_and_text/test_structure_and_text.py @@ -1,3 +1,4 @@ +import importlib.util import json import warnings from collections import Counter @@ -47,6 +48,21 @@ def test_structure_and_text( if request.config.getoption("--create-missing-files"): with report.expected_structure_and_text.open("w") as f: json.dump(actual_json, f, sort_keys=True, indent=4, ensure_ascii=False) + if report.expected_structure_and_text_postprocessor.exists(): + path = report.expected_structure_and_text_postprocessor + if path.exists(): + spec = importlib.util.spec_from_file_location("postprocessor", path) + assert spec is not None, f"Could not load {path} (spec is None))" + assert ( + spec.loader is not None + ), f"Could not load {path} (spec.loader is None))" + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + if hasattr(module, "main"): + module.main() + else: + msg = f"The module {module.__name__} does not have a main function." + raise AttributeError(msg) warnings.warn( f"Created {report.expected_structure_and_text.name}. Please manually review it and commit the file.", stacklevel=0, diff --git a/tests/types.py b/tests/types.py index 5932beb..fb14798 100644 --- a/tests/types.py +++ b/tests/types.py @@ -46,6 +46,10 @@ def expected_structure_and_text(self) -> Path: def actual_structure_and_text(self) -> Path: return self.path / "actual-structure-and-text.json" + @property + def expected_structure_and_text_postprocessor(self) -> Path: + return self.path / "postprocess_expected_structure_and_text.py" + @property def actual_structure_and_text_summary(self) -> Path: return self.path / "actual-structure-and-text_summary.json"