Fix timezone test and remove duplicated tests

This commit is contained in:
Alvaro Mariano 2023-06-25 18:51:09 +00:00
parent b75e5c66e5
commit 4c52d9f5af
5 changed files with 4 additions and 65 deletions

View file

@ -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")

View file

@ -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"

View file

@ -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

13
poetry.lock generated
View file

@ -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"

View file

@ -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"