Add unknown_case_counter/tests/test_core.py
This commit is contained in:
parent
586dc6d1b2
commit
48d240a307
1 changed files with 74 additions and 0 deletions
74
unknown_case_counter/tests/test_core.py
Normal file
74
unknown_case_counter/tests/test_core.py
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import csv
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
|
||||
from src.unknown_case_counter import core
|
||||
|
||||
@pytest.fixture
|
||||
def sample_csv(tmp_path: Path) -> Path:
|
||||
data = [
|
||||
["case_id", "previous_status", "new_status", "unknown_reason"],
|
||||
["1", "PASS", "Unknown", "timeout"],
|
||||
["2", "FAIL", "Unknown", "schema_error"],
|
||||
["3", "FAIL", "PASS", ""],
|
||||
["4", "PASS", "Unknown", "timeout"],
|
||||
["5", "Unknown", "Unknown", "corrupt_data"],
|
||||
["6", "PASS", "FAIL", ""],
|
||||
]
|
||||
file_path = tmp_path / "delta_cases.csv"
|
||||
with open(file_path, "w", newline="", encoding="utf-8") as f:
|
||||
writer = csv.writer(f)
|
||||
writer.writerows(data)
|
||||
return file_path
|
||||
|
||||
|
||||
def test_count_unknown_reasons_basic(sample_csv):
|
||||
result = core.count_unknown_reasons(str(sample_csv))
|
||||
assert isinstance(result, dict)
|
||||
expected_keys = {"timeout", "schema_error", "corrupt_data"}
|
||||
assert expected_keys == set(result.keys())
|
||||
assert result["timeout"] == 2
|
||||
assert result["schema_error"] == 1
|
||||
assert result["corrupt_data"] == 1
|
||||
|
||||
|
||||
def test_empty_file(tmp_path):
|
||||
file_path = tmp_path / "empty.csv"
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write("case_id,previous_status,new_status,unknown_reason\n")
|
||||
result = core.count_unknown_reasons(str(file_path))
|
||||
assert result == {}
|
||||
|
||||
|
||||
def test_no_unknown_rows(tmp_path):
|
||||
data = [
|
||||
["case_id", "previous_status", "new_status", "unknown_reason"],
|
||||
["1", "PASS", "FAIL", ""],
|
||||
["2", "FAIL", "PASS", ""],
|
||||
]
|
||||
file_path = tmp_path / "cases.csv"
|
||||
with open(file_path, "w", newline="", encoding="utf-8") as f:
|
||||
writer = csv.writer(f)
|
||||
writer.writerows(data)
|
||||
result = core.count_unknown_reasons(str(file_path))
|
||||
assert result == {}
|
||||
|
||||
|
||||
def test_invalid_file_path_raises(tmp_path):
|
||||
invalid = tmp_path / "not_exists.csv"
|
||||
with pytest.raises(FileNotFoundError):
|
||||
core.count_unknown_reasons(str(invalid))
|
||||
|
||||
|
||||
def test_invalid_data_missing_columns(tmp_path):
|
||||
file_path = tmp_path / "bad.csv"
|
||||
with open(file_path, "w", newline="", encoding="utf-8") as f:
|
||||
f.write("case_id,new_status\n1,Unknown\n")
|
||||
# Should handle missing columns gracefully, returning empty dict or raising ValueError
|
||||
try:
|
||||
result = core.count_unknown_reasons(str(file_path))
|
||||
assert isinstance(result, dict)
|
||||
except ValueError:
|
||||
pytest.skip("Function validates input strictly and raises on missing columns.")
|
||||
Loading…
Reference in a new issue