Debian Codegraph @

tests/test_valve.py

raw symbols SCIP
1 """Valve object test module"""
2
3 import unittest
4 from unittest.mock import patch
5 import uuid
6 import json
7
8 from random import randrange
9 from rachiopy import Valve
10 from tests.constants import VALVE_API_URL, AUTHTOKEN, RESPONSE200, RESPONSE204
11
12
13 class TestValveMethods(unittest.TestCase):
14 """Class containing the Valve object test cases."""
15
16 def setUp(self):
17 self.valve = Valve(AUTHTOKEN)
18
19 def test_init(self):
20 """Test if the constructor works as expected."""
21 self.assertEqual(self.valve.authtoken, AUTHTOKEN)
22
23 @patch("requests.Session.request")
24 def test_get_valve(self, mock):
25 """Test if the get_valve method works as expected."""
26 mock.return_value = RESPONSE200
27
28 valveid = uuid.uuid4()
29
30 self.valve.get_valve(valveid)
31
32 args, kwargs = mock.call_args
33
34 # Check that the mock function is called with the rights args.
35 self.assertEqual(args[1], f"{VALVE_API_URL}/valve/getValve/{valveid}")
36 self.assertEqual(args[0], "GET")
37 self.assertEqual(kwargs["data"], None)
38
39 @patch("requests.Session.request")
40 def test_get_base_station(self, mock):
41 """Test if the get_base_station method works as expected."""
42 mock.return_value = RESPONSE200
43
44 baseid = str(uuid.uuid4())
45
46 self.valve.get_base_station(baseid)
47
48 args, kwargs = mock.call_args
49
50 # Check that the mock function is called with the rights args.
51 self.assertEqual(
52 args[1], f"{VALVE_API_URL}/valve/getBaseStation/{baseid}"
53 )
54 self.assertEqual(args[0], "GET")
55 self.assertEqual(kwargs["data"], None)
56
57 @patch("requests.Session.request")
58 def test_list_base_stations(self, mock):
59 """Test if the list_base_stations method works as expected."""
60 mock.return_value = RESPONSE200
61
62 userid = str(uuid.uuid4())
63
64 self.valve.list_base_stations(userid)
65
66 args, kwargs = mock.call_args
67
68 # Check that the mock function is called with the rights args.
69 self.assertEqual(
70 args[1], f"{VALVE_API_URL}/valve/listBaseStations/{userid}"
71 )
72 self.assertEqual(args[0], "GET")
73 self.assertEqual(kwargs["data"], None)
74
75 @patch("requests.Session.request")
76 def test_list_valves(self, mock):
77 """Test if the list_valves method works as expected."""
78 mock.return_value = RESPONSE200
79
80 baseid = str(uuid.uuid4())
81
82 self.valve.list_valves(baseid)
83
84 args, kwargs = mock.call_args
85
86 # Check that the mock function is called with the rights args.
87 self.assertEqual(args[1], f"{VALVE_API_URL}/valve/listValves/{baseid}")
88 self.assertEqual(args[0], "GET")
89 self.assertEqual(kwargs["data"], None)
90
91 @patch("requests.Session.request")
92 def test_set_default_runtime(self, mock):
93 """Test if the set_default_runtime method works as expected."""
94 mock.return_value = RESPONSE200
95
96 valveid = str(uuid.uuid4())
97 duration = randrange(86400)
98
99 self.valve.set_default_runtime(valveid, duration)
100
101 args, kwargs = mock.call_args
102
103 # Check that the mock function is called with the rights args.
104 self.assertEqual(args[1], f"{VALVE_API_URL}/valve/setDefaultRuntime")
105 self.assertEqual(args[0], "PUT")
106 self.assertEqual(
107 kwargs["data"],
108 json.dumps(
109 {"valveId": valveid, "defaultRuntimeSeconds": duration}
110 ),
111 )
112
113 @patch("requests.Session.request")
114 def test_set_default_runtime_exception(self, mock):
115 """Test if the set_default_runtime method catches incorrect values."""
116 mock.return_value = RESPONSE200
117
118 valveid = str(uuid.uuid4())
119 duration1 = randrange(-50, -1)
120 duration2 = randrange(86401, 86500)
121
122 # Check that values should be within range.
123 self.assertRaises(
124 AssertionError, self.valve.start_watering, valveid, duration1
125 )
126 self.assertRaises(
127 AssertionError, self.valve.start_watering, valveid, duration2
128 )
129
130 @patch("requests.Session.request")
131 def test_start_watering(self, mock):
132 """Test if the start_watering method works as expected."""
133 mock.return_value = RESPONSE204
134
135 valveid = str(uuid.uuid4())
136 duration = randrange(86400)
137
138 self.valve.start_watering(valveid, duration)
139
140 args, kwargs = mock.call_args
141
142 # Check that the mock function is called with the rights args.
143 self.assertEqual(args[1], f"{VALVE_API_URL}/valve/startWatering")
144 self.assertEqual(args[0], "PUT")
145 self.assertEqual(
146 kwargs["data"],
147 json.dumps({"valveId": valveid, "durationSeconds": duration}),
148 )
149
150 @patch("requests.Session.request")
151 def test_start_watering_exception(self, mock):
152 """Test if the start_watering method catches incorrect values."""
153 mock.return_value = RESPONSE204
154
155 valveid = str(uuid.uuid4())
156 duration1 = randrange(-50, -1)
157 duration2 = randrange(86401, 86500)
158
159 # Check that values should be within range.
160 self.assertRaises(
161 AssertionError, self.valve.start_watering, valveid, duration1
162 )
163 self.assertRaises(
164 AssertionError, self.valve.start_watering, valveid, duration2
165 )
166
167 @patch("requests.Session.request")
168 def test_stop_watering(self, mock):
169 """Test if the stop_watering method works as expected."""
170 mock.return_value = RESPONSE204
171
172 valveid = str(uuid.uuid4())
173
174 self.valve.stop_watering(valveid)
175
176 args, kwargs = mock.call_args
177
178 # Check that the mock function is called with the rights args.
179 self.assertEqual(args[1], f"{VALVE_API_URL}/valve/stopWatering")
180 self.assertEqual(args[0], "PUT")
181 self.assertEqual(kwargs["data"], json.dumps({"valveId": valveid}))