Files
karung-counting-feedmill-se…/tests/test_logger.py
T

60 lines
2.0 KiB
Python

"""Smoke tests for src/logger.py (CSVLogger) — stdlib only, no flask/cv2."""
import csv
from src.batch import BatchRecord
from src.logger import CSVLogger
def test_log_batch_and_log_event_write_csvs(tmp_path):
logger = CSVLogger(str(tmp_path))
rec = BatchRecord(
batch_id=7,
start_time=1700000000.0,
end_time=1700000060.0,
loading_count=5,
unloading_count=2,
box_loading_count=1,
box_unloading_count=0,
)
logger.log_batch(rec)
logger.log_event(7, 123, "unloading", 1700000030.0, "sack")
batch_csv = tmp_path / "batch_summary.csv"
event_csv = tmp_path / "sack_events.csv"
assert batch_csv.exists() and batch_csv.stat().st_size > 0
assert event_csv.exists() and event_csv.stat().st_size > 0
with open(batch_csv, newline="") as f:
rows = list(csv.reader(f))
assert rows[0][0] == "batch_id"
data = rows[1]
assert data[0] == "7"
assert data[3] == "60.0" # duration_s
assert data[4] == "5" # loading
assert data[5] == "2" # unloading
assert data[6] == "3" # net
assert data[7] == "1" # box_loading
with open(event_csv, newline="") as f:
erows = list(csv.reader(f))
assert erows[0][:5] == ["timestamp", "batch_id", "event", "track_id", "direction"]
ev = erows[1]
assert ev[1] == "7"
assert ev[2] == "sack_crossed"
assert ev[3] == "T-0123"
assert ev[4] == "unloading"
def test_logger_appends_rows_and_creates_nested_dir(tmp_path):
nested = tmp_path / "a" / "b"
logger = CSVLogger(str(nested))
logger.log_batch(BatchRecord(batch_id=1, start_time=0.0, end_time=1.0,
loading_count=1, unloading_count=0))
logger.log_batch(BatchRecord(batch_id=2, start_time=0.0, end_time=1.0,
loading_count=2, unloading_count=0))
with open(nested / "batch_summary.csv", newline="") as f:
rows = list(csv.reader(f))
# header written once, both batches appended
assert [r[0] for r in rows[1:]] == ["1", "2"]