Add data_logging/tests/test_core.py

This commit is contained in:
Mika 2026-05-03 02:07:40 +00:00
parent ae231f852d
commit 8f9e251ee5

View file

@ -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}])