Skip to content

Commit

Permalink
Add snapshot tests (#253)
Browse files Browse the repository at this point in the history
* added snapshots and new testdata
  • Loading branch information
DeltaDaniel authored Oct 23, 2024
1 parent 518c732 commit 2f826c3
Show file tree
Hide file tree
Showing 9 changed files with 2,404 additions and 12 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,7 @@ dmypy.json

# vscode settings
.vscode/

# version number for ebdamame; gets auto-generated during the command
# python -m build
src/ebdamame/version.py
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# to update all repo revisions just run: pre-commit autoupdate
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v5.0.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 22.10.0
rev: 24.10.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/pycqa/isort
rev: 5.10.1
rev: 5.13.2
hooks:
- id: isort
name: isort (python)
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ test_packaging = [
tests = [
"pytest==8.3.3",
"pytest-datafiles==3.0.0",
"pytest-subtests==0.13.1"
"pytest-subtests==0.13.1",
"syrupy==4.7.2"
]
type_check = [
"mypy==1.12.1"
Expand Down Expand Up @@ -96,3 +97,7 @@ exclude = ["/unittests"]
[tool.hatch.build.targets.wheel]
only-include = ["src"]
sources = ["src"]

[tool.pytest.ini_options]
pythonpath = ["."]
markers = ["snapshot: mark a test as a snapshot test"]
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ charset-normalizer==3.3.2
# via requests
click==8.1.7
# via -r requirements.in
colorama==0.4.6
# via click
idna==3.7
# via requests
lxml==5.2.1
Expand Down
8 changes: 8 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ deps =
setenv = PYTHONPATH = {toxinidir}/src
commands = python -m pytest --basetemp={envtmpdir} {posargs}

[testenv:snapshots]
# the tests environment is called by the Github action that runs the unit tests
deps =
-r requirements.txt
.[tests]
setenv = PYTHONPATH = {toxinidir}/src
commands = python -m pytest -m snapshot --basetemp={envtmpdir} {posargs} --snapshot-update

[testenv:linting]
# the linting environment is called by the Github Action that runs the linter
deps =
Expand Down
2,359 changes: 2,359 additions & 0 deletions unittests/__snapshots__/test_highlevel.ambr

Large diffs are not rendered by default.

Binary file added unittests/test_data/ebd20240403_v35.docx
Binary file not shown.
Binary file added unittests/test_data/ebd20250404_v40b.docx
Binary file not shown.
30 changes: 22 additions & 8 deletions unittests/test_highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,27 +212,38 @@ def test_convert_docx_table_to_ebd_table(
actual = converter.convert_docx_tables_to_ebd_table()
assert actual == expected

@pytest.mark.snapshot
@pytest.mark.datafiles("unittests/test_data/ebd20221128.docx")
@pytest.mark.datafiles("unittests/test_data/ebd20230619_v33.docx")
@pytest.mark.datafiles("unittests/test_data/ebd20230629_v34.docx")
@pytest.mark.datafiles("unittests/test_data/ebd20240403_v35.docx")
@pytest.mark.datafiles("unittests/test_data/ebd20250404_v40b.docx")
@pytest.mark.parametrize(
"get_ebd_keys_and_files",
[
[ # some are commented to improve performance make sure to update snapshots if needed
# pytest.param(
# "ebd20221128.docx", # this is used as positional argument for the indirect fixture
# ),
# pytest.param(
# "ebd20230619_v33.docx", # this is used as positional argument for the indirect fixture
# id="19.06.2023 v3.3 / FV2304",
# ),
pytest.param(
"ebd20221128.docx", # this is used as positional argument for the indirect fixture
"ebd20230629_v34.docx",
id="19.06.2023 v3.4 / FV2310",
),
pytest.param(
"ebd20230619_v33.docx", # this is used as positional argument for the indirect fixture
id="19.06.2023 v3.3 / FV2304",
"ebd20240403_v35.docx",
id="08.10.2024 v3.5 / FV2410",
),
pytest.param(
"ebd20230629_v34.docx",
id="19.06.2023 v3.4 / FV2310",
"ebd20250404_v40b.docx",
id="08.10.2024 v3.5 / FV2504",
),
],
indirect=["get_ebd_keys_and_files"], # see `def get_ebd_keys_and_files(datafiles, request)`
)
def test_extraction(self, datafiles, get_ebd_keys_and_files: List[Tuple[str, str]], subtests):
def test_extraction(self, datafiles, get_ebd_keys_and_files: List[Tuple[str, str]], subtests, snapshot):
"""
tests the extraction and conversion without specific assertions
"""
Expand Down Expand Up @@ -260,10 +271,11 @@ def test_extraction(self, datafiles, get_ebd_keys_and_files: List[Tuple[str, str
)
actual = converter.convert_docx_tables_to_ebd_table()
assert isinstance(actual, EbdTable)
assert actual == snapshot(name=ebd_key)
# In the long run, all these catchers shall be removed.
except AttributeError as attribute_error:
if attribute_error.name == "_column_index_step_number":
pytest.skip("https://github.com/Hochfrequenz/ebdamame/issues/71")
pytest.skip(f"{ebd_key}\t https://github.com/Hochfrequenz/ebdamame/issues/71")
except TableNotFoundError:
# https://github.com/Hochfrequenz/ebdamame/issues/9
pass # ignore for now
Expand All @@ -284,6 +296,8 @@ def test_extraction(self, datafiles, get_ebd_keys_and_files: List[Tuple[str, str
case "The cell content 'gültiges daten-ergebnis' does not belong to a ja/nein cell":
# https://github.com/Hochfrequenz/ebdamame/issues/74
issue_number = "74"
case "No cell containing a valid step number found.":
issue_number = "to be added"
case _:
raise
error_msg = f"Error while scraping '{ebd_key}' (#{issue_number}): {value_error}"
Expand Down

0 comments on commit 2f826c3

Please sign in to comment.