# tests/test_service_availability.py import pytest def test_grant_and_revoke( service_availability_repo, member_repo, service_type_repo, ): """ Verify that: • `grant` adds a new (member, service_type) pair idempotently. • `revoke` removes the pair. • The helper `members_for_type` returns the expected IDs. """ # ------------------------------------------------------------------ # Arrange – fetch the IDs we know exist from the fixture. # ------------------------------------------------------------------ # Alice is member_id 1, Bob is member_id 2 (AUTOINCREMENT order). alice_id = 1 bob_id = 2 # Service type IDs correspond to the order we inserted them: # 9AM → 1, 11AM → 2, 6PM → 3 nine_am_id = 1 eleven_am_id = 2 six_pm_id = 3 # ------------------------------------------------------------------ # Act – try granting a *new* availability that wasn't seeded. # We'll give Alice the 11AM slot (she didn't have it before). # ------------------------------------------------------------------ new_pair = service_availability_repo.grant(alice_id, eleven_am_id) # ------------------------------------------------------------------ # Assert – the row exists and the helper returns the right member list. # ------------------------------------------------------------------ assert new_pair.MemberId == alice_id assert new_pair.ServiceTypeId == eleven_am_id # `members_for_type` should now contain Alice (1) **and** Bob (2) for 11AM. members_for_11am = service_availability_repo.members_for_type(eleven_am_id) assert set(members_for_11am) == {alice_id, bob_id} # ------------------------------------------------------------------ # Revoke the newly added pair and ensure it disappears. # ------------------------------------------------------------------ service_availability_repo.revoke(alice_id, eleven_am_id) # After revocation the 11AM list should contain **only** Bob. members_after_revoke = service_availability_repo.members_for_type(eleven_am_id) assert members_after_revoke == [bob_id] # Also verify that `get` returns None for the removed pair. assert service_availability_repo.get(alice_id, eleven_am_id) is None def test_list_by_member(service_availability_repo): """ Validate that `list_by_member` returns exactly the slots we seeded. """ # Alice (member_id 1) should have 9AM (1) and 6PM (3) alice_slots = service_availability_repo.list_by_member(1) alice_type_ids = sorted([s.ServiceTypeId for s in alice_slots]) assert alice_type_ids == [1, 3] # Bob (member_id 2) should have 11AM (2) and 6PM (3) bob_slots = service_availability_repo.list_by_member(2) bob_type_ids = sorted([s.ServiceTypeId for s in bob_slots]) assert bob_type_ids == [2, 3]