forked from RenWal/fritzbox-munin-fast
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #18 from ma4nn/add_unit_tests_for_smart_home
added unit tests for smart home plugin
- Loading branch information
Showing
8 changed files
with
94 additions
and
25 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,10 +1,18 @@ | ||
from unittest.mock import patch | ||
from unittest.mock import patch,MagicMock | ||
import pytest | ||
from test_response_mock import ResponseMock | ||
from test_fritzconnection_mock import FritzConnectionMock | ||
|
||
# @see https://docs.pytest.org/en/7.3.x/example/parametrize.html#indirect-parametrization | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def fixture_version(request): | ||
def fixture_version(request): # request param is fixture | ||
with patch('requests.request', side_effect=ResponseMock) as mock_requests: | ||
mock_requests.side_effect.version = request.param if hasattr(request, "param") else None | ||
yield | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def connection(request): | ||
return FritzConnectionMock(version = request.param if hasattr(request, "param") else None) |
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,9 @@ | ||
[ | ||
{ | ||
"NewTemperatureIsEnabled": "ENABLED", | ||
"NewDeviceId": 16, | ||
"NewDeviceName": "Lichterkette", | ||
"NewProductName": "FRITZ!DECT 210", | ||
"NewTemperatureCelsius": 70 | ||
} | ||
] |
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,31 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Unit tests for smart home temperature module | ||
""" | ||
|
||
import pytest | ||
from unittest.mock import MagicMock | ||
from fritzbox_smart_home_temperature import FritzboxSmartHomeTemperature | ||
|
||
|
||
@pytest.mark.parametrize("connection", ["7590-7.57"], indirect=True) | ||
class TestFritzboxSmartHome: | ||
def test_config(self, connection: MagicMock, capsys): # pylint: disable=unused-argument | ||
smart_home = FritzboxSmartHomeTemperature(connection) | ||
smart_home.print_config() | ||
|
||
assert capsys.readouterr().out == """graph_title Smart Home temperature | ||
graph_vlabel degrees Celsius | ||
graph_category sensors | ||
graph_scale no | ||
t16.label Lichterkette | ||
t16.type GAUGE | ||
t16.graph LINE | ||
t16.info Temperature [FRITZ!DECT 210] | ||
""" | ||
|
||
def test_smart_home(self, connection: MagicMock, capsys): # pylint: disable=unused-argument | ||
uptime = FritzboxSmartHomeTemperature(connection) | ||
uptime.print_stats() | ||
|
||
assert capsys.readouterr().out == "t16.value 7.0\n" |
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,22 @@ | ||
import os | ||
import json | ||
|
||
|
||
class FritzConnectionMock: # pylint: disable=too-few-public-methods | ||
version: str | ||
|
||
def __init__(self, version): | ||
self.version = version | ||
|
||
|
||
def call_action(self, *args, **kwargs) -> {}: | ||
index = 0 | ||
if 'arguments' in kwargs and 'NewIndex' in kwargs['arguments']: | ||
index = kwargs['arguments']['NewIndex'] | ||
|
||
file_name = 'smart_home_devices.json' | ||
file_dir = f"{os.path.dirname(__file__)}/fixtures/fritzbox{self.version}" | ||
with open(file_dir + "/" + file_name, "r", encoding="utf-8") as file: | ||
json_object = json.load(file) | ||
|
||
return json_object[index] if len(json_object) > index else {} |
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