Skip to content

Commit

Permalink
Fixed failing tests from module move
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiky30 committed Dec 5, 2024
1 parent 5bcefbf commit 08abbec
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 26 deletions.
9 changes: 9 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from unittest.mock import patch

import pytest
from rest_framework.test import APIClient


@pytest.fixture
def client():
return APIClient()


# Component Temp probe
@pytest.fixture
def temp_probe_path():
with patch("shedpi_components.temperature_probe.Path"):
yield
Empty file added shedpi_components/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions shedpi_components/am2302.py
Original file line number Diff line number Diff line change
@@ -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
"""
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
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,
)
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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 3 additions & 3 deletions standalone_modules/shed_pi_module_utils/base_protocol.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from warnings import deprecated

from standalone_modules.shed_pi_module_utils.data_submission import (
ReadingSubmissionService,
)
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion standalone_modules/temperature_module/device_protocol.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
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,
)
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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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
"""
Expand All @@ -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
Expand Down

0 comments on commit 08abbec

Please sign in to comment.