Skip to content

Commit 42080bd

Browse files
authored
Update test framework for dynamic parametrization (python-kasa#810)
1 parent 652696a commit 42080bd

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

kasa/tests/device_fixtures.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from typing import Dict, Set
1+
from typing import Dict, List, Set
22

33
import pytest
44

55
from kasa import (
66
Credentials,
77
Device,
8+
DeviceType,
89
Discover,
910
)
1011
from kasa.iot import IotBulb, IotDimmer, IotLightStrip, IotPlug, IotStrip, IotWallSwitch
@@ -127,13 +128,29 @@
127128
IP_MODEL_CACHE: Dict[str, str] = {}
128129

129130

131+
def parametrize_combine(parametrized: List[pytest.MarkDecorator]):
132+
"""Combine multiple pytest parametrize dev marks into one set of fixtures."""
133+
fixtures = set()
134+
for param in parametrized:
135+
if param.args[0] != "dev":
136+
raise Exception(f"Supplied mark is not for dev fixture: {param.args[0]}")
137+
fixtures.update(param.args[1])
138+
return pytest.mark.parametrize(
139+
"dev",
140+
sorted(list(fixtures)),
141+
indirect=True,
142+
ids=idgenerator,
143+
)
144+
145+
130146
def parametrize(
131147
desc,
132148
*,
133149
model_filter=None,
134150
protocol_filter=None,
135151
component_filter=None,
136152
data_root_filter=None,
153+
device_type_filter=None,
137154
ids=None,
138155
):
139156
if ids is None:
@@ -146,6 +163,7 @@ def parametrize(
146163
protocol_filter=protocol_filter,
147164
component_filter=component_filter,
148165
data_root_filter=data_root_filter,
166+
device_type_filter=device_type_filter,
149167
),
150168
indirect=True,
151169
ids=ids,
@@ -169,7 +187,6 @@ def parametrize(
169187
protocol_filter={"IOT"},
170188
)
171189

172-
bulb = parametrize("bulbs", model_filter=BULBS, protocol_filter={"SMART", "IOT"})
173190
plug = parametrize("plugs", model_filter=PLUGS, protocol_filter={"IOT", "SMART"})
174191
plug_iot = parametrize("plugs iot", model_filter=PLUGS, protocol_filter={"IOT"})
175192
wallswitch = parametrize(
@@ -216,9 +233,16 @@ def parametrize(
216233
model_filter=BULBS_IOT_VARIABLE_TEMP,
217234
protocol_filter={"IOT"},
218235
)
236+
237+
bulb_smart = parametrize(
238+
"bulb devices smart",
239+
device_type_filter=[DeviceType.Bulb, DeviceType.LightStrip],
240+
protocol_filter={"SMART"},
241+
)
219242
bulb_iot = parametrize(
220243
"bulb devices iot", model_filter=BULBS_IOT, protocol_filter={"IOT"}
221244
)
245+
bulb = parametrize_combine([bulb_smart, bulb_iot])
222246

223247
strip_iot = parametrize(
224248
"strip devices iot", model_filter=STRIPS_IOT, protocol_filter={"IOT"}
@@ -233,9 +257,6 @@ def parametrize(
233257
switch_smart = parametrize(
234258
"switch devices smart", model_filter=SWITCHES_SMART, protocol_filter={"SMART"}
235259
)
236-
bulb_smart = parametrize(
237-
"bulb devices smart", model_filter=BULBS_SMART, protocol_filter={"SMART"}
238-
)
239260
dimmers_smart = parametrize(
240261
"dimmer devices smart", model_filter=DIMMERS_SMART, protocol_filter={"SMART"}
241262
)

kasa/tests/fixtureinfo.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
from pathlib import Path
55
from typing import Dict, List, NamedTuple, Optional, Set
66

7+
from kasa.device_factory import _get_device_type_from_sys_info
8+
from kasa.device_type import DeviceType
9+
from kasa.smart.smartdevice import SmartDevice
10+
711

812
class FixtureInfo(NamedTuple):
913
name: str
@@ -83,6 +87,7 @@ def filter_fixtures(
8387
protocol_filter: Optional[Set[str]] = None,
8488
model_filter: Optional[Set[str]] = None,
8589
component_filter: Optional[str] = None,
90+
device_type_filter: Optional[List[DeviceType]] = None,
8691
):
8792
"""Filter the fixtures based on supplied parameters.
8893
@@ -108,6 +113,19 @@ def _component_match(fixture_data: FixtureInfo, component_filter):
108113
}
109114
return component_filter in components
110115

116+
def _device_type_match(fixture_data: FixtureInfo, device_type):
117+
if (component_nego := fixture_data.data.get("component_nego")) is None:
118+
return _get_device_type_from_sys_info(fixture_data.data) in device_type
119+
components = [component["id"] for component in component_nego["component_list"]]
120+
if (info := fixture_data.data.get("get_device_info")) and (
121+
type_ := info.get("type")
122+
):
123+
return (
124+
SmartDevice._get_device_type_from_components(components, type_)
125+
in device_type
126+
)
127+
return False
128+
111129
filtered = []
112130
if protocol_filter is None:
113131
protocol_filter = {"IOT", "SMART"}
@@ -120,6 +138,10 @@ def _component_match(fixture_data: FixtureInfo, component_filter):
120138
continue
121139
if component_filter and not _component_match(fixture_data, component_filter):
122140
continue
141+
if device_type_filter and not _device_type_match(
142+
fixture_data, device_type_filter
143+
):
144+
continue
123145

124146
filtered.append(fixture_data)
125147

0 commit comments

Comments
 (0)