Skip to content

Commit

Permalink
tests: refactor into Event, Group, Export classes for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
elliot-100 committed Jul 11, 2024
1 parent 486981f commit 2e72a25
Showing 1 changed file with 163 additions and 164 deletions.
327 changes: 163 additions & 164 deletions tests/test_spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,6 @@ async def wrapper(*args, **kwargs):
_SpondBase.require_authentication = mock_require_authentication(Spond.get_event)


@pytest.fixture
def mock_events():
"""Mock a minimal list of events."""
return [
{
"id": "ID1",
"name": "Event One",
},
{
"id": "ID2",
"name": "Event Two",
},
]


@pytest.fixture
def mock_groups():
"""Mock a minimal list of groups."""
return [
{
"id": "ID1",
"name": "Group One",
},
{
"id": "ID2",
"name": "Group Two",
},
]


@pytest.fixture
def mock_token():
return MOCK_TOKEN
Expand All @@ -63,137 +33,166 @@ def mock_payload():
return MOCK_PAYLOAD


@pytest.mark.asyncio
async def test_get_event__happy_path(mock_events, mock_token):
"""Test that a valid `id` returns the matching event."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.events = mock_events
s.token = mock_token
g = await s.get_event("ID1")

assert g == {
"id": "ID1",
"name": "Event One",
}


@pytest.mark.asyncio
async def test_get_event__no_match_raises_exception(mock_events, mock_token):
"""Test that a non-matched `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.events = mock_events
s.token = mock_token

with pytest.raises(KeyError):
await s.get_event("ID3")


@pytest.mark.asyncio
async def test_get_event__blank_id_match_raises_exception(mock_events, mock_token):
"""Test that a blank `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.events = mock_events
s.token = mock_token

with pytest.raises(KeyError):
await s.get_event("")


@pytest.mark.asyncio
async def test_get_group__happy_path(mock_groups, mock_token):
"""Test that a valid `id` returns the matching group."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.groups = mock_groups
s.token = mock_token
g = await s.get_group("ID2")

assert g == {
"id": "ID2",
"name": "Group Two",
}


@pytest.mark.asyncio
async def test_get_group__no_match_raises_exception(mock_groups, mock_token):
"""Test that a non-matched `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.groups = mock_groups
s.token = mock_token

with pytest.raises(KeyError):
await s.get_group("ID3")


@pytest.mark.asyncio
async def test_get_group__blank_id_raises_exception(mock_groups, mock_token):
"""Test that a blank `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.groups = mock_groups
s.token = mock_token

with pytest.raises(KeyError):
await s.get_group("")


@pytest.mark.asyncio
@patch("aiohttp.ClientSession.get")
async def test_get_export(mock_get, mock_token):
s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.token = mock_token

mock_binary = b"\x68\x65\x6c\x6c\x6f\x77\x6f\x72\x6c\x64" # helloworld
mock_get.return_value.__aenter__.return_value.status = 200
mock_get.return_value.__aenter__.return_value.read = AsyncMock(
return_value=mock_binary
)

data = await s.get_event_attendance_xlsx(uid="ID1")

mock_url = "https://api.spond.com/core/v1/sponds/ID1/export"
mock_get.assert_called_once_with(
mock_url,
headers={
"content-type": "application/json",
"Authorization": f"Bearer {mock_token}",
},
)
assert data == mock_binary


@pytest.mark.asyncio
@patch("aiohttp.ClientSession.put")
async def test_change_response(mock_put, mock_payload, mock_token):
s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.token = mock_token

mock_response_data = {
"acceptedIds": ["PID1", "PID2"],
"declinedIds": ["PID3"],
"unansweredIds": [],
"waitinglistIds": [],
"unconfirmedIds": [],
"declineMessages": {"PID3": "sick cannot make it"},
}
mock_put.return_value.__aenter__.return_value.status = 200
mock_put.return_value.__aenter__.return_value.json = AsyncMock(
return_value=mock_response_data
)

response = await s.change_response(uid="ID1", user="PID3", payload=mock_payload)

mock_url = "https://api.spond.com/core/v1/sponds/ID1/responses/PID3"
mock_put.assert_called_once_with(
mock_url,
headers={
"content-type": "application/json",
"Authorization": f"Bearer {mock_token}",
},
json=mock_payload,
)
assert response == mock_response_data
class TestEventMethods:

@pytest.fixture
def mock_events(self):
"""Mock a minimal list of events."""
return [
{
"id": "ID1",
"name": "Event One",
},
{
"id": "ID2",
"name": "Event Two",
},
]

@pytest.mark.asyncio
async def test_get_event__happy_path(self, mock_events, mock_token):
"""Test that a valid `id` returns the matching event."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.events = mock_events
s.token = mock_token
g = await s.get_event("ID1")

assert g == {
"id": "ID1",
"name": "Event One",
}

@pytest.mark.asyncio
async def test_get_event__no_match_raises_exception(self, mock_events, mock_token):
"""Test that a non-matched `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.events = mock_events
s.token = mock_token

with pytest.raises(KeyError):
await s.get_event("ID3")

@pytest.mark.asyncio
async def test_get_event__blank_id_match_raises_exception(
self, mock_events, mock_token
):
"""Test that a blank `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.events = mock_events
s.token = mock_token

with pytest.raises(KeyError):
await s.get_event("")

@pytest.mark.asyncio
@patch("aiohttp.ClientSession.put")
async def test_change_response(self, mock_put, mock_payload, mock_token):
s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.token = mock_token

mock_response_data = {
"acceptedIds": ["PID1", "PID2"],
"declinedIds": ["PID3"],
"unansweredIds": [],
"waitinglistIds": [],
"unconfirmedIds": [],
"declineMessages": {"PID3": "sick cannot make it"},
}
mock_put.return_value.__aenter__.return_value.status = 200
mock_put.return_value.__aenter__.return_value.json = AsyncMock(
return_value=mock_response_data
)

response = await s.change_response(uid="ID1", user="PID3", payload=mock_payload)

mock_url = "https://api.spond.com/core/v1/sponds/ID1/responses/PID3"
mock_put.assert_called_once_with(
mock_url,
headers={
"content-type": "application/json",
"Authorization": f"Bearer {mock_token}",
},
json=mock_payload,
)
assert response == mock_response_data


class TestGroupMethods:
@pytest.fixture
def mock_groups(self):
"""Mock a minimal list of groups."""
return [
{
"id": "ID1",
"name": "Group One",
},
{
"id": "ID2",
"name": "Group Two",
},
]

@pytest.mark.asyncio
async def test_get_group__happy_path(self, mock_groups, mock_token):
"""Test that a valid `id` returns the matching group."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.groups = mock_groups
s.token = mock_token
g = await s.get_group("ID2")

assert g == {
"id": "ID2",
"name": "Group Two",
}

@pytest.mark.asyncio
async def test_get_group__no_match_raises_exception(self, mock_groups, mock_token):
"""Test that a non-matched `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.groups = mock_groups
s.token = mock_token

with pytest.raises(KeyError):
await s.get_group("ID3")

@pytest.mark.asyncio
async def test_get_group__blank_id_raises_exception(self, mock_groups, mock_token):
"""Test that a blank `id` raises KeyError."""

s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.groups = mock_groups
s.token = mock_token

with pytest.raises(KeyError):
await s.get_group("")


class TestExportMethod:
@pytest.mark.asyncio
@patch("aiohttp.ClientSession.get")
async def test_get_export(self, mock_get, mock_token):
s = Spond(MOCK_USERNAME, MOCK_PASSWORD)
s.token = mock_token

mock_binary = b"\x68\x65\x6c\x6c\x6f\x77\x6f\x72\x6c\x64" # helloworld
mock_get.return_value.__aenter__.return_value.status = 200
mock_get.return_value.__aenter__.return_value.read = AsyncMock(
return_value=mock_binary
)

data = await s.get_event_attendance_xlsx(uid="ID1")

mock_url = "https://api.spond.com/core/v1/sponds/ID1/export"
mock_get.assert_called_once_with(
mock_url,
headers={
"content-type": "application/json",
"Authorization": f"Bearer {mock_token}",
},
)
assert data == mock_binary

0 comments on commit 2e72a25

Please sign in to comment.