Skip to content

Commit 9346bc4

Browse files
committed
unit-tests: follow tested package structure
1 parent c200f96 commit 9346bc4

File tree

10 files changed

+200
-197
lines changed

10 files changed

+200
-197
lines changed

tests/unit/test_async.py

Lines changed: 0 additions & 89 deletions
This file was deleted.

tests/unit/test_async/__init__.py

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
import enapter
4+
5+
6+
async def test_aclose() -> None:
7+
@enapter.async_.generator
8+
async def agen():
9+
yield 1
10+
yield 2
11+
yield 3 # pragma: no cover
12+
13+
async with agen() as g:
14+
assert await g.__anext__() == 1
15+
assert await g.__anext__() == 2
16+
17+
with pytest.raises(StopAsyncIteration):
18+
await g.__anext__()
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
import enapter
6+
7+
8+
async def test_run_not_implemented() -> None:
9+
class R(enapter.async_.Routine):
10+
pass
11+
12+
with pytest.raises(TypeError):
13+
R() # type: ignore
14+
15+
16+
async def test_start_twice() -> None:
17+
started = asyncio.Event()
18+
19+
class R(enapter.async_.Routine):
20+
async def _run(self) -> None:
21+
started.set()
22+
await asyncio.Event().wait()
23+
24+
async with R() as r:
25+
await started.wait()
26+
with pytest.raises(RuntimeError):
27+
await r.start()
28+
29+
30+
async def test_stop_without_start() -> None:
31+
class R(enapter.async_.Routine):
32+
async def _run(self) -> None:
33+
await asyncio.Event().wait() # pragma: no cover
34+
35+
r = R()
36+
with pytest.raises(RuntimeError):
37+
await r.stop()
38+
39+
40+
async def test_context_manager() -> None:
41+
started = asyncio.Event()
42+
done = asyncio.Event()
43+
44+
class R(enapter.async_.Routine):
45+
async def _run(self) -> None:
46+
started.set()
47+
try:
48+
await asyncio.Event().wait()
49+
finally:
50+
done.set()
51+
52+
async with R():
53+
await started.wait()
54+
55+
assert done.is_set()
56+
57+
58+
async def test_task_group() -> None:
59+
started = asyncio.Event()
60+
done = asyncio.Event()
61+
62+
class R(enapter.async_.Routine):
63+
async def _run(self) -> None:
64+
started.set()
65+
try:
66+
await asyncio.Event().wait()
67+
finally:
68+
done.set()
69+
70+
async with asyncio.TaskGroup() as tg:
71+
async with R(task_group=tg):
72+
await started.wait()
73+
74+
assert done.is_set()

tests/unit/test_log.py

Lines changed: 0 additions & 69 deletions
This file was deleted.

tests/unit/test_log/__init__.py

Whitespace-only changes.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import contextlib
2+
import io
3+
import json
4+
import re
5+
from typing import Generator
6+
7+
import enapter
8+
9+
10+
def test_record_ends_with_newline() -> None:
11+
buf = io.StringIO()
12+
13+
with configure_logging(level="info", stream=buf):
14+
enapter.log.LOGGER.info("hello")
15+
16+
assert buf.getvalue().endswith("\n")
17+
18+
19+
def test_record_fields() -> None:
20+
buf = io.StringIO()
21+
22+
with configure_logging(level="info", stream=buf):
23+
enapter.log.LOGGER.info("hello")
24+
25+
record = json.loads(buf.getvalue())
26+
27+
time = record.pop("time")
28+
assert re.match(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+", time) is not None
29+
30+
assert record["level"] == "INFO"
31+
assert record["name"] == "enapter"
32+
assert record["message"] == "hello"
33+
34+
35+
def test_exc_info() -> None:
36+
buf = io.StringIO()
37+
38+
with configure_logging(level="info", stream=buf):
39+
try:
40+
raise RuntimeError("oops")
41+
except RuntimeError:
42+
enapter.log.LOGGER.exception("boom")
43+
44+
record = json.loads(buf.getvalue())
45+
46+
assert record["message"] == "boom"
47+
assert "Traceback (most recent call last)" in record["exc_info"]
48+
assert 'RuntimeError("oops")' in record["exc_info"]
49+
50+
51+
def test_stack_info() -> None:
52+
buf = io.StringIO()
53+
54+
with configure_logging(level="info", stream=buf):
55+
enapter.log.LOGGER.info("hello", stack_info=True)
56+
57+
record = json.loads(buf.getvalue())
58+
59+
assert record["message"] == "hello"
60+
assert "Stack (most recent call last)" in record["stack_info"]
61+
assert "test_stack_info" in record["stack_info"]
62+
63+
64+
@contextlib.contextmanager
65+
def configure_logging(*args, **kwargs) -> Generator[None, None, None]:
66+
enapter.log.configure(*args, **kwargs)
67+
try:
68+
yield
69+
finally:
70+
enapter.log.configure(level=None)

tests/unit/test_mqtt/test_api/test_device.py

Lines changed: 0 additions & 39 deletions
This file was deleted.

tests/unit/test_mqtt/test_api/test_device/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)