-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconftest.py
188 lines (141 loc) · 5.12 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import sys
from os import environ, listdir, path
from unittest.mock import MagicMock, PropertyMock, patch
import pytest
pytest_plugins = ("pytest_snapshot", "tests.plugins.snapshot_reporter")
# add packages to sys path so they can be imported normally in tests
packages_dir = path.join(path.dirname(__file__), "packages")
for dir in listdir(packages_dir):
subdir = path.join(packages_dir, dir)
sys.path.append(subdir)
# mock modules that are not installed but need to be impored
for module in [
"pitop.common.ptdm.zmq",
"imageio",
"zmq",
"smbus2",
"smbus",
"atexit",
"RPi",
"RPi.GPIO",
"spidev",
"pyinotify",
]:
sys.modules[module] = MagicMock()
# use gpiozero fake pins
environ["GPIOZERO_PIN_FACTORY"] = "mock"
@pytest.fixture
def oled_mocks():
SIZE = (128, 64)
MODE = "1"
SPI_BUS = 0
patches = {}
patches["miniscreen_lock_file_monitor"] = patch(
"pitop.miniscreen.miniscreen.MiniscreenLockFileMonitor"
)
patches["fps_regulator"] = patch("pitop.miniscreen.oled.oled.FPS_Regulator")
patches["ptdm_sub_client"] = patch(
"pitop.miniscreen.oled.core.device_controller.PTDMSubscribeClient"
)
patches["ptdm_req_client_device_controller"] = patch(
"pitop.miniscreen.oled.core.device_controller.PTDMRequestClient"
)
patches["ptdm_subscribe_client_oled"] = patch(
"pitop.miniscreen.oled.oled.PTDMSubscribeClient"
)
patches["ptdm_subscribe_client_miniscreen"] = patch(
"pitop.miniscreen.miniscreen.PTDMSubscribeClient"
)
patches["ptlock"] = patch("pitop.miniscreen.miniscreen.PTLock")
patches["spi_client"] = patch("pitop.miniscreen.oled.core.device_controller.spi")
patches["sh1106_client"] = patch(
"pitop.miniscreen.oled.core.device_controller.sh1106"
)
mocks = {}
for p in patches:
mocks[p] = patches[p].start()
device_mock = MagicMock()
device_mock.mode = MODE
device_mock.size = SIZE
device_mock.spi_bus = SPI_BUS
device_mock.contrast = MagicMock()
device_mock.bounding_box = (0, 0, SIZE[0] - 1, SIZE[1] - 1)
sh1106_mock = mocks["sh1106_client"]
sh1106_mock.return_value = device_mock
controller = MagicMock()
controller.get_device.return_value = sh1106_mock
yield {
"ptdm_req_client_mock": mocks["ptdm_req_client_device_controller"],
}
for p in patches:
patches[p].stop()
@pytest.fixture
def oled(oled_mocks):
from pitop.miniscreen.oled import OLED # noqa: E402
oled = OLED()
yield oled
del oled
@pytest.fixture
def analog_sensor_mocks():
plate_interface_patch = patch("pitop.pma.adc_base.PlateInterface")
from pitop.pma import LightSensor, Potentiometer, SoundSensor, UltrasonicSensor
LightSensor.read = MagicMock(return_value=0)
Potentiometer.read = MagicMock(return_value=0)
# object properties are mocked differently.
# we need to keep track of the mock object to be able to modify the returned value.
# also, the mock object can't be set a s an attribute directly, since it will only store its value
us_patch_obj = patch.object(
UltrasonicSensor, "distance", return_value=0, new_callable=PropertyMock
)
us_mock_obj = us_patch_obj.start()
UltrasonicSensor._mock = {"distance": us_mock_obj}
ss_patch_obj = patch.object(
SoundSensor, "reading", return_value=0, new_callable=PropertyMock
)
ss_mock_obj = ss_patch_obj.start()
SoundSensor._mock = {"reading": ss_mock_obj}
plate_interface_patch.start()
yield {
"light_sensor": LightSensor.read,
"sound_sensor": SoundSensor._mock.get("reading"),
"potentiometer": Potentiometer.read,
"ultrasonic_sensor": UltrasonicSensor._mock.get("distance"),
}
@pytest.fixture
def light_sensor_mock(analog_sensor_mocks):
yield analog_sensor_mocks.get("light_sensor")
@pytest.fixture
def sound_sensor_mock(analog_sensor_mocks):
yield analog_sensor_mocks.get("sound_sensor")
@pytest.fixture
def potentiometer_mock(analog_sensor_mocks):
yield analog_sensor_mocks.get("potentiometer")
@pytest.fixture
def ultrasonic_sensor_mock(analog_sensor_mocks):
yield analog_sensor_mocks.get("ultrasonic_sensor")
TESTS_FONT_DIR = f"{path.dirname(path.realpath(__file__))}/tests/fonts"
VERA_DIR = f"{TESTS_FONT_DIR}/ttf-bitstream-vera/"
ROBOTO_DIR = f"{TESTS_FONT_DIR}/roboto/"
@pytest.fixture
def fonts_mock(mocker):
from pitop.miniscreen.oled.assistant import Fonts
mocker.patch.object(Fonts, "_roboto_directory", ROBOTO_DIR)
mocker.patch.object(Fonts, "_vera_directory", VERA_DIR)
@pytest.fixture
def create_sim():
# this is used to ensure sim teardown
from pitop.simulation import simulate
sims = []
def _create_sim(component, scale=None, size=None):
sim = simulate(component, scale, size)
sims.append(sim)
return sim
yield _create_sim
for sim in sims:
sim.stop()
@pytest.fixture(autouse=True)
def reload_modules():
modules_to_reload = ["pitop.processing"]
for module_to_reload in modules_to_reload:
if module_to_reload in sys.modules:
del sys.modules[module_to_reload]