Skip to content

Commit

Permalink
Fix timezone test and remove duplicated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroLQueiroz committed Jun 25, 2023
1 parent b75e5c6 commit 4c52d9f
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 65 deletions.
3 changes: 2 additions & 1 deletion notifications/base/models.py
Expand Up @@ -299,6 +299,7 @@ def target_object_url(self):
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.
Expand Down Expand Up @@ -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")
46 changes: 0 additions & 46 deletions 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"
6 changes: 1 addition & 5 deletions notifications/tests/tests.py
Expand Up @@ -8,8 +8,6 @@
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
Expand Down Expand Up @@ -49,9 +47,7 @@ def test_use_timezone(self):
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
Expand Down
13 changes: 1 addition & 12 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Expand Up @@ -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"
Expand Down

0 comments on commit 4c52d9f

Please sign in to comment.