Files
glutz-maintenances/tests/test_extend_pins.py
T
gurix d2a612a37e Add pytest suite, uv project setup and ruff compliance
- pyproject.toml: dev deps via uv (pytest, ruff), pytest and ruff config
- tests/: 24 tests for extend_pins (helpers + mocked main flow) and
  glutz_api.load_env; no network access
- translate source code to English per AGENT.md
- fix ruff findings (unused import, max() simplification, shebang)
2026-10-23 08:49:49 +02:00

199 lines
6.9 KiB
Python

"""Unit tests for extend_pins.py (helpers and main() flow, rpc mocked)."""
from datetime import datetime
import pytest
from conftest import USERS, code_row, run_main
import glutz_api
from extend_pins import fmt_ts, full_day_window, parse_ts, wyby_users
@pytest.fixture(autouse=True)
def no_env(monkeypatch):
"""Never touch the real .env / environment from tests."""
monkeypatch.setattr(glutz_api, "load_env", lambda *a, **k: None)
# --- helpers ---------------------------------------------------------------
class TestWybyUsers:
def test_detects_label_ending_in_six_digits(self):
assert wyby_users(USERS) == {str(u["id"]): u["label"] for u in USERS[:1]}
def test_ignores_single_digit_labels_and_groups(self):
users = [
{"id": 1, "label": "Hueb 8a - Gast 1", "class": 1},
{"id": 2, "label": "Unused Media", "class": 3},
{"id": 3, "label": None, "class": 1},
]
assert wyby_users(users) == {}
class TestParseTs:
def test_parses_milliseconds(self):
assert parse_ts("2026-10-24T17:45:00.000") == datetime(2026, 10, 24, 17, 45)
@pytest.mark.parametrize("raw", [None, "", "not-a-date"])
def test_returns_none_for_missing_or_invalid(self, raw):
assert parse_ts(raw) is None
def test_fmt_ts_roundtrip(self):
dt = datetime(2099, 2, 18, 0, 0, 0)
assert parse_ts(fmt_ts(dt)) == dt
class TestFullDayWindow:
def test_single_day_booking(self):
vf, vt = (
parse_ts("2099-02-18T17:45:00.000"),
parse_ts("2099-02-18T22:15:00.000"),
)
assert full_day_window(vf, vt, 15) == (
datetime(2099, 2, 18, 0, 0),
datetime(2099, 2, 18, 23, 59, 59),
)
def test_multi_day_booking(self):
vf, vt = (
parse_ts("2099-02-18T17:45:00.000"),
parse_ts("2099-02-20T22:15:00.000"),
)
assert full_day_window(vf, vt, 15) == (
datetime(2099, 2, 18, 0, 0),
datetime(2099, 2, 20, 23, 59, 59),
)
def test_start_at_midnight_buffer_crosses_back(self):
# Booking starts 00:00 -> wyby window reaches to previous day 23:45.
vf, vt = (
parse_ts("2099-02-17T23:45:00.000"),
parse_ts("2099-02-18T20:15:00.000"),
)
start, _ = full_day_window(vf, vt, 15)
assert start == datetime(2099, 2, 18, 0, 0)
def test_end_shortly_after_midnight_buffer_crosses_forward(self):
# Booking ends 23:50 -> wyby window reaches into next day 00:05.
vf, vt = (
parse_ts("2099-02-18T16:45:00.000"),
parse_ts("2099-02-19T00:05:00.000"),
)
_, end = full_day_window(vf, vt, 15)
assert end == datetime(2099, 2, 18, 23, 59, 59)
def test_end_before_start_is_clamped(self):
vf, vt = parse_ts("2099-02-18T10:00:00"), parse_ts("2099-02-18T10:01:00")
start, end = full_day_window(vf, vt, 15)
assert (start, end) == (
datetime(2099, 2, 18, 0, 0),
datetime(2099, 2, 18, 23, 59, 59),
)
def test_zero_buffer_falls_back_to_plain_dates(self):
vf, vt = parse_ts("2099-02-18T00:00:00"), parse_ts("2099-02-19T23:59:59")
assert full_day_window(vf, vt, 0) == (
datetime(2099, 2, 18, 0, 0),
datetime(2099, 2, 19, 23, 59, 59),
)
# --- main() flow -----------------------------------------------------------
class TestMainDryRun:
def test_plans_but_writes_nothing(self, install_rpc, monkeypatch, capsys):
calls = install_rpc(codes=[code_row()])
run_main(monkeypatch)
out = capsys.readouterr().out
assert not any(m == "eAccess.setModel" for m, _ in calls)
assert "DRY-RUN" in out
assert "PLAN" in out
assert "2099-02-18T00:00:00" in out
assert "2099-02-20T23:59:59" in out
def test_no_wyby_users_nothing_to_do(self, install_rpc, monkeypatch, capsys):
install_rpc(codes=[code_row(userId=42)])
run_main(monkeypatch)
out = capsys.readouterr().out
assert "No changes needed" in out
assert "PLAN" not in out
class TestMainApply:
def test_writes_update_with_preserved_fields(
self, install_rpc, monkeypatch, capsys
):
calls = install_rpc(codes=[code_row()])
run_main(monkeypatch, "--apply")
writes = [p for m, p in calls if m == "eAccess.setModel"]
assert len(writes) == 1
collection, data = writes[0]
assert collection == "Codes"
assert data == {
"id": "77",
"userId": "50",
"code": "225313",
"description": "reservation",
"actionProfileId": "2",
"validFrom": "2099-02-18T00:00:00",
"validTo": "2099-02-20T23:59:59",
}
assert "1/1 changed" in capsys.readouterr().out
def test_action_profile_defaults_to_1_when_missing(self, install_rpc, monkeypatch):
calls = install_rpc(codes=[code_row(actionProfileId=None)])
run_main(monkeypatch, "--apply")
_, data = next(p for m, p in calls if m == "eAccess.setModel")
assert data["actionProfileId"] == "1"
def test_skip_reasons(self, install_rpc, monkeypatch, capsys):
codes = [
code_row(id=1), # planned
code_row(id=2, userId=50, code="111111", validFrom=None, validTo=None),
code_row(
id=3,
validFrom="2099-03-01T00:00:00.000",
validTo="2099-03-01T23:59:59.000",
),
code_row(
id=4, validFrom="2020-01-01T17:45:00", validTo="2020-01-01T22:15:00"
),
]
calls = install_rpc(codes=codes)
run_main(monkeypatch, "--apply")
out = capsys.readouterr().out
writes = [p for m, p in calls if m == "eAccess.setModel"]
assert len(writes) == 1
assert writes[0][1]["id"] == "1"
assert "no validity window" in out
assert "already full-day" in out
assert "already expired" in out
def test_only_code_filter(self, install_rpc, monkeypatch, capsys):
codes = [code_row(id=1, code="225313"), code_row(id=2, code="999999")]
calls = install_rpc(codes=codes)
run_main(monkeypatch, "--apply", "--only-code", "999999")
writes = [p for m, p in calls if m == "eAccess.setModel"]
assert len(writes) == 1
assert writes[0][1]["code"] == "999999"
def test_write_result_without_id_counts_as_failure(self, install_rpc, monkeypatch):
install_rpc(codes=[code_row()], write_result={})
with pytest.raises(SystemExit) as excinfo:
run_main(monkeypatch, "--apply")
assert excinfo.value.code == 1
def test_rpc_error_during_write_exits_nonzero(self, install_rpc, monkeypatch):
install_rpc(codes=[code_row()], write_error="HTTP 500: boom")
with pytest.raises(SystemExit) as excinfo:
run_main(monkeypatch, "--apply")
assert excinfo.value.code == 1