184 lines
6.2 KiB
Python
184 lines
6.2 KiB
Python
"""Tests for wyby_sync: planning logic with mocked Glutz API + WybyClient."""
|
|
|
|
from datetime import date
|
|
|
|
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
|
|
|
|
|
|
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
|
|
|
|
|
|
class TestLogRouting:
|
|
def test_pin_collision_logged_as_warning(self, sync, capsys):
|
|
from wyby_sync import logger
|
|
|
|
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())
|
|
for s in sync.skipped:
|
|
(logger.warning if "resolve manually" in s else logger.info)("SKIP %s", s)
|
|
captured = capsys.readouterr()
|
|
assert "resolve manually" in captured.err
|
|
assert "resolve manually" not in captured.out
|