From 8f9e251ee5bbbca317cd7961b51e8abfae408ccd Mon Sep 17 00:00:00 2001 From: Mika Date: Sun, 3 May 2026 02:07:40 +0000 Subject: [PATCH] Add data_logging/tests/test_core.py --- data_logging/tests/test_core.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 data_logging/tests/test_core.py diff --git a/data_logging/tests/test_core.py b/data_logging/tests/test_core.py new file mode 100644 index 0000000..63f7ea6 --- /dev/null +++ b/data_logging/tests/test_core.py @@ -0,0 +1,33 @@ +import pytest +from pathlib import Path +import pandas as pd +from data_logging import core + +@pytest.fixture +def log_entries(): + return [ + core.LogEntry(timestamp="2024-06-01T12:00:00Z", water_level=1.23, ground_vibration=0.05, ai_label="steady"), + core.LogEntry(timestamp="2024-06-01T12:01:00Z", water_level=1.45, ground_vibration=0.07, ai_label="periodic"), + ] + + +def test_export_to_csv_creates_file(tmp_path: Path, log_entries): + csv_path = core.export_to_csv(log_entries) + assert isinstance(csv_path, str) + output_path = Path(csv_path) + assert output_path.exists(), "CSV file was not created." + + +def test_export_to_csv_content(log_entries, tmp_path: Path): + csv_path = core.export_to_csv(log_entries) + df = pd.read_csv(csv_path) + assert not df.empty + assert list(df.columns) == ["timestamp", "water_level", "ground_vibration", "ai_label"] + assert len(df) == 2 + assert df.iloc[0]["ai_label"] == "steady" + assert pytest.approx(df.iloc[1]["water_level"], 0.001) == 1.45 + + +def test_export_to_csv_invalid_input_raises(tmp_path: Path): + with pytest.raises((TypeError, AttributeError)): + core.export_to_csv([{"timestamp": "bad", "wrong": 123}])