Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dgomes committed Apr 24, 2022
1 parent 3e5fdc5 commit 1467412
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 22 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Tests

# Run this workflow every time a new commit pushed to your repository
on: push

jobs:

unit_tests:
name: Tests
runs-on: ubuntu-latest

steps:
# Checks out a copy of your repository on the ubuntu-latest machine
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Test with pytest
timeout-minutes: 3
run: |
pytest -vvv
7 changes: 3 additions & 4 deletions dali_mqtt_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def main(args):
dali_driver = DaliServer("localhost", 55825)

retries = 0
while True:
while retries < MAX_RETRIES:
try:
mqttc = create_mqtt_client(
dali_driver,
Expand All @@ -483,12 +483,11 @@ def main(args):
)
except Exception as e:
logger.error("%s: %s", type(e).__name__, e)
if retries == MAX_RETRIES:
logger.error("Maximum retries of %d reached, exiting...", retries)
break
time.sleep(random.randint(MIN_BACKOFF_TIME, MAX_BACKOFF_TIME))
retries += 1

logger.error("Maximum retries of %d reached, exiting...", retries)


if __name__ == "__main__":
parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
Expand Down
5 changes: 2 additions & 3 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import sys

PROJECT_PATH = os.getcwd()
SOURCE_PATH = os.path.join(
PROJECT_PATH,"."
)
SOURCE_PATH = os.path.join(PROJECT_PATH, ".")
sys.path.append(SOURCE_PATH)
62 changes: 47 additions & 15 deletions tests/test_hasseb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
DEFAULT_LOG_LEVEL,
DEFAULT_LOG_COLOR,
DEFAULT_HA_DISCOVERY_PREFIX,
MAX_RETRIES,
)


@pytest.fixture
def args():
mock_args = mock.Mock()
Expand All @@ -25,24 +27,54 @@ def args():
mock_args.log_color = DEFAULT_LOG_COLOR
return mock_args


@pytest.fixture
def config():
return {"config": "config.yaml",
"dali_driver": "hasseb",
"dali_lamps": 2,
"mqtt_server": "localhost",
"mqtt_port": 1883,
"mqtt_base_topic": "dali2mqtt",
"ha_discovery_prefix": "homeassistant",
"log_level": "info",
"log_color": False,
return {
"config": "config.yaml",
"dali_driver": "hasseb",
"dali_lamps": 2,
"mqtt_server": "localhost",
"mqtt_port": 1883,
"mqtt_base_topic": "dali2mqtt",
"ha_discovery_prefix": "homeassistant",
"log_level": "info",
"log_color": False,
}

def test_main(args, config):
with mock.patch('dali_mqtt_daemon.create_mqtt_client', return_value=mock.Mock()) as mock_mqtt_client:
with mock.patch("dali_mqtt_daemon.delay", return_value=0):
with mock.patch('yaml.load', return_value={}) as mock_config_file:
mock_mqtt_client.loop_forever = mock.Mock()
@pytest.fixture
def fake_data_object():
driver = mock.Mock()
driver.send = lambda x: 0x00

return {
"driver": driver,
"base_topic": "test",
"ha_prefix": "hass",
"log_level": "debug",
"devices_names_config": None
}

@pytest.fixture
def fake_mqttc():
mqttc = mock.Mock()
def loop_forever():
import sys
raise Exception()
mqttc.loop_forever = loop_forever
return mqttc


def test_main(args, config, fake_mqttc, caplog):
"""Test main loop."""
with mock.patch(
"dali_mqtt_daemon.create_mqtt_client", return_value=fake_mqttc
) as mock_mqtt_client:
with mock.patch("yaml.safe_load", return_value={}) as mock_config_file:
with mock.patch("time.sleep", return_value=None) as sleep:
main(args)
mock_config_file.assert_called()
assert mock_mqtt_client.call_count == 11
assert sleep.call_count == MAX_RETRIES
assert mock_mqtt_client.call_count == MAX_RETRIES
assert any("Could not load a configuration from config.yaml" in rec.message for rec in caplog.records)
assert any("Maximum retries of 10 reached, exiting" in rec.message for rec in caplog.records)

0 comments on commit 1467412

Please sign in to comment.