Skip to content

Commit 5c2b1c5

Browse files
committed
QA: Add basic tests.
1 parent 685fc4d commit 5c2b1c5

File tree

4 files changed

+114
-14
lines changed

4 files changed

+114
-14
lines changed

.gitignore

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
__pycache__/
1+
build/
2+
_build/
3+
*.o
4+
*.so
5+
*.a
26
*.py[cod]
7+
*.egg-info
38
dist/
4-
sdist/
5-
env/
6-
build/
7-
develop-eggs/
8-
eggs/
9-
*.egg-info/
10-
.installed.cfg
11-
*.egg
9+
__pycache__
10+
.DS_Store
1211
*.deb
1312
*.dsc
1413
*.build
1514
*.changes
1615
*.orig.*
17-
library/debian/
1816
packaging/*tar.xz
19-
pip-log.txt
20-
pip-delete-this-directory.txt
21-
.DS_Store
22-
17+
library/debian/
18+
.coverage
19+
.pytest_cache
20+
.tox

tests/conftest.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import sys
2+
3+
import mock
4+
import pytest
5+
6+
7+
# MockSMbus borrowed from https://github.com/pimoroni/i2cdevice-python
8+
class MockSMBus:
9+
def __init__(self, i2c_bus, default_registers=None):
10+
self.regs = [0 for _ in range(255)]
11+
if default_registers is not None:
12+
for index in default_registers.keys():
13+
self.regs[index] = default_registers.get(index)
14+
15+
def write_i2c_block_data(self, i2c_address, register, values):
16+
self.regs[register:register + len(values)] = values
17+
18+
def read_i2c_block_data(self, i2c_address, register, length):
19+
return self.regs[register:register + length]
20+
21+
def read_byte_data(self, i2c_address, register):
22+
return self.read_i2c_block_data(i2c_address, register, 1)[0]
23+
24+
def write_byte_data(self, i2c_address, register, value):
25+
self.write_i2c_block_data(i2c_address, register, [value])
26+
27+
28+
@pytest.fixture(scope="function", autouse=True)
29+
def cleanup():
30+
yield
31+
del sys.modules["cap1xxx"]
32+
33+
34+
@pytest.fixture(scope="function")
35+
def smbus2():
36+
sys.modules["smbus2"] = mock.MagicMock()
37+
yield sys.modules["smbus2"]
38+
del sys.modules["smbus2"]
39+
40+
41+
@pytest.fixture(scope="function")
42+
def gpio():
43+
sys.modules["RPi"] = mock.Mock()
44+
sys.modules["RPi.GPIO"] = mock.MagicMock()
45+
yield
46+
del sys.modules["RPi"]
47+
del sys.modules["RPi.GPIO"]

tests/test_io.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from conftest import MockSMBus
2+
3+
4+
def test_inputs(gpio, smbus2):
5+
import cap1xxx
6+
7+
smbus2.SMBus.return_value = MockSMBus(1, default_registers={
8+
cap1xxx.R_PRODUCT_ID: cap1xxx.PID_CAP1208,
9+
cap1xxx.R_INPUT_STATUS: 0b10101010
10+
})
11+
12+
cap1208 = cap1xxx.Cap1208()
13+
assert cap1208.get_input_status() == [ "none", "press", "none", "press", "none", "press", "none", "press"]
14+
15+
16+
def test_outputs(gpio, smbus2):
17+
import cap1xxx
18+
19+
mock = MockSMBus(1, default_registers={
20+
cap1xxx.R_PRODUCT_ID: cap1xxx.PID_CAP1188,
21+
cap1xxx.R_INPUT_STATUS: 0b10101010
22+
})
23+
24+
smbus2.SMBus.return_value = mock
25+
26+
cap1188 = cap1xxx.Cap1188()
27+
cap1188.set_led_state(0, True)
28+
29+
assert mock.read_byte_data(cap1xxx.DEFAULT_ADDR, cap1xxx.R_LED_OUTPUT_CON) == 0b00000001

tests/test_setup.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
3+
4+
def test_setup(gpio, smbus2):
5+
import cap1xxx
6+
7+
smbus2.SMBus(1).read_byte_data.return_value = cap1xxx.PID_CAP1208
8+
cap1208 = cap1xxx.Cap1208()
9+
assert cap1208.number_of_leds == 8
10+
11+
smbus2.SMBus(1).read_byte_data.return_value = cap1xxx.PID_CAP1188
12+
cap1188 = cap1xxx.Cap1188()
13+
assert cap1188.number_of_leds == 8
14+
15+
smbus2.SMBus(1).read_byte_data.return_value = cap1xxx.PID_CAP1166
16+
cap1166 = cap1xxx.Cap1166()
17+
assert cap1166.number_of_leds == 6
18+
19+
20+
def test_setup_invalid_pid(gpio, smbus2):
21+
import cap1xxx
22+
23+
smbus2.SMBus(1).read_byte_data.return_value = 0x00
24+
25+
with pytest.raises(RuntimeError):
26+
_ = cap1xxx.Cap1208()

0 commit comments

Comments
 (0)