Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb41d39
commit 5de134e
Showing
8 changed files
with
218 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,58 @@ | ||
import factory | ||
from django.contrib.contenttypes.models import ContentType | ||
from swapper import load_model | ||
|
||
from notifications.models import Notification | ||
from notifications.tests.factories.users import Actor, Recipient, Target | ||
from notifications.tests.factories.users import ( | ||
ActorFactory, | ||
RecipientFactory, | ||
TargetFactory, | ||
) | ||
|
||
VERB_LIST_SHORT = ("reached level 60", "joined to site") | ||
|
||
VERB_LIST_WITH_TARGET = ( | ||
"commented on", | ||
"started follow", | ||
"liked", | ||
) | ||
|
||
VERB_LIST = ( | ||
"commented", | ||
VERB_LIST_FULL = ( | ||
"closed", | ||
"opened", | ||
"liked", | ||
"deleted", | ||
) | ||
|
||
Notification = load_model("notifications", "Notification") | ||
|
||
|
||
class NotificationFactory(factory.django.DjangoModelFactory): | ||
recipient = factory.SubFactory(Recipient) | ||
class NotificationShortFactory(factory.django.DjangoModelFactory): | ||
recipient = factory.SubFactory(RecipientFactory) | ||
|
||
actor = factory.SubFactory(Actor) | ||
actor = factory.SubFactory(ActorFactory) | ||
actor_object_id = factory.SelfAttribute("actor.id") | ||
actor_content_type = factory.LazyAttribute(lambda obj: ContentType.objects.get_for_model(obj.actor)) | ||
|
||
verb = factory.Iterator(VERB_LIST) | ||
verb = factory.Iterator(VERB_LIST_SHORT) | ||
description = factory.Faker("catch_phrase") | ||
|
||
target = factory.SubFactory(Target) | ||
class Meta: | ||
model = Notification | ||
|
||
|
||
class NotificationWithTargetFactory(NotificationShortFactory): | ||
verb = factory.Iterator(VERB_LIST_WITH_TARGET) | ||
|
||
target = factory.SubFactory(TargetFactory) | ||
target_object_id = factory.SelfAttribute("target.id") | ||
target_content_type = factory.LazyAttribute(lambda obj: ContentType.objects.get_for_model(obj.target)) | ||
|
||
action_object = factory.SubFactory(Target) | ||
|
||
class NotificationWithActionObjectFactory(NotificationShortFactory): | ||
verb = factory.Iterator(VERB_LIST_WITH_TARGET) | ||
action_object = factory.SubFactory(TargetFactory) | ||
action_object_object_id = factory.SelfAttribute("action_object.id") | ||
action_object_content_type = factory.LazyAttribute(lambda obj: ContentType.objects.get_for_model(obj.action_object)) | ||
|
||
class Meta: | ||
model = Notification | ||
|
||
class NotificationFullFactory(NotificationWithTargetFactory, NotificationWithActionObjectFactory): | ||
verb = factory.Iterator(VERB_LIST_FULL) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from datetime import datetime, timedelta | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from django.urls import NoReverseMatch | ||
from freezegun import freeze_time | ||
from swapper import load_model | ||
|
||
from notifications.tests.factories.notifications import ( | ||
NotificationFullFactory, | ||
NotificationShortFactory, | ||
NotificationWithActionObjectFactory, | ||
NotificationWithTargetFactory, | ||
) | ||
|
||
Notification = load_model("notifications", "Notification") | ||
|
||
|
||
@pytest.mark.django_db | ||
def test__str__(): | ||
notification = NotificationShortFactory() | ||
|
||
notification_str = str(notification) | ||
assert str(notification.actor) in notification_str | ||
assert str(notification.verb) in notification_str | ||
assert str(notification.action_object) not in notification_str | ||
assert str(notification.target) not in notification_str | ||
|
||
notification = NotificationWithTargetFactory() | ||
notification_str = str(notification) | ||
assert str(notification.actor) in notification_str | ||
assert str(notification.verb) in notification_str | ||
assert str(notification.target) in notification_str | ||
assert str(notification.action_object) not in notification_str | ||
|
||
notification = NotificationWithActionObjectFactory() | ||
notification_str = str(notification) | ||
assert str(notification.actor) in notification_str | ||
assert str(notification.verb) in notification_str | ||
assert str(notification.action_object) in notification_str | ||
assert str(notification.target) not in notification_str | ||
|
||
notification = NotificationFullFactory() | ||
notification_str = str(notification) | ||
assert str(notification.actor) in notification_str | ||
assert str(notification.verb) in notification_str | ||
assert str(notification.target) in notification_str | ||
assert str(notification.action_object) in notification_str | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"increase,expected_result", | ||
( | ||
({"minutes": 10}, "10\xa0minutes"), | ||
({"days": 2}, "2\xa0days"), | ||
), | ||
) | ||
@pytest.mark.django_db | ||
def test_timesince(increase, expected_result): | ||
initial_date = datetime(2023, 1, 1, 0, 0, 0) | ||
with freeze_time(initial_date): | ||
notification = NotificationShortFactory() | ||
with freeze_time(initial_date + timedelta(**increase)): | ||
assert notification.timesince() == expected_result | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_slug(): | ||
notification = NotificationShortFactory() | ||
assert notification.id == notification.slug | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"before,method", | ||
( | ||
(True, "mark_as_read"), | ||
(False, "mark_as_unread"), | ||
), | ||
) | ||
@pytest.mark.django_db | ||
def test_mark_as_read_unread(before, method): | ||
notification = NotificationShortFactory(unread=before) | ||
|
||
assert Notification.objects.filter(unread=before).count() == 1 | ||
func = getattr(notification, method) | ||
func() | ||
assert Notification.objects.filter(unread=before).count() == 0 | ||
assert Notification.objects.filter(unread=not before).count() == 1 | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_build_url(): | ||
notification = NotificationShortFactory() | ||
|
||
url = notification.actor_object_url() | ||
|
||
assert "<a href=" in url | ||
assert str(notification.actor.id) in url | ||
|
||
with patch("notifications.models.base.reverse") as mock: | ||
mock.side_effect = NoReverseMatch | ||
url = notification.actor_object_url() | ||
assert notification.actor.id == url | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"increase,expected_result", | ||
( | ||
({"minutes": 10}, "today"), | ||
({"days": 1}, "yesterday"), | ||
), | ||
) | ||
@pytest.mark.django_db | ||
def test_natural_day(increase, expected_result): | ||
initial_date = datetime(2023, 1, 1, 0, 0, 0) | ||
with freeze_time(initial_date): | ||
notification = NotificationShortFactory() | ||
with freeze_time(initial_date + timedelta(**increase)): | ||
assert notification.naturalday() == expected_result |
Oops, something went wrong.