From 4c52d9f5af0d92fd02df1021a86550c890a504ad Mon Sep 17 00:00:00 2001 From: Alvaro Mariano Date: Sun, 25 Jun 2023 18:51:09 +0000 Subject: [PATCH] Fix timezone test and remove duplicated tests --- notifications/base/models.py | 3 +- .../tests/sample_notifications/tests.py | 46 ------------------- notifications/tests/tests.py | 6 +-- poetry.lock | 13 +----- pyproject.toml | 1 - 5 files changed, 4 insertions(+), 65 deletions(-) diff --git a/notifications/base/models.py b/notifications/base/models.py index 1951acb..05fdbec 100644 --- a/notifications/base/models.py +++ b/notifications/base/models.py @@ -299,6 +299,7 @@ class AbstractNotification(models.Model): return self.target_object_id +# TODO: move to notifications/signals.py def notify_handler(verb, **kwargs): """ Handler function to create Notification instance upon action signal call. @@ -361,5 +362,5 @@ def notify_handler(verb, **kwargs): return new_notifications -# connect the signal +# TODO: Move to notifications/apps.py::ready() notify.connect(notify_handler, dispatch_uid="notifications.models.notification") diff --git a/notifications/tests/sample_notifications/tests.py b/notifications/tests/sample_notifications/tests.py index a5d96a1..e69de29 100644 --- a/notifications/tests/sample_notifications/tests.py +++ b/notifications/tests/sample_notifications/tests.py @@ -1,46 +0,0 @@ -import os -from unittest import skipUnless - -import swapper -from django.contrib.auth import get_user_model - -from notifications.signals import notify -from notifications.tests.tests import AdminTest as BaseAdminTest -from notifications.tests.tests import NotificationTest as BaseNotificationTest - -Notification = swapper.load_model("notifications", "Notification") -User = get_user_model() - - -@skipUnless(os.environ.get("SAMPLE_APP", False), "Running tests on standard django-notifications models") -class AdminTest(BaseAdminTest): - @classmethod - def setUpClass(cls): - super().setUpClass() - BaseAdminTest.app_name = "sample_notifications" - - -@skipUnless(os.environ.get("SAMPLE_APP", False), "Running tests on standard django-notifications models") -class NotificationTest(BaseNotificationTest): - pass - - -class TestExtraDataCustomAccessor(NotificationTest): - def setUp(self): - self.from_user = User.objects.create_user(username="from_extra", password="pwd", email="example@example.com") - self.to_user = User.objects.create_user(username="to_extra", password="pwd", email="example@example.com") - notify.send( - self.from_user, - recipient=self.to_user, - verb="commented", - action_object=self.from_user, - url="/learn/ask-a-pro/q/test-question-9/299/", - other_content="Hello my 'world'", - details="test detail", - ) - - def test_extra_data(self): - notification = Notification.objects.get(details="test detail") - assert notification, "Expected a notification retrieved by custom extra data accessor" - assert notification.details == "test detail", "Custom accessor should return set value" - assert "details" not in notification.data, "Custom accessor should not be in json data" diff --git a/notifications/tests/tests.py b/notifications/tests/tests.py index 1f31c6d..dd24763 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -8,8 +8,6 @@ Replace this with more appropriate tests for your application. import json from datetime import datetime, timezone -import pytz -from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.auth.models import Group from django.core.exceptions import ImproperlyConfigured @@ -49,9 +47,7 @@ class NotificationTest(TestCase): to_user = User.objects.create(username="to", password="pwd", email="example@example.com") notify.send(from_user, recipient=to_user, verb="commented", action_object=from_user) notification = Notification.objects.get(recipient=to_user) - delta = datetime.now().replace(tzinfo=timezone.utc) - localtime( - notification.timestamp, pytz.timezone(settings.TIME_ZONE) - ) + delta = datetime.now(tz=timezone.utc) - localtime(notification.timestamp) self.assertTrue(delta.seconds < 60) # The delta between the two events will still be less than a second despite the different timezones # The call to now and the immediate call afterwards will be within a short period of time, not 8 hours as the diff --git a/poetry.lock b/poetry.lock index c0d78ec..5790756 100644 --- a/poetry.lock +++ b/poetry.lock @@ -990,17 +990,6 @@ files = [ [package.dependencies] six = ">=1.5" -[[package]] -name = "pytz" -version = "2023.3" -description = "World timezone definitions, modern and historical" -optional = false -python-versions = "*" -files = [ - {file = "pytz-2023.3-py2.py3-none-any.whl", hash = "sha256:a151b3abb88eda1d4e34a9814df37de2a80e301e68ba0fd856fb9b46bfbbbffb"}, - {file = "pytz-2023.3.tar.gz", hash = "sha256:1d8ce29db189191fb55338ee6d0387d82ab59f3d00eac103412d64e0ebd0c588"}, -] - [[package]] name = "pyyaml" version = "6.0" @@ -1326,4 +1315,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.8.1" -content-hash = "5124ddae507dc735742cabf924f10727740142438abaa081cd1a239b1c1c2d7a" +content-hash = "ce4ffef738877cba76f3286abe45942472b1f1c9b637b0aa859f38dd34341aa8" diff --git a/pyproject.toml b/pyproject.toml index d68e4bc..044ee00 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -99,7 +99,6 @@ pylint-django = "^2" pytest = "^7" pytest-cov = "^4" pytest-django = "^4" -pytz = "^2023.3" tox = "^4" psycopg2-binary = "^2.9.6" django-test-migrations = "^1.3.0"