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)
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""Shared fixtures: canned Glutz API data and a fake rpc()."""
|
||||
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
import glutz_api
|
||||
from extend_pins import main
|
||||
|
||||
USERS = [
|
||||
{"id": 50, "label": "Graf Markus 225313", "class": 1}, # wyby booking user
|
||||
{"id": 42, "label": "Markus", "class": 1}, # manual user -> excluded
|
||||
{"id": 33, "label": "Forum", "class": 1}, # manual user -> excluded
|
||||
{"id": 12, "label": "Gr. Gemeinschaftsraum Hueb. 8a", "class": 2}, # group
|
||||
]
|
||||
|
||||
FUTURE_FROM = "2099-02-18T17:45:00.000"
|
||||
FUTURE_TO = "2099-02-20T22:15:00.000"
|
||||
|
||||
|
||||
def code_row(**overrides):
|
||||
"""A wyby code row: booking Fri 18:00 - Sun 22:00 with +/-15 min window."""
|
||||
row = {
|
||||
"id": 77,
|
||||
"userId": 50,
|
||||
"code": "225313",
|
||||
"description": "reservation",
|
||||
"actionProfileId": "2",
|
||||
"validFrom": FUTURE_FROM,
|
||||
"validTo": FUTURE_TO,
|
||||
}
|
||||
row.update(overrides)
|
||||
return row
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def install_rpc(monkeypatch):
|
||||
"""Install a fake glutz_api.rpc; returns (calls, set_calls) lists."""
|
||||
calls = []
|
||||
|
||||
def _install(users=USERS, codes=None, write_result=None, write_error=None):
|
||||
codes = [] if codes is None else codes
|
||||
|
||||
def fake_rpc(method, params):
|
||||
calls.append((method, params))
|
||||
if method == "eAccess.getModel":
|
||||
return {"UsersGroups": users, "Codes": codes}[params[0]]
|
||||
if write_error is not None:
|
||||
raise SystemExit(write_error)
|
||||
return {"id": "99"} if write_result is None else write_result
|
||||
|
||||
monkeypatch.setattr(glutz_api, "rpc", fake_rpc)
|
||||
return calls
|
||||
|
||||
return _install
|
||||
|
||||
|
||||
def run_main(monkeypatch, *argv):
|
||||
monkeypatch.setattr(sys, "argv", ["extend_pins.py", *argv])
|
||||
main()
|
||||
@@ -0,0 +1,198 @@
|
||||
"""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
|
||||
@@ -0,0 +1,31 @@
|
||||
"""Tests for glutz_api.load_env (rpc() itself needs a live system)."""
|
||||
|
||||
import os
|
||||
|
||||
import glutz_api
|
||||
|
||||
|
||||
def test_load_env_reads_keys(tmp_path, monkeypatch):
|
||||
monkeypatch.delenv("GLUTZ_TEST_KEY", raising=False)
|
||||
monkeypatch.delenv("GLUTZ_TEST_QUOTED", raising=False)
|
||||
env = tmp_path / ".env"
|
||||
env.write_text('# comment\n\nGLUTZ_TEST_KEY=plain\nGLUTZ_TEST_QUOTED="in quotes"\n')
|
||||
|
||||
glutz_api.load_env(env)
|
||||
|
||||
assert os.environ["GLUTZ_TEST_KEY"] == "plain"
|
||||
assert os.environ["GLUTZ_TEST_QUOTED"] == "in quotes"
|
||||
|
||||
|
||||
def test_load_env_does_not_override_existing(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("GLUTZ_TEST_KEY", "from-env")
|
||||
env = tmp_path / ".env"
|
||||
env.write_text("GLUTZ_TEST_KEY=from-file\n")
|
||||
|
||||
glutz_api.load_env(env)
|
||||
|
||||
assert os.environ["GLUTZ_TEST_KEY"] == "from-env"
|
||||
|
||||
|
||||
def test_load_env_missing_file_is_a_noop(tmp_path):
|
||||
glutz_api.load_env(tmp_path / "does-not-exist") # must not raise
|
||||
Reference in New Issue
Block a user