diff --git a/conftest.py b/conftest.py index ba3dfa6..826870d 100644 --- a/conftest.py +++ b/conftest.py @@ -1,3 +1,5 @@ +from unittest.mock import patch + import pytest from rest_framework.test import APIClient @@ -5,3 +7,10 @@ @pytest.fixture def client(): return APIClient() + + +# Component Temp probe +@pytest.fixture +def temp_probe_path(): + with patch("shedpi_components.temperature_probe.Path"): + yield diff --git a/shedpi_components/__init__.py b/shedpi_components/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shedpi_components/am2302.py b/shedpi_components/am2302.py new file mode 100644 index 0000000..d5c3732 --- /dev/null +++ b/shedpi_components/am2302.py @@ -0,0 +1,4 @@ +""" +Code based on: https://github.com/Gozem/am2320/blob/45a20076efb9a19e91bd50f229d1cdd53f1134d4/am2320.py#L1 +License: MIT https://github.com/Gozem/am2320/blob/45a20076efb9a19e91bd50f229d1cdd53f1134d4/LICENSE#L1 +""" diff --git a/standalone_modules/temperature_module/temperature_probe.py b/shedpi_components/temperature_probe.py similarity index 95% rename from standalone_modules/temperature_module/temperature_probe.py rename to shedpi_components/temperature_probe.py index 2f90a8c..beda6bc 100644 --- a/standalone_modules/temperature_module/temperature_probe.py +++ b/shedpi_components/temperature_probe.py @@ -8,6 +8,8 @@ ReadingSubmissionService, ) +# TODO: Remove the submission_service from the component, it's an anti pattern + class TempProbe: def __init__(self, submission_service: ReadingSubmissionService): diff --git a/shedpi_hub_dashboard/tests/integration/test_module_reading_submission.py b/shedpi_hub_dashboard/tests/integration/test_module_reading_submission.py index 5d58a23..5932da2 100644 --- a/shedpi_hub_dashboard/tests/integration/test_module_reading_submission.py +++ b/shedpi_hub_dashboard/tests/integration/test_module_reading_submission.py @@ -4,6 +4,9 @@ import pytest from rest_framework import status +from shedpi_components.temperature_probe import ( + TempProbe, +) from shedpi_hub_dashboard.models import DeviceModuleReading from shedpi_hub_dashboard.tests.utils.factories import ( DeviceModuleFactory, @@ -11,9 +14,6 @@ from standalone_modules.shed_pi_module_utils.data_submission import ( ReadingSubmissionService, ) -from standalone_modules.temperature_module.temperature_probe import ( - TempProbe, -) @patch("standalone_modules.temperature_module.temperature_probe.Path") diff --git a/standalone_modules/shed_pi_example_device_installation/device_protocol.py b/standalone_modules/shed_pi_example_device_installation/device_protocol.py index fbd8868..f42509f 100644 --- a/standalone_modules/shed_pi_example_device_installation/device_protocol.py +++ b/standalone_modules/shed_pi_example_device_installation/device_protocol.py @@ -1,12 +1,12 @@ import time +from shedpi_components.temperature_probe import TempProbe from standalone_modules.rpi.device import RPIDevice from standalone_modules.shed_pi_module_utils.base_protocol import BaseProtocol from standalone_modules.shed_pi_module_utils.data_submission import ( ReadingSubmissionService, ) from standalone_modules.shed_pi_module_utils.utils import check_arch_is_arm, logger -from standalone_modules.temperature_module.temperature_probe import TempProbe TIME_TO_SLEEP = 60 # time in seconds diff --git a/standalone_modules/shed_pi_module_utils/base_protocol.py b/standalone_modules/shed_pi_module_utils/base_protocol.py index fab8a05..376598f 100644 --- a/standalone_modules/shed_pi_module_utils/base_protocol.py +++ b/standalone_modules/shed_pi_module_utils/base_protocol.py @@ -1,5 +1,3 @@ -from warnings import deprecated - from standalone_modules.shed_pi_module_utils.data_submission import ( ReadingSubmissionService, ) @@ -33,6 +31,8 @@ def shutdown(self) -> None: """ ... - @deprecated("Deprecated run method is replaced by start") def run(self) -> None: + """ + @deprecated("Deprecated run method is replaced by start") + """ raise NotImplementedError diff --git a/standalone_modules/temperature_module/device_protocol.py b/standalone_modules/temperature_module/device_protocol.py index ed1a8db..855dbd4 100644 --- a/standalone_modules/temperature_module/device_protocol.py +++ b/standalone_modules/temperature_module/device_protocol.py @@ -1,11 +1,11 @@ import time +from shedpi_components.temperature_probe import TempProbe from standalone_modules.shed_pi_module_utils.base_protocol import BaseProtocol from standalone_modules.shed_pi_module_utils.data_submission import ( ReadingSubmissionService, ) from standalone_modules.shed_pi_module_utils.utils import check_arch_is_arm, logger -from standalone_modules.temperature_module.temperature_probe import TempProbe TIME_TO_SLEEP = 60 # time in seconds diff --git a/standalone_modules/temperature_module/tests/integration/test_temp_module.py b/standalone_modules/temperature_module/tests/integration/test_temp_module.py index ad76fff..4493844 100644 --- a/standalone_modules/temperature_module/tests/integration/test_temp_module.py +++ b/standalone_modules/temperature_module/tests/integration/test_temp_module.py @@ -1,9 +1,12 @@ import json -from unittest.mock import Mock, patch +from unittest.mock import Mock import pytest from rest_framework import status +from shedpi_components.temperature_probe import ( + TempProbe, +) from shedpi_hub_dashboard.models import DeviceModuleReading from shedpi_hub_dashboard.tests.utils.factories import ( DeviceModuleFactory, @@ -11,14 +14,10 @@ from standalone_modules.shed_pi_module_utils.data_submission import ( ReadingSubmissionService, ) -from standalone_modules.temperature_module.temperature_probe import ( - TempProbe, -) -@patch("standalone_modules.temperature_module.temperature_probe.Path") @pytest.mark.django_db -def test_temperature_module_reading_submission(mocked_path, live_server): +def test_temperature_module_reading_submission(temp_probe_path, live_server): schema = { "$id": "https://example.com/person.schema.json", "$schema": "https://json-schema.org/draft/2020-12/schema", diff --git a/standalone_modules/temperature_module/tests/unit/test_temp_module.py b/standalone_modules/temperature_module/tests/unit/test_temp_module.py index b214eb5..e204205 100644 --- a/standalone_modules/temperature_module/tests/unit/test_temp_module.py +++ b/standalone_modules/temperature_module/tests/unit/test_temp_module.py @@ -1,7 +1,10 @@ -from unittest.mock import Mock, patch +from unittest.mock import Mock import pytest +from shedpi_components.temperature_probe import ( + TempProbe, +) from shedpi_hub_dashboard.models import DeviceModuleReading from shedpi_hub_dashboard.tests.utils.factories import ( DeviceModuleFactory, @@ -10,13 +13,9 @@ ReadingSubmissionService, ) from standalone_modules.temperature_module.device_protocol import DeviceProtocol -from standalone_modules.temperature_module.temperature_probe import ( - TempProbe, -) -@patch("standalone_modules.temperature_module.temperature_probe.Path") -def test_temp_probe_reading_happy_path(mocked_path): +def test_temp_probe_reading_happy_path(temp_probe_path): # FIXME: Get the actual readout from the modules probe = TempProbe(submission_service=Mock()) probe.read_temp_raw = Mock( @@ -30,8 +29,7 @@ def test_temp_probe_reading_happy_path(mocked_path): assert temp == 12.345 -@patch("standalone_modules.temperature_module.temperature_probe.Path") -def test_temp_probe_reading_invalid_reading(mocked_path): +def test_temp_probe_reading_invalid_reading(temp_probe_path): """ TODO: - Find what a real invalid reading looks like @@ -49,8 +47,7 @@ def test_temp_probe_reading_invalid_reading(mocked_path): probe.read_temp() -@patch("standalone_modules.temperature_module.temperature_probe.Path") -def test_temp_probe_reading_invalid_reading_missing_expected_params(mocked_path): +def test_temp_probe_reading_invalid_reading_missing_expected_params(temp_probe_path): """ YES is missing from the data feed """ @@ -74,9 +71,8 @@ def test_temp_probe_reading_invalid_reading_missing_expected_params(mocked_path) probe.read_temp_raw.call_count == 2 -@patch("standalone_modules.temperature_module.temperature_probe.Path") @pytest.mark.django_db -def test_temp_logger(mocked_path, live_server): +def test_temp_logger(temp_probe_path, live_server): # Submission service submission_service = ReadingSubmissionService() submission_service.base_url = live_server.url