2026-11-12 11:53:41 +01:00
|
|
|
"""Tests for wyby_sync: planning logic with mocked Glutz API + WybyClient."""
|
|
|
|
|
|
2026-11-13 13:33:41 +01:00
|
|
|
from datetime import date, timedelta
|
2026-11-12 11:53:41 +01:00
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
from conftest import USERS, code_row
|
|
|
|
|
|
|
|
|
|
from wyby_client import Booking
|
|
|
|
|
from wyby_sync import Sync, parse_object_map
|
|
|
|
|
|
|
|
|
|
OBJ_ID = "34a24dc9-1111-1111-1111-111111111111"
|
|
|
|
|
GROUP = {"id": 12, "label": "Gr. Gemeinschaftsraum Hueb. 8a", "class": 2}
|
|
|
|
|
WYBY_USER = {"id": 55, "label": "Tsechung Jangchup 237251", "class": 1}
|
|
|
|
|
FUTURE = date.today().year + 1 # keep fixtures in the future
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def booking(
|
|
|
|
|
number="002237251",
|
|
|
|
|
pin="237251",
|
|
|
|
|
start=date(FUTURE, 12, 27),
|
|
|
|
|
end=date(FUTURE, 12, 27),
|
|
|
|
|
):
|
|
|
|
|
return Booking(
|
|
|
|
|
number=number,
|
|
|
|
|
pin=pin,
|
|
|
|
|
start_date=start,
|
|
|
|
|
end_date=end,
|
|
|
|
|
label=f"Tsechung Jangchup {pin}",
|
|
|
|
|
object_id=OBJ_ID,
|
|
|
|
|
object_name="Gemeinschaftsraum Huebwis",
|
|
|
|
|
customer="Jangchup Tsechung",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def sync():
|
|
|
|
|
s = Sync(
|
|
|
|
|
apply=False,
|
|
|
|
|
horizon_days=90,
|
|
|
|
|
object_map=[("34a24dc9", "Gr. Gemeinschaftsraum Hueb. 8a")],
|
|
|
|
|
profile_id="2",
|
|
|
|
|
)
|
|
|
|
|
s.users = USERS + [GROUP, WYBY_USER]
|
|
|
|
|
s.codes = []
|
|
|
|
|
s.relations = []
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestParseObjectMap:
|
|
|
|
|
def test_parse(self):
|
|
|
|
|
assert parse_object_map("34a24dc9=Gr. Hueb, 8b1f91ff=Gr. Langäri") == [
|
|
|
|
|
("34a24dc9", "Gr. Hueb"),
|
|
|
|
|
("8b1f91ff", "Gr. Langäri"),
|
|
|
|
|
]
|
|
|
|
|
assert parse_object_map("") == []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestPlanBooking:
|
|
|
|
|
def test_new_booking_plans_relation_and_code(self, sync):
|
|
|
|
|
sync.plan_booking(booking(), "12", date.today())
|
|
|
|
|
planned = [(c, d) for _, c, d in sync.planned]
|
|
|
|
|
assert ("UserGroupRelations", {"parentId": "12", "childId": "55"}) in planned
|
|
|
|
|
codes = [d for c, d in planned if c == "Codes"]
|
|
|
|
|
assert codes == [
|
|
|
|
|
{
|
|
|
|
|
"userId": "55",
|
|
|
|
|
"code": "237251",
|
|
|
|
|
"description": "wyby 002237251",
|
|
|
|
|
"actionProfileId": "2",
|
|
|
|
|
"validFrom": f"{FUTURE}-12-27T00:00:00",
|
|
|
|
|
"validTo": f"{FUTURE}-12-27T23:59:59",
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
assert not sync.skipped or all("already" not in s for s in sync.skipped)
|
|
|
|
|
|
|
|
|
|
def test_expired_booking_skipped(self, sync):
|
|
|
|
|
sync.plan_booking(
|
|
|
|
|
booking(start=date(2020, 1, 1), end=date(2020, 1, 1)), "12", date.today()
|
|
|
|
|
)
|
|
|
|
|
assert not sync.planned
|
|
|
|
|
assert any("expired" in s for s in sync.skipped)
|
|
|
|
|
|
|
|
|
|
def test_already_full_day_skipped(self, sync):
|
|
|
|
|
sync.codes = [
|
|
|
|
|
code_row(
|
|
|
|
|
id=99,
|
|
|
|
|
userId=55,
|
|
|
|
|
code="237251",
|
|
|
|
|
validFrom=f"{FUTURE}-12-27T00:00:00.000",
|
|
|
|
|
validTo=f"{FUTURE}-12-27T23:59:59.000",
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
sync.relations = [{"parentId": "12", "childId": "55"}]
|
|
|
|
|
sync.plan_booking(booking(), "12", date.today())
|
|
|
|
|
assert not sync.planned
|
|
|
|
|
assert any("already full-day" in s for s in sync.skipped)
|
|
|
|
|
|
|
|
|
|
def test_narrow_window_updates_existing_code(self, sync):
|
|
|
|
|
sync.codes = [
|
|
|
|
|
code_row(
|
|
|
|
|
id=99,
|
|
|
|
|
userId=55,
|
|
|
|
|
code="237251",
|
|
|
|
|
validFrom=f"{FUTURE}-12-27T12:45:00.000",
|
|
|
|
|
validTo=f"{FUTURE}-12-27T17:15:00.000",
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
sync.relations = [{"parentId": "12", "childId": "55"}]
|
|
|
|
|
sync.plan_booking(booking(), "12", date.today())
|
|
|
|
|
assert len(sync.planned) == 1
|
|
|
|
|
_ctx, collection, data = sync.planned[0]
|
|
|
|
|
assert collection == "Codes"
|
|
|
|
|
assert data["id"] == "99"
|
|
|
|
|
assert data["validFrom"] == f"{FUTURE}-12-27T00:00:00"
|
|
|
|
|
assert data["validTo"] == f"{FUTURE}-12-27T23:59:59"
|
|
|
|
|
assert data["actionProfileId"] == "2" # preserved
|
|
|
|
|
|
|
|
|
|
def test_unknown_user_plans_creation_first(self, sync):
|
|
|
|
|
sync.plan_booking(booking(pin="999999", number="002299999"), "12", date.today())
|
|
|
|
|
creations = [d for _, c, d in sync.planned if c == "UsersGroups"]
|
|
|
|
|
assert creations == [{"label": "Tsechung Jangchup 999999", "class": "1"}]
|
|
|
|
|
# no code planned in the same pass (user id unknown until created)
|
|
|
|
|
assert not [d for _, c, d in sync.planned if c == "Codes"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRun:
|
|
|
|
|
def test_run_reports_failure_and_returns_false(self, sync, monkeypatch):
|
|
|
|
|
import glutz_api
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
glutz_api, "rpc", lambda m, p: (_ for _ in ()).throw(SystemExit("HTTP 500"))
|
|
|
|
|
)
|
|
|
|
|
sync.planned = [("ctx", "Codes", {"id": "1"})]
|
|
|
|
|
assert sync.run() is False
|
|
|
|
|
assert sync.failed == 1
|
|
|
|
|
|
|
|
|
|
def test_run_success(self, sync, monkeypatch):
|
|
|
|
|
import glutz_api
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(glutz_api, "rpc", lambda m, p: {"id": "9"})
|
|
|
|
|
sync.planned = [("ctx", "Codes", {"id": "1", "code": "1"})]
|
|
|
|
|
assert sync.run() is True
|
2026-11-12 11:56:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestPinCollision:
|
|
|
|
|
def test_pin_owned_by_foreign_user_is_skipped_with_warning(self, sync):
|
|
|
|
|
sync.relations = [{"parentId": "12", "childId": "55"}]
|
|
|
|
|
sync.codes = [
|
|
|
|
|
code_row(id=66, userId=6, code="237251", description="Bajo Temporär")
|
|
|
|
|
]
|
|
|
|
|
sync.plan_booking(booking(), "12", date.today())
|
|
|
|
|
assert not sync.planned
|
|
|
|
|
assert any("resolve manually" in s for s in sync.skipped)
|
|
|
|
|
|
|
|
|
|
def test_own_code_still_updates(self, sync):
|
|
|
|
|
sync.relations = [{"parentId": "12", "childId": "55"}]
|
|
|
|
|
sync.codes = [
|
|
|
|
|
code_row(
|
|
|
|
|
id=99,
|
|
|
|
|
userId=55,
|
|
|
|
|
code="237251",
|
|
|
|
|
validFrom=f"{FUTURE}-12-27T12:45:00.000",
|
|
|
|
|
validTo=f"{FUTURE}-12-27T17:15:00.000",
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
sync.plan_booking(booking(), "12", date.today())
|
|
|
|
|
assert len(sync.planned) == 1
|
2026-11-12 15:29:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestLogRouting:
|
|
|
|
|
def test_pin_collision_logged_as_warning(self, sync, capsys):
|
2026-11-12 15:35:38 +01:00
|
|
|
from wyby_sync import setup_logging
|
2026-11-12 15:29:57 +01:00
|
|
|
|
2026-11-12 15:35:38 +01:00
|
|
|
setup_logging()
|
2026-11-12 15:29:57 +01:00
|
|
|
sync.relations = [{"parentId": "12", "childId": "55"}]
|
|
|
|
|
sync.codes = [
|
|
|
|
|
code_row(id=66, userId=6, code="237251", description="Bajo Temporär")
|
|
|
|
|
]
|
|
|
|
|
sync.plan_booking(booking(), "12", date.today())
|
2026-11-12 15:35:38 +01:00
|
|
|
sync.log_skips()
|
2026-11-12 15:29:57 +01:00
|
|
|
captured = capsys.readouterr()
|
|
|
|
|
assert "resolve manually" in captured.err
|
|
|
|
|
assert "resolve manually" not in captured.out
|
2026-11-13 13:33:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestCleanup:
|
|
|
|
|
def test_old_wyby_user_selected(self, sync):
|
|
|
|
|
old = date(2026, 11, 4) # >7 days before "today" (2026-11-12)
|
|
|
|
|
sync.users = sync.users + [{"id": 70, "label": "Alt Kunde 111111", "class": 1}]
|
|
|
|
|
sync.codes = [
|
|
|
|
|
code_row(
|
|
|
|
|
id=80,
|
|
|
|
|
userId=70,
|
|
|
|
|
code="111111",
|
|
|
|
|
validFrom=f"{old}T00:00:00.000",
|
|
|
|
|
validTo=f"{old + timedelta(days=1)}T23:59:59.000",
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
sync.plan_cleanup(date.today())
|
|
|
|
|
assert sync.cleanup_ids == ["70"]
|
|
|
|
|
assert any("CLEANUP" in s for s in sync.skipped)
|
|
|
|
|
|
|
|
|
|
def test_future_and_recent_users_kept(self, sync):
|
|
|
|
|
today = date.today()
|
|
|
|
|
sync.users = sync.users + [
|
|
|
|
|
{"id": 71, "label": "Aktiv Kunde 222222", "class": 1},
|
|
|
|
|
{"id": 72, "label": "Frisch Kunde 333333", "class": 1},
|
|
|
|
|
{"id": 73, "label": "Manuell ohne Ziffern", "class": 1},
|
|
|
|
|
{"id": 74, "label": "Kein Code 444444", "class": 1},
|
|
|
|
|
]
|
|
|
|
|
sync.codes = [
|
|
|
|
|
code_row(
|
|
|
|
|
id=81,
|
|
|
|
|
userId=71,
|
|
|
|
|
code="222222",
|
|
|
|
|
validFrom=f"{today}T00:00:00.000",
|
|
|
|
|
validTo=f"{today}T23:59:59.000",
|
|
|
|
|
),
|
|
|
|
|
code_row(
|
|
|
|
|
id=82,
|
|
|
|
|
userId=72,
|
|
|
|
|
code="333333",
|
|
|
|
|
validFrom=f"{today - timedelta(days=3)}T00:00:00.000",
|
|
|
|
|
validTo=f"{today - timedelta(days=2)}T23:59:59.000",
|
|
|
|
|
),
|
|
|
|
|
code_row(
|
|
|
|
|
id=83,
|
|
|
|
|
userId=73,
|
|
|
|
|
code="555555",
|
|
|
|
|
validFrom="2020-01-01T00:00:00.000",
|
|
|
|
|
validTo="2020-01-02T00:00:00.000",
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
sync.plan_cleanup(today)
|
|
|
|
|
assert sync.cleanup_ids == []
|
|
|
|
|
|
|
|
|
|
def test_run_deletes_with_media_pool(self, sync, monkeypatch):
|
|
|
|
|
import glutz_api
|
|
|
|
|
|
|
|
|
|
calls = []
|
|
|
|
|
monkeypatch.setattr(
|
|
|
|
|
glutz_api, "rpc", lambda m, p: calls.append((m, p)) or ["70"]
|
|
|
|
|
)
|
|
|
|
|
sync.users = sync.users + [{"id": 70, "label": "Alt Kunde 111111", "class": 1}]
|
|
|
|
|
sync.codes = [
|
|
|
|
|
code_row(
|
|
|
|
|
id=80,
|
|
|
|
|
userId=70,
|
|
|
|
|
code="111111",
|
|
|
|
|
validFrom="2026-01-01T00:00:00.000",
|
|
|
|
|
validTo="2026-01-02T00:00:00.000",
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
sync.plan_cleanup(date.today())
|
|
|
|
|
sync.planned = []
|
|
|
|
|
assert sync.run() is True
|
|
|
|
|
assert ("eAccess.deleteUsersAndGroups", [["70"], {"mediaPoolId": "1"}]) in calls
|