Skip to content

Commit

Permalink
Add basic test for message reader class
Browse files Browse the repository at this point in the history
Although this is partially covered by the functional tests of the
CLI [^1], this test adds more coverage by not specifying a limit
of the samples to collect.

[^1]: https://github.com/benthorner/PyPMS/blob/f71628418e4a9d4ab89eea356926b4f158a76e7a/tests/test_cli.py#L32-L45
  • Loading branch information
benthorner authored and avaldebe committed Nov 8, 2022
1 parent 9466171 commit f3d3ae9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/pms/core/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def __call__(self, *, raw: Optional[bool] = None):
for row in self.data:
time, message = int(row["time"]), bytes.fromhex(row["hex"])
yield RawData(time, message) if raw else self.sensor.decode(message, time=time)
if self.samples: # pragma: no cover
if self.samples:
self.samples -= 1
if self.samples <= 0:
break
Expand Down
14 changes: 14 additions & 0 deletions tests/core/test_reader.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest

from pms.core import reader
from pms.core.sensor import Sensor
from tests.conftest import captured_data


class MockReader(reader.Reader):
Expand Down Expand Up @@ -162,3 +164,15 @@ def sys_exit(*_args):
raise Exception("should not get here")

assert "exit" in str(e.value)


def test_message_reader():
message_reader = reader.MessageReader(
path=captured_data,
sensor=Sensor["PMS3003"],
)

with message_reader:
values = list(message_reader())

assert len(values) == 10

0 comments on commit f3d3ae9

Please sign in to comment.