diff --git a/unknown_case_counter/tests/test_core.py b/unknown_case_counter/tests/test_core.py new file mode 100644 index 0000000..0488fa4 --- /dev/null +++ b/unknown_case_counter/tests/test_core.py @@ -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.") \ No newline at end of file