Debian Codegraph @

foobot_async/tests/test_foobot_async.py

raw
1 import aiohttp
2 import asyncio
3 import pytest
4 from aioresponses import aioresponses
5 from datetime import datetime
6 from foobot_async import FoobotClient
7
8 loop = asyncio.new_event_loop()
9 asyncio.set_event_loop(loop)
10 client = FoobotClient('token', '[email protected]')
11
12
13 def test_get_devices_request():
14 with aioresponses() as mocked:
15 mocked.get('https://api.foobot.io/v2/owner/[email protected]/device/',
16 status=200, body='''[{"uuid": "1234127987696AB",
17 "userId": 2353,
18 "mac": "013843C3C20A",
19 "name": "FooBot"}]''')
20 resp = loop.run_until_complete(client.get_devices())
21
22 assert [dict(uuid="1234127987696AB",
23 userId=2353,
24 mac="013843C3C20A",
25 name="FooBot")] == resp
26
27 # def test_get_devices_with_session_request():
28 # session = aiohttp.ClientSession()
29 # client = FoobotClient('token', '[email protected]', session)
30 #
31 # with aioresponses() as mocked:
32 # mocked.get('https://api.foobot.io/v2/owner/[email protected]/device/',
33 # status=200, body='''[{"uuid": "1234127987696AB",
34 # "userId": 2353,
35 # "mac": "013843C3C20A",
36 # "name": "FooBot"}]''')
37 # resp = loop.run_until_complete(client.get_devices())
38 #
39 # assert [dict(uuid="1234127987696AB",
40 # userId=2353,
41 # mac="013843C3C20A",
42 # name="FooBot")] == resp
43
44
45 def test_failed_auth_request():
46 with aioresponses() as mocked:
47 mocked.get('https://api.foobot.io/v2/owner/[email protected]/device/',
48 status=401, body='{"message": "invalid key provided"}')
49
50 with pytest.raises(FoobotClient.AuthFailure):
51 loop.run_until_complete(client.get_devices())
52
53
54 def test_failed_bad_format_request():
55 with aioresponses() as mocked:
56 mocked.get('https://api.foobot.io/v2/owner/[email protected]/device/',
57 status=400, body='''{"state": 400,
58 "message": "ParseException : Bad date format : bad date format [test]",
59 "requestedUri": "/v2/device/26025766336015C0/datapoint/test/last/test/",
60 "stack": "[obfuscated]",
61 "propagatedException": null}''')
62
63 with pytest.raises(FoobotClient.BadFormat):
64 loop.run_until_complete(client.get_devices())
65
66
67 def test_forbidden_access_request():
68 with aioresponses() as mocked:
69 mocked.get('https://api.foobot.io/v2/owner/[email protected]/device/',
70 status=403, body='''{"{"state": 403, "message": null,
71 "stack": "403 - Forbidden",
72 "propagatedException": null }":
73 "invalid key provided"}''')
74
75 with pytest.raises(FoobotClient.ForbiddenAccess):
76 loop.run_until_complete(client.get_devices())
77
78
79 def test_overquota_request():
80 with aioresponses() as mocked:
81 mocked.get('https://api.foobot.io/v2/owner/[email protected]/device/',
82 status=429, body='')
83
84 with pytest.raises(FoobotClient.TooManyRequests):
85 loop.run_until_complete(client.get_devices())
86
87
88 def test_internal_error_request():
89 with aioresponses() as mocked:
90 mocked.get('https://api.foobot.io/v2/owner/[email protected]/device/',
91 status=500, body='')
92
93 with pytest.raises(FoobotClient.InternalError):
94 loop.run_until_complete(client.get_devices())
95
96
97 def test_unhandled_error_request():
98 with aioresponses() as mocked:
99 mocked.get('https://api.foobot.io/v2/owner/[email protected]/device/',
100 status=404, body='')
101
102 with pytest.raises(FoobotClient.ClientError):
103 loop.run_until_complete(client.get_devices())
104
105 def test_get_last_data_request():
106 body = '''{"uuid": "1234127987696AB",
107 "start": 1518131274,
108 "end": 1518131874,
109 "sensors": ["time", "pm", "tmp", "hum", "co2", "voc", "allpollu"],
110 "units": [ "s", "ugm3", "C", "pc", "ppm", "ppb", "%" ],
111 "datapoints": [ [ 1518131274, 135.70001, 21.046001, 46.6885, 1178.0,
112 325.5, 131.19643 ] ] }'''
113
114 with aioresponses() as mocked:
115 mocked.get('https://api.foobot.io/v2/device/1234127987696AB/datapoint/600/last/601/',
116 status=200, body=body)
117
118 resp = loop.run_until_complete(client.get_last_data("1234127987696AB",
119 600, 601))
120 assert dict(time=1518131274,
121 pm=135.70001,
122 tmp=21.046001,
123 hum=46.6885,
124 co2=1178.0,
125 voc=325.5,
126 allpollu=131.19643) == resp[0]
127
128
129 def test_get_historical_data_request():
130 body = '''{"uuid": "1234127987696AB",
131 "start": 1518131274,
132 "end": 1518131874,
133 "sensors": ["time", "pm", "tmp", "hum", "co2", "voc", "allpollu"],
134 "units": [ "s", "ugm3", "C", "pc", "ppm", "ppb", "%" ],
135 "datapoints": [ [ 1518131274, 135.70001, 21.046001, 46.6885, 1178.0,
136 325.5, 131.19643 ] ] }'''
137
138 with aioresponses() as mocked:
139 mocked.get('https://api.foobot.io/v2/device/1234127987696AB/datapoint/1518121274/1518131274/3600/',
140 status=200, body=body)
141
142 resp = loop.run_until_complete(client.get_historical_data("1234127987696AB",
143 datetime.utcfromtimestamp(1518121274),
144 datetime.utcfromtimestamp(1518131274),
145 3600))
146 assert dict(time=1518131274,
147 pm=135.70001,
148 tmp=21.046001,
149 hum=46.6885,
150 co2=1178.0,
151 voc=325.5,
152 allpollu=131.19643) == resp[0]
153
154
155 def test_get_bad_data_request():
156 body = '''{"uuid": "1234127987696AB",
157 "start": 1518131274,
158 "end": 1518131874}'''
159
160 with aioresponses() as mocked:
161 mocked.get('https://api.foobot.io/v2/device/1234127987696AB/datapoint/1518121274/1518131274/3600/',
162 status=200, body=body)
163
164 with pytest.raises(FoobotClient.InvalidData):
165 loop.run_until_complete(client.get_historical_data("1234127987696AB",
166 datetime.utcfromtimestamp(1518121274),
167 datetime.utcfromtimestamp(1518131274),
168 3600))