Debian Codegraph @

tests/test_device.py

raw symbols SCIP
1 """Device object test module"""
2
3 import unittest
4 import uuid
5 import json
6 from unittest.mock import patch
7 from random import randrange
8
9 from rachiopy import Device
10 from tests.constants import BASE_API_URL, AUTHTOKEN, RESPONSE200, RESPONSE204
11
12
13 class TestDeviceMethods(unittest.TestCase):
14 """Class containing the Device object test cases."""
15
16 def setUp(self):
17 self.device = Device(AUTHTOKEN)
18
19 def test_init(self):
20 """Test if the constructor works as expected."""
21 self.assertEqual(self.device.authtoken, AUTHTOKEN)
22
23 @patch("requests.Session.request")
24 def test_get(self, mock):
25 """Test if the get method works as expected."""
26 mock.return_value = RESPONSE200
27
28 deviceid = str(uuid.uuid4())
29
30 self.device.get(deviceid)
31
32 args, kwargs = mock.call_args
33
34 # Check that the mock function is called with the rights args.
35 self.assertEqual(
36 args[1], f"{BASE_API_URL}/device/{deviceid}",
37 )
38 self.assertEqual(args[0], "GET")
39 self.assertEqual(kwargs["data"], None)
40
41 @patch("requests.Session.request")
42 def test_current_schedule(self, mock):
43 """Test if the current schedule method works as expected."""
44 mock.return_value = RESPONSE200
45
46 deviceid = str(uuid.uuid4())
47
48 self.device.current_schedule(deviceid)
49
50 args, kwargs = mock.call_args
51
52 # Check that the mock function is called with the rights args.
53 self.assertEqual(
54 args[1], f"{BASE_API_URL}/device/{deviceid}/current_schedule",
55 )
56 self.assertEqual(args[0], "GET")
57 self.assertEqual(kwargs["data"], None)
58
59 @patch("requests.Session.request")
60 def test_event(self, mock):
61 """Test if the event method works as expected."""
62 mock.return_value = RESPONSE200
63
64 deviceid = str(uuid.uuid4())
65 starttime = 1414818000000
66 endtime = 1415739608103
67
68 self.device.event(deviceid, starttime, endtime)
69
70 args, kwargs = mock.call_args
71
72 # Check that the mock function is called with the rights args.
73 self.assertEqual(
74 args[1],
75 f"{BASE_API_URL}/device/"
76 f"{deviceid}/event?startTime="
77 f"{starttime}&endTime="
78 f"{endtime}",
79 )
80 self.assertEqual(args[0], "GET")
81 self.assertEqual(kwargs["data"], None)
82
83 @patch("requests.Session.request")
84 def test_forecast(self, mock):
85 """Test if the forecast method works as expected."""
86 mock.return_value = RESPONSE200
87
88 deviceid = str(uuid.uuid4())
89
90 self.device.forecast(deviceid)
91
92 args, kwargs = mock.call_args
93
94 # Check that the mock function is called with the rights args.
95 self.assertEqual(
96 args[1], f"{BASE_API_URL}/device/{deviceid}/forecast?units=US",
97 )
98 self.assertEqual(args[0], "GET")
99 self.assertEqual(kwargs["data"], None)
100
101 self.device.forecast(deviceid, "US")
102
103 args, kwargs = mock.call_args
104
105 # Check that the mock function is called with the rights args.
106 self.assertEqual(
107 args[1], f"{BASE_API_URL}/device/{deviceid}/forecast?units=US",
108 )
109 self.assertEqual(args[0], "GET")
110 self.assertEqual(kwargs["data"], None)
111
112 self.device.forecast(deviceid, "METRIC")
113
114 args, kwargs = mock.call_args
115
116 # Check that the mock function is called with the rights args.
117 self.assertEqual(
118 args[1],
119 f"{BASE_API_URL}/device/{deviceid}/forecast?units=METRIC",
120 )
121 self.assertEqual(args[0], "GET")
122 self.assertEqual(kwargs["data"], None)
123
124 # Check that values should be within range.
125 self.assertRaises(AssertionError, self.device.forecast, deviceid, "")
126
127 @patch("requests.Session.request")
128 def test_stop_water(self, mock):
129 """Test if the stop water method works as expected."""
130 mock.return_value = RESPONSE204
131
132 deviceid = str(uuid.uuid4())
133
134 self.device.stop_water(deviceid)
135
136 args, kwargs = mock.call_args
137
138 # Check that the mock function is called with the rights args.
139 self.assertEqual(
140 args[1], f"{BASE_API_URL}/device/stop_water",
141 )
142 self.assertEqual(args[0], "PUT")
143 self.assertEqual(kwargs["data"], json.dumps({"id": deviceid}))
144
145 @patch("requests.Session.request")
146 def test_rain_delay(self, mock):
147 """Test if the rain delay method works as expected."""
148 mock.return_value = RESPONSE204
149
150 deviceid = str(uuid.uuid4())
151 duration = randrange(604800)
152
153 self.device.rain_delay(deviceid, duration)
154
155 args, kwargs = mock.call_args
156
157 # Check that the mock function is called with the rights args.
158 self.assertEqual(
159 args[1], f"{BASE_API_URL}/device/rain_delay",
160 )
161 self.assertEqual(args[0], "PUT")
162 self.assertEqual(
163 kwargs["data"], json.dumps({"id": deviceid, "duration": duration})
164 )
165
166 # Check that values should be within range.
167 self.assertRaises(AssertionError, self.device.rain_delay, deviceid, -1)
168 self.assertRaises(
169 AssertionError, self.device.rain_delay, deviceid, 604801
170 )
171
172 @patch("requests.Session.request")
173 def test_turn_on(self, mock):
174 """Test if the turn on method works as expected."""
175 mock.return_value = RESPONSE204
176
177 deviceid = str(uuid.uuid4())
178
179 self.device.turn_on(deviceid)
180
181 args, kwargs = mock.call_args
182
183 # Check that the mock function is called with the rights args.
184 self.assertEqual(
185 args[1], f"{BASE_API_URL}/device/on",
186 )
187 self.assertEqual(args[0], "PUT")
188 self.assertEqual(kwargs["data"], json.dumps({"id": deviceid}))
189
190 @patch("requests.Session.request")
191 def test_turn_off(self, mock):
192 """Test if the turn off method works as expected."""
193 mock.return_value = RESPONSE204
194
195 deviceid = str(uuid.uuid4())
196
197 self.device.turn_off(deviceid)
198
199 args, kwargs = mock.call_args
200
201 # Check that the mock function is called with the rights args.
202 self.assertEqual(
203 args[1], f"{BASE_API_URL}/device/off",
204 )
205 self.assertEqual(args[0], "PUT")
206 self.assertEqual(kwargs["data"], json.dumps({"id": deviceid}))
207
208 @patch("requests.Session.request")
209 def test_pause_zone_run(self, mock):
210 """Test if the pause zone run method works as expected."""
211 mock.return_value = RESPONSE204
212
213 deviceid = str(uuid.uuid4())
214 duration = randrange(3600)
215
216 self.device.pause_zone_run(deviceid, duration)
217
218 args, kwargs = mock.call_args
219
220 # Check that the mock function is called with the rights args.
221 self.assertEqual(
222 args[1], f"{BASE_API_URL}/device/pause_zone_run",
223 )
224 self.assertEqual(args[0], "PUT")
225 self.assertEqual(
226 kwargs["data"], json.dumps({"id": deviceid, "duration": duration})
227 )
228
229 # Check that values should be within range.
230 self.assertRaises(
231 AssertionError, self.device.pause_zone_run, deviceid, -1
232 )
233 self.assertRaises(
234 AssertionError, self.device.pause_zone_run, deviceid, 3601
235 )
236
237 @patch("requests.Session.request")
238 def test_resume_zone_run(self, mock):
239 """Test if the resume zone run method works as expected."""
240 mock.return_value = RESPONSE204
241
242 deviceid = str(uuid.uuid4())
243
244 self.device.resume_zone_run(deviceid)
245
246 args, kwargs = mock.call_args
247
248 # Check that the mock function is called with the rights args.
249 self.assertEqual(
250 args[1], f"{BASE_API_URL}/device/resume_zone_run",
251 )
252 self.assertEqual(args[0], "PUT")
253 self.assertEqual(kwargs["data"], json.dumps({"id": deviceid}))