-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from kardia-as/add-unit-tests
Add unit tests
- Loading branch information
Showing
47 changed files
with
5,763 additions
and
133 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.5.0 | ||
hooks: | ||
- id: debug-statements | ||
|
||
- repo: https://github.com/pycqa/flake8 | ||
rev: 6.0.0 | ||
rev: 5.0.4 | ||
hooks: | ||
- id: flake8 | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.4.0 | ||
|
||
- repo: https://github.com/PyCQA/isort | ||
rev: 5.12.0 | ||
hooks: | ||
- id: debug-statements | ||
- id: no-commit-to-branch | ||
args: | ||
- --branch=dev | ||
- --branch=main | ||
- --branch=rc | ||
- id: isort |
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,6 @@ | ||
pytest>=7.3.1 | ||
pytest-asyncio>=0.21.0 | ||
pytest-timeout>=2.1.0 | ||
pytest-mock>=3.10.0 | ||
pytest-cov>=4.1.0 | ||
flake8==5.0.4 |
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 @@ | ||
"""Tests for zigpy-zboss.""" |
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 @@ | ||
"""Tests for api.""" |
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,73 @@ | ||
"""Test cases for zigpy-zboss API connect/close methods.""" | ||
import pytest | ||
|
||
from zigpy_zboss.api import ZBOSS | ||
|
||
from ..conftest import BaseServerZBOSS, config_for_port_path | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_connect_no_test(make_zboss_server): | ||
"""Test that ZBOSS.connect() can connect.""" | ||
zboss_server = make_zboss_server(server_cls=BaseServerZBOSS) | ||
zboss = ZBOSS(config_for_port_path(zboss_server.port_path)) | ||
|
||
await zboss.connect() | ||
|
||
# Nothing will be sent | ||
assert zboss_server._uart.data_received.call_count == 0 | ||
|
||
zboss.close() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_api_close(connected_zboss, mocker): | ||
"""Test that ZBOSS.close() properly cleans up the object.""" | ||
zboss, zboss_server = connected_zboss | ||
uart = zboss._uart | ||
mocker.spy(uart, "close") | ||
|
||
# add some dummy listeners, should be cleared on close | ||
zboss._listeners = { | ||
'listener1': [mocker.Mock()], 'listener2': [mocker.Mock()] | ||
} | ||
|
||
zboss.close() | ||
|
||
# Make sure our UART was actually closed | ||
assert zboss._uart is None | ||
assert zboss._app is None | ||
assert uart.close.call_count == 1 | ||
|
||
# ZBOSS.close should not throw any errors if called multiple times | ||
zboss.close() | ||
zboss.close() | ||
|
||
def dict_minus(d, minus): | ||
return {k: v for k, v in d.items() if k not in minus} | ||
|
||
ignored_keys = [ | ||
"_blocking_request_lock", | ||
"_reset_uart_reconnect", | ||
"_disconnected_event", | ||
"nvram", | ||
"version" | ||
] | ||
|
||
# Closing ZBOSS should reset it completely to that of a fresh object | ||
# We have to ignore our mocked method and the lock | ||
zboss2 = ZBOSS(zboss._config) | ||
assert ( | ||
zboss2._blocking_request_lock.locked() | ||
== zboss._blocking_request_lock.locked() | ||
) | ||
assert dict_minus(zboss.__dict__, ignored_keys) == dict_minus( | ||
zboss2.__dict__, ignored_keys | ||
) | ||
|
||
zboss2.close() | ||
zboss2.close() | ||
|
||
assert dict_minus(zboss.__dict__, ignored_keys) == dict_minus( | ||
zboss2.__dict__, ignored_keys | ||
) |
Oops, something went wrong.