Skip to content

Commit

Permalink
Fix tests to minimize false positives
Browse files Browse the repository at this point in the history
  • Loading branch information
vladistan committed Aug 3, 2024
1 parent 96ca895 commit 40d4f60
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions tests/test_parse/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
asdf
"""

import itertools
import os
import pathlib
from pathlib import Path
Expand Down Expand Up @@ -41,50 +42,61 @@ def save_expected_results(energy_source):
)


@pytest.mark.parametrize("data_file_name", os.listdir(_ROOT_DIR / "data" / "electricity"))
def test_parse_electricity_feed(data_file_name):
def check_file(data_file: Path):
"""
Verify that parsing an electricity XML file works as intended.
Compares the string form of a parsed XML to a saved text file.
"""
data_path = _ROOT_DIR / "data" / "electricity"
expected_results_path = _ROOT_DIR / "expected_results" / "electricity"

atom_forest = parse.parse_feed(data_path / data_file_name)
atom_forest = parse.parse_feed(str(data_file))
parsed_feed_representation = parse.parse_feed_representation(atom_forest)
result_file_name = data_file_name.strip("xml") + "txt"
expected_results_file = expected_results_path / result_file_name
with open(expected_results_file) as f:
expected_result_file_name = (
data_file.parent.parent.parent
/ "expected_results"
/ data_file.parent.name
/ data_file.with_suffix(".txt").name
)
with open(expected_result_file_name) as f:
expected_lines = map(str.strip, f)
result_lines = map(str.strip, parsed_feed_representation.splitlines())

sentinel = object()
for line_num, (expected_line, result_line) in enumerate(
zip(f, parsed_feed_representation.splitlines()), start=1
itertools.zip_longest(expected_lines, result_lines, fillvalue=sentinel), start=1
):
assert expected_line.strip("\n") == result_line.strip(
"\n"
), f"Mismatch in file {data_file_name} at line {line_num}"
if expected_line == "":
continue
if result_line == "":
continue
if expected_line is sentinel:
assert False, f"Expected results ended before parsed results in file {data_file} {line_num}"
elif result_line is sentinel:
assert False, f"Parsed results ended before expected results in file {data_file} {line_num}"
else:
assert expected_line == result_line, f"Mismatch in file {data_file} at line {line_num}"


@pytest.mark.parametrize("data_file_name", map(str, (_ROOT_DIR / "data" / "electricity").iterdir()))
def test_parse_electricity_feed(data_file_name):
"""
Verify that parsing an electricity XML files works as intended.
"""
check_file(Path(data_file_name))


@pytest.mark.parametrize("data_file_name", os.listdir(_ROOT_DIR / "data" / "natural_gas"))
@pytest.mark.parametrize("data_file_name", map(str, (_ROOT_DIR / "data" / "natural_gas").iterdir()))
def test_parse_natural_gas_feed(data_file_name):
"""
Verify that parsing a natural gas XML file works as intended.
Compares the string form of a parsed XML to a saved text file.
"""
data_path = _ROOT_DIR / "data" / "natural_gas"
expected_results_path = _ROOT_DIR / "expected_results" / "natural_gas"

parsed_feed = parse.parse_feed(data_path / data_file_name)
parsed_feed_representation = parse.parse_feed_representation(parsed_feed)
result_file_name = data_file_name.strip("xml") + "txt"
expected_results_file = expected_results_path / result_file_name
with open(expected_results_file) as f:
for line_num, (expected_line, result_line) in enumerate(
zip(f, parsed_feed_representation.splitlines()), start=1
):
assert expected_line.strip("\n") == result_line.strip(
"\n"
), f"Mismatch in file {data_file_name} at line {line_num}"
check_file(Path(data_file_name))


@pytest.mark.parametrize("energy_source", ["electricity", "natural_gas"])
def test_quick(energy_source):
files = (_ROOT_DIR / "data" / energy_source).iterdir()
data_file_name = next(files)
check_file(data_file_name)
data_file_name = next(files)
check_file(data_file_name)


if __name__ == "__main__":
Expand Down

0 comments on commit 40d4f60

Please sign in to comment.