Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@
},
"image": "mcr.microsoft.com/devcontainers/python:3",
"name": "Asynchronous Python client for Twente Milieu API",
"updateContentCommand": ". ${NVM_DIR}/nvm.sh && nvm install && nvm use && npm install && poetry install && poetry run pre-commit install"
"updateContentCommand": "git config --global --add safe.directory '*' && . ${NVM_DIR}/nvm.sh && nvm install && nvm use && npm install && poetry install && poetry run pre-commit install"
}
21 changes: 21 additions & 0 deletions src/twentemilieu/twentemilieu.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ class WasteType(IntEnum):
TREE = 6
PACKAGES = 10

@classmethod
def _missing_(cls, value: object) -> WasteType:
"""Fallback for unknown waste types.

Some waste types returned from the Twente Milieu API are semantically the same
as some types already defined in this enum. This maps the API value to the
correct enum value.

An example would be for packages. Some housing has a container for packages,
and high-density living may need to place their packages at a central point
for pick-up. Both is a pick-up for packages, but their waste type returned
from the API are different.

"""
if not isinstance(value, int):
raise TypeError(value)

return {
56: WasteType.PACKAGES,
}[value]


@dataclass
class TwenteMilieu:
Expand Down
16 changes: 15 additions & 1 deletion tests/test_twentemilieu.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ async def test_json_request(aresponses: ResponsesMockServer) -> None:
await twente.close()


async def test_wastetype_fallback() -> None:
"""Test the WasteType fallback is handled correctly."""
assert (
WasteType(56) == WasteType.PACKAGES
), "Fallback for high-density packages not handled!"

with pytest.raises(TypeError):
WasteType._missing_("wrong_type")


async def test_internal_session(aresponses: ResponsesMockServer) -> None:
"""Test JSON response is handled correctly."""
aresponses.add(
Expand Down Expand Up @@ -238,6 +248,10 @@ async def test_update(aresponses: ResponsesMockServer) -> None:
"pickupDates": ["2019-07-22T00:00:00"],
"pickupType": 2,
},
{
"pickupDates": ["2019-07-23T00:00:00"],
"pickupType": 56,
},
{"pickupDates": [], "pickupType": 10},
],
},
Expand All @@ -255,4 +269,4 @@ async def test_update(aresponses: ResponsesMockServer) -> None:
]
assert pickups[WasteType.ORGANIC] == [date(2019, 7, 19), date(2019, 7, 20)]
assert pickups[WasteType.PAPER] == [date(2019, 7, 22)]
assert not pickups[WasteType.PACKAGES]
assert pickups[WasteType.PACKAGES] == [date(2019, 7, 23)]