Debian Codegraph @

tests/test_zone.py

raw symbols SCIP
1 """Zone object test module"""
2
3 import unittest
4 from unittest.mock import patch
5 import uuid
6 import random
7 import json
8
9 from random import randrange
10 from rachiopy import Zone
11 from rachiopy.zone import ZoneSchedule
12 from tests.constants import BASE_API_URL, AUTHTOKEN, RESPONSE200, RESPONSE204
13
14
15 class TestZoneMethods(unittest.TestCase):
16 """Class containing the Zone object test cases."""
17
18 def setUp(self):
19 self.zone = Zone(AUTHTOKEN)
20
21 zone1id = str(uuid.uuid4())
22 zone2id = str(uuid.uuid4())
23 zone3id = str(uuid.uuid4())
24 duration1 = randrange(10800)
25 duration2 = randrange(10800)
26 duration3 = randrange(10800)
27
28 self.zones = []
29 self.zones.append((zone1id, duration1))
30 self.zones.append((zone2id, duration2))
31 self.zones.append((zone3id, duration3))
32
33 def test_init(self):
34 """Test if the constructor works as expected."""
35 self.assertEqual(self.zone.authtoken, AUTHTOKEN)
36
37 @patch("requests.Session.request")
38 def test_get(self, mock):
39 """Test if the get method works as expected."""
40 mock.return_value = RESPONSE200
41
42 zoneid = uuid.uuid4()
43
44 self.zone.get(zoneid)
45
46 args, kwargs = mock.call_args
47
48 # Check that the mock function is called with the rights args.
49 self.assertEqual(args[1], f"{BASE_API_URL}/zone/{zoneid}")
50 self.assertEqual(args[0], "GET")
51 self.assertEqual(kwargs["data"], None)
52
53 @patch("requests.Session.request")
54 def test_start(self, mock):
55 """Test if the start method works as expected."""
56 mock.return_value = RESPONSE204
57
58 zoneid = str(uuid.uuid4())
59 duration = randrange(10800)
60
61 self.zone.start(zoneid, duration)
62
63 args, kwargs = mock.call_args
64
65 # Check that the mock function is called with the rights args.
66 self.assertEqual(args[1], f"{BASE_API_URL}/zone/start")
67 self.assertEqual(args[0], "PUT")
68 self.assertEqual(
69 kwargs["data"], json.dumps({"id": zoneid, "duration": duration})
70 )
71
72 # Check that values should be within range.
73 self.assertRaises(AssertionError, self.zone.start, zoneid, -1)
74 self.assertRaises(AssertionError, self.zone.start, zoneid, 10801)
75
76 @patch("requests.Session.request")
77 def test_start_multiple(self, mock):
78 """Test if the start multiple method works as expected."""
79 mock.return_value = RESPONSE204
80 zones = [
81 {"id": data[0], "duration": data[1], "sortOrder": count}
82 for (count, data) in enumerate(self.zones, 1)
83 ]
84 self.zone.start_multiple(zones)
85
86 args, kwargs = mock.call_args
87
88 # Check that the mock function is called with the rights args.
89 self.assertEqual(args[1], f"{BASE_API_URL}/zone/start_multiple")
90 self.assertEqual(args[0], "PUT")
91 self.assertEqual(
92 kwargs["data"],
93 json.dumps(
94 {
95 "zones": [
96 {
97 "id": data[0],
98 "duration": data[1],
99 "sortOrder": count,
100 }
101 for (count, data) in enumerate(self.zones, 1)
102 ]
103 }
104 ),
105 )
106
107 @patch("requests.Session.request")
108 def test_set_moisture_percent(self, mock):
109 """Test if the set moisture percent method works as expected."""
110 mock.return_value = RESPONSE204
111
112 zoneid = str(uuid.uuid4())
113 percent = round(random.random(), 1)
114
115 self.zone.set_moisture_percent(zoneid, percent)
116
117 args, kwargs = mock.call_args
118
119 # Check that the mock function is called with the rights args.
120 self.assertEqual(args[1], f"{BASE_API_URL}/zone/setMoisturePercent")
121 self.assertEqual(args[0], "PUT")
122 self.assertEqual(
123 kwargs["data"], json.dumps({"id": zoneid, "percent": percent})
124 )
125
126 # Check that values should be within range.
127 self.assertRaises(
128 AssertionError, self.zone.set_moisture_percent, zoneid, -0.1
129 )
130 self.assertRaises(
131 AssertionError, self.zone.set_moisture_percent, zoneid, 1.1
132 )
133
134 @patch("requests.Session.request")
135 def test_set_moisture_level(self, mock):
136 """Test if the set moisture level method works as expected."""
137 mock.return_value = RESPONSE204
138
139 zoneid = str(uuid.uuid4())
140 level = round(random.uniform(0.0, 100.0), 2)
141
142 self.zone.set_moisture_level(zoneid, level)
143
144 args, kwargs = mock.call_args
145
146 # Check that the mock function is called with the rights args.
147 self.assertEqual(args[1], f"{BASE_API_URL}/zone/setMoistureLevel")
148 self.assertEqual(args[0], "PUT")
149 self.assertEqual(
150 kwargs["data"], json.dumps({"id": zoneid, "level": level})
151 )
152
153 @patch("requests.Session.request")
154 def test_zoneschedule(self, mock):
155 """Test if the zoneschedule helper class works as expected."""
156 mock.return_value = RESPONSE204
157
158 zoneschedule = self.zone.schedule()
159 for zone in self.zones:
160 zoneschedule.enqueue(zone[0], zone[1])
161 zoneschedule.start()
162
163 args, kwargs = mock.call_args
164
165 # Check that the mock function is called with the rights args.
166 self.assertEqual(args[1], f"{BASE_API_URL}/zone/start_multiple")
167 self.assertEqual(args[0], "PUT")
168 self.assertEqual(
169 kwargs["data"],
170 json.dumps(
171 {
172 "zones": [
173 {
174 "id": data[0],
175 "duration": data[1],
176 "sortOrder": count,
177 }
178 for (count, data) in enumerate(self.zones, 1)
179 ]
180 }
181 ),
182 )
183
184 @patch("requests.Session.request")
185 def test_zoneschedule_with_statement(self, mock):
186 """Test if the zoneschedule with statement works as expected."""
187 mock.return_value = RESPONSE204
188
189 with ZoneSchedule(self.zone) as zoneschedule:
190 for zone in self.zones:
191 zoneschedule.enqueue(zone[0], zone[1])
192
193 args, kwargs = mock.call_args
194
195 # Check that the mock function is called with the rights args.
196 self.assertEqual(args[1], f"{BASE_API_URL}/zone/start_multiple")
197 self.assertEqual(args[0], "PUT")
198 self.assertEqual(
199 kwargs["data"],
200 json.dumps(
201 {
202 "zones": [
203 {
204 "id": data[0],
205 "duration": data[1],
206 "sortOrder": count,
207 }
208 for (count, data) in enumerate(self.zones, 1)
209 ]
210 }
211 ),
212 )