From abffe24cf89e8c6770896c3b935d5758d9b891a5 Mon Sep 17 00:00:00 2001 From: Niall Byrne Date: Sun, 30 Jan 2022 16:44:10 +0000 Subject: [PATCH] refactor(MODULES): group gpio integrations --- documentation/source/conf.py | 3 +- pi_portal/cli.py | 6 +-- pi_portal/modules/__init__.py | 3 -- pi_portal/modules/integrations/__init__.py | 1 + .../modules/integrations/gpio/__init__.py | 0 .../gpio/door_monitor.py} | 4 +- .../integrations/gpio/tests/__init__.py | 0 .../gpio/tests/test_door_monitor.py} | 39 +++++++++++-------- pi_portal/modules/slack_cli.py | 2 +- .../slack_cli/test_slack_cli_commands.py | 8 ++-- pi_portal/tests/test_cli.py | 24 ++++++------ 11 files changed, 47 insertions(+), 43 deletions(-) create mode 100644 pi_portal/modules/integrations/gpio/__init__.py rename pi_portal/modules/{monitor.py => integrations/gpio/door_monitor.py} (98%) create mode 100644 pi_portal/modules/integrations/gpio/tests/__init__.py rename pi_portal/modules/{tests/test_monitor.py => integrations/gpio/tests/test_door_monitor.py} (77%) diff --git a/documentation/source/conf.py b/documentation/source/conf.py index 562e82da..18fd1800 100644 --- a/documentation/source/conf.py +++ b/documentation/source/conf.py @@ -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", ] diff --git a/pi_portal/cli.py b/pi_portal/cli.py index 5df6500e..1ff6b418 100644 --- a/pi_portal/cli.py +++ b/pi_portal/cli.py @@ -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 @@ -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 ) diff --git a/pi_portal/modules/__init__.py b/pi_portal/modules/__init__.py index 15eeeba7..e69de29b 100644 --- a/pi_portal/modules/__init__.py +++ b/pi_portal/modules/__init__.py @@ -1,3 +0,0 @@ -"""Modules for the pi_portal cli.""" - -from . import monitor diff --git a/pi_portal/modules/integrations/__init__.py b/pi_portal/modules/integrations/__init__.py index 89521453..14cb56a7 100644 --- a/pi_portal/modules/integrations/__init__.py +++ b/pi_portal/modules/integrations/__init__.py @@ -1,3 +1,4 @@ """Integration Modules for the pi_portal cli.""" from . import slack +from .gpio import door_monitor diff --git a/pi_portal/modules/integrations/gpio/__init__.py b/pi_portal/modules/integrations/gpio/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pi_portal/modules/monitor.py b/pi_portal/modules/integrations/gpio/door_monitor.py similarity index 98% rename from pi_portal/modules/monitor.py rename to pi_portal/modules/integrations/gpio/door_monitor.py index 202ffe85..a5d0ac09 100644 --- a/pi_portal/modules/monitor.py +++ b/pi_portal/modules/integrations/gpio/door_monitor.py @@ -1,4 +1,4 @@ -"""Door Monitor Class.""" +"""DoorMonitor Class.""" import logging import os @@ -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 diff --git a/pi_portal/modules/integrations/gpio/tests/__init__.py b/pi_portal/modules/integrations/gpio/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pi_portal/modules/tests/test_monitor.py b/pi_portal/modules/integrations/gpio/tests/test_door_monitor.py similarity index 77% rename from pi_portal/modules/tests/test_monitor.py rename to pi_portal/modules/integrations/gpio/tests/test_door_monitor.py index 7cb22c8a..2b80ec07 100644 --- a/pi_portal/modules/tests/test_monitor.py +++ b/pi_portal/modules/integrations/gpio/tests/test_door_monitor.py @@ -1,31 +1,31 @@ -"""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) @@ -33,7 +33,7 @@ def test_initialize(self): @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 @@ -41,9 +41,10 @@ def test_initialize_hardware(self, m_setup): 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 @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/pi_portal/modules/slack_cli.py b/pi_portal/modules/slack_cli.py index a9e55b55..31dc4148 100644 --- a/pi_portal/modules/slack_cli.py +++ b/pi_portal/modules/slack_cli.py @@ -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}" ) diff --git a/pi_portal/modules/tests/slack_cli/test_slack_cli_commands.py b/pi_portal/modules/tests/slack_cli/test_slack_cli_commands.py index 2731e2b5..576bc5b7 100644 --- a/pi_portal/modules/tests/slack_cli/test_slack_cli_commands.py +++ b/pi_portal/modules/tests/slack_cli/test_slack_cli_commands.py @@ -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' ) @@ -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' ) @@ -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' ) @@ -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' ) diff --git a/pi_portal/tests/test_cli.py b/pi_portal/tests/test_cli.py index 2342afca..20f11e58 100644 --- a/pi_portal/tests/test_cli.py +++ b/pi_portal/tests/test_cli.py @@ -13,17 +13,17 @@ 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) @@ -31,8 +31,8 @@ def test_slack_bot(self, m_state, m_slack): 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}" @@ -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}" @@ -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}"