Skip to content

Commit 6abd716

Browse files
committed
tests: add type annotations
1 parent 3c1c1e3 commit 6abd716

File tree

8 files changed

+52
-27
lines changed

8 files changed

+52
-27
lines changed

tests/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .fake_data_generator import FakeDataGenerator
2+
3+
__all__ = ["FakeDataGenerator"]

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
@pytest.fixture(name="fake", scope="session")
10-
def fixture_fake_data_generator():
10+
def fixture_fake_data_generator() -> FakeDataGenerator:
1111
"""A fixture that returns an instance of a fake data generator."""
1212
seed = int(os.getenv("FAKE_DATA_GENERATOR_SEED", time.time()))
1313
print(f"fake data generator seed: {seed}")

tests/fake_data_generator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55

66
class FakeDataGenerator:
7-
def __init__(self, seed: int):
7+
8+
def __init__(self, seed: int) -> None:
89
self._random = random.Random(seed)
910
self._fake = self._new_faker(seed)
1011

@@ -19,7 +20,7 @@ def hardware_id(self) -> str:
1920
def channel_id(self) -> str:
2021
return self._fake.word()
2122

22-
def _new_faker(self, seed):
23+
def _new_faker(self, seed) -> faker.Faker:
2324
fake = faker.Faker()
2425
fake.seed_instance(seed)
2526
return fake

tests/integration/conftest.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import asyncio
22
import os
33
import socket
4+
from typing import AsyncGenerator, Generator
45

56
import docker
7+
import docker.models.containers
68
import pytest
79

810
import enapter
@@ -11,7 +13,9 @@
1113

1214

1315
@pytest.fixture(name="enapter_mqtt_client")
14-
async def fixture_enapter_mqtt_client(mosquitto_container):
16+
async def fixture_enapter_mqtt_client(
17+
mosquitto_container: docker.models.containers.Container,
18+
) -> AsyncGenerator[enapter.mqtt.Client, None]:
1519
ports = mosquitto_container.ports[MOSQUITTO_PORT]
1620
assert ports
1721
async with asyncio.TaskGroup() as tg:
@@ -24,7 +28,9 @@ async def fixture_enapter_mqtt_client(mosquitto_container):
2428

2529

2630
@pytest.fixture(scope="session", name="mosquitto_container")
27-
def fixture_mosquitto_container(docker_client):
31+
def fixture_mosquitto_container(
32+
docker_client: docker.DockerClient,
33+
) -> Generator[docker.models.containers.Container, None]:
2834
name = "enapter-python-sdk-integration-tests-mosquitto"
2935

3036
try:
@@ -50,15 +56,15 @@ def fixture_mosquitto_container(docker_client):
5056
mosquitto.stop()
5157

5258

53-
def random_unused_port():
59+
def random_unused_port() -> int:
5460
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
5561
s.bind(("", 0))
5662
addr = s.getsockname()
5763
return addr[1]
5864

5965

6066
@pytest.fixture(name="docker_client", scope="session")
61-
def fixture_docker_client():
67+
def fixture_docker_client() -> Generator[docker.DockerClient, None]:
6268
docker_client = docker.from_env()
6369
try:
6470
yield docker_client

tests/integration/test_mqtt.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import asyncio
22
import time
33

4+
import docker.models.containers
5+
46
import enapter
57

68

79
class TestClient:
810

9-
async def test_sanity(self, enapter_mqtt_client):
11+
async def test_sanity(self, enapter_mqtt_client: enapter.mqtt.Client) -> None:
1012
async with asyncio.TaskGroup() as tg:
1113
async with HeartbitSender(tg, enapter_mqtt_client) as heartbit_sender:
1214
async with enapter_mqtt_client.subscribe(
@@ -15,7 +17,9 @@ async def test_sanity(self, enapter_mqtt_client):
1517
msg = await messages.__anext__()
1618
assert int(msg.payload) <= time.time()
1719

18-
async def test_consume_after_another_subscriber_left(self, enapter_mqtt_client):
20+
async def test_consume_after_another_subscriber_left(
21+
self, enapter_mqtt_client: enapter.mqtt.Client
22+
) -> None:
1923
async with asyncio.TaskGroup() as tg:
2024
async with HeartbitSender(tg, enapter_mqtt_client) as heartbit_sender:
2125
async with enapter_mqtt_client.subscribe(
@@ -31,7 +35,9 @@ async def test_consume_after_another_subscriber_left(self, enapter_mqtt_client):
3135
msg = await messages_1.__anext__()
3236
assert int(msg.payload) <= time.time()
3337

34-
async def test_two_subscriptions(self, enapter_mqtt_client):
38+
async def test_two_subscriptions(
39+
self, enapter_mqtt_client: enapter.mqtt.Client
40+
) -> None:
3541
async with asyncio.TaskGroup() as tg:
3642
async with HeartbitSender(tg, enapter_mqtt_client) as heartbit_sender:
3743
for i in range(2):
@@ -41,7 +47,9 @@ async def test_two_subscriptions(self, enapter_mqtt_client):
4147
msg = await messages.__anext__()
4248
assert int(msg.payload) <= time.time()
4349

44-
async def test_two_subscribers(self, enapter_mqtt_client):
50+
async def test_two_subscribers(
51+
self, enapter_mqtt_client: enapter.mqtt.Client
52+
) -> None:
4553
async with asyncio.TaskGroup() as tg:
4654
async with HeartbitSender(tg, enapter_mqtt_client) as heartbit_sender:
4755
async with enapter_mqtt_client.subscribe(
@@ -54,7 +62,11 @@ async def test_two_subscribers(self, enapter_mqtt_client):
5462
msg = await messages.__anext__()
5563
assert int(msg.payload) <= time.time()
5664

57-
async def test_broker_restart(self, mosquitto_container, enapter_mqtt_client):
65+
async def test_broker_restart(
66+
self,
67+
mosquitto_container: docker.models.containers.Container,
68+
enapter_mqtt_client: enapter.mqtt.Client,
69+
) -> None:
5870
async with asyncio.TaskGroup() as tg:
5971
async with HeartbitSender(tg, enapter_mqtt_client) as heartbit_sender:
6072
async with enapter_mqtt_client.subscribe(
@@ -72,16 +84,16 @@ class HeartbitSender(enapter.async_.Routine):
7284
def __init__(
7385
self,
7486
task_group: asyncio.TaskGroup,
75-
enapter_mqtt_client,
76-
topic="heartbits",
77-
interval=0.5,
78-
):
87+
enapter_mqtt_client: enapter.mqtt.Client,
88+
topic: str = "heartbits",
89+
interval: float = 0.5,
90+
) -> None:
7991
super().__init__(task_group=task_group)
8092
self.enapter_mqtt_client = enapter_mqtt_client
8193
self.topic = topic
8294
self.interval = interval
8395

84-
async def _run(self):
96+
async def _run(self) -> None:
8597
while True:
8698
payload = str(int(time.time()))
8799
try:

tests/unit/test_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
class TestGenerator:
77

8-
async def test_aclose(self):
8+
async def test_aclose(self) -> None:
99
@enapter.async_.generator
1010
async def agen():
1111
yield 1

tests/unit/test_log.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22
import io
33
import json
44
import re
5+
from typing import Generator
56

67
import enapter
78

89

910
class TestJSONFormat:
10-
def test_record_ends_with_newline(self):
11+
12+
def test_record_ends_with_newline(self) -> None:
1113
buf = io.StringIO()
1214

1315
with self.configure(level="info", stream=buf):
1416
enapter.log.LOGGER.info("hello")
1517

1618
assert buf.getvalue().endswith("\n")
1719

18-
def test_record_fields(self):
20+
def test_record_fields(self) -> None:
1921
buf = io.StringIO()
2022

2123
with self.configure(level="info", stream=buf):
@@ -30,7 +32,7 @@ def test_record_fields(self):
3032
assert record["name"] == "enapter"
3133
assert record["message"] == "hello"
3234

33-
def test_exc_info(self):
35+
def test_exc_info(self) -> None:
3436
buf = io.StringIO()
3537

3638
with self.configure(level="info", stream=buf):
@@ -45,7 +47,7 @@ def test_exc_info(self):
4547
assert "Traceback (most recent call last)" in record["exc_info"]
4648
assert 'RuntimeError("oops")' in record["exc_info"]
4749

48-
def test_stack_info(self):
50+
def test_stack_info(self) -> None:
4951
buf = io.StringIO()
5052

5153
with self.configure(level="info", stream=buf):
@@ -59,7 +61,7 @@ def test_stack_info(self):
5961

6062
@staticmethod
6163
@contextlib.contextmanager
62-
def configure(*args, **kwargs):
64+
def configure(*args, **kwargs) -> Generator[None, None]:
6365
enapter.log.configure(*args, **kwargs)
6466
try:
6567
yield

tests/unit/test_mqtt/test_api/test_device.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
from unittest import mock
22

33
import enapter
4+
import tests
45

56

67
class TestChannel:
78

8-
async def test_publish_telemetry(self, fake) -> None:
9+
async def test_publish_telemetry(self, fake: tests.FakeDataGenerator) -> None:
910
hardware_id = fake.hardware_id()
1011
channel_id = fake.channel_id()
1112
timestamp = fake.timestamp()
12-
mock_client = mock.Mock()
13+
mock_client = mock.AsyncMock()
1314
channel = enapter.mqtt.api.device.Channel(
1415
client=mock_client, hardware_id=hardware_id, channel_id=channel_id
1516
)
@@ -21,11 +22,11 @@ async def test_publish_telemetry(self, fake) -> None:
2122
'{"timestamp": ' + str(timestamp) + ', "alerts": null}',
2223
)
2324

24-
async def test_publish_properties(self, fake) -> None:
25+
async def test_publish_properties(self, fake: tests.FakeDataGenerator) -> None:
2526
hardware_id = fake.hardware_id()
2627
channel_id = fake.channel_id()
2728
timestamp = fake.timestamp()
28-
mock_client = mock.Mock()
29+
mock_client = mock.AsyncMock()
2930
channel = enapter.mqtt.api.device.Channel(
3031
client=mock_client, hardware_id=hardware_id, channel_id=channel_id
3132
)

0 commit comments

Comments
 (0)