-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
62 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |