Debian Codegraph @

nsw_fuel_tests/unit.py

raw
1 import datetime
2 import json
3 import os
4 import unittest
5
6 from requests_mock import Mocker
7
8 from nsw_fuel import FuelCheckClient, Period, FuelCheckError
9 from nsw_fuel.client import API_URL_BASE
10
11
12 class FuelCheckClientTest(unittest.TestCase):
13 def test_construction(self) -> None:
14 FuelCheckClient()
15
16 @Mocker()
17 def test_get_fuel_prices(self, m: Mocker) -> None:
18 fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures/all_prices.json')
19 with open(fixture_path) as fixture:
20 m.get(
21 '{}/prices'.format(API_URL_BASE),
22 json=json.load(fixture)
23 )
24 client = FuelCheckClient()
25 response = client.get_fuel_prices()
26
27 self.assertEqual(len(response.stations), 2)
28 self.assertEqual(len(response.prices), 5)
29 self.assertEqual(response.stations[0].name, 'Cool Fuel Brand Hurstville')
30 self.assertEqual(response.stations[1].name, 'Fake Fuel Brand Kogarah')
31 self.assertEqual(response.prices[0].fuel_type, 'DL')
32 self.assertEqual(response.prices[1].fuel_type, 'E10')
33 self.assertEqual(response.prices[1].station_code, 1)
34 self.assertEqual(response.prices[3].fuel_type, 'P95')
35 self.assertEqual(response.prices[3].station_code, 2)
36
37 @Mocker()
38 def test_get_fuel_prices_server_error(self, m: Mocker) -> None:
39 m.get(
40 '{}/prices'.format(API_URL_BASE),
41 status_code=500,
42 text='Internal Server Error.',
43 )
44
45 client = FuelCheckClient()
46 with self.assertRaises(FuelCheckError) as cm:
47 client.get_fuel_prices()
48
49 self.assertEqual(str(cm.exception), 'Internal Server Error.')
50
51 @Mocker()
52 def test_get_fuel_prices_for_station(self, m: Mocker) -> None:
53 m.get('{}/prices/station/100'.format(API_URL_BASE), json={
54 'prices': [
55 {
56 'fueltype': 'E10',
57 'price': 146.9,
58 'lastupdated': '02/06/2018 02:03:04',
59 },
60 {
61 'fueltype': 'P95',
62 'price': 150.0,
63 'lastupdated': '02/06/2018 02:03:04',
64 }
65 ]
66 })
67 client = FuelCheckClient()
68 result = client.get_fuel_prices_for_station(100)
69 self.assertEqual(len(result), 2)
70 self.assertEqual(result[0].fuel_type, 'E10')
71 self.assertEqual(result[0].price, 146.9)
72 self.assertEqual(result[0].last_updated, datetime.datetime(
73 day=2,
74 month=6,
75 year=2018,
76 hour=2,
77 minute=3,
78 second=4,
79 ))
80
81 @Mocker()
82 def test_get_fuel_prices_within_radius(self, m: Mocker) -> None:
83 m.post('{}/prices/nearby'.format(API_URL_BASE), json={
84 'stations': [
85 {
86 'stationid': 'SAAAAAA',
87 'brandid': 'BAAAAAA',
88 'brand': 'Cool Fuel Brand',
89 'code': 678,
90 'name': 'Cool Fuel Brand Luxembourg',
91 'address': '123 Fake Street',
92 'location': {},
93 },
94 {
95 'stationid': 'SAAAAAB',
96 'brandid': 'BAAAAAB',
97 'brand': 'Fake Fuel Brand',
98 'code': 679,
99 'name': 'Fake Fuel Brand Luxembourg',
100 'address': '123 Fake Street',
101 'location': {},
102 },
103 {
104 'stationid': 'SAAAAAB',
105 'brandid': 'BAAAAAB',
106 'brand': 'Fake Fuel Brand2',
107 'code': 880,
108 'name': 'Fake Fuel Brand2 Luxembourg',
109 'address': '123 Fake Street',
110 'location': {},
111 },
112 ],
113 'prices': [
114 {
115 'stationcode': 678,
116 'fueltype': 'P95',
117 'price': 150.9,
118 'priceunit': 'litre',
119 'description': None,
120 'lastupdated': '2018-06-02 00:46:31'
121 },
122 {
123 'stationcode': 678,
124 'fueltype': 'P95',
125 'price': 130.9,
126 'priceunit': 'litre',
127 'description': None,
128 'lastupdated': '2018-06-02 00:46:31'
129 },
130 {
131 'stationcode': 880,
132 'fueltype': 'P95',
133 'price': 155.9,
134 'priceunit': 'litre',
135 'description': None,
136 'lastupdated': '2018-06-02 00:46:31'
137 }
138 ],
139 })
140
141 client = FuelCheckClient()
142 result = client.get_fuel_prices_within_radius(
143 longitude=151.0,
144 latitude=-33.0,
145 radius=10,
146 fuel_type='E10',
147 )
148 self.assertEqual(len(result), 3)
149 self.assertEqual(result[0].station.code, 678)
150 self.assertEqual(result[0].price.price, 150.9)
151
152 @Mocker()
153 def test_get_fuel_price_trends(self, m: Mocker) -> None:
154 m.post('{}/prices/trends/'.format(API_URL_BASE), json={
155 'Variances': [
156 {'Code': 'E10', 'Period': 'Day', 'Price': 150.0},
157 {'Code': 'E10', 'Period': 'Week', 'Price': 151.0},
158 {'Code': 'E10', 'Period': 'Month', 'Price': 152.0},
159 {'Code': 'E10', 'Period': 'Year', 'Price': 153.0},
160 {'Code': 'P95', 'Period': 'Day', 'Price': 150.0},
161 {'Code': 'P95', 'Period': 'Week', 'Price': 151.0},
162 {'Code': 'P95', 'Period': 'Month', 'Price': 152.0},
163 {'Code': 'P95', 'Period': 'Year', 'Price': 153.0},
164 ],
165 'AveragePrices': [
166 {'Code': 'E10', 'Period': 'Day', 'Price': 150.0,
167 'Captured': '2018-06-02'},
168 {'Code': 'E10', 'Period': 'Year', 'Price': 151.0,
169 'Captured': 'October 2017'}
170 ],
171 })
172
173 client = FuelCheckClient()
174 result = client.get_fuel_price_trends(
175 longitude=151.0,
176 latitude=-33.0,
177 fuel_types=['E10', 'P95']
178 )
179
180 self.assertEqual(len(result.variances), 8)
181 self.assertEqual(result.variances[0].price, 150.0)
182 self.assertEqual(result.variances[0].period, Period.DAY)
183 self.assertEqual(result.variances[0].fuel_type, 'E10')
184
185 self.assertEqual(len(result.average_prices), 2)
186 self.assertEqual(result.average_prices[0].fuel_type, 'E10')
187 self.assertEqual(result.average_prices[0].period, Period.DAY)
188 self.assertEqual(result.average_prices[0].captured,
189 datetime.datetime(year=2018, month=6, day=2))
190 self.assertEqual(result.average_prices[0].price, 150.0)
191
192 self.assertEqual(result.average_prices[1].period, Period.YEAR)
193 self.assertEqual(result.average_prices[1].captured,
194 datetime.datetime(year=2017, month=10, day=1))
195
196 @Mocker()
197 def test_get_fuel_prices_for_station_client_error(self, m: Mocker) -> None:
198 m.get(
199 '{}/prices/station/21199'.format(API_URL_BASE),
200 status_code=400,
201 json={
202 "errorDetails": [
203 {
204 "code": "E0014",
205 "description": "Invalid service station code \"21199\""
206 }
207 ]
208 }
209 )
210 client = FuelCheckClient()
211 with self.assertRaises(FuelCheckError) as cm:
212 client.get_fuel_prices_for_station(21199)
213
214 self.assertEqual(str(cm.exception), 'Invalid service station code "21199"')
215
216 @Mocker()
217 def test_get_fuel_prices_for_station_server_error(self, m: Mocker) -> None:
218 m.get(
219 '{}/prices/station/21199'.format(API_URL_BASE),
220 status_code=500,
221 text='Internal Server Error.',
222 )
223 client = FuelCheckClient()
224 with self.assertRaises(FuelCheckError) as cm:
225 client.get_fuel_prices_for_station(21199)
226
227 self.assertEqual(str(cm.exception), 'Internal Server Error.')
228
229 @Mocker()
230 def test_get_fuel_prices_within_radius_server_error(self, m: Mocker) -> None:
231 m.post(
232 '{}/prices/nearby'.format(API_URL_BASE),
233 status_code=500,
234 text='Internal Server Error.',
235 )
236 client = FuelCheckClient()
237 with self.assertRaises(FuelCheckError) as cm:
238 client.get_fuel_prices_within_radius(
239 longitude=151.0,
240 latitude=-33.0,
241 radius=10,
242 fuel_type='E10',
243 )
244
245 self.assertEqual(str(cm.exception), 'Internal Server Error.')
246
247 @Mocker()
248 def test_get_fuel_price_trends_server_error(self, m: Mocker) -> None:
249 m.post(
250 '{}/prices/trends/'.format(API_URL_BASE),
251 status_code=500,
252 text='Internal Server Error.',
253 )
254 client = FuelCheckClient()
255 with self.assertRaises(FuelCheckError) as cm:
256 client.get_fuel_price_trends(
257 longitude=151.0,
258 latitude=-33.0,
259 fuel_types=['E10', 'P95']
260 )
261
262 self.assertEqual(str(cm.exception), 'Internal Server Error.')
263
264 @Mocker()
265 def test_get_reference_data(self, m: Mocker) -> None:
266 fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures/lovs.json')
267 with open(fixture_path) as fixture:
268 m.get(
269 '{}/lovs'.format(API_URL_BASE),
270 json=json.load(fixture)
271 )
272 client = FuelCheckClient()
273 response = client.get_reference_data()
274 self.assertEqual(len(response.brands), 2)
275 self.assertEqual(len(response.fuel_types), 2)
276 self.assertEqual(len(response.stations), 2)
277 self.assertEqual(len(response.trend_periods), 2)
278 self.assertEqual(len(response.sort_fields), 2)
279 self.assertEqual(response.brands[0], 'Cool Fuel Brand')
280 self.assertEqual(response.fuel_types[0].code, 'E10')
281 self.assertEqual(response.fuel_types[0].name, 'Ethanol 94')
282 self.assertEqual(response.stations[0].name, 'Cool Fuel Brand Hurstville')
283 self.assertEqual(response.trend_periods[0].period, 'Day')
284 self.assertEqual(response.trend_periods[0].description, 'Description for day')
285 self.assertEqual(response.sort_fields[0].code, 'Sort 1')
286 self.assertEqual(response.sort_fields[0].name, 'Sort field 1')
287
288 @Mocker()
289 def test_get_reference_data_client_error(self, m: Mocker) -> None:
290 m.get(
291 '{}/lovs'.format(API_URL_BASE),
292 status_code=400,
293 json={
294 "errorDetails": {
295 "code": "-2146233033",
296 "message": "String was not recognized as a valid DateTime."
297 }
298 })
299
300 client = FuelCheckClient()
301 with self.assertRaises(FuelCheckError) as cm:
302 client.get_reference_data()
303
304 self.assertEqual(
305 str(cm.exception),
306 'String was not recognized as a valid DateTime.'
307 )
308
309 @Mocker()
310 def test_get_reference_data_server_error(self, m: Mocker) -> None:
311 m.get(
312 '{}/lovs'.format(API_URL_BASE),
313 status_code=500,
314 text='Internal Server Error.',
315 )
316
317 client = FuelCheckClient()
318 with self.assertRaises(FuelCheckError) as cm:
319 client.get_reference_data()
320
321 self.assertEqual(str(cm.exception), 'Internal Server Error.')