mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-22 19:23:34 +00:00
Put child fixtures in subfolder (#809)
This should prevent child fixtures from hubs breaking tests due to missing discovery info. To get these devices in `filter_fixtures` include protocol string of `SMART.CHILD`.
This commit is contained in:
parent
0d5a3c8439
commit
ced879498b
@ -39,6 +39,7 @@ SmartCall = namedtuple("SmartCall", "module request should_succeed child_device_
|
||||
FixtureResult = namedtuple("FixtureResult", "filename, folder, data")
|
||||
|
||||
SMART_FOLDER = "kasa/tests/fixtures/smart/"
|
||||
SMART_CHILD_FOLDER = "kasa/tests/fixtures/smart/child/"
|
||||
IOT_FOLDER = "kasa/tests/fixtures/"
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -531,7 +532,9 @@ def get_smart_child_fixture(response):
|
||||
model += f"({region})"
|
||||
|
||||
save_filename = f"{model}_{hw_version}_{sw_version}.json"
|
||||
return FixtureResult(filename=save_filename, folder=SMART_FOLDER, data=response)
|
||||
return FixtureResult(
|
||||
filename=save_filename, folder=SMART_CHILD_FOLDER, data=response
|
||||
)
|
||||
|
||||
|
||||
async def get_smart_fixtures(device: SmartDevice, batch_size: int):
|
||||
|
@ -277,7 +277,7 @@ check_categories()
|
||||
|
||||
|
||||
def device_for_fixture_name(model, protocol):
|
||||
if protocol == "SMART":
|
||||
if "SMART" in protocol:
|
||||
for d in PLUGS_SMART:
|
||||
if d in model:
|
||||
return SmartDevice
|
||||
@ -345,17 +345,21 @@ async def get_device_for_fixture(fixture_data: FixtureInfo):
|
||||
d = device_for_fixture_name(fixture_data.name, fixture_data.protocol)(
|
||||
host="127.0.0.123"
|
||||
)
|
||||
if fixture_data.protocol == "SMART":
|
||||
if "SMART" in fixture_data.protocol:
|
||||
d.protocol = FakeSmartProtocol(fixture_data.data, fixture_data.name)
|
||||
else:
|
||||
d.protocol = FakeIotProtocol(fixture_data.data)
|
||||
|
||||
discovery_data = None
|
||||
if "discovery_result" in fixture_data.data:
|
||||
discovery_data = {"result": fixture_data.data["discovery_result"]}
|
||||
else:
|
||||
elif "system" in fixture_data.data:
|
||||
discovery_data = {
|
||||
"system": {"get_sysinfo": fixture_data.data["system"]["get_sysinfo"]}
|
||||
}
|
||||
if discovery_data: # Child devices do not have discovery info
|
||||
d.update_from_discover_info(discovery_data)
|
||||
|
||||
await _update_and_close(d)
|
||||
return d
|
||||
|
||||
|
@ -8,7 +8,7 @@ from kasa.xortransport import XorEncryption
|
||||
|
||||
from .fakeprotocol_iot import FakeIotProtocol
|
||||
from .fakeprotocol_smart import FakeSmartProtocol
|
||||
from .fixtureinfo import FIXTURE_DATA, FixtureInfo, filter_fixtures, idgenerator
|
||||
from .fixtureinfo import FixtureInfo, filter_fixtures, idgenerator
|
||||
|
||||
|
||||
def _make_unsupported(device_family, encrypt_type):
|
||||
@ -42,8 +42,10 @@ UNSUPPORTED_DEVICES = {
|
||||
}
|
||||
|
||||
|
||||
def parametrize_discovery(desc, root_key):
|
||||
filtered_fixtures = filter_fixtures(desc, data_root_filter=root_key)
|
||||
def parametrize_discovery(desc, *, data_root_filter, protocol_filter=None):
|
||||
filtered_fixtures = filter_fixtures(
|
||||
desc, data_root_filter=data_root_filter, protocol_filter=protocol_filter
|
||||
)
|
||||
return pytest.mark.parametrize(
|
||||
"discovery_mock",
|
||||
filtered_fixtures,
|
||||
@ -52,10 +54,15 @@ def parametrize_discovery(desc, root_key):
|
||||
)
|
||||
|
||||
|
||||
new_discovery = parametrize_discovery("new discovery", "discovery_result")
|
||||
new_discovery = parametrize_discovery(
|
||||
"new discovery", data_root_filter="discovery_result"
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(params=FIXTURE_DATA, ids=idgenerator)
|
||||
@pytest.fixture(
|
||||
params=filter_fixtures("discoverable", protocol_filter={"SMART", "IOT"}),
|
||||
ids=idgenerator,
|
||||
)
|
||||
def discovery_mock(request, mocker):
|
||||
fixture_info: FixtureInfo = request.param
|
||||
fixture_data = fixture_info.data
|
||||
@ -128,7 +135,7 @@ def discovery_mock(request, mocker):
|
||||
side_effect=lambda *_, **__: [(None, None, None, None, (dm.ip, 0))],
|
||||
)
|
||||
|
||||
if fixture_info.protocol == "SMART":
|
||||
if "SMART" in fixture_info.protocol:
|
||||
proto = FakeSmartProtocol(fixture_data, fixture_info.name)
|
||||
else:
|
||||
proto = FakeIotProtocol(fixture_data)
|
||||
@ -142,7 +149,10 @@ def discovery_mock(request, mocker):
|
||||
yield dm
|
||||
|
||||
|
||||
@pytest.fixture(params=FIXTURE_DATA, ids=idgenerator)
|
||||
@pytest.fixture(
|
||||
params=filter_fixtures("discoverable", protocol_filter={"SMART", "IOT"}),
|
||||
ids=idgenerator,
|
||||
)
|
||||
def discovery_data(request, mocker):
|
||||
"""Return raw discovery file contents as JSON. Used for discovery tests."""
|
||||
fixture_info = request.param
|
||||
|
@ -29,8 +29,17 @@ SUPPORTED_SMART_DEVICES = [
|
||||
)
|
||||
]
|
||||
|
||||
SUPPORTED_SMART_CHILD_DEVICES = [
|
||||
(device, "SMART.CHILD")
|
||||
for device in glob.glob(
|
||||
os.path.dirname(os.path.abspath(__file__)) + "/fixtures/smart/child/*.json"
|
||||
)
|
||||
]
|
||||
|
||||
SUPPORTED_DEVICES = SUPPORTED_IOT_DEVICES + SUPPORTED_SMART_DEVICES
|
||||
|
||||
SUPPORTED_DEVICES = (
|
||||
SUPPORTED_IOT_DEVICES + SUPPORTED_SMART_DEVICES + SUPPORTED_SMART_CHILD_DEVICES
|
||||
)
|
||||
|
||||
|
||||
def idgenerator(paramtuple: FixtureInfo):
|
||||
@ -50,6 +59,8 @@ def get_fixture_info() -> List[FixtureInfo]:
|
||||
folder = Path(__file__).parent / "fixtures"
|
||||
if protocol == "SMART":
|
||||
folder = folder / "smart"
|
||||
if protocol == "SMART.CHILD":
|
||||
folder = folder / "smart/child"
|
||||
p = folder / file
|
||||
|
||||
with open(p) as f:
|
||||
|
1
kasa/tests/fixtures/smart/child/.gitkeep
vendored
Normal file
1
kasa/tests/fixtures/smart/child/.gitkeep
vendored
Normal file
@ -0,0 +1 @@
|
||||
Can be deleted when first fixture is added
|
Loading…
Reference in New Issue
Block a user