Skip to content

Commit

Permalink
split core.reader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avaldebe committed Dec 8, 2022
1 parent 2df8482 commit dcf4143
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 62 deletions.
Empty file added tests/core/reader/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions tests/core/reader/test_MessageReader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest

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


@pytest.fixture
def reader() -> MessageReader:
return MessageReader(captured_data, Sensor["PMS3003"])


def test_reader(reader: MessageReader):
with reader:
values = tuple(reader())

assert len(values) == 10


def test_closed(reader: MessageReader):
values = tuple(reader())
assert len(values) == 0
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
import pytest

from pms import SensorWarmingUp, SensorWarning
from pms.core.reader import MessageReader, Reader, SensorReader, UnableToRead, exit_on_fail
from pms.core.sensor import Sensor
from tests.conftest import captured_data


class MockReader(Reader):
def __init__(self, raise_on_enter=False):
self.raise_on_enter = raise_on_enter

def __call__(self):
raise NotImplementedError()

def open(self):
if self.raise_on_enter:
raise UnableToRead()
self.entered = True

def close(self):
self.exited = True
from pms.core.reader import SensorReader, UnableToRead


@pytest.fixture
Expand Down Expand Up @@ -296,46 +278,3 @@ def test_sensor_reader_sensor_no_response(sensor_reader_factory):
pass

assert "did not respond" in str(e.value)


def test_exit_on_fail_no_error(monkeypatch):
# prevent the helper exiting the test suite
monkeypatch.setattr("pms.core.reader.sys.exit", lambda: None)
mock_reader = MockReader()

with exit_on_fail(mock_reader) as yielded:
assert yielded == mock_reader

assert mock_reader.entered
assert mock_reader.exited


def test_exit_on_fail_error(monkeypatch):
def sys_exit(*_args):
raise Exception("exit")

# prevent the helper exiting the test suite
monkeypatch.setattr("pms.core.reader.sys.exit", sys_exit)
mock_reader = MockReader(raise_on_enter=True)

with pytest.raises(Exception) as e:
with exit_on_fail(mock_reader):
raise Exception("should not get here")

assert "exit" in str(e.value)


def test_message_reader():
message_reader = MessageReader(captured_data, Sensor["PMS3003"])

with message_reader:
values = tuple(message_reader())

assert len(values) == 10


def test_message_reader_closed():
message_reader = MessageReader(captured_data, Sensor["PMS3003"])

values = tuple(message_reader())
assert len(values) == 0
52 changes: 52 additions & 0 deletions tests/core/reader/test_exit_on_fail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest

from pms.core.reader import Reader, UnableToRead, exit_on_fail


class MockReader(Reader):
def __init__(self, raise_on_enter=False):
self.raise_on_enter = raise_on_enter

def __call__(self):
raise NotImplementedError()

def open(self):
if self.raise_on_enter:
raise UnableToRead()
self.entered = True

def close(self):
self.exited = True


@pytest.fixture()
def reader(monkeypatch) -> MockReader:
# prevent the helper exiting the test suite
monkeypatch.setattr("pms.core.reader.sys.exit", lambda: None)
return MockReader()


@pytest.fixture()
def reader_error(monkeypatch) -> MockReader:
def sys_exit(*args):
raise Exception("exit")

# prevent the helper exiting the test suite
monkeypatch.setattr("pms.core.reader.sys.exit", sys_exit)
return MockReader(raise_on_enter=True)


def test_reader(reader: MockReader):
with exit_on_fail(reader) as yielded:
assert yielded == reader

assert reader.entered
assert reader.exited


def test_reader_error(reader_error: MockReader):
with pytest.raises(Exception) as e:
with exit_on_fail(reader_error):
raise Exception("should not get here")

assert "exit" in str(e.value) # type:ignore[unreachable]

0 comments on commit dcf4143

Please sign in to comment.