Skip to content

Commit

Permalink
Add logging mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
nickw444 committed Mar 29, 2020
1 parent f8cdb16 commit b05d6c7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions appdaemon_testing/hass_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class StateSpy:
class HassDriver:
def __init__(self):
self._mocks = dict(
log=mock.Mock(),
error=mock.Mock(),
call_service=mock.Mock(),
cancel_timer=mock.Mock(),
get_state=mock.Mock(side_effect=self._se_get_state),
Expand Down
6 changes: 4 additions & 2 deletions appdaemon_testing/pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import appdaemon.plugins.hass.hassapi as hass
import pytest
from appdaemon.logging import Logging

from ..hass_driver import HassDriver

Expand Down Expand Up @@ -39,8 +40,9 @@ def decorator(fn) -> Callable[..., T]:
@wraps(fn)
def inner(*_args, **_kwargs) -> T:
ad = mock.Mock()
logging = mock.Mock()
app = App(ad, App.__name__, logging, args or {}, {}, {}, {})
logging_impl = mock.Mock()
logging_impl.log_levels = Logging.log_levels
app = App(ad, App.__name__, logging_impl, args or {}, {}, {}, {})
if initialize:
app.initialize()
fn(*_args, **_kwargs)
Expand Down
24 changes: 24 additions & 0 deletions appdaemon_testing_tests/test_hass_driver.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from unittest import mock

import appdaemon.plugins.hass.hassapi as hass
import pytest

from appdaemon_testing import HassDriver
from appdaemon_testing.pytest import automation_fixture


def test_get_state(hass_driver):
Expand Down Expand Up @@ -156,6 +158,28 @@ def test_setup_does_not_trigger_spys(hass_driver):
assert handler.call_count == 1


class MyLoggingApp(hass.Hass):
def initialize(self):
self.log('This is a log')
self.error('This is an error')


@automation_fixture(MyLoggingApp, initialize=False)
def my_logging_app():
pass


def test_log(hass_driver, my_logging_app: MyLoggingApp):
hass_driver.inject_mocks()

my_logging_app.initialize()

log = hass_driver.get_mock("log")
error = hass_driver.get_mock("error")
log.assert_called_once_with('This is a log')
error.assert_called_once_with('This is an error')


@pytest.fixture
def hass_driver() -> HassDriver:
hass_driver = HassDriver()
Expand Down

0 comments on commit b05d6c7

Please sign in to comment.