Skip to content

Commit

Permalink
chore/lint: use strict type-check in tests (#282)
Browse files Browse the repository at this point in the history
Co-authored-by: Konstantin <konstantin.klein+github@hochfrequenz.de>
  • Loading branch information
hf-kklein and Konstantin authored Oct 29, 2024
1 parent d681046 commit 1941b37
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 35 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ tests = [
]
type_check = [
"mypy==1.13.0",
"types-requests==2.32.0.20241016"
"types-requests==2.32.0.20241016",
"lxml-stubs==0.5.1"
]

[project.urls]
Expand Down
9 changes: 9 additions & 0 deletions src/rebdhuhn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@
from rebdhuhn.graph_conversion import convert_table_to_digraph, convert_table_to_graph
from rebdhuhn.graphviz import convert_dot_to_svg_kroki, convert_graph_to_dot
from rebdhuhn.plantuml import convert_graph_to_plantuml, convert_plantuml_to_svg_kroki

__all__ = [
"convert_table_to_digraph",
"convert_table_to_graph",
"convert_dot_to_svg_kroki",
"convert_graph_to_dot",
"convert_graph_to_plantuml",
"convert_plantuml_to_svg_kroki",
]
6 changes: 3 additions & 3 deletions src/rebdhuhn/add_watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pathlib import Path
from typing import TextIO, Tuple, Union

from lxml import etree # type:ignore[import-untyped]
from lxml import etree
from svgutils.compose import SVG, Figure # type:ignore[import-untyped]

# Sets the size of the watermark compared to the smaller dimension of the ebd diagram
Expand Down Expand Up @@ -46,8 +46,8 @@ def get_dimensions_of_svg(svg_as_bytes: Union[BytesIO, TextIO]) -> Tuple[float,
root = tree.getroot()
# root.attrib["height"] gives a string like "123px"
# for further usage, we have to remove the unit and convert it to integer
width_of_svg_in_px = convert_dimension_to_float(root.attrib["width"])
height_of_svg_in_px = convert_dimension_to_float(root.attrib["height"])
width_of_svg_in_px = convert_dimension_to_float(str(root.attrib["width"]))
height_of_svg_in_px = convert_dimension_to_float(str(root.attrib["height"]))

return width_of_svg_in_px, height_of_svg_in_px

Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ commands =
setenv = PYTHONPATH = {toxinidir}/src
deps =
-r requirements.txt
.[tests]
.[type_check]
commands =
mypy --show-error-codes --strict src/rebdhuhn
mypy --show-error-codes unittests
mypy --show-error-codes --strict unittests
# add single files (ending with .py) or packages here

[testenv:coverage]
Expand Down
14 changes: 7 additions & 7 deletions unittests/test_ebd_table_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cattrs
import pytest # type:ignore[import]
import pytest

from rebdhuhn.models.ebd_table import (
EbdCheckResult,
Expand Down Expand Up @@ -46,7 +46,7 @@ class TestEbdTableModels:
)
],
)
def test_instantiation(self, table: EbdTable):
def test_instantiation(self, table: EbdTable) -> None:
"""
The test is successful already if the instantiation in the parametrization worked
"""
Expand Down Expand Up @@ -118,11 +118,11 @@ def test_instantiation(self, table: EbdTable):
),
],
)
def test_has_subsequent_steps(self, row: EbdTableRow, expected_result: bool):
def test_has_subsequent_steps(self, row: EbdTableRow, expected_result: bool) -> None:
actual = row.has_subsequent_steps()
assert actual == expected_result

def test_ebd_table_row_use_cases(self):
def test_ebd_table_row_use_cases(self) -> None:
row_17_in_e0462 = EbdTableRow(
step_number="17",
description="Liegt das Eingangsdatum der Anmeldung mehr als sechs Wochen nach dem Lieferbeginndatum der Anmeldung?",
Expand All @@ -144,7 +144,7 @@ def test_ebd_table_row_use_cases(self):
assert row_17_in_e0462.use_cases is not None
# if it can be instantiated with use cases that's a good enough test for the model

def test_answer_code_aastersik(self):
def test_answer_code_astersik(self) -> None:
"""
This is an example from 6.27.1 E_0455_Information prüfen.
The tests asserts that the validator of the result code allow the result code 'A**' which is used in E_0455.
Expand All @@ -157,7 +157,7 @@ def test_answer_code_aastersik(self):
assert isinstance(sub_row, EbdTableSubRow)
assert sub_row.result_code == "A**"

def test_2023_answer_code_regex(self):
def test_2023_answer_code_regex(self) -> None:
"""
This is an example from E_0406.
The test asserts that the validator of the result code allows the result code 'AC7'.
Expand All @@ -170,7 +170,7 @@ def test_2023_answer_code_regex(self):
assert isinstance(sub_row, EbdTableSubRow)
assert sub_row.result_code == "AC7"

def test_collect_answer_codes_instruction(self):
def test_collect_answer_codes_instruction(self) -> None:
snippet_from_e0453 = EbdTable(
metadata=EbdTableMetaData(
ebd_code="E_0453",
Expand Down
16 changes: 8 additions & 8 deletions unittests/test_errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pytest # type:ignore[import]
import pytest

from rebdhuhn import convert_graph_to_plantuml, convert_table_to_graph
from rebdhuhn.models import EbdTable
Expand All @@ -25,39 +25,39 @@ class TestErrors:
"""

@pytest.mark.parametrize("table", [pytest.param(table_e0459)])
def test_not_exactly_two_outgoing_edges_error(self, table: EbdTable):
def test_not_exactly_two_outgoing_edges_error(self, table: EbdTable) -> None:
ebd_graph = convert_table_to_graph(table)
with pytest.raises(NotExactlyTwoOutgoingEdgesError):
_ = convert_graph_to_plantuml(ebd_graph)

@pytest.mark.parametrize("table", [pytest.param(table_e0266)])
def test_loops_in_the_tree_error(self, table: EbdTable):
def test_loops_in_the_tree_error(self, table: EbdTable) -> None:
ebd_graph = convert_table_to_graph(table)
with pytest.raises(PathsNotGreaterThanOneError):
_ = convert_graph_to_plantuml(ebd_graph)

@pytest.mark.parametrize("table", [pytest.param(table_e0454)])
def test_too_complex_for_plantuml(self, table: EbdTable):
def test_too_complex_for_plantuml(self, table: EbdTable) -> None:
ebd_graph = convert_table_to_graph(table)
with pytest.raises(GraphTooComplexForPlantumlError):
_ = convert_graph_to_plantuml(ebd_graph)

@pytest.mark.parametrize("table", [pytest.param(e_0401)])
def test_key_error_because_first_node_has_key_other_than_1(self, table: EbdTable):
def test_key_error_because_first_node_has_key_other_than_1(self, table: EbdTable) -> None:
_ = convert_table_to_graph(table) # must _not_ raise a key error anymore

@pytest.mark.parametrize("table", [pytest.param(e_0529)])
def test_backreference_to_missing_node_with_key_1(self, table: EbdTable):
def test_backreference_to_missing_node_with_key_1(self, table: EbdTable) -> None:
graph = convert_table_to_graph(table)
_ = convert_graph_to_plantuml(graph) # must _not_ raise an assertion error anymore

@pytest.mark.parametrize("table", [pytest.param(e_0404)])
def test_ende_in_wrong_column_error(self, table: EbdTable):
def test_ende_in_wrong_column_error(self, table: EbdTable) -> None:
with pytest.raises(EndeInWrongColumnError):
_ = convert_table_to_graph(table)

@pytest.mark.parametrize("table", [pytest.param(table_e0462)])
def test_cross_reference_not_supported_error(self, table: EbdTable):
def test_cross_reference_not_supported_error(self, table: EbdTable) -> None:
with pytest.raises(EbdCrossReferenceNotSupportedError) as exc_info:
_ = convert_table_to_graph(table)
assert exc_info.value.cross_reference == "E_0402_Prüfen,"
33 changes: 18 additions & 15 deletions unittests/test_table_to_graph.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# mypy: disable-error-code="no-untyped-def"
import os
from pathlib import Path
from typing import List, Optional

import pytest # type:ignore[import]
from lxml import etree # type:ignore[import]
from networkx import DiGraph # type:ignore[import]
import pytest
from lxml import etree
from networkx import DiGraph # type:ignore[import-untyped]

from rebdhuhn import convert_graph_to_plantuml, convert_plantuml_to_svg_kroki, convert_table_to_graph
from rebdhuhn.graph_conversion import get_all_edges, get_all_nodes
Expand Down Expand Up @@ -33,12 +34,12 @@ class InterceptedKrokiClient(Kroki):
purposes.
"""

def __init__(self):
def __init__(self) -> None:
super().__init__()
self.intercepted_kroki_response: Optional[str] = None
self.intercepted_kroki_response_with_xml_comment: Optional[str] = None

def convert_dot_to_svg(self, *args, **kwargs):
def convert_dot_to_svg(self, *args, **kwargs) -> str:
result = super().convert_dot_to_svg(*args, **kwargs)
self.intercepted_kroki_response = result
result_tree = etree.fromstring(result.encode("utf-8"))
Expand Down Expand Up @@ -72,7 +73,7 @@ class TestEbdTableModels:
)
],
)
def test_get_all_nodes(self, table: EbdTable, expected_result: List[EbdGraphNode]):
def test_get_all_nodes(self, table: EbdTable, expected_result: List[EbdGraphNode]) -> None:
actual = get_all_nodes(table)
assert actual == expected_result

Expand Down Expand Up @@ -123,7 +124,7 @@ def test_get_all_nodes(self, table: EbdTable, expected_result: List[EbdGraphNode
)
],
)
def test_get_all_edges(self, table: EbdTable, expected_result: List[EbdGraphEdge]):
def test_get_all_edges(self, table: EbdTable, expected_result: List[EbdGraphEdge]) -> None:
actual = get_all_edges(table)
assert actual == expected_result

Expand Down Expand Up @@ -167,7 +168,7 @@ def test_table_to_digraph(self, table: EbdTable, expected_description: str):
svg_file.write(svg_code)

@staticmethod
def create_and_save_svg_test(ebd_graph: EbdGraph):
def create_and_save_svg_test(ebd_graph: EbdGraph) -> str:
"""
Creates the test svgs and saves them to the output folder.
Also returns the kroki answer svg code as string to be stored for the mock request.
Expand All @@ -187,6 +188,7 @@ def create_and_save_svg_test(ebd_graph: EbdGraph):
svg_file.write(svg_code)

svg_code_for_mock = kroki_client.intercepted_kroki_response_with_xml_comment
assert svg_code_for_mock is not None
return svg_code_for_mock

@pytest.mark.parametrize(
Expand All @@ -213,7 +215,7 @@ def create_and_save_svg_test(ebd_graph: EbdGraph):
),
],
)
def test_table_to_digraph_dot_real_kroki_request(self, table: EbdTable, expected_description: str):
def test_table_to_digraph_dot_real_kroki_request(self, table: EbdTable, expected_description: str) -> None:
"""
Test the conversion pipeline. The results are stored in `unittests/output` for you to inspect the result
manually. The test only checks if the svg can be built.
Expand Down Expand Up @@ -257,7 +259,7 @@ def test_table_to_digraph_dot_real_kroki_request(self, table: EbdTable, expected
),
],
)
def test_table_to_digraph_dot_with_mock(self, table: EbdTable, expected_description: str, requests_mock):
def test_table_to_digraph_dot_with_mock(self, table: EbdTable, expected_description: str, requests_mock) -> None:
"""
Test the conversion pipeline. The results are stored in `unittests/output` for you to inspect the result
manually. The test only checks if the svg can be built.
Expand All @@ -275,7 +277,7 @@ def test_table_to_digraph_dot_with_mock(self, table: EbdTable, expected_descript
self.create_and_save_svg_test(ebd_graph)

@staticmethod
def create_and_save_watermark_and_background_svg(add_background: bool):
def create_and_save_watermark_and_background_svg(add_background: bool) -> str:
"""
Creates the test svgs and saves them to the output folder.
Also returns the kroki answer svg code as string to be stored for the mock request.
Expand Down Expand Up @@ -308,6 +310,7 @@ def create_and_save_watermark_and_background_svg(add_background: bool):
ebd_svg.write(svg_code_with_watermark)

svg_code_for_mock = kroki_client.intercepted_kroki_response_with_xml_comment
assert svg_code_for_mock is not None
return svg_code_for_mock

@pytest.mark.parametrize(
Expand Down Expand Up @@ -348,7 +351,7 @@ def test_table_to_digraph_dot_with_watermark_real_kroki_request(self, add_backgr
),
],
)
def test_table_to_digraph_dot_with_watermark_with_mock(self, add_background: bool, requests_mock):
def test_table_to_digraph_dot_with_watermark_with_mock(self, add_background: bool, requests_mock) -> None:
"""
Test the combination of background and watermark addition to the svg. The results are stored in
`unittests/output` for you to inspect the result manually.
Expand All @@ -361,7 +364,7 @@ def test_table_to_digraph_dot_with_watermark_with_mock(self, add_background: boo
requests_mock.post("http://localhost:8125/", text=kroki_response_string)
self.create_and_save_watermark_and_background_svg(add_background)

def test_table_to_digraph_dot_with_background(self, requests_mock):
def test_table_to_digraph_dot_with_background(self, requests_mock) -> None:
"""
Test the addition of a background in 'HF white' to the svg. The results are stored in
`unittests/output` for you to inspect the result manually.
Expand Down Expand Up @@ -396,7 +399,7 @@ def test_table_to_digraph_dot_with_background(self, requests_mock):
with open(file_path2, "w", encoding="utf-8") as ebd_svg:
ebd_svg.write(svg_code)

def test_table_e0401_too_complex_for_plantuml(self):
def test_table_e0401_too_complex_for_plantuml(self) -> None:
"""
Test the conversion pipeline for E_0401. In this case the plantuml conversion should fail because the graph is
too complex for this implementation.
Expand Down Expand Up @@ -463,7 +466,7 @@ def test_table_e0401_too_complex_for_plantuml(self):
# todo: add E_0462
],
)
def test_table_to_graph(self, table: EbdTable, expected_result: EbdGraph):
def test_table_to_graph(self, table: EbdTable, expected_result: EbdGraph) -> None:
actual = convert_table_to_graph(table)
pytest.skip("todo @leon - wird später in den examples.py ergänzt")
assert actual == expected_result

0 comments on commit 1941b37

Please sign in to comment.