-
-
Notifications
You must be signed in to change notification settings - Fork 187
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
Refactor split smartdevice tests to test_{iot,smart}device #822
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
"""Tests for all devices.""" | ||
import importlib | ||
import inspect | ||
import pkgutil | ||
import sys | ||
from unittest.mock import Mock, patch | ||
|
||
import pytest | ||
|
||
import kasa | ||
from kasa import Credentials, Device, DeviceConfig | ||
from kasa.iot import IotDevice | ||
from kasa.smart import SmartChildDevice, SmartDevice | ||
|
||
|
||
def _get_subclasses(of_class): | ||
package = sys.modules["kasa"] | ||
subclasses = set() | ||
for _, modname, _ in pkgutil.iter_modules(package.__path__): | ||
importlib.import_module("." + modname, package="kasa") | ||
module = sys.modules["kasa." + modname] | ||
for name, obj in inspect.getmembers(module): | ||
if ( | ||
inspect.isclass(obj) | ||
and issubclass(obj, of_class) | ||
and module.__package__ != "kasa" | ||
): | ||
subclasses.add((module.__package__ + "." + name, obj)) | ||
return subclasses | ||
|
||
|
||
device_classes = pytest.mark.parametrize( | ||
"device_class_name_obj", _get_subclasses(Device), ids=lambda t: t[0] | ||
) | ||
|
||
|
||
async def test_alias(dev): | ||
test_alias = "TEST1234" | ||
original = dev.alias | ||
|
||
assert isinstance(original, str) | ||
await dev.set_alias(test_alias) | ||
await dev.update() | ||
assert dev.alias == test_alias | ||
|
||
await dev.set_alias(original) | ||
await dev.update() | ||
assert dev.alias == original | ||
|
||
|
||
@device_classes | ||
async def test_device_class_ctors(device_class_name_obj): | ||
"""Make sure constructor api not broken for new and existing SmartDevices.""" | ||
host = "127.0.0.2" | ||
port = 1234 | ||
credentials = Credentials("foo", "bar") | ||
config = DeviceConfig(host, port_override=port, credentials=credentials) | ||
klass = device_class_name_obj[1] | ||
if issubclass(klass, SmartChildDevice): | ||
parent = SmartDevice(host, config=config) | ||
dev = klass( | ||
parent, {"dummy": "info", "device_id": "dummy"}, {"dummy": "components"} | ||
) | ||
else: | ||
dev = klass(host, config=config) | ||
assert dev.host == host | ||
assert dev.port == port | ||
assert dev.credentials == credentials | ||
|
||
|
||
async def test_create_device_with_timeout(): | ||
"""Make sure timeout is passed to the protocol.""" | ||
host = "127.0.0.1" | ||
dev = IotDevice(host, config=DeviceConfig(host, timeout=100)) | ||
assert dev.protocol._transport._timeout == 100 | ||
dev = SmartDevice(host, config=DeviceConfig(host, timeout=100)) | ||
assert dev.protocol._transport._timeout == 100 | ||
|
||
|
||
async def test_create_thin_wrapper(): | ||
"""Make sure thin wrapper is created with the correct device type.""" | ||
mock = Mock() | ||
config = DeviceConfig( | ||
host="test_host", | ||
port_override=1234, | ||
timeout=100, | ||
credentials=Credentials("username", "password"), | ||
) | ||
with patch("kasa.device_factory.connect", return_value=mock) as connect: | ||
dev = await Device.connect(config=config) | ||
assert dev is mock | ||
|
||
connect.assert_called_once_with( | ||
host=None, | ||
config=config, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"device_class, use_class", kasa.deprecated_smart_devices.items() | ||
) | ||
def test_deprecated_devices(device_class, use_class): | ||
package_name = ".".join(use_class.__module__.split(".")[:-1]) | ||
msg = f"{device_class} is deprecated, use {use_class.__name__} from package {package_name} instead" | ||
with pytest.deprecated_call(match=msg): | ||
getattr(kasa, device_class) | ||
packages = package_name.split(".") | ||
module = __import__(packages[0]) | ||
for _ in packages[1:]: | ||
module = importlib.import_module(package_name, package=module.__name__) | ||
getattr(module, use_class.__name__) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"exceptions_class, use_class", kasa.deprecated_exceptions.items() | ||
) | ||
def test_deprecated_exceptions(exceptions_class, use_class): | ||
msg = f"{exceptions_class} is deprecated, use {use_class.__name__} instead" | ||
with pytest.deprecated_call(match=msg): | ||
getattr(kasa, exceptions_class) | ||
getattr(kasa, use_class.__name__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not completely happy about the naming of this file, as it doesn't really test the
Device
API at the moment, but rather some other common functionalities.