File: test_module.py

package info (click to toggle)
pytouchlinesl 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 380 kB
  • sloc: python: 758; makefile: 2
file content (214 lines) | stat: -rw-r--r-- 6,579 bytes parent folder | download
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
import asyncio

import pytest

from pytouchlinesl.client.models import GlobalScheduleModel
from pytouchlinesl.zone import Zone


@pytest.mark.asyncio
async def test_zones(test_module):
    zones = await test_module.zones()
    # There should be 10 enabled zones
    assert len(zones) == 10
    # Ensure that each of the zones has been correctly constructed by the
    # modules method.
    for z in zones:
        assert isinstance(z, Zone)
        if z.mode == "constantTemp":
            assert z.schedule is None
        else:
            assert isinstance(z.schedule, GlobalScheduleModel)


@pytest.mark.asyncio
async def test_zones_include_off(test_module):
    zones = await test_module.zones(include_off=True)
    # There should be 40 total zones, 10 enabled, 30 disabled
    assert len(zones) == 40


@pytest.mark.asyncio
async def test_zones_include_off_and_refresh(test_module):
    zones = await test_module.zones(include_off=True, refresh=True)
    # There should be 40 total zones, 10 enabled, 30 disabled
    assert len(zones) == 40


@pytest.mark.asyncio
async def test_zones_cache(test_module):
    await test_module.zones()
    initial_fetch_time = test_module._last_fetched
    await asyncio.sleep(0.5)

    await test_module.zones()
    assert initial_fetch_time == test_module._last_fetched


@pytest.mark.asyncio
async def test_zones_cache_expired(test_module_short_cache):
    await test_module_short_cache.zones()
    initial_fetch_time = test_module_short_cache._last_fetched
    await asyncio.sleep(0.25)

    await test_module_short_cache.zones()
    assert initial_fetch_time != test_module_short_cache._last_fetched


@pytest.mark.asyncio
async def test_zones_force_refresh(test_module):
    await test_module.zones()
    initial_fetch_time = test_module._last_fetched
    await asyncio.sleep(0.5)
    await test_module.zones(refresh=True)
    assert initial_fetch_time != test_module._last_fetched


@pytest.mark.asyncio
@pytest.mark.parametrize("id", [2335, 2354])  # test both enabled and disabled zones
async def test_zone(id, test_module):
    z = await test_module.zone(zone_id=id)
    assert z.id == id


@pytest.mark.asyncio
async def test_zone_non_existent(test_module):
    z = await test_module.zone(zone_id=1234)
    assert z is None


@pytest.mark.asyncio
async def test_zone_refresh(test_module):
    await test_module.zones()
    last_fetched = test_module._last_fetched
    await asyncio.sleep(0.5)
    await test_module.zone(zone_id=2335, refresh=True)
    assert last_fetched != test_module._last_fetched


@pytest.mark.asyncio
@pytest.mark.parametrize("name", ["Kitchen", "Zone 2"])  # test both enabled and disabled zones
async def test_zone_by_name(name, test_module):
    z = await test_module.zone_by_name(zone_name=name)
    assert z.name == name


@pytest.mark.asyncio
async def test_zone_by_name_non_existent(test_module):
    z = await test_module.zone_by_name(zone_name="Foobar")
    assert z is None


@pytest.mark.asyncio
async def test_zone_by_name_refresh(test_module):
    await test_module.zones()
    last_fetched = test_module._last_fetched
    await asyncio.sleep(0.5)
    await test_module.zone_by_name(zone_name="Kitchen", refresh=True)
    assert last_fetched != test_module._last_fetched


@pytest.mark.asyncio
async def test_schedules(test_module):
    schedules = await test_module.schedules()
    # There should be 10 enabled schedules
    assert len(schedules) == 5
    # Ensure that each of the schedules has been correctly constructed by the
    # modules method.
    for z in schedules:
        assert isinstance(z, GlobalScheduleModel)


@pytest.mark.asyncio
async def test_schedules_cache(test_module):
    await test_module.schedules()
    initial_fetch_time = test_module._last_fetched
    await asyncio.sleep(0.5)

    await test_module.schedules()
    assert initial_fetch_time == test_module._last_fetched


@pytest.mark.asyncio
async def test_schedules_cache_expired(test_module_short_cache):
    await test_module_short_cache.schedules()
    initial_fetch_time = test_module_short_cache._last_fetched
    await asyncio.sleep(0.5)

    await test_module_short_cache.schedules()
    assert initial_fetch_time != test_module_short_cache._last_fetched


@pytest.mark.asyncio
async def test_schedules_force_refresh(test_module):
    await test_module.schedules()
    initial_fetch_time = test_module._last_fetched
    await asyncio.sleep(0.5)
    await test_module.schedules(refresh=True)
    assert initial_fetch_time != test_module._last_fetched


@pytest.mark.asyncio
@pytest.mark.parametrize("id", [2948, 2967])  # test both enabled and disabled zones
async def test_schedule(id, test_module):
    z = await test_module.schedule(schedule_id=id)
    assert z.id == id


@pytest.mark.asyncio
async def test_schedule_non_existent(test_module):
    z = await test_module.schedule(schedule_id=1234)
    assert z is None


@pytest.mark.asyncio
async def test_schedule_refresh(test_module):
    await test_module.schedules()
    last_fetched = test_module._last_fetched
    await asyncio.sleep(0.5)
    await test_module.schedule(schedule_id=2948, refresh=True)
    assert last_fetched != test_module._last_fetched


@pytest.mark.asyncio
@pytest.mark.parametrize("name", ["Living Spaces", "Bedrooms"])
async def test_schedule_by_name(name, test_module):
    z = await test_module.schedule_by_name(schedule_name=name)
    assert z.name == name


@pytest.mark.asyncio
async def test_schedule_by_name_non_existent(test_module):
    z = await test_module.schedule_by_name(schedule_name="Foobar")
    assert z is None


@pytest.mark.asyncio
async def test_schedule_by_name_refresh(test_module):
    await test_module.schedules()
    last_fetched = test_module._last_fetched
    await asyncio.sleep(0.5)
    await test_module.schedule_by_name(schedule_name="Holiday", refresh=True)
    assert last_fetched != test_module._last_fetched


@pytest.mark.asyncio
@pytest.mark.parametrize("idx", [0, 1])
async def test_schedule_by_idx(idx, test_module):
    z = await test_module.schedule_by_idx(schedule_idx=idx)
    assert z.index == idx


@pytest.mark.asyncio
async def test_schedule_by_idx_non_existent(test_module):
    z = await test_module.schedule_by_idx(schedule_idx=10)
    assert z is None


@pytest.mark.asyncio
async def test_schedule_by_idx_refresh(test_module):
    await test_module.schedules()
    last_fetched = test_module._last_fetched
    await asyncio.sleep(0.5)
    await test_module.schedule_by_idx(schedule_idx="Holiday", refresh=True)
    assert last_fetched != test_module._last_fetched