From 97e121785fac63e0308c9a55a0fe29e47567fcad Mon Sep 17 00:00:00 2001 From: Konstantin Date: Thu, 7 Nov 2024 14:56:29 +0100 Subject: [PATCH] make main testable --- docker-compose.yaml | 4 +++- src/ebd_toolchain/main.py | 8 ++++++++ unittests/test_main.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 unittests/test_main.py diff --git a/docker-compose.yaml b/docker-compose.yaml index c6339fd..ec6319c 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,7 +1,9 @@ services: kroki: image: yuzutech/kroki:0.24.1 - + ports: + - "8000:8000" # Expose localhost:8000 + # this is required for the test_main.py to directly use kroki scrape-and-plot: build: . image: ghcr.io/hochfrequenz/ebd_toolchain:latest diff --git a/src/ebd_toolchain/main.py b/src/ebd_toolchain/main.py index d4c4268..6c095fa 100644 --- a/src/ebd_toolchain/main.py +++ b/src/ebd_toolchain/main.py @@ -113,6 +113,11 @@ def main(input_path: Path, output_path: Path, export_types: list[Literal["puml", """ A program to get a machine-readable version of the AHBs docx files published by edi@energy. """ + _main(input_path, output_path, export_types) + + +def _main(input_path: Path, output_path: Path, export_types: list[Literal["puml", "dot", "json", "svg"]]) -> None: + """same as main but without the click decorators""" settings = Settings() # type:ignore[call-arg] # read settings from environment variable/.env file kroki_client = Kroki(kroki_host=f"http://{settings.kroki_host}:{settings.kroki_port}") @@ -195,6 +200,9 @@ def handle_known_error(error: Exception, ebd_key: str) -> None: # e.g. AssertionError: If indegree > 1, the number of paths should always be greater than 1 too. click.secho(str(assertion_error), fg="red") # both the SVG and dot path require graphviz to work, hence the common error handling block + except Exception as unknown_error: + click.secho(str(unknown_error), fg="red") + continue click.secho(json.dumps({str(k): v for k, v in error_sources.items()}, indent=4)) click.secho("🏁Finished") diff --git a/unittests/test_main.py b/unittests/test_main.py new file mode 100644 index 0000000..f1c7c97 --- /dev/null +++ b/unittests/test_main.py @@ -0,0 +1,35 @@ +""" +tests the main script +""" + +from pathlib import Path +from typing import Literal +from _pytest.monkeypatch import MonkeyPatch +import pytest + +from ebd_toolchain.main import _main + +repo_root = Path(__file__).parent.parent +recent_docx_file = ( + repo_root + / "edi_energy_mirror" + / "edi_energy_de" + / "FV2504" + / "Entscheidungsbaum-DiagrammeundCodelisten-informatorischeLesefassung4.0a_99991231_20250404.docx" +) +assert recent_docx_file.exists() + + +@pytest.mark.parametrize( + "input_path, export_types", [pytest.param(recent_docx_file, ["puml", "dot", "json", "svg"], id="recent call")] +) +def test_main( + input_path: Path, + export_types: list[Literal["puml", "dot", "json", "svg"]], + tmp_path: Path, + monkeypatch: MonkeyPatch, +) -> None: + monkeypatch.setenv("KROKI_PORT", "8000") + monkeypatch.setenv("KROKI_HOST", "localhost") + _main(input_path, tmp_path, export_types) + # we don't assert on the results but instead just check that it doesn't crash