Skip to content

Commit 50663e8

Browse files
committed
Tests improvements
- fixed warnings in tests - improved periodic_task function - added new hooks to .pre-commit-config
1 parent 8e22f9e commit 50663e8

4 files changed

Lines changed: 29 additions & 2 deletions

File tree

.pre-commit-config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.2.0
3+
rev: v4.3.0
44
hooks:
55
- id: check-yaml
66
- id: end-of-file-fixer
77
- id: trailing-whitespace
8+
- id: name-tests-test
9+
args: ["--pytest-test-first"]
810

911
- repo: https://github.com/pycqa/isort
1012
rev: 5.10.1

src/runtime_config/libs/asyncio_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def periodic_task(
77
) -> asyncio.Task: # type: ignore[type-arg]
88
async def wrapper() -> None:
99
while True:
10-
await func()
1110
await asyncio.sleep(callback_time)
11+
await func()
1212

1313
return asyncio.create_task(wrapper())

tests/libs/test_asyncio_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest
2+
from pytest_mock import MockerFixture
3+
4+
from runtime_config.libs.asyncio_utils import periodic_task
5+
6+
7+
async def test_periodic_task(mocker: MockerFixture):
8+
# arrange
9+
callback_time = 10
10+
func_mock = mocker.AsyncMock(side_effect=Exception)
11+
sleep_mock = mocker.patch('runtime_config.libs.asyncio_utils.asyncio.sleep')
12+
13+
# act
14+
with pytest.raises(Exception):
15+
await periodic_task(func=func_mock, callback_time=callback_time)
16+
17+
# assert
18+
assert func_mock.call_count == 1
19+
sleep_mock.assert_called_with(callback_time)

tests/test_runtime_config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from runtime_config.runtime_config import _instance
1212

1313

14+
@pytest.mark.usefixtures('mock_periodic_task')
1415
class TestRuntimeConfig:
1516
async def test_create(self, mocker: MockerFixture, init_settings):
1617
# arrange
@@ -431,3 +432,8 @@ def source_mock_fixture(mocker: MockerFixture):
431432
source_mock = mocker.Mock(spec=sources.ConfigServerSrc)
432433
source_mock.get_settings.return_value = []
433434
return source_mock
435+
436+
437+
@pytest.fixture(name='mock_periodic_task')
438+
def mock_periodic_task_fixture(mocker: MockerFixture):
439+
mocker.patch('runtime_config.runtime_config.periodic_task')

0 commit comments

Comments
 (0)