wohnbot/tests/test_error_rate_plot.py

51 lines
No EOL
1.7 KiB
Python

import os
import sys
from pathlib import Path
import pytest
from unittest.mock import patch, mock_open, MagicMock
sys.path.append(str(Path(__file__).parent.parent))
from application_handler import ApplicationHandler
@pytest.fixture
def temp_applications_file(tmp_path):
data_dir = tmp_path / "data"
data_dir.mkdir()
file = data_dir / "applications.json"
file.write_text("{}", encoding="utf-8")
return file
class DummyStateManager:
email = None
password = None
logged_in = False
def set_autopilot(self, enabled): pass
def is_autopilot_enabled(self): return False
@patch("matplotlib.pyplot.savefig")
def test_generate_error_rate_plot_no_data(mock_savefig, temp_applications_file):
handler = ApplicationHandler(None, DummyStateManager(), applications_file=temp_applications_file)
plot_path, summary = handler._generate_error_rate_plot()
assert plot_path is None or plot_path == ""
assert summary == ""
@patch("matplotlib.pyplot.savefig")
def test_generate_error_rate_plot_with_data(mock_savefig, temp_applications_file):
handler = ApplicationHandler(None, DummyStateManager(), applications_file=temp_applications_file)
# Write valid data to the temp applications file
temp_applications_file.write_text('''
{
"1": {"timestamp": "2025-12-25T12:00:00", "company": "CompanyA", "success": true},
"2": {"timestamp": "2025-12-26T12:00:00", "company": "CompanyB", "success": false}
}
''', encoding="utf-8")
plot_path, summary = handler._generate_error_rate_plot()
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()