Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Apr 12, 2024
1 parent 17f880f commit 7bd7fe8
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 25 deletions.
4 changes: 2 additions & 2 deletions requirements_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
# -----------------------------------------------------------------------------
# Packages to run source tests
# -----------------------------------------------------------------------------
packaging == 24.0
pytest == 7.4.4
packaging == 23.2
pytest == 8.1.1
pytest-asyncio == 0.23.6
2 changes: 1 addition & 1 deletion src/HABApp/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
# Development versions contain the DEV-COUNTER postfix:
# - 24.01.0.DEV-1

__version__ = '24.04.1.DEV-1'
__version__ = '24.04.0.DEV-1'
10 changes: 5 additions & 5 deletions src/HABApp/core/internals/wrapped_function/wrapped_thread.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import io
import logging
from cProfile import Profile
from concurrent.futures import ThreadPoolExecutor
from pstats import SortKey
from pstats import Stats
from cProfile import Profile
from pstats import SortKey, Stats
from threading import Lock
from time import monotonic
from typing import Callable, Any, Set, Final, Dict, Tuple
from typing import Optional
from typing import Any, Callable, Dict, Final, Optional, Set, Tuple

from HABApp.core.const import loop
from HABApp.core.internals import Context, ContextProvidingObj

from .base import WrappedFunctionBase, default_logger


POOL: Optional[ThreadPoolExecutor] = None
POOL_THREADS: int = 0
POOL_INFO: Set['PoolFunc'] = set()
Expand Down
17 changes: 12 additions & 5 deletions src/HABApp/core/internals/wrapped_function/wrapper.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import logging
from asyncio import iscoroutinefunction
from typing import Union, Optional, Callable, Type
from typing import Callable, Optional, Type, Union

from HABApp.config import CONFIG
from HABApp.core.internals import Context
from HABApp.core.internals.wrapped_function.base import TYPE_WRAPPED_FUNC_OBJ
from HABApp.core.internals.wrapped_function.wrapped_async import WrappedAsyncFunction, TYPE_FUNC_ASYNC
from HABApp.core.internals.wrapped_function.wrapped_async import TYPE_FUNC_ASYNC, WrappedAsyncFunction
from HABApp.core.internals.wrapped_function.wrapped_sync import WrappedSyncFunction
from HABApp.core.internals.wrapped_function.wrapped_thread import HINT_FUNC_SYNC, WrappedThreadFunction, \
create_thread_pool, stop_thread_pool, run_in_thread_pool
from HABApp.core.internals.wrapped_function.wrapped_thread import (
HINT_FUNC_SYNC,
WrappedThreadFunction,
create_thread_pool,
run_in_thread_pool,
stop_thread_pool,
)


def wrap_func(func: Union[HINT_FUNC_SYNC, TYPE_FUNC_ASYNC],
Expand All @@ -25,7 +30,9 @@ def wrap_func(func: Union[HINT_FUNC_SYNC, TYPE_FUNC_ASYNC],
type_name: str = func.__class__.__name__
except Exception:
type_name = type(func)
raise ValueError(f'Callable or coroutine function expected! Got "{func}" (type {type_name:s})')

msg = f'Callable or coroutine function expected! Got "{func}" (type {type_name:s})'
raise TypeError(msg)

if iscoroutinefunction(func):
return WrappedAsyncFunction(func, name=name, logger=logger, context=context)
Expand Down
10 changes: 5 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def f(*args, **kwargs):
return f


@pytest.fixture(autouse=True, scope='function')
@pytest.fixture(autouse=True)
def show_errors(monkeypatch):
# Patch the wrapper so that we always raise the exception
monkeypatch.setattr(HABApp.core.wrapper, 'ignore_exception', raise_err)
monkeypatch.setattr(HABApp.core.wrapper, 'log_exception', raise_err)


@pytest.fixture(autouse=True, scope='function')
@pytest.fixture(autouse=True)
def use_dummy_cfg(monkeypatch):
cfg = get_dummy_cfg()
monkeypatch.setattr(HABApp, 'CONFIG', cfg)
Expand All @@ -59,12 +59,12 @@ def event_loop():
async_context.reset(token)


@pytest.fixture(scope='function')
@pytest.fixture()
def ir():
return ItemRegistry()


@pytest.fixture(autouse=True, scope='function')
@pytest.fixture(autouse=True)
def clean_objs(ir: ItemRegistry, eb: EventBus, request):
markers = request.node.own_markers
for marker in markers:
Expand All @@ -83,7 +83,7 @@ def clean_objs(ir: ItemRegistry, eb: EventBus, request):
r.restore()


@pytest.fixture(autouse=True, scope='function')
@pytest.fixture(autouse=True)
def test_logs(caplog, request):
caplog.set_level(logging.DEBUG)

Expand Down
7 changes: 5 additions & 2 deletions tests/helpers/sync_worker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from HABApp.core.internals.wrapped_function import wrapped_thread
from HABApp.core.internals.wrapped_function import wrapper as wrapper_module


class SyncTestWorker:
Expand All @@ -9,7 +10,9 @@ def submit(callback, *args, **kwargs):
callback(*args, **kwargs)


@pytest.fixture(scope="function")
@pytest.fixture()
def sync_worker(monkeypatch):
monkeypatch.setattr(wrapped_thread, 'POOL', SyncTestWorker())
yield

assert not hasattr(wrapper_module, 'SYNC_CLS')
monkeypatch.setattr(wrapper_module, 'SYNC_CLS', wrapper_module.WrappedThreadFunction, raising=False)
2 changes: 1 addition & 1 deletion tests/rule_runner/rule_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import HABApp.rule.rule as rule_module
import HABApp.rule.scheduler.habappschedulerview as ha_sched
from HABApp.core.asyncio import async_context
from HABApp.core.internals import setup_internals, ItemRegistry, EventBus
from HABApp.core.internals import EventBus, ItemRegistry, setup_internals
from HABApp.core.internals.proxy import ConstProxyObj
from HABApp.core.internals.wrapped_function import wrapped_thread, wrapper
from HABApp.core.internals.wrapped_function.wrapped_thread import WrappedThreadFunction
Expand Down
6 changes: 3 additions & 3 deletions tests/test_core/test_wrapped_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@


def test_error():
with pytest.raises(ValueError) as e:
with pytest.raises(TypeError) as e:
wrap_func(None)
assert str(e.value) == 'Callable or coroutine function expected! Got "None" (type NoneType)'

with pytest.raises(ValueError) as e:
with pytest.raises(TypeError) as e:
wrap_func(6)
assert str(e.value) == 'Callable or coroutine function expected! Got "6" (type int)'

with pytest.raises(ValueError) as e:
with pytest.raises(TypeError) as e:
wrap_func(date(2023, 12, 24))
assert str(e.value) == 'Callable or coroutine function expected! Got "2023-12-24" (type date)'

Expand Down
2 changes: 1 addition & 1 deletion tests/test_docs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from inspect import getmembers, isclass
from pathlib import Path

from easyconfig import yaml
from pydantic import BaseModel

import HABApp.config.models
from HABApp.config import CONFIG
from easyconfig import yaml


def test_sample_yaml(pytestconfig):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_packages.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re
from pathlib import Path

from packaging.version import VERSION_PATTERN

import HABApp.__check_dependency_packages__
from HABApp import __version__

Expand Down

0 comments on commit 7bd7fe8

Please sign in to comment.