Skip to content

Commit

Permalink
Put child fixtures in subfolder (#809)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
sdb9696 committed Mar 6, 2024
1 parent 0d5a3c8 commit ced8794
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
5 changes: 4 additions & 1 deletion devtools/dump_devinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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__)
Expand Down Expand Up @@ -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):
Expand Down
12 changes: 8 additions & 4 deletions kasa/tests/device_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def 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
Expand Down Expand Up @@ -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"]}
}
d.update_from_discover_info(discovery_data)
if discovery_data: # Child devices do not have discovery info
d.update_from_discover_info(discovery_data)

await _update_and_close(d)
return d

Expand Down
24 changes: 17 additions & 7 deletions kasa/tests/discovery_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

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):
Expand Down Expand Up @@ -42,8 +42,10 @@ def _make_unsupported(device_family, encrypt_type):
}


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,
Expand All @@ -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
Expand Down Expand Up @@ -128,7 +135,7 @@ async def mock_discover(self):
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)
Expand All @@ -142,7 +149,10 @@ async def _query(request, retry_count: int = 3):
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
Expand Down
13 changes: 12 additions & 1 deletion kasa/tests/fixtureinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,17 @@ class FixtureInfo(NamedTuple):
)
]

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):
Expand All @@ -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:
Expand Down
1 change: 1 addition & 0 deletions kasa/tests/fixtures/smart/child/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Can be deleted when first fixture is added

0 comments on commit ced8794

Please sign in to comment.