|
2 | 2 | import sys |
3 | 3 |
|
4 | 4 |
|
5 | | -def test_setup(): |
6 | | - sys.modules['RPi'] = mock.Mock() |
7 | | - sys.modules['RPi.GPIO'] = mock.Mock() |
8 | | - sys.modules['plasma'] = mock.Mock() |
| 5 | +def force_reimport(module): |
| 6 | + """Force the module under test to be re-imported. |
| 7 | +
|
| 8 | + Because pytest runs all tests within the same scope (this makes me cry) |
| 9 | + we have to do some manual housekeeping to avoid tests polluting each other. |
| 10 | +
|
| 11 | + In this case, the first `from fanshim import FanShim` would import both plasma |
| 12 | + and RPi.GPIO from the first test run fixtures. Since we want *clean* fixtures |
| 13 | + for each test this is not only no good but the results are outright weird- |
| 14 | +
|
| 15 | + IE: functions we expect to be called will have no calls because FanShim |
| 16 | + receives an entirely different mock object to the one we're validating against. |
| 17 | +
|
| 18 | + Since conftest.py already does some sys.modules mangling I see no reason not to |
| 19 | + do the same thing here. |
| 20 | + """ |
| 21 | + try: |
| 22 | + del sys.modules[module] |
| 23 | + except KeyError: |
| 24 | + pass |
| 25 | + |
| 26 | + |
| 27 | +def test_setup(GPIO, plasma): |
| 28 | + force_reimport('fanshim') |
9 | 29 |
|
10 | 30 | from fanshim import FanShim |
11 | 31 | fanshim = FanShim() |
12 | | - del fanshim |
| 32 | + |
| 33 | + GPIO.setwarnings.assert_called_once_with(False) |
| 34 | + GPIO.setmode.assert_called_once_with(GPIO.BCM) |
| 35 | + GPIO.setup.assert_has_calls([ |
| 36 | + mock.call(fanshim._pin_fancontrol, GPIO.OUT), |
| 37 | + mock.call(fanshim._pin_button, GPIO.IN, pull_up_down=GPIO.PUD_UP) |
| 38 | + ]) |
| 39 | + |
| 40 | + plasma.legacy.set_clear_on_exit.assert_called_once_with(True) |
| 41 | + plasma.legacy.set_light_count.assert_called_once_with(1) |
| 42 | + plasma.legacy.set_light.assert_called_once_with(0, 0, 0, 0) |
| 43 | + |
| 44 | + |
| 45 | +def test_button_disable(GPIO, plasma): |
| 46 | + force_reimport('fanshim') |
| 47 | + |
| 48 | + from fanshim import FanShim |
| 49 | + fanshim = FanShim(disable_button=True) |
| 50 | + |
| 51 | + GPIO.setwarnings.assert_called_once_with(False) |
| 52 | + GPIO.setmode.assert_called_once_with(GPIO.BCM) |
| 53 | + GPIO.setup.assert_called_once_with(fanshim._pin_fancontrol, GPIO.OUT) |
| 54 | + |
| 55 | + |
| 56 | +def test_led_disable(GPIO, plasma): |
| 57 | + force_reimport('fanshim') |
| 58 | + |
| 59 | + from fanshim import FanShim |
| 60 | + fanshim = FanShim(disable_led=True) |
| 61 | + |
| 62 | + GPIO.setwarnings.assert_called_once_with(False) |
| 63 | + GPIO.setmode.assert_called_once_with(GPIO.BCM) |
| 64 | + GPIO.setup.assert_has_calls([ |
| 65 | + mock.call(fanshim._pin_fancontrol, GPIO.OUT), |
| 66 | + mock.call(fanshim._pin_button, GPIO.IN, pull_up_down=GPIO.PUD_UP) |
| 67 | + ]) |
| 68 | + |
| 69 | + plasma.legacy.set_clear_on_exit.assert_not_called() |
| 70 | + plasma.legacy.set_light_count.assert_not_called() |
| 71 | + plasma.legacy.set_light.assert_not_called() |
| 72 | + |
| 73 | + fanshim.set_light(0, 0, 0) |
| 74 | + plasma.legacy.set_light.assert_not_called() |
| 75 | + plasma.legacy.show.assert_not_called() |
0 commit comments