Skip to content

Commit

Permalink
make main testable
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin committed Nov 7, 2024
1 parent ca2ed6f commit 97e1217
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/ebd_toolchain/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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")

Expand Down
35 changes: 35 additions & 0 deletions unittests/test_main.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 97e1217

Please sign in to comment.