-
Notifications
You must be signed in to change notification settings - Fork 394
/
test_models.py
182 lines (149 loc) · 5.88 KB
/
test_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from datetime import datetime, timedelta
from unittest.mock import patch
import pytest
from django.core.exceptions import ImproperlyConfigured
from django.test import override_settings
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.django_db
def test_slug():
notification = NotificationShortFactory()
assert notification.id == notification.slug
@pytest.mark.parametrize(
"field,initial_status,method_name,expected",
(
("emailed", True, "mark_as_sent", True),
("emailed", False, "mark_as_sent", True),
("emailed", True, "mark_as_unsent", False),
("emailed", False, "mark_as_unsent", False),
("public", True, "mark_as_public", True),
("public", False, "mark_as_public", True),
("public", True, "mark_as_private", False),
("public", False, "mark_as_private", False),
("unread", True, "mark_as_read", False),
("unread", False, "mark_as_read", False),
("unread", True, "mark_as_unread", True),
("unread", False, "mark_as_unread", True),
),
)
@pytest.mark.django_db
def test_mark_as_methods(field, initial_status, method_name, expected):
notification = NotificationShortFactory(**{field: initial_status})
func = getattr(notification, method_name)
func()
assert getattr(notification, field, None) is expected
@pytest.mark.django_db
def test_mark_as_active():
notification = NotificationShortFactory(deleted=True)
with pytest.raises(ImproperlyConfigured):
notification.mark_as_active()
with override_settings(DJANGO_NOTIFICATIONS_CONFIG={"SOFT_DELETE": True}):
notification.mark_as_active()
assert notification.deleted is False
@pytest.mark.django_db
def test_mark_as_deleted():
notification = NotificationShortFactory(deleted=False)
with override_settings(DJANGO_NOTIFICATIONS_CONFIG={"SOFT_DELETE": True}):
notification.mark_as_deleted()
assert notification.deleted is True
notification.mark_as_deleted()
assert Notification.objects.count() == 0
@pytest.mark.parametrize(
"method,field",
(
("actor_object_url", "actor"),
("action_object_url", "action_object"),
("target_object_url", "target"),
),
)
@pytest.mark.django_db
def test_build_url(method, field):
notification = NotificationFullFactory()
url = getattr(notification, method)()
assert "<a href=" in url
assert str(getattr(notification, field).id) in url
with patch("notifications.models.base.reverse") as mock:
mock.side_effect = NoReverseMatch
url = getattr(notification, method)()
assert getattr(notification, field).id == url
@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.parametrize(
"increase,expected_result",
(
({"minutes": 10}, "today"),
({"days": 1}, "yesterday"),
),
)
@pytest.mark.django_db
def test_naturalday(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
@pytest.mark.parametrize(
"increase,expected_result",
(
({"minutes": 10}, "10\xa0minutes ago"),
({"days": 1}, "1\xa0day ago"),
),
)
@pytest.mark.django_db
def test_naturaltime(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.naturaltime() == expected_result
@pytest.mark.django_db
def test_extra_data():
data = {"url": "/learn/ask-a-pro/q/test-question-9/299/", "other_content": "Hello my 'world'"}
notification = NotificationFullFactory(data=data)
assert notification.data == data