Skip to content

Commit

Permalink
refactor(MODULES): group gpio integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
niall-byrne committed Feb 11, 2022
1 parent 8521bc3 commit abffe24
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 43 deletions.
3 changes: 2 additions & 1 deletion documentation/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@
# Exclude tests from sphinx_autopackagesummary here
autosummary_mock_imports = [
"pi_portal.tests",
"pi_portal.modules.tests",
"pi_portal.modules.configuration.tests",
"pi_portal.modules.general.tests",
"pi_portal.modules.tests",
"pi_portal.modules.integrations.gpio.tests",
"pi_portal.modules.integrations.tests",
"pi_portal.modules.system.tests",
]
Expand Down
6 changes: 3 additions & 3 deletions pi_portal/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""The Pi Portal Door Monitor Application."""
"""The Pi Portal Door DoorMonitor Application."""

import click
from . import config, modules
from . import config
from .modules import configuration, general, integrations, system


Expand All @@ -14,7 +14,7 @@ def cli():
def monitor():
"""Begin monitoring the door."""
configuration.state.State().load()
door_monitor = modules.monitor.Monitor()
door_monitor = integrations.door_monitor.DoorMonitor()
door_monitor.log = general.logger.setup_logger(
door_monitor.log, config.LOGFILE_PATH
)
Expand Down
3 changes: 0 additions & 3 deletions pi_portal/modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
"""Modules for the pi_portal cli."""

from . import monitor
1 change: 1 addition & 0 deletions pi_portal/modules/integrations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Integration Modules for the pi_portal cli."""

from . import slack
from .gpio import door_monitor
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Door Monitor Class."""
"""DoorMonitor Class."""

import logging
import os
Expand Down Expand Up @@ -27,7 +27,7 @@ class DoorState(Enum):
CLOSED = False


class Monitor:
class DoorMonitor:
"""Door state monitor and logger."""

hardware: Dict[int, int] = config.GPIO_SWITCHES
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
"""Test Door Monitor Class."""
"""Test the DoorMonitor Class."""

import logging
from unittest import TestCase, mock

from pi_portal import config
from pi_portal.modules import monitor
from pi_portal.modules.configuration.tests.fixtures import mock_state
from pi_portal.modules.integrations import slack
from pi_portal.modules.integrations.gpio import door_monitor


class TestMonitorLogger(TestCase):
"""Test the Monitor class."""
"""Test the DoorMonitor class."""

@mock_state.patch
def setUp(self):
self.monitor = monitor.Monitor()
self.monitor = door_monitor.DoorMonitor()
self.monitor.slack_client = mock.MagicMock()
self.monitor.interval = 0.01

@mock_state.patch
def test_initialize(self):
instance = monitor.Monitor()
instance = door_monitor.DoorMonitor()
self.assertTrue(instance.running)
self.assertEqual(instance.logger_name, "pi_portal")
self.assertEqual(instance.state, config.GPIO_INITIAL_STATE)
self.assertEqual(instance.interval, 0.5)
self.assertEqual(instance.GPIO, monitor.RPi.GPIO)
self.assertEqual(instance.GPIO, door_monitor.RPi.GPIO)
self.assertEqual(instance.GPIO_OPEN, True)
self.assertIsInstance(instance.log, logging.Logger)
self.assertIsInstance(instance.slack_client, slack.Client)

@mock_state.patch
@mock.patch("RPi.GPIO.setup")
def test_initialize_hardware(self, m_setup):
instance = monitor.Monitor()
instance = door_monitor.DoorMonitor()
for _, pin in instance.hardware.items():
m_setup.assert_any_call(
pin, instance.GPIO.IN, pull_up_down=instance.GPIO.PUD_UP
)
self.assertEqual(m_setup.call_count, len(instance.hardware))

@mock.patch(
monitor.__name__ + ".Monitor.running", new_callable=mock.PropertyMock
door_monitor.__name__ + ".DoorMonitor.running",
new_callable=mock.PropertyMock
)
@mock.patch(monitor.__name__ + ".Monitor.update_state")
@mock.patch(door_monitor.__name__ + ".DoorMonitor.update_state")
def test_loop_no_change(self, m_update_state, m_running):
m_running.side_effect = [True, False]
m_update_state.return_value = None
Expand All @@ -55,9 +56,10 @@ def test_loop_no_change(self, m_update_state, m_running):
self.monitor.slack_client.send_message.assert_not_called()

@mock.patch(
monitor.__name__ + ".Monitor.running", new_callable=mock.PropertyMock
door_monitor.__name__ + ".DoorMonitor.running",
new_callable=mock.PropertyMock
)
@mock.patch(monitor.__name__ + ".Monitor.GPIO.input")
@mock.patch(door_monitor.__name__ + ".DoorMonitor.GPIO.input")
def test_loop_front_door_opens(self, m_input, m_running):
self.monitor.state = {
1: False,
Expand All @@ -75,9 +77,10 @@ def test_loop_front_door_opens(self, m_input, m_running):
)

@mock.patch(
monitor.__name__ + ".Monitor.running", new_callable=mock.PropertyMock
door_monitor.__name__ + ".DoorMonitor.running",
new_callable=mock.PropertyMock
)
@mock.patch(monitor.__name__ + ".Monitor.GPIO.input")
@mock.patch(door_monitor.__name__ + ".DoorMonitor.GPIO.input")
def test_loop_back_door_opens(self, m_input, m_running):
self.monitor.state = {
1: False,
Expand All @@ -95,9 +98,10 @@ def test_loop_back_door_opens(self, m_input, m_running):
)

@mock.patch(
monitor.__name__ + ".Monitor.running", new_callable=mock.PropertyMock
door_monitor.__name__ + ".DoorMonitor.running",
new_callable=mock.PropertyMock
)
@mock.patch(monitor.__name__ + ".Monitor.GPIO.input")
@mock.patch(door_monitor.__name__ + ".DoorMonitor.GPIO.input")
def test_loop_front_door_closes(self, m_input, m_running):
self.monitor.state = {
1: True,
Expand All @@ -115,9 +119,10 @@ def test_loop_front_door_closes(self, m_input, m_running):
)

@mock.patch(
monitor.__name__ + ".Monitor.running", new_callable=mock.PropertyMock
door_monitor.__name__ + ".DoorMonitor.running",
new_callable=mock.PropertyMock
)
@mock.patch(monitor.__name__ + ".Monitor.GPIO.input")
@mock.patch(door_monitor.__name__ + ".DoorMonitor.GPIO.input")
def test_loop_back_door_closes(self, m_input, m_running):
self.monitor.state = {
1: True,
Expand Down
2 changes: 1 addition & 1 deletion pi_portal/modules/slack_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def command_uptime(self):

self.slack_client.send_message(
f"System Uptime > {system_uptime}\n"
f"Monitor Uptime > {monitor_uptime}\n"
f"Door Monitor Uptime > {monitor_uptime}\n"
f"Bot Uptime > {bot_uptime}"
)

Expand Down
8 changes: 4 additions & 4 deletions pi_portal/modules/tests/slack_cli/test_slack_cli_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def test_command_uptime_not_running(self, m_linux_uptime):
self.cli.command_uptime()
self.cli.slack_client.send_message.assert_called_once_with(
'System Uptime > 15 hours\n'
'Monitor Uptime > Not Running\n'
'Door Monitor Uptime > Not Running\n'
'Bot Uptime > Not Running'
)

Expand All @@ -198,7 +198,7 @@ def test_command_uptime_app_running(self, m_linux_uptime):
self.cli.command_uptime()
self.cli.slack_client.send_message.assert_called_once_with(
'System Uptime > 15 hours\n'
'Monitor Uptime > 4 hours\n'
'Door Monitor Uptime > 4 hours\n'
'Bot Uptime > Not Running'
)

Expand All @@ -214,7 +214,7 @@ def test_command_uptime_bot_running(self, m_linux_uptime):
self.cli.command_uptime()
self.cli.slack_client.send_message.assert_called_once_with(
'System Uptime > 15 hours\n'
'Monitor Uptime > Not Running\n'
'Door Monitor Uptime > Not Running\n'
'Bot Uptime > 3 hours'
)

Expand All @@ -230,7 +230,7 @@ def test_command_uptime_both_running(self, m_linux_uptime):
self.cli.command_uptime()
self.cli.slack_client.send_message.assert_called_once_with(
'System Uptime > 15 hours\n'
'Monitor Uptime > 3 hours\n'
'Door Monitor Uptime > 3 hours\n'
'Bot Uptime > 3 hours'
)

Expand Down
24 changes: 12 additions & 12 deletions pi_portal/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ class TestCLI(TestCase):
def setUp(self):
self.runner = CliRunner()

@patch(cli.__name__ + ".modules.monitor")
@patch(cli.__name__ + ".modules.configuration.state")
@patch(cli.__name__ + ".integrations.door_monitor")
@patch(cli.__name__ + ".configuration.state")
def test_monitor(self, m_state, m_monitor):
command = "monitor"
self.runner.invoke(cli.cli, command)
m_monitor.Monitor.assert_called_once_with()
m_monitor.Monitor.return_value.start.assert_called_once_with()
m_monitor.DoorMonitor.assert_called_once_with()
m_monitor.DoorMonitor.return_value.start.assert_called_once_with()
m_state.State.return_value.load.assert_called_once_with()

@patch(cli.__name__ + ".modules.integrations.slack")
@patch(cli.__name__ + ".modules.configuration.state")
@patch(cli.__name__ + ".integrations.slack")
@patch(cli.__name__ + ".configuration.state")
def test_slack_bot(self, m_state, m_slack):
command = "slack_bot"
self.runner.invoke(cli.cli, command)
m_slack.Client.assert_called_once_with()
m_slack.Client.return_value.subscribe.assert_called_once_with()
m_state.State.return_value.load.assert_called_once_with()

@patch(cli.__name__ + ".modules.integrations.slack")
@patch(cli.__name__ + ".modules.configuration.state")
@patch(cli.__name__ + ".integrations.slack")
@patch(cli.__name__ + ".configuration.state")
def test_upload_snapshot(self, m_state, m_slack):
mock_snapshot_name = __file__
command = f"upload_snapshot {mock_snapshot_name}"
Expand All @@ -43,8 +43,8 @@ def test_upload_snapshot(self, m_state, m_slack):
mock_snapshot_name
)

@patch(cli.__name__ + ".modules.integrations.slack")
@patch(cli.__name__ + ".modules.configuration.state")
@patch(cli.__name__ + ".integrations.slack")
@patch(cli.__name__ + ".configuration.state")
def test_upload_video(self, m_state, m_slack):
mock_video_name = __file__
command = f"upload_video {mock_video_name}"
Expand All @@ -55,8 +55,8 @@ def test_upload_video(self, m_state, m_slack):
mock_video_name
)

@patch(cli.__name__ + ".modules.system.installer")
@patch(cli.__name__ + ".modules.configuration.state")
@patch(cli.__name__ + ".system.installer")
@patch(cli.__name__ + ".configuration.state")
def test_installer(self, m_state, m_installer):
mock_config_file = __file__
command = f"installer {mock_config_file}"
Expand Down

0 comments on commit abffe24

Please sign in to comment.