File: test_init.py

package info (click to toggle)
python-mill-local 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 164 kB
  • sloc: python: 350; sh: 5; makefile: 2
file content (267 lines) | stat: -rw-r--r-- 12,837 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""Test Mill."""
import json

import pytest
from aiohttp import ClientResponseError

from mill_local import Mill, MillOilHeater, OperationMode, OilHeaterPowerLevels

device_ip = "192.168.2.123"
local_api_url = f"http://{device_ip}"


async def test_init_when_websession_is_present(client_session):
    """Test Mill init and default values."""
    mill = Mill(device_ip, client_session)

    # test default values
    assert mill.device_ip == device_ip
    assert mill.websession is not None
    assert mill.url == local_api_url
    assert mill._timeout_seconds == 15
    assert mill.name == ""
    assert mill.version == ""
    assert mill.mac_address is None


async def test_connect_when_successful(mocked_response, client_session, status_command_response):
    """Test successful connection to Mill device."""
    mill = Mill(device_ip, client_session)
    mocked_response.get(f"{local_api_url}/status", status=200, payload=status_command_response)
    returned_data = await mill.connect()

    # assert returned data, we don't bother asserting every response property
    assert returned_data is not None
    assert len(returned_data.keys()) == 6

    # assert data in Mill object
    assert mill.name == "Mill panel"
    assert mill.version == "0x221017"
    assert mill.mac_address == "13:37:A6:5E:D3:CB"


async def test_connect_when_error_raised(mocked_response, client_session):
    """Test error raised when connecting to Mill device and None returned."""
    mill = Mill(device_ip, client_session)
    mocked_response.get(f"{local_api_url}/status", status=400)

    with pytest.raises(ClientResponseError) as exp_400_info:
        returned_data = await mill.connect()
        assert exp_400_info.value.status == 400
        assert returned_data is None
        assert mill.name == ""
        assert mill.version == ""
        assert mill.mac_address is None


async def test_fetch_heater_sensor_when_successful(mocked_response, client_session, control_status_response):
    """Test successful reading heater and sensor data."""
    mill = Mill(device_ip, client_session)
    mocked_response.get(f"{local_api_url}/control-status", status=200, payload=control_status_response)

    returned_data = await mill.fetch_heater_and_sensor_data()
    assert returned_data is not None
    # we don't bother asserting every response property
    assert len(returned_data.keys()) == 11


async def test_fetch_heater_sensor_when_error_raised(mocked_response, client_session, control_status_response):
    """Test error raised when reading heater and sensor data and None returned."""
    mill = Mill(device_ip, client_session)
    mocked_response.get(f"{local_api_url}/control-status", status=400)

    with pytest.raises(ClientResponseError) as exp_400_info:
        returned_data = await mill.fetch_heater_and_sensor_data()
        assert exp_400_info.value.status == 400
        assert returned_data is None


async def test_set_target_temperature_when_successful(mocked_response, client_session,
                                                      generic_status_ok_response):
    """Test successful setting the device target temperature."""
    mill = Mill(device_ip, client_session)
    mocked_response.post(f"{local_api_url}/set-temperature", status=200, payload=generic_status_ok_response)

    returned_data = await mill.set_target_temperature(20.5)
    assert returned_data is None
    mocked_response.assert_called_once_with(url=f"{local_api_url}/set-temperature",
                                            method="POST",
                                            data=json.dumps({
                                                "type": "Normal",
                                                "value": 20.5
                                            }))


async def test_set_target_temperature_when_error_raised(mocked_response, client_session,
                                                        generic_status_ok_response):
    """Test error raised when setting the device target temperature."""
    mill = Mill(device_ip, client_session)
    mocked_response.post(f"{local_api_url}/set-temperature", status=400)

    with pytest.raises(ClientResponseError) as exp_400_info:
        returned_data = await mill.set_target_temperature(20.5)
        assert exp_400_info.value.status == 400
        assert returned_data is None
        mocked_response.assert_called_once_with(url=f"{local_api_url}/set-temperature",
                                                method="POST",
                                                data=json.dumps({
                                                    "type": "Normal",
                                                    "value": 20.5
                                                }))

async def test_set_heater_power_when_successful(mocked_response, client_session,
                                                      generic_status_ok_response):
    """Test successful setting the device oil heater power."""
    mill = MillOilHeater(device_ip, client_session)
    mocked_response.post(f"{local_api_url}/oil-heater-power", status=200, payload=generic_status_ok_response)

    returned_data = await mill.set_heater_power(OilHeaterPowerLevels.HIGH)
    assert returned_data is None
    mocked_response.assert_called_once_with(url=f"{local_api_url}/oil-heater-power",
                                            method="POST",
                                            data=json.dumps({
                                                "heating_level_percentage": OilHeaterPowerLevels.HIGH.value,
                                            }))


async def test_set_heater_power_when_error_raised(mocked_response, client_session,
                                                        generic_status_ok_response):
    """Test error raised when setting the device oil heater power."""
    mill = MillOilHeater(device_ip, client_session)
    mocked_response.post(f"{local_api_url}/oil-heater-power", status=400)

    with pytest.raises(ClientResponseError) as exp_400_info:
        returned_data = await mill.set_heater_power(OilHeaterPowerLevels.HIGH)
        assert exp_400_info.value.status == 400
        assert returned_data is None
        mocked_response.assert_called_once_with(url=f"{local_api_url}/oil-heater-power",
                                                method="POST",
                                                data=json.dumps({
                                                    "heating_level_percentage": OilHeaterPowerLevels.HIGH.value,
                                                }))


async def test_fetch_heater_power_when_successful(mocked_response, client_session, oil_heater_power_response):
    """Test successful reading heater and sensor data."""
    mill = MillOilHeater(device_ip, client_session)
    mocked_response.get(f"{local_api_url}/oil-heater-power", status=200, payload=oil_heater_power_response)

    returned_data = await mill.fetch_heater_power_data()
    assert returned_data is not None
    assert len(returned_data.keys()) == 2
    assert type(returned_data.get("value")) is int


async def test_fetch_heater_power_when_error_raised(mocked_response, client_session, oil_heater_power_response):
    """Test error raised when reading heater and sensor data and None returned."""
    mill = MillOilHeater(device_ip, client_session)
    mocked_response.get(f"{local_api_url}/oil-heater-power", status=400)

    with pytest.raises(ClientResponseError) as exp_400_info:
        returned_data = await mill.fetch_heater_power_data()
        assert exp_400_info.value.status == 400
        assert returned_data is None


async def test_set_operation_mode_control_individually_when_successful(mocked_response, client_session,
                                                                       generic_status_ok_response):
    """Test successful setting the device operation mode to 'control individually'."""
    mill = Mill(device_ip, client_session)
    mocked_response.post(f"{local_api_url}/operation-mode", status=200, payload=generic_status_ok_response)

    returned_data = await mill.set_operation_mode_control_individually()
    assert returned_data is None
    mocked_response.assert_called_once_with(url=f"{local_api_url}/operation-mode",
                                            method="POST",
                                            data=json.dumps({
                                                "mode": OperationMode.CONTROL_INDIVIDUALLY.value
                                            }))


async def test_set_operation_mode_control_individually_when_error_raised(mocked_response, client_session,
                                                                       generic_status_ok_response):
    """Test error raised when setting the device operation mode to 'control individually'."""
    mill = Mill(device_ip, client_session)
    mocked_response.post(f"{local_api_url}/operation-mode", status=400)

    with pytest.raises(ClientResponseError) as exp_400_info:
        returned_data = await mill.set_operation_mode_control_individually()
        assert exp_400_info.value.status == 400
        assert returned_data is None
        mocked_response.assert_called_once_with(url=f"{local_api_url}/operation-mode",
                                                method="POST",
                                                data=json.dumps({
                                                    "mode": OperationMode.CONTROL_INDIVIDUALLY.value
                                                }))


async def test_set_operation_mode_off_when_successful(mocked_response, client_session, generic_status_ok_response):
    """Test successful setting the device operation mode to 'off'."""
    mill = Mill(device_ip, client_session)
    mocked_response.post(f"{local_api_url}/operation-mode", status=200, payload=generic_status_ok_response)

    returned_data = await mill.set_operation_mode_off()
    assert returned_data is None
    mocked_response.assert_called_once_with(url=f"{local_api_url}/operation-mode",
                                            method="POST",
                                            data=json.dumps({
                                                "mode": OperationMode.OFF.value
                                            }))


async def test_set_operation_mode_off_when_error_raised(mocked_response, client_session, generic_status_ok_response):
    """Test error raised when setting the device operation mode to 'off'."""
    mill = Mill(device_ip, client_session)
    mocked_response.post(f"{local_api_url}/operation-mode", status=400)

    with pytest.raises(ClientResponseError) as exp_400_info:
        returned_data = await mill.set_operation_mode_off()
        assert exp_400_info.value.status == 400
        assert returned_data is None
        mocked_response.assert_called_once_with(url=f"{local_api_url}/operation-mode",
                                                method="POST",
                                                data=json.dumps({
                                                    "mode": OperationMode.OFF.value
                                                }))


async def test_post_request_rais_error_on_400_and_500(mocked_response, client_session):
    """Test that get_request rais exception when status 400 or higher."""
    mill = Mill(device_ip, client_session)
    # with response body
    mocked_response.post(f"{local_api_url}/operation-mode", status=400, body=json.dumps({
        "status": "Failed to parse message body"
    }))
    # without response body
    mocked_response.post(f"{local_api_url}/operation-mode", status=500)

    with pytest.raises(ClientResponseError) as exp_400_info:
        await mill._post_request(command="operation-mode", payload={"mode": OperationMode.OFF.value})

    assert exp_400_info.value.status == 400

    with pytest.raises(ClientResponseError) as exp_500_info:
        await mill._post_request(command="operation-mode", payload={"mode": OperationMode.OFF.value})

    assert exp_500_info.value.status == 500


async def test_get_request_rais_error_on_400_and_500(mocked_response, client_session):
    """Test that get_request rais exception when status 400 or higher."""
    mill = Mill(device_ip, client_session)
    # with response body
    mocked_response.get(f"{local_api_url}/status", status=400, body=json.dumps({
        "status": "Failed to parse message body"
    }))
    # without response body
    mocked_response.get(f"{local_api_url}/status", status=500)

    with pytest.raises(ClientResponseError) as exp_400_info:
        await mill._get_request("status")

    assert exp_400_info.value.status == 400

    with pytest.raises(ClientResponseError) as exp_500_info:
        await mill._get_request("status")

    assert exp_500_info.value.status == 500