File: test_api.py

package info (click to toggle)
python-huum 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: python: 454; makefile: 2
file content (167 lines) | stat: -rw-r--r-- 4,780 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
from typing import Any
from unittest import TestCase
from unittest.mock import patch

import pytest

from huum.exceptions import BadRequest, Forbidden, NotAuthenticated, RequestError
from huum.huum import Huum
from huum.schemas import HuumStatusResponse
from tests.utils import MockResponse


@pytest.mark.asyncio
@patch("aiohttp.ClientSession._request")
async def test_status_idle(mock_request: Any) -> None:
    idle_status_response = {
        "maxHeatingTime": "3",
        "statusCode": 232,
        "door": True,
        "paymentEndDate": None,
        "temperature": "21",
        "saunaName": "test",
    }
    expected_result = HuumStatusResponse.from_dict(idle_status_response)
    mock_request.return_value = MockResponse(idle_status_response, 200)

    huum = Huum("test", "test")
    await huum.open_session()
    response = await huum.status()

    TestCase().assertDictEqual(response.to_dict(), expected_result.to_dict())


@pytest.mark.asyncio
@patch("aiohttp.ClientSession._request")
async def test_status_heating(mock_request: Any) -> None:
    heating_status_response = {
        "maxHeatingTime": "3",
        "statusCode": 231,
        "door": True,
        "paymentEndDate": None,
        "temperature": "21",
        "targetTemperature": "75",
        "startDate": 1631623054,
        "endDate": 1631633854,
        "duration": 179,
        "saunaName": "test",
    }
    expected_result = HuumStatusResponse.from_dict(heating_status_response)
    mock_request.return_value = MockResponse(heating_status_response, 200)

    huum = Huum("test", "test")
    await huum.open_session()
    response = await huum.status()

    TestCase().assertDictEqual(response.to_dict(), expected_result.to_dict())


@pytest.mark.asyncio
@patch("aiohttp.ClientSession._request")
async def test_heating_stop(mock_request: Any) -> None:
    heating_stop_response = {
        "maxHeatingTime": "3",
        "statusCode": 232,
        "door": True,
        "paymentEndDate": None,
        "temperature": "22",
        "targetTemperature": "75",
        "startDate": 1631685790,
        "endDate": 1631685790,
        "duration": 0,
        "saunaName": "test",
    }
    expected_result = HuumStatusResponse.from_dict(heating_stop_response)
    mock_request.return_value = MockResponse(heating_stop_response, 200)

    huum = Huum("test", "test")
    await huum.open_session()
    response = await huum.turn_off()

    TestCase().assertDictEqual(response.to_dict(), expected_result.to_dict())


@pytest.mark.asyncio
@patch("aiohttp.ClientSession._request")
async def test_heating_start(mock_request: Any) -> None:
    heating_start_response = {
        "maxHeatingTime": "3",
        "statusCode": 231,
        "door": True,
        "paymentEndDate": None,
        "temperature": "22",
        "targetTemperature": "75",
        "startDate": 1631685780,
        "endDate": 1631696580,
        "duration": 180,
        "saunaName": "test",
    }
    expected_result = HuumStatusResponse.from_dict(heating_start_response)
    mock_request.return_value = MockResponse(heating_start_response, 200)

    huum = Huum("test", "test")
    await huum.open_session()
    response = await huum.turn_on(75)

    TestCase().assertDictEqual(response.to_dict(), expected_result.to_dict())


@pytest.mark.asyncio
@patch("aiohttp.ClientSession._request")
async def test_toggle_light(mock_request: Any) -> None:
    toggle_light_response = {
        "maxHeatingTime": "3",
        "statusCode": 231,
        "door": True,
        "paymentEndDate": None,
        "temperature": "22",
        "targetTemperature": "75",
        "startDate": 1631685780,
        "endDate": 1631696580,
        "duration": 180,
        "saunaName": "test",
        "light": 1,
    }
    expected_result = HuumStatusResponse.from_dict(toggle_light_response)
    mock_request.return_value = MockResponse(toggle_light_response, 200)

    huum = Huum("test", "test")
    await huum.open_session()
    response = await huum.toggle_light()

    TestCase().assertDictEqual(response.to_dict(), expected_result.to_dict())


@pytest.mark.parametrize(
    (
        "status_code",
        "exception",
    ),
    [
        (
            400,
            BadRequest,
        ),
        (
            401,
            NotAuthenticated,
        ),
        (
            403,
            Forbidden,
        ),
        (
            500,
            RequestError,
        ),
    ],
)
@pytest.mark.asyncio
@patch("aiohttp.ClientSession._request")
async def test_error_codes(mock_request: Any, status_code: int, exception: type[Exception]) -> None:
    mock_request.return_value = MockResponse({}, status_code)

    huum = Huum("test", "test")
    await huum.open_session()
    with pytest.raises(exception):
        await huum.status()