Add logger_setup/tests/test_core.py
This commit is contained in:
parent
7eb3280169
commit
b4f13031c8
1 changed files with 76 additions and 0 deletions
76
logger_setup/tests/test_core.py
Normal file
76
logger_setup/tests/test_core.py
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
import csv
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import time
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
import src.logger_setup.core as core
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def sample_reading():
|
||||||
|
ts = datetime.now()
|
||||||
|
return core.SensorReading(
|
||||||
|
timestamp=ts,
|
||||||
|
temperature=22.5,
|
||||||
|
humidity=55.0,
|
||||||
|
pressure=1013.25,
|
||||||
|
lux=150.0,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_sensor_reading_to_dict_contains_expected_keys(sample_reading):
|
||||||
|
data = sample_reading.to_dict()
|
||||||
|
expected_keys = {"timestamp", "temperature", "humidity", "pressure", "lux"}
|
||||||
|
assert expected_keys.issubset(set(data.keys())), "All expected keys should be present."
|
||||||
|
assert isinstance(data["timestamp"], str) or isinstance(data["timestamp"], datetime)
|
||||||
|
|
||||||
|
|
||||||
|
def test_save_to_csv_creates_valid_file(sample_reading):
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
csv_path = os.path.join(tmpdir, "sensor_log.csv")
|
||||||
|
# simulate data list like global buffer
|
||||||
|
if hasattr(core, "_data_buffer"):
|
||||||
|
core._data_buffer.clear()
|
||||||
|
core._data_buffer.append(sample_reading)
|
||||||
|
else:
|
||||||
|
# fallback for minimal implementation
|
||||||
|
core._data_buffer = [sample_reading]
|
||||||
|
core.save_to_csv(csv_path)
|
||||||
|
assert os.path.exists(csv_path)
|
||||||
|
with open(csv_path, newline="") as csvfile:
|
||||||
|
reader = csv.DictReader(csvfile)
|
||||||
|
rows = list(reader)
|
||||||
|
assert len(rows) == 1
|
||||||
|
row = rows[0]
|
||||||
|
assert float(row["temperature_c"]) == pytest.approx(22.5)
|
||||||
|
assert float(row["humidity_percent"]) == pytest.approx(55.0)
|
||||||
|
assert float(row["pressure_hpa"]) == pytest.approx(1013.25)
|
||||||
|
assert float(row["lux"]) == pytest.approx(150.0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_logging_runs_for_short_interval(monkeypatch):
|
||||||
|
calls = []
|
||||||
|
|
||||||
|
def fake_reading():
|
||||||
|
return core.SensorReading(datetime.now(), 20.0, 50.0, 1012.0, 100.0)
|
||||||
|
|
||||||
|
monkeypatch.setattr(core, "_read_sensors", fake_reading)
|
||||||
|
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
csv_path = os.path.join(tmpdir, "sensor_log.csv")
|
||||||
|
|
||||||
|
def limited_sleep(duration):
|
||||||
|
calls.append(duration)
|
||||||
|
if len(calls) > 1:
|
||||||
|
raise KeyboardInterrupt
|
||||||
|
|
||||||
|
monkeypatch.setattr(time, "sleep", limited_sleep)
|
||||||
|
try:
|
||||||
|
core.start_logging(interval=0.1)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
pass
|
||||||
|
|
||||||
|
assert len(calls) >= 1, "start_logging should perform at least one loop iteration."
|
||||||
Loading…
Reference in a new issue