Skip to content

Commit

Permalink
using conftest
Browse files Browse the repository at this point in the history
add shutdown_button.py
rename python files
  • Loading branch information
veloxid committed Feb 8, 2020
1 parent 741de8b commit a8f4fb4
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 154 deletions.
Empty file added components/__init__.py
Empty file.
Empty file.
6 changes: 3 additions & 3 deletions components/gpio_control/gpio_control.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import configparser
import logging

from components.gpio_control.SimpleButton import SimpleButton
from components.gpio_control.TwoButtonControl import TwoButtonControl
from components.gpio_control.simple_button import SimpleButton
from components.gpio_control.two_button_control import TwoButtonControl
from helperscripts import function_calls

from components.gpio_control.RotaryEncoder import RotaryEncoder
from components.gpio_control.rotary_encoder import RotaryEncoder

logger = logging.getLogger(__name__)

Expand Down
97 changes: 0 additions & 97 deletions components/gpio_control/rotary-encoder.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# rotary volume knob
# these files belong all together:
# RPi-Jukebox-RFID/scripts/rotary-encoder.py
# RPi-Jukebox-RFID/scripts/RotaryEncoder.py
# RPi-Jukebox-RFID/scripts/rotary_encoder.py
# RPi-Jukebox-RFID/misc/sampleconfigs/phoniebox-rotary-encoder.service.stretch-default.sample
# See wiki for more info: https://github.com/MiczFlor/RPi-Jukebox-RFID/wiki

Expand Down
48 changes: 48 additions & 0 deletions components/gpio_control/shutdown_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import math
import time
from RPi import GPIO
import logging
from components.gpio_control.simple_button import SimpleButton


logger = logging.getLogger(__name__)
class ShutdownButton(SimpleButton):

def __init__(self, pin, action=lambda *args: None, name=None, bouncetime=500, edge=GPIO.FALLING,
hold_time=.1, led_pin=None, time_pressed=2):
self.led_pin = led_pin
self.time_pressed = 2
self.iteration_time = .2
super(ShutdownButton, self).__init__(pin=pin, action=action, name=name, bouncetime=bouncetime, edge=edge,
hold_time=hold_time, hold_repeat=False)


# function to provide user feedback (= flashing led) while the shutdown button is pressed
# do not directly call shutdown, in case it was hit accedently
# shutdown is only issued when the button remains pressed for all interations of the for loop
def set_led(self, status):
if self.led_pin is not None:
logger.debug('set LED on pin {} to {}'.format(self.led_pin, status))
GPIO.output(self.led_pin, status)
else:
logger.debug('cannot set LED to {}: no LED pin defined'.format(status))

def callbackFunctionHandler(self, *args):
status = False
n_checks = math.ceil(self.time_pressed/self.iteration_time)
logger.debug('ShutdownButton pressed, ensuring long press for {} seconds, checking each {}s: {}'.format(
self.time_pressed, self.iteration_time, n_checks
))
for x in range(n_checks):
self.set_led(x & 1)
time.sleep(.2)
status = not self.is_pressed
if status:
break
self.set_led(status)
if not status:
# trippel off period to indicate command accepted
time.sleep(.6)
self.set_led(GPIO.HIGH)
# leave it on for the moment, it will be off when the system is down
self.when_pressed(*args)
File renamed without changes.
2 changes: 0 additions & 2 deletions components/gpio_control/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@
MockRPi.GPIO.LOW = 0
patcher = patch.dict("sys.modules", modules)
patcher.start()
import RPi.GPIO
GPIO = RPi.GPIO
2 changes: 1 addition & 1 deletion components/gpio_control/test/test_RotaryEncoder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from mock import MagicMock

from components.gpio_control.RotaryEncoder import RotaryEncoder
from components.gpio_control.rotary_encoder import RotaryEncoder
from RPi import GPIO

pinA = 1
Expand Down
2 changes: 1 addition & 1 deletion components/gpio_control/test/test_SimpleButton.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
patcher.start()

import RPi.GPIO as GPIO
from components.gpio_control.SimpleButton import SimpleButton
from components.gpio_control.simple_button import SimpleButton

pin = 1
mockedAction = MagicMock()
Expand Down
62 changes: 31 additions & 31 deletions components/gpio_control/test/test_TwoButtonControl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
from mock import MagicMock

from components.gpio_control import TwoButtonControl
from components.gpio_control import two_button_control


@pytest.fixture
Expand Down Expand Up @@ -37,11 +37,11 @@ def test_functionCallTwoButtonsOnlyBtn1Pressed(btn1Mock,
functionCallBothPressedMock):
btn1Mock.is_pressed = True
btn2Mock.is_pressed = False
func = TwoButtonControl.functionCallTwoButtons(btn1Mock,
btn2Mock,
functionCall1Mock,
functionCall2Mock,
functionCallBothPressed=functionCallBothPressedMock)
func = two_button_control.functionCallTwoButtons(btn1Mock,
btn2Mock,
functionCall1Mock,
functionCall2Mock,
functionCallBothPressed=functionCallBothPressedMock)
func()
functionCall1Mock.assert_called_once_with()
functionCall2Mock.assert_not_called()
Expand All @@ -55,8 +55,8 @@ def test_functionCallTwoButtonsBothBtnsPressedFunctionCallBothPressedExists(btn1
functionCallBothPressedMock):
btn1Mock.is_pressed = True
btn2Mock.is_pressed = True
func = TwoButtonControl.functionCallTwoButtons(btn1Mock, btn2Mock, functionCall1Mock, functionCall2Mock,
functionCallBothPressed=functionCallBothPressedMock)
func = two_button_control.functionCallTwoButtons(btn1Mock, btn2Mock, functionCall1Mock, functionCall2Mock,
functionCallBothPressed=functionCallBothPressedMock)
func()
functionCall1Mock.assert_not_called()
functionCall2Mock.assert_not_called()
Expand All @@ -69,8 +69,8 @@ def test_functionCallTwoButtonsBothBtnsPressedFunctionCallBothPressedIsNone(btn1
functionCall2Mock):
btn1Mock.is_pressed = True
btn2Mock.is_pressed = True
func = TwoButtonControl.functionCallTwoButtons(btn1Mock, btn2Mock, functionCall1Mock, functionCall2Mock,
functionCallBothPressed=None)
func = two_button_control.functionCallTwoButtons(btn1Mock, btn2Mock, functionCall1Mock, functionCall2Mock,
functionCallBothPressed=None)
func()
functionCall1Mock.assert_not_called()
functionCall2Mock.assert_not_called()
Expand All @@ -86,28 +86,28 @@ def two_button_control():
mockedFunction1.reset_mock()
mockedFunction2.reset_mock()
mockedFunction3.reset_mock()
return TwoButtonControl.TwoButtonControl(bcmPin1=1,
bcmPin2=2,
functionCallBtn1=mockedFunction1,
functionCallBtn2=mockedFunction2,
functionCallTwoBtns=mockedFunction3,
pull_up=True,
hold_repeat=False,
hold_time=0.3,
name='TwoButtonControl')
return two_button_control.TwoButtonControl(bcmPin1=1,
bcmPin2=2,
functionCallBtn1=mockedFunction1,
functionCallBtn2=mockedFunction2,
functionCallTwoBtns=mockedFunction3,
pull_up=True,
hold_repeat=False,
hold_time=0.3,
name='TwoButtonControl')


class TestTwoButtonControl:
def test_init(self):
TwoButtonControl.TwoButtonControl(bcmPin1=1,
bcmPin2=2,
functionCallBtn1=mockedFunction1,
functionCallBtn2=mockedFunction2,
functionCallTwoBtns=mockedFunction3,
pull_up=True,
hold_repeat=False,
hold_time=0.3,
name='TwoButtonControl')
two_button_control.TwoButtonControl(bcmPin1=1,
bcmPin2=2,
functionCallBtn1=mockedFunction1,
functionCallBtn2=mockedFunction2,
functionCallTwoBtns=mockedFunction3,
pull_up=True,
hold_repeat=False,
hold_time=0.3,
name='TwoButtonControl')

def test_btn1_pressed(self, two_button_control):
pinA = two_button_control.bcmPin1
Expand All @@ -119,7 +119,7 @@ def func(pin):
else:
print('Cannot find pin {} in values: {}'.format(pin,values))
return None
TwoButtonControl.GPIO.input.side_effect = func
two_button_control.GPIO.input.side_effect = func
two_button_control.action()
mockedFunction1.assert_called_once()
mockedFunction2.assert_not_called()
Expand All @@ -130,7 +130,7 @@ def func(pin):
def test_btn2_pressed(self, two_button_control):
pinA = two_button_control.bcmPin1
pinB = two_button_control.bcmPin2
TwoButtonControl.GPIO.input.side_effect = lambda pin: {pinA: True, pinB: False}[pin]
two_button_control.GPIO.input.side_effect = lambda pin: {pinA: True, pinB: False}[pin]
two_button_control.action()
mockedFunction1.assert_not_called()
mockedFunction2.assert_called_once()
Expand All @@ -141,7 +141,7 @@ def test_btn2_pressed(self, two_button_control):
def test_btn1_and_btn2_pressed(self, two_button_control):
pinA = two_button_control.bcmPin1
pinB = two_button_control.bcmPin2
TwoButtonControl.GPIO.input.side_effect = lambda pin: {pinA: False, pinB: False}[pin]
two_button_control.GPIO.input.side_effect = lambda pin: {pinA: False, pinB: False}[pin]
two_button_control.action()
mockedFunction1.assert_not_called()
mockedFunction2.assert_not_called()
Expand Down
20 changes: 3 additions & 17 deletions components/gpio_control/test/test_gpio_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,6 @@
import logging

from mock import patch, MagicMock

from components.gpio_control.test import mockedFunction1, mockedFunction2, mockedFunction3

MockRPi = MagicMock()
modules = {
"RPi": MockRPi,
"RPi.GPIO": MockRPi.GPIO,
}
patcher = patch.dict("sys.modules", modules)
patcher.start()

MockRPi.GPIO.RISING = 31
MockRPi.GPIO.FALLING = 32
MockRPi.GPIO.BOTH = 33
MockRPi.GPIO.HIGH = 1
MockRPi.GPIO.LOW = 0

from components.gpio_control.gpio_control import get_all_devices

# def test_functionCallTwoButtonsOnlyBtn2Pressed(btn1Mock, btn2Mock, functionCall1Mock, functionCall2Mock,
Expand All @@ -32,6 +15,9 @@
# functionCall2Mock.assert_called_once_with()
# functionCallBothPressedMock.assert_not_called()

mockedFunction1 = MagicMock()
mockedFunction2 = MagicMock()
mockedFunction3 = MagicMock()

mockedFunction1.side_effect = lambda *args: print('MockFunction1 called')
mockedFunction2.side_effect = lambda *args: print('MockFunction2 called')
Expand Down
43 changes: 43 additions & 0 deletions components/gpio_control/test/test_shutdown_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest

from mock import Mock, patch
import mock

from components.gpio_control.shutdown_button import ShutdownButton, GPIO

mock_time = Mock()

mocked_function = Mock()


@pytest.fixture
def shutdown_button():
mocked_function.reset_mock()
return ShutdownButton(pin=1, action=mocked_function)


class TestShutdownButton():
def test_init(self):
ShutdownButton(pin=1)

@patch('time.sleep', mock_time)
def test_action(self, shutdown_button):
for i in range(9):
GPIO.input.reset_mock()
GPIO.input.side_effect = i*[0]+[1]
shutdown_button.callbackFunctionHandler()
assert GPIO.input.call_count == i+1
mocked_function.assert_not_called()


@patch('time.sleep', mock_time)
def test_action2(self, shutdown_button):
GPIO.input.side_effect = lambda *args: 1
shutdown_button.callbackFunctionHandler()
mocked_function.assert_not_called()

@patch('time.sleep', mock_time)
def test_action3(self, shutdown_button):
GPIO.input.side_effect = lambda *args: 0
shutdown_button.callbackFunctionHandler()
mocked_function.assert_called_once()
Loading

0 comments on commit a8f4fb4

Please sign in to comment.