import pytest import tempfile from pathlib import Path from log_handler import core @pytest.fixture def sample_log_file(tmp_path: Path) -> Path: sample_content = ("""2024-05-01 23:45:01, Temp=14.5C, SensorTemp=36.2C\n""" "2024-05-01 23:50:01, Temp=14.7C, SensorTemp=36.4C\n""" "Irrelevante Zeile ohne Daten\n""" "2024-05-01 23:55:01, Temp=14.8C, SensorTemp=36.6C\n") file_path = tmp_path / "sample_log.log" file_path.write_text(sample_content) return file_path def test_parse_log_file_returns_list(sample_log_file: Path): result = core.parse_log_file(str(sample_log_file)) assert isinstance(result, list) assert all(hasattr(r, "timestamp") for r in result) assert all(hasattr(r, "temperature") for r in result) assert all(hasattr(r, "sensor_temp") for r in result) def test_parse_log_file_correct_values(sample_log_file: Path): result = core.parse_log_file(str(sample_log_file)) assert len(result) == 3 first = result[0] assert first.timestamp.startswith("2024-05-01 23:45") assert 10.0 < first.temperature < 20.0 assert 30.0 < first.sensor_temp < 40.0 def test_parse_log_file_handles_invalid_lines(tmp_path: Path): corrupted_file = tmp_path / "bad.log" corrupted_file.write_text("no valid data here\n") result = core.parse_log_file(str(corrupted_file)) assert isinstance(result, list) assert result == [] def test_parse_log_file_invalid_input_type(): with pytest.raises((TypeError, FileNotFoundError)): core.parse_log_file(None) # type: ignore def test_parse_log_file_nonexistent_file(): with pytest.raises(FileNotFoundError): core.parse_log_file("./does_not_exist.log") def test_data_integrity_fields(sample_log_file: Path): result = core.parse_log_file(str(sample_log_file)) for entry in result: assert isinstance(entry.timestamp, str) assert isinstance(entry.temperature, float) assert isinstance(entry.sensor_temp, float) def test_parse_log_file_output_consistency(sample_log_file: Path): result1 = core.parse_log_file(str(sample_log_file)) result2 = core.parse_log_file(str(sample_log_file)) assert result1 == result2