Skip to content

Commit

Permalink
Add test to cover sensor reader temporary failure
Browse files Browse the repository at this point in the history
This will give us more confidence when we refactor these exception
handlers in the next commits.
  • Loading branch information
benthorner authored and avaldebe committed Dec 6, 2022
1 parent f8d6d59 commit c6f42d7
Show file tree
Hide file tree
Showing 2 changed files with 43 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 @@ -165,7 +165,7 @@ def __call__(self, *, raw: Optional[bool] = None):
except (SensorWarmingUp, InconsistentObservation) as e:
logger.debug(e)
time.sleep(5)
except SensorWarning as e: # pragma: no cover
except SensorWarning as e:
logger.debug(e)
self.serial.reset_input_buffer()
else:
Expand Down
42 changes: 42 additions & 0 deletions tests/core/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,31 @@ def passive_read(n):
)


@pytest.fixture
def mock_sensor_temp_failure(mock_serial):
def passive_read(n):
if n == 1:
# first return garbage data (bad checksum)
return (
b"BM\x00\x1c" # expected header
+ b"\0" * 26 # payload (to total 32 bytes)
+ b"\x00\xFF" # checksum
)
else:
# then behave like the original stub again
return (
b"BM\x00\x1c" # expected header
+ b".........................." # payload
+ b"\x05W" # checksum
)

mock_serial.stub(
name="passive_read",
receive_bytes=b"BM\xe2\x00\x00\x01q",
send_fn=passive_read,
)


@pytest.fixture
def sensor_reader_factory(monkeypatch, mock_sensor):
def factory(
Expand Down Expand Up @@ -204,6 +229,23 @@ def test_sensor_reader_warm_up(
assert len(obs) == 1


def test_sensor_reader_temp_failure(
mock_sensor,
sensor_reader_factory,
mock_sensor_temp_failure,
):
sensor_reader = sensor_reader_factory()

with sensor_reader as r:
obs = list(r())

# check one sample still acquired
assert len(obs) == 1

# check two samples were attempted
assert mock_sensor.stubs["passive_read"].calls == 2


def test_sensor_reader_sensor_mismatch(mock_sensor, sensor_reader_factory):
sensor_reader = sensor_reader_factory()

Expand Down

0 comments on commit c6f42d7

Please sign in to comment.