-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_local.py
64 lines (46 loc) · 1.95 KB
/
test_local.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
# test_local.py
# Runs PiBox simulation to test without RPi hardware access
import logging
import sys
import threading
from unittest.mock import Mock, patch
# Mock the Pi-Specific imports that would otherwise fail with module not found. Methods will be mocked later
sys.modules['RPi'] = Mock()
sys.modules['RPi.GPIO'] = Mock()
# Mock any API modules - application specific behavior will be specified below
sys.modules['api'] = Mock()
from tests.box_sim import *
# Application Specific
########################################
API_MODULE = 'api.weather'
class FakeAPI:
""" Update FakeAPI methods to correctly mock the behavior of the real API in api/ """
def __init__(self):
pass
def conditions(self):
return {'yesterday': {'high': 65}, 'today': {'high': 66, 'rain': 1, 'conditions': 'weather'}, 'tomorrow': {'high': 90, 'rain': 0}}
#########################################
def run_local_test():
""" Runs Box hardware class in thread, along with box simulator GUI """
fake_box = BoxWindow(button_callback=lambda: True)
def fake_pwm_factory(pin, freq):
return FakePWM(fake_box, pin, freq)
fakeapi = FakeAPI()
with patch('RPi.GPIO.PWM', fake_pwm_factory), patch(API_MODULE, fakeapi):
########################################
from apps.piweatherbox import WeatherBox
box = WeatherBox()
########################################
fake_box.set_callback(box._on_press)
# Run real box in thread
logic_thread = threading.Thread(target=box.mainloop)
logic_thread.start()
# Run fake box as main process, piping commands to real box
fake_box.mainloop()
# Kill real box thread
box._running = False
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(filename)s - %(message)s',
datefmt='%B %d, %Y %H:%M:%S')
run_local_test()