File: test_zone.py

package info (click to toggle)
rachiopy 1.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 328 kB
  • sloc: python: 1,053; makefile: 14; sh: 5
file content (212 lines) | stat: -rw-r--r-- 7,023 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""Zone object test module"""

import unittest
from unittest.mock import patch
import uuid
import random
import json

from random import randrange
from rachiopy import Zone
from rachiopy.zone import ZoneSchedule
from tests.constants import BASE_API_URL, AUTHTOKEN, RESPONSE200, RESPONSE204


class TestZoneMethods(unittest.TestCase):
    """Class containing the Zone object test cases."""

    def setUp(self):
        self.zone = Zone(AUTHTOKEN)

        zone1id = str(uuid.uuid4())
        zone2id = str(uuid.uuid4())
        zone3id = str(uuid.uuid4())
        duration1 = randrange(10800)
        duration2 = randrange(10800)
        duration3 = randrange(10800)

        self.zones = []
        self.zones.append((zone1id, duration1))
        self.zones.append((zone2id, duration2))
        self.zones.append((zone3id, duration3))

    def test_init(self):
        """Test if the constructor works as expected."""
        self.assertEqual(self.zone.authtoken, AUTHTOKEN)

    @patch("requests.Session.request")
    def test_get(self, mock):
        """Test if the get method works as expected."""
        mock.return_value = RESPONSE200

        zoneid = uuid.uuid4()

        self.zone.get(zoneid)

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(args[1], f"{BASE_API_URL}/zone/{zoneid}")
        self.assertEqual(args[0], "GET")
        self.assertEqual(kwargs["data"], None)

    @patch("requests.Session.request")
    def test_start(self, mock):
        """Test if the start method works as expected."""
        mock.return_value = RESPONSE204

        zoneid = str(uuid.uuid4())
        duration = randrange(10800)

        self.zone.start(zoneid, duration)

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(args[1], f"{BASE_API_URL}/zone/start")
        self.assertEqual(args[0], "PUT")
        self.assertEqual(
            kwargs["data"], json.dumps({"id": zoneid, "duration": duration})
        )

        # Check that values should be within range.
        self.assertRaises(AssertionError, self.zone.start, zoneid, -1)
        self.assertRaises(AssertionError, self.zone.start, zoneid, 10801)

    @patch("requests.Session.request")
    def test_start_multiple(self, mock):
        """Test if the start multiple method works as expected."""
        mock.return_value = RESPONSE204
        zones = [
            {"id": data[0], "duration": data[1], "sortOrder": count}
            for (count, data) in enumerate(self.zones, 1)
        ]
        self.zone.start_multiple(zones)

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(args[1], f"{BASE_API_URL}/zone/start_multiple")
        self.assertEqual(args[0], "PUT")
        self.assertEqual(
            kwargs["data"],
            json.dumps(
                {
                    "zones": [
                        {
                            "id": data[0],
                            "duration": data[1],
                            "sortOrder": count,
                        }
                        for (count, data) in enumerate(self.zones, 1)
                    ]
                }
            ),
        )

    @patch("requests.Session.request")
    def test_set_moisture_percent(self, mock):
        """Test if the set moisture percent method works as expected."""
        mock.return_value = RESPONSE204

        zoneid = str(uuid.uuid4())
        percent = round(random.random(), 1)

        self.zone.set_moisture_percent(zoneid, percent)

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(args[1], f"{BASE_API_URL}/zone/setMoisturePercent")
        self.assertEqual(args[0], "PUT")
        self.assertEqual(
            kwargs["data"], json.dumps({"id": zoneid, "percent": percent})
        )

        # Check that values should be within range.
        self.assertRaises(
            AssertionError, self.zone.set_moisture_percent, zoneid, -0.1
        )
        self.assertRaises(
            AssertionError, self.zone.set_moisture_percent, zoneid, 1.1
        )

    @patch("requests.Session.request")
    def test_set_moisture_level(self, mock):
        """Test if the set moisture level method works as expected."""
        mock.return_value = RESPONSE204

        zoneid = str(uuid.uuid4())
        level = round(random.uniform(0.0, 100.0), 2)

        self.zone.set_moisture_level(zoneid, level)

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(args[1], f"{BASE_API_URL}/zone/setMoistureLevel")
        self.assertEqual(args[0], "PUT")
        self.assertEqual(
            kwargs["data"], json.dumps({"id": zoneid, "level": level})
        )

    @patch("requests.Session.request")
    def test_zoneschedule(self, mock):
        """Test if the zoneschedule helper class works as expected."""
        mock.return_value = RESPONSE204

        zoneschedule = self.zone.schedule()
        for zone in self.zones:
            zoneschedule.enqueue(zone[0], zone[1])
        zoneschedule.start()

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(args[1], f"{BASE_API_URL}/zone/start_multiple")
        self.assertEqual(args[0], "PUT")
        self.assertEqual(
            kwargs["data"],
            json.dumps(
                {
                    "zones": [
                        {
                            "id": data[0],
                            "duration": data[1],
                            "sortOrder": count,
                        }
                        for (count, data) in enumerate(self.zones, 1)
                    ]
                }
            ),
        )

    @patch("requests.Session.request")
    def test_zoneschedule_with_statement(self, mock):
        """Test if the zoneschedule with statement works as expected."""
        mock.return_value = RESPONSE204

        with ZoneSchedule(self.zone) as zoneschedule:
            for zone in self.zones:
                zoneschedule.enqueue(zone[0], zone[1])

        args, kwargs = mock.call_args

        # Check that the mock function is called with the rights args.
        self.assertEqual(args[1], f"{BASE_API_URL}/zone/start_multiple")
        self.assertEqual(args[0], "PUT")
        self.assertEqual(
            kwargs["data"],
            json.dumps(
                {
                    "zones": [
                        {
                            "id": data[0],
                            "duration": data[1],
                            "sortOrder": count,
                        }
                        for (count, data) in enumerate(self.zones, 1)
                    ]
                }
            ),
        )