Skip to content

Commit

Permalink
pytest + some service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yozik04 committed Feb 15, 2023
1 parent ea8e584 commit d810dc0
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__pycache__
.idea
/.vscode
/venv
/.venv
.DS_Store
*.ipynb
.coverage
Expand All @@ -10,3 +10,4 @@ coverage.xml
/nibe_mqtt.egg-info
/.tox
config.yaml
.pytest_cache
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ voluptuous>=0.13.0
pyyaml>=6.0
python-slugify>=6.0.0

pytest
pytest-asyncio

tox>=3.0.0
black>=22.0.0
isort>=5.0.0
isort>=5.0.0
68 changes: 33 additions & 35 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
import unittest

from nibe_mqtt.config import schema


class ConfigTestCase(unittest.TestCase):
def test_nibegw(self):
config = schema(
{
"mqtt": {"host": "192.168.1.2"},
"nibe": {
"nibegw": {"ip": "192.168.1.3"},
"model": "F1255",
"poll": {"coils": ["bt50-room-temp-s1-40033", 40033]},
},
}
)

print(config)

def test_modbus(self):
config = schema(
{
"mqtt": {"host": "192.168.1.2"},
"nibe": {
"modbus": {"url": "tcp://127.0.0.1:502", "slave_id": 1},
"model": "F1255",
"poll": {"coils": ["bt50-room-temp-s1-40033", 40033]},
},
}
)

print(config)


if __name__ == "__main__":
unittest.main()
def test_nibegw():
config = schema(
{
"mqtt": {"host": "192.168.1.2"},
"nibe": {
"nibegw": {"ip": "192.168.1.3"},
"model": "F1255",
"poll": {"coils": ["bt50-room-temp-s1-40033", 40033]},
},
}
)

assert "modbus" not in config["nibe"]
assert config["nibe"]["poll"]["interval"] == 60

print(config)

def test_modbus():
config = schema(
{
"mqtt": {"host": "192.168.1.2"},
"nibe": {
"modbus": {"url": "tcp://127.0.0.1:502", "slave_id": 1},
"model": "F1255",
"poll": {"coils": ["bt50-room-temp-s1-40033", 40033]},
},
}
)

assert "nibegw" not in config["nibe"]
assert config["nibe"]["poll"]["interval"] == 60

print(config)
71 changes: 71 additions & 0 deletions tests/test_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

from unittest import mock

import pytest

from nibe_mqtt.config import schema
from nibe_mqtt.service import Service


@pytest.fixture
def nibegw_config():
return schema({
"mqtt": {"host": "127.0.0.1"},
"nibe": {
"nibegw": {"ip": "127.0.0.1"},
"model": "F1255"
},
})

@pytest.fixture
def modbus_config():
return schema({
"mqtt": {"host": "127.0.0.1"},
"nibe": {
"modbus": {"url": "tcp://127.0.0.1:502", "slave_id": 1},
"model": "S2125"
},
})

@pytest.fixture(name="nibegw_connection", autouse=True)
def nibegw_connection():
from nibe.connection.nibegw import NibeGW
return mock.Mock(spec=NibeGW)


@pytest.fixture(name="modbus_connection", autouse=True)
def modbus_connection():
from nibe.connection.modbus import Modbus
return mock.Mock(spec=Modbus)


@pytest.fixture(name="mqtt_connection", autouse=True)
def mqtt_connection():
from nibe_mqtt.mqtt import MqttConnection
return mock.Mock(spec=MqttConnection)


@pytest.mark.asyncio
async def test_service_nibegw(nibegw_config):
service = Service(nibegw_config)

await service.start()

assert len(service.heatpump.get_coils()) > 0

outdoor_temperature = service.heatpump.get_coil_by_address(40004)
outdoor_temperature.value = 10
service.on_coil_update(outdoor_temperature)


@pytest.mark.asyncio
async def test_service_modbus(modbus_config):
service = Service(modbus_config)

await service.start()

assert len(service.heatpump.get_coils()) > 0

outdoor_temperature = service.heatpump.get_coil_by_address(30002)
outdoor_temperature.value = 10
service.on_coil_update(outdoor_temperature)
5 changes: 4 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ skip_missing_interpreters = True

[testenv]
changedir = tests
commands = python -m unittest
deps =
pytest
pytest-asyncio
commands = pytest --basetemp="{envtmpdir}" {posargs}

0 comments on commit d810dc0

Please sign in to comment.