-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_twentemilieu.py
272 lines (235 loc) · 8.54 KB
/
test_twentemilieu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""Tests for `twentemilieu.twentemilieu`."""
# pylint: disable=protected-access
import asyncio
import json
import socket
from datetime import date
from unittest.mock import patch
import aiohttp
import pytest
from aresponses import Response, ResponsesMockServer
from twentemilieu import TwenteMilieu, WasteType
from twentemilieu.exceptions import (
TwenteMilieuAddressError,
TwenteMilieuConnectionError,
TwenteMilieuError,
)
API_HOST = "twentemilieuapi.ximmio.com"
async def test_json_request(aresponses: ResponsesMockServer) -> None:
"""Test JSON response is handled correctly."""
aresponses.add(
API_HOST,
"/api/",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text='{"status": "ok"}',
),
)
async with aiohttp.ClientSession() as session:
twente = TwenteMilieu(post_code="1234AB", house_number=1, session=session)
response = await twente._request("")
assert response["status"] == "ok"
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(
API_HOST,
"/api/",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text='{"status": "ok"}',
),
)
async with TwenteMilieu(post_code="1234AB", house_number=1) as twente:
response = await twente._request("")
assert response["status"] == "ok"
async def test_internal_eventloop(aresponses: ResponsesMockServer) -> None:
"""Test JSON response is handled correctly."""
aresponses.add(
API_HOST,
"/api/",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text='{"status": "ok"}',
),
)
async with TwenteMilieu(post_code="1234AB", house_number=1) as twente:
response = await twente._request("")
assert response["status"] == "ok"
async def test_timeout(aresponses: ResponsesMockServer) -> None:
"""Test request timeout from Twente Milieu."""
# Faking a timeout by sleeping
async def response_handler(_: aiohttp.ClientResponse) -> Response:
await asyncio.sleep(2)
return aresponses.Response(body="Goodmorning!")
aresponses.add(API_HOST, "/api/", "POST", response_handler)
async with aiohttp.ClientSession() as session:
twente = TwenteMilieu(
post_code="1234AB",
house_number=1,
session=session,
request_timeout=1,
)
with pytest.raises(TwenteMilieuConnectionError):
assert await twente._request("")
async def test_http_error400(aresponses: ResponsesMockServer) -> None:
"""Test HTTP 404 response handling."""
aresponses.add(
API_HOST,
"/api/",
"POST",
aresponses.Response(text="OMG PUPPIES!", status=404),
)
async with aiohttp.ClientSession() as session:
twente = TwenteMilieu(post_code="1234AB", house_number=1, session=session)
with pytest.raises(TwenteMilieuError):
assert await twente._request("")
async def test_http_error500(aresponses: ResponsesMockServer) -> None:
"""Test HTTP 500 response handling."""
aresponses.add(
API_HOST,
"/api/",
"POST",
aresponses.Response(
body=b'{"status":"nok"}',
status=500,
headers={"Content-Type": "application/json"},
),
)
async with aiohttp.ClientSession() as session:
twente = TwenteMilieu(post_code="1234AB", house_number=1, session=session)
with pytest.raises(TwenteMilieuError):
assert await twente._request("")
async def test_unexpected_response(aresponses: ResponsesMockServer) -> None:
"""Test unexpected response handling."""
aresponses.add(
API_HOST,
"/api/",
"POST",
aresponses.Response(text="OMG PUPPIES!", status=200),
)
async with aiohttp.ClientSession() as session:
twente = TwenteMilieu(post_code="1234AB", house_number=1, session=session)
with pytest.raises(TwenteMilieuError):
assert await twente._request("")
async def test_communication_error() -> None:
"""Test communication error handling."""
async with aiohttp.ClientSession() as session:
twente = TwenteMilieu(post_code="1234AB", house_number=1, session=session)
with (
patch.object(
session,
"request",
side_effect=socket.gaierror,
),
pytest.raises(TwenteMilieuConnectionError),
):
assert await twente._request("")
async def test_unique_id(aresponses: ResponsesMockServer) -> None:
"""Test request of a unique address identifier."""
aresponses.add(
API_HOST,
"/api/FetchAdress",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text='{"dataList": [{"UniqueId": "12345"}]}',
),
)
async with aiohttp.ClientSession() as session:
twente = TwenteMilieu(post_code="1234AB", house_number=1, session=session)
unique_id = await twente.unique_id()
assert unique_id == "12345"
unique_id = await twente.unique_id()
assert unique_id == "12345"
async def test_invalid_address(aresponses: ResponsesMockServer) -> None:
"""Test request of invalid address information."""
aresponses.add(
API_HOST,
"/api/FetchAdress",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text='{"dataList": []}',
),
)
async with aiohttp.ClientSession() as session:
twente = TwenteMilieu(post_code="1234AB", house_number=1, session=session)
with pytest.raises(TwenteMilieuAddressError):
assert await twente.unique_id()
async def test_update(aresponses: ResponsesMockServer) -> None:
"""Test request for updating data from Twente Milieu."""
aresponses.add(
API_HOST,
"/api/FetchAdress",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text='{"dataList": [{"UniqueId": "12345"}]}',
),
)
aresponses.add(
API_HOST,
"/api/GetCalendar",
"POST",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=json.dumps(
{
"dataList": [
{
"pickupDates": [
"2019-07-20T00:00:00",
"2019-07-19T00:00:00",
],
"pickupType": 1,
},
{
"pickupDates": ["2019-07-21T00:00:00"],
"pickupType": 0,
},
{
"pickupDates": ["2019-08-22T00:00:00"],
"pickupType": 0,
},
{
"pickupDates": ["2019-07-22T00:00:00"],
"pickupType": 2,
},
{
"pickupDates": ["2019-07-23T00:00:00"],
"pickupType": 56,
},
{"pickupDates": [], "pickupType": 10},
],
},
),
),
)
async with aiohttp.ClientSession() as session:
twente = TwenteMilieu(post_code="1234AB", house_number=1, session=session)
pickups = await twente.update()
assert pickups[WasteType.NON_RECYCLABLE] == [
date(2019, 7, 21),
date(2019, 8, 22),
]
assert pickups[WasteType.ORGANIC] == [date(2019, 7, 19), date(2019, 7, 20)]
assert pickups[WasteType.PAPER] == [date(2019, 7, 22)]
assert pickups[WasteType.PACKAGES] == [date(2019, 7, 23)]