Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Dec 5, 2024
1 parent 681cef8 commit b80ee51
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
22 changes: 18 additions & 4 deletions run/conf_testing/lib/HABAppTests/test_rule/_com_patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@
from HABApp.config import CONFIG


class PatcherName:
def __init__(self, header: str):
self.header = header
self.logged = False


class BasePatcher:
def __init__(self, name: str, logger_name: str) -> None:
@staticmethod
def create_name(header: str) -> PatcherName:
return PatcherName(header)

def __init__(self, name: PatcherName, logger_name: str) -> None:
self._log: Final = logging.getLogger('Com').getChild(logger_name)
self.name: Final = name
self.monkeypatch: Final = MonkeyPatch()

def log(self, msg: str) -> None:
if not self.name.logged:
self._log.debug('')
self._log.debug(self.name.header)
self.name.logged = True
self._log.debug(msg)

def __exit__(self, exc_type: type[BaseException] | None, exc_val: BaseException | None,
Expand Down Expand Up @@ -106,7 +120,7 @@ def __init__(self, name: str) -> None:

def wrap_sse(self, to_wrap: Callable[[dict], Any]) -> Callable[[dict], Any]:
def new_call(_dict: dict) -> Any:
self.log(f'{_dict}')
self.log(f'{"SSE":^6s} {_dict}')
return to_wrap(_dict)
return new_call

Expand All @@ -122,13 +136,13 @@ def __init__(self, name: str) -> None:

def wrap_msg(self, func: Callable[[str, Any, bool], Any]) -> Callable[[str, Any, bool], Any]:
def new_call(topic: str, payload: Any, retain: bool) -> Any:
self.log(f'{"MSG":3s} {"R" if retain else " "} {topic} {payload}')
self.log(f'{"MSG":^6s} {"R" if retain else " "} {topic} {payload}')
return func(topic, payload, retain)
return new_call

def pub_msg(self, func: Callable[[str, Any, int, bool], Any]) -> Callable[[str, Any, int, bool], Any]:
async def wrapped_publish(topic: str, payload: Any, qos: int = 0, retain: bool = False) -> Any:
self.log(f'{"PUB":3s} {"R" if retain else " "}{qos:d} {topic} {payload}')
self.log(f'{"PUB":^6s} {"R" if retain else " "}{qos:d} {topic} {payload}')
return await func(topic, payload, qos, retain)
return wrapped_publish

Expand Down
9 changes: 6 additions & 3 deletions run/conf_testing/lib/HABAppTests/test_rule/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from HABApp.core.internals import EventBusListener, wrap_func, WrappedFunctionBase
from HABAppTests.test_rule._com_patcher import BasePatcher, MqttPatcher, RestPatcher, SsePatcher
from HABAppTests.test_rule.test_result import TestResult, TestResultStatus
from HABAppTests.utils import get_file_path_of_obj


class TmpLogLevel:
Expand Down Expand Up @@ -101,11 +102,13 @@ async def run(self, res: TestResult) -> TestResult:
async with ExecutionEventCatcher('warning', HABApp.core.const.topics.TOPIC_WARNINGS) as worker_warnings, \
ExecutionEventCatcher('error', HABApp.core.const.topics.TOPIC_ERRORS) as worker_errors:

name = f'{res.cls_name}.{res.test_name}'
try:
suffix = f' (from "{get_file_path_of_obj(self.func)}")'
except ValueError:
suffix = ''

name = RestPatcher.create_name(f'{res.cls_name:s}.{res.test_name:s}{suffix:s}')
b = BasePatcher(name, 'TC')
b.log('')
b.log(name)

try:
with RestPatcher(name), SsePatcher(name), MqttPatcher(name):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import logging

import HABApp
from HABApp.core import shutdown
from HABApp.core.const.topics import TOPIC_FILES
from HABApp.core.lib import SingleTask
from HABApp.core.wrapper import ignore_exception
from HABAppTests.test_rule.test_case import TestResult, TestResultStatus

from .test_rule import TestBaseRule, TestRuleStatus
from HABApp.core import shutdown


log = logging.getLogger('HABApp.Tests')

Expand Down Expand Up @@ -36,6 +38,7 @@ def _get_next_rule(self) -> TestBaseRule | None:
return rule
return None

@ignore_exception
async def _run_tests(self) -> None:
results: list[TestResult] = []

Expand Down

0 comments on commit b80ee51

Please sign in to comment.