38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
|
|
import os
|
||
|
|
import pytest
|
||
|
|
from unittest.mock import patch, mock_open
|
||
|
|
from archive.test_errorrate_runner import generate_error_rate_plot
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def mock_data_dir(tmp_path):
|
||
|
|
"""Fixture to create a temporary data directory."""
|
||
|
|
data_dir = tmp_path / "data"
|
||
|
|
data_dir.mkdir()
|
||
|
|
return data_dir
|
||
|
|
|
||
|
|
@patch("builtins.open", new_callable=mock_open, read_data="{}")
|
||
|
|
@patch("os.path.exists", return_value=True)
|
||
|
|
def test_generate_error_rate_plot_no_data(mock_exists, mock_open, mock_data_dir):
|
||
|
|
"""Test generate_error_rate_plot with no data."""
|
||
|
|
plot_path, summary = generate_error_rate_plot(str(mock_data_dir / "applications.json"))
|
||
|
|
assert plot_path is None
|
||
|
|
assert summary == ""
|
||
|
|
|
||
|
|
@patch("builtins.open", new_callable=mock_open)
|
||
|
|
@patch("os.path.exists", return_value=True)
|
||
|
|
@patch("matplotlib.pyplot.savefig")
|
||
|
|
def test_generate_error_rate_plot_with_data(mock_savefig, mock_exists, mock_open, mock_data_dir):
|
||
|
|
"""Test generate_error_rate_plot with valid data."""
|
||
|
|
mock_open.return_value.read.return_value = """
|
||
|
|
{
|
||
|
|
"1": {"timestamp": "2025-12-25T12:00:00", "company": "CompanyA", "success": true},
|
||
|
|
"2": {"timestamp": "2025-12-26T12:00:00", "company": "CompanyB", "success": false}
|
||
|
|
}
|
||
|
|
"""
|
||
|
|
plot_path, summary = generate_error_rate_plot(str(mock_data_dir / "applications.json"))
|
||
|
|
assert plot_path is not None
|
||
|
|
assert "Total attempts" in summary
|
||
|
|
assert "Successes" in summary
|
||
|
|
assert "Failures" in summary
|
||
|
|
assert "Overall error rate" in summary
|
||
|
|
mock_savefig.assert_called_once()
|