-
Notifications
You must be signed in to change notification settings - Fork 19
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
5 changed files
with
116 additions
and
71 deletions.
There are no files selected for viewing
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
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,80 @@ | ||
import struct | ||
import sys | ||
|
||
import mock | ||
import pytest | ||
|
||
|
||
class MockSerial: | ||
def __init__(self, *args, **kwargs): | ||
self.ptr = 0 | ||
self.sof = b"\x42\x4d" | ||
self.data = self.sof | ||
self.data += struct.pack(">H", 28) | ||
self.data += b"\x00" * 26 | ||
checksum = struct.pack(">H", sum(bytearray(self.data))) | ||
self.data += checksum | ||
|
||
def read(self, length): | ||
result = self.data[self.ptr : self.ptr + length] | ||
self.ptr += length | ||
if self.ptr >= len(self.data): | ||
self.ptr = 0 | ||
return result | ||
|
||
def flushInput(self): | ||
pass | ||
|
||
def close(self): | ||
pass | ||
|
||
|
||
class MockSerialFail(MockSerial): | ||
def __init__(self, *args, **kwargs): | ||
pass | ||
|
||
def read(self, length): | ||
return b"\x00" * length | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def pms5003(): | ||
import pms5003 | ||
yield pms5003 | ||
del sys.modules['pms5003'] | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def gpiod(): | ||
sys.modules['gpiod'] = mock.Mock() | ||
sys.modules['gpiod.line'] = mock.Mock() | ||
yield sys.modules['gpiod'] | ||
del sys.modules['gpiod.line'] | ||
del sys.modules['gpiod'] | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def gpiodevice(): | ||
gpiodevice = mock.Mock() | ||
gpiodevice.get_pins_for_platform.return_value = [(mock.Mock(), 0), (mock.Mock(), 0)] | ||
|
||
sys.modules['gpiodevice'] = gpiodevice | ||
yield gpiodevice | ||
del sys.modules['gpiodevice'] | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def serial(): | ||
sys.modules['serial'] = mock.Mock() | ||
sys.modules['serial'].Serial = MockSerial | ||
yield sys.modules['serial'] | ||
del sys.modules['serial'] | ||
|
||
|
||
@pytest.fixture(scope='function', autouse=False) | ||
def serial_fail(): | ||
sys.modules['serial'] = mock.Mock() | ||
sys.modules['serial'].Serial = MockSerialFail | ||
yield sys.modules['serial'] | ||
del sys.modules['serial'] | ||
|
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 |
---|---|---|
@@ -1,74 +1,22 @@ | ||
import struct | ||
import sys | ||
|
||
import mock | ||
import pytest | ||
|
||
|
||
class MockSerialFail: | ||
def __init__(self): | ||
pass | ||
|
||
def read(self, length): | ||
return b"\x00" * length | ||
|
||
|
||
class MockSerial: | ||
def __init__(self): | ||
self.ptr = 0 | ||
self.sof = b"\x42\x4d" | ||
self.data = self.sof | ||
self.data += struct.pack(">H", 28) | ||
self.data += b"\x00" * 26 | ||
checksum = struct.pack(">H", sum(bytearray(self.data))) | ||
self.data += checksum | ||
|
||
def read(self, length): | ||
result = self.data[self.ptr : self.ptr + length] | ||
self.ptr += length | ||
if self.ptr >= len(self.data): | ||
self.ptr = 0 | ||
return result | ||
|
||
|
||
def _mock(): | ||
sys.modules["RPi"] = mock.Mock() | ||
sys.modules["RPi.GPIO"] = mock.Mock() | ||
sys.modules["serial"] = mock.Mock() | ||
def test_setup(gpiod, gpiodevice, serial, pms5003): | ||
_ = pms5003.PMS5003() | ||
|
||
|
||
def test_setup(): | ||
_mock() | ||
import pms5003 | ||
|
||
sensor = pms5003.PMS5003() | ||
del sensor | ||
|
||
|
||
def test_double_setup(): | ||
_mock() | ||
import pms5003 | ||
|
||
def test_double_setup(gpiod, gpiodevice, serial, pms5003): | ||
sensor = pms5003.PMS5003() | ||
sensor.setup() | ||
|
||
|
||
def test_read(): | ||
_mock() | ||
import pms5003 | ||
|
||
def test_read(gpiod, gpiodevice, serial, pms5003): | ||
sensor = pms5003.PMS5003() | ||
sensor._serial = MockSerial() | ||
data = sensor.read() | ||
data.pm_ug_per_m3(2.5) | ||
|
||
|
||
def test_read_fail(): | ||
_mock() | ||
import pms5003 | ||
|
||
def test_read_fail(gpiod, gpiodevice, serial_fail, pms5003): | ||
sensor = pms5003.PMS5003() | ||
sensor._serial = MockSerialFail() | ||
with pytest.raises(pms5003.ReadTimeoutError): | ||
data = sensor.read() | ||
del data | ||
_ = sensor.read() |
This breaks my Raspberry Pi 2 because it can't find an entry in the dict. Some people were able to fix the problem by adding their own entry: https://forums.pimoroni.com/t/issue-with-enviro-with-rpi-zero-display-and-particles-not-working/23365/21