Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🥅Catch Kroki HTTP 400 as "known error" in both Dot and Puml code paths #92

Merged
merged 9 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v4
with:
submodules: 'recursive'
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
Expand Down
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
39 changes: 39 additions & 0 deletions unittests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
tests the main script
"""

from pathlib import Path
from typing import Literal

import pytest
from _pytest.monkeypatch import MonkeyPatch

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")
# if you run into ConnectionErrors use
# docker-compose up -d
# in the repo root
_main(input_path, tmp_path, export_types)
# we don't assert on the results but instead just check that it doesn't crash
Loading