Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Put child fixtures in subfolder #809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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