From e399551ef1cf3387074fe177475824ac94660a11 Mon Sep 17 00:00:00 2001 From: Tyrel Narciso Date: Tue, 19 Mar 2024 15:14:30 -0700 Subject: [PATCH] DBC22-1890: Unit tests for current weather model --- src/backend/apps/shared/tests.py | 5 +--- src/backend/apps/weather/models.py | 2 +- .../tests/test_current_weather_model.py | 23 +++++++++++++++++++ .../tests/test_regional_weather_populate.py | 9 ++++---- 4 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 src/backend/apps/weather/tests/test_current_weather_model.py diff --git a/src/backend/apps/shared/tests.py b/src/backend/apps/shared/tests.py index 1fe78218f..ff7573395 100644 --- a/src/backend/apps/shared/tests.py +++ b/src/backend/apps/shared/tests.py @@ -3,16 +3,14 @@ from apps.cms.models import Ferry from apps.event.models import Event +from apps.shared.views import FeedbackView from apps.weather.models import RegionalWeather from apps.webcam.models import Webcam from django.core.cache import cache from django.test import TestCase from httpx import HTTPStatusError - from rest_framework.test import APIRequestFactory -from apps.shared.views import FeedbackView - logger = logging.getLogger(__name__) @@ -52,4 +50,3 @@ def tearDown(self): Event.objects.all().delete() Ferry.objects.all().delete() RegionalWeather.objects.all().delete() - diff --git a/src/backend/apps/weather/models.py b/src/backend/apps/weather/models.py index 8e63f98eb..bf1953f0a 100644 --- a/src/backend/apps/weather/models.py +++ b/src/backend/apps/weather/models.py @@ -57,5 +57,5 @@ def __str__(self): return f"Current weather for {self.pk}" def save(self, *args, **kwargs): - self.location = Point(self.location_latitude, self.location_longitude) + self.location = Point((self.location_latitude, self.location_longitude)) super().save(*args, **kwargs) diff --git a/src/backend/apps/weather/tests/test_current_weather_model.py b/src/backend/apps/weather/tests/test_current_weather_model.py new file mode 100644 index 000000000..b381fdcec --- /dev/null +++ b/src/backend/apps/weather/tests/test_current_weather_model.py @@ -0,0 +1,23 @@ +from unittest.mock import patch + +from apps.shared.tests import BaseTest +from apps.weather.models import BaseModel, CurrentWeather +from apps.weather.serializers import CurrentWeatherSerializer + + +def mocked_method(self): + # Method to block the base model save function + return True + +class TestCurrentWeatherModel(BaseTest): + + @patch.object(BaseModel, 'save', mocked_method) + def test_model_save(self): + current_weather_test = CurrentWeather( + weather_station_name="Vancouver", + location_latitude=58.66, + location_longitude=-124.64, + ) + current_weather_test.save(); + + assert(current_weather_test.location == "SRID=4326;POINT (58.66 -124.64)") diff --git a/src/backend/apps/weather/tests/test_regional_weather_populate.py b/src/backend/apps/weather/tests/test_regional_weather_populate.py index 073239e06..21d1fb16c 100644 --- a/src/backend/apps/weather/tests/test_regional_weather_populate.py +++ b/src/backend/apps/weather/tests/test_regional_weather_populate.py @@ -1,17 +1,16 @@ import json from pathlib import Path -from unittest import skip +from unittest import mock, skip from unittest.mock import patch + +from apps.feed.client import FeedClient from apps.shared.tests import BaseTest, MockResponse from apps.weather.models import RegionalWeather from apps.weather.tasks import ( populate_all_regional_weather_data, populate_regional_weather_from_data, - populate_all_regional_weather_data, ) from apps.weather.tests.test_data.regional_weather_parsed_feed import json_feed -from apps.feed.client import FeedClient -from unittest import mock class TestRegionalWeatherModel(BaseTest): def setUp(self): @@ -82,4 +81,4 @@ def test_populate_all_regional_weather_data(self): ] populate_all_regional_weather_data() - assert len(RegionalWeather.objects.all()) == 1 \ No newline at end of file + assert len(RegionalWeather.objects.all()) == 1