Skip to content

Commit 843200d

Browse files
committed
standalone: add unit-tests
1 parent 45f9ef6 commit 843200d

File tree

12 files changed

+839
-40
lines changed

12 files changed

+839
-40
lines changed

src/enapter/mqtt/api/config.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
import dataclasses
12
import os
23
from typing import MutableMapping, Self
34

45

6+
@dataclasses.dataclass(repr=False)
57
class TLSConfig:
68

9+
secret_key: str
10+
cert: str
11+
ca_cert: str
12+
13+
def __repr__(self) -> str:
14+
return "mqtt.api.TLSConfig(...)"
15+
716
@classmethod
817
def from_env(
918
cls, env: MutableMapping[str, str] = os.environ, namespace: str = "ENAPTER_"
@@ -30,14 +39,16 @@ def pem(value: str) -> str:
3039

3140
return cls(secret_key=pem(secret_key), cert=pem(cert), ca_cert=pem(ca_cert))
3241

33-
def __init__(self, secret_key: str, cert: str, ca_cert: str) -> None:
34-
self.secret_key = secret_key
35-
self.cert = cert
36-
self.ca_cert = ca_cert
37-
3842

43+
@dataclasses.dataclass
3944
class Config:
4045

46+
host: str
47+
port: int
48+
user: str | None = None
49+
password: str | None = None
50+
tls_config: TLSConfig | None = None
51+
4152
@classmethod
4253
def from_env(
4354
cls, env: MutableMapping[str, str] = os.environ, namespace: str = "ENAPTER_"
@@ -51,27 +62,6 @@ def from_env(
5162
tls_config=TLSConfig.from_env(env, namespace=namespace),
5263
)
5364

54-
def __init__(
55-
self,
56-
host: str,
57-
port: int,
58-
user: str | None = None,
59-
password: str | None = None,
60-
tls_config: TLSConfig | None = None,
61-
) -> None:
62-
self.host = host
63-
self.port = port
64-
self.user = user
65-
self.password = password
66-
self.tls_config = tls_config
67-
6865
@property
6966
def tls(self) -> TLSConfig | None:
7067
return self.tls_config
71-
72-
def __repr__(self) -> str:
73-
return "mqtt.api.Config(host=%r, port=%r, tls=%r)" % (
74-
self.host,
75-
self.port,
76-
self.tls is not None,
77-
)

src/enapter/standalone/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
from .config import Config
1+
from .config import (
2+
CommunicationConfig,
3+
CommunicationConfigV1,
4+
CommunicationConfigV3,
5+
Config,
6+
)
27
from .device import Device
38
from .device_protocol import (
49
CommandArgs,
@@ -10,16 +15,21 @@
1015
)
1116
from .logger import Logger
1217
from .run import run
18+
from .ucm import UCM
1319

1420
__all__ = [
1521
"CommandArgs",
1622
"CommandResult",
1723
"Config",
24+
"CommunicationConfig",
25+
"CommunicationConfigV1",
26+
"CommunicationConfigV3",
1827
"Device",
1928
"DeviceProtocol",
2029
"Log",
2130
"Logger",
2231
"Properties",
2332
"Telemetry",
2433
"run",
34+
"UCM",
2535
]

src/enapter/standalone/config.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def from_env(
4747
prefix = namespace + "STANDALONE_COMMUNICATION_"
4848
try:
4949
blob = env[namespace + "VUCM_BLOB"]
50-
LOGGER.warn(
50+
LOGGER.warning(
5151
"`%s` is deprecated and will be removed soon. Please use `%s`.",
5252
namespace + "VUCM_BLOB",
5353
prefix + "CONFIG",
@@ -115,8 +115,8 @@ def from_config_v3(cls, config: "CommunicationConfigV3") -> Self:
115115
ca_cert=config.mqtt_credentials.ca_chain,
116116
),
117117
)
118-
case _:
119-
raise NotImplementedError(config.mqtt_protocol)
118+
case _: # pragma: no cover
119+
raise NotImplementedError(config.mqtt_protocol) # pragma: no cover
120120
assert mqtt_api_config is not None
121121
return cls(
122122
mqtt_api_config=mqtt_api_config,
@@ -205,8 +205,8 @@ def from_dto(cls, dto: dict[str, Any]) -> Self:
205205
mqtt_credentials = cls.MQTTSCredentials.from_dto(
206206
dto["mqtt_credentials"]
207207
)
208-
case _:
209-
raise NotImplementedError(mqtt_protocol)
208+
case _: # pragma: no cover
209+
raise NotImplementedError(mqtt_protocol) # pragma: no cover
210210
assert mqtt_credentials is not None
211211
return cls(
212212
mqtt_host=dto["mqtt_host"],

src/enapter/standalone/device.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def __init__(
2727

2828
@abc.abstractmethod
2929
async def run(self) -> None:
30-
pass
30+
pass # pragma: no cover
3131

3232
@property
3333
def logger(self) -> Logger:

src/enapter/standalone/device_protocol.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ class Log:
1818
class DeviceProtocol(Protocol):
1919

2020
async def run(self) -> None:
21-
pass
21+
pass # pragma: no cover
2222

2323
async def stream_properties(self) -> AsyncGenerator[Properties, None]:
24-
yield {}
24+
yield {} # pragma: no cover
2525

2626
async def stream_telemetry(self) -> AsyncGenerator[Telemetry, None]:
27-
yield {}
27+
yield {} # pragma: no cover
2828

2929
async def stream_logs(self) -> AsyncGenerator[Log, None]:
30-
yield Log("debug", "", False)
30+
yield Log("debug", "", False) # pragma: no cover
3131

3232
async def execute_command(self, name: str, args: CommandArgs) -> CommandResult:
33-
pass
33+
pass # pragma: no cover

src/enapter/standalone/mqtt_adapter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ async def _stream_logs(self) -> None:
8181
self._logger.warning(log.message)
8282
case "error":
8383
self._logger.error(log.message)
84-
case _:
85-
raise NotImplementedError(log.severity)
84+
case _: # pragma: no cover
85+
raise NotImplementedError(log.severity) # pragma: no cover
8686
await self._publish_log(
8787
mqtt.api.device.Log(
8888
timestamp=int(time.time()),

tests/unit/test_standalone/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)