Skip to content

Commit fa99340

Browse files
committed
added unittests for Notification metrics report
1 parent 260416d commit fa99340

2 files changed

Lines changed: 133 additions & 6 deletions

File tree

osf/metrics/reporters/notification_count.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
from osf.metrics.reports import (
2-
MonthlyReport,
3-
NotificationSummaryReport,
4-
)
1+
from osf.metrics.reports import NotificationSummaryReport
2+
from osf.metrics.reporters._base import MonthlyReporter
53
from osf.models.notification_subscription import NotificationSubscription
64

7-
class NotificationCountReporter(MonthlyReport):
5+
class NotificationCountReporter(MonthlyReporter):
86
report_name = 'Notification Metrics'
97

108
def report(self):
@@ -20,4 +18,4 @@ def report(self):
2018
created__gt=target_month, created__lt=next_month
2119
).values('user').distinct().count(),
2220
)
23-
return [report]
21+
return report
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import datetime
2+
from django.test import TestCase
3+
from osf.metrics.reporters import NotificationCountReporter
4+
from osf.metrics.utils import YearMonth
5+
from osf.models import NotificationType
6+
from ._testutils import list_monthly_reports
7+
from osf_tests.factories import NotificationSubscriptionFactory, UserFactory
8+
9+
10+
class TestNotificationsCountReporter(TestCase):
11+
def setUp(self):
12+
self.now = datetime.datetime.now()
13+
self.year_month = YearMonth(self.now.year, self.now.month)
14+
self.month_start = self.year_month.month_start()
15+
self.month_end = self.year_month.month_end()
16+
17+
@staticmethod
18+
def createNotificationSubscription(created, user, notification_type):
19+
notification_subscription = NotificationSubscriptionFactory(user=user, notification_type=notification_type)
20+
notification_subscription.created = created
21+
notification_subscription.save()
22+
23+
def test_no_data(self):
24+
reports = list_monthly_reports(NotificationCountReporter(self.year_month))
25+
assert len(reports) == 1
26+
report = reports[0]
27+
assert report.notification_subscriptions_count == 0
28+
assert report.notification_subscriptions_users_count == 0
29+
30+
def test_all_data_outside_month(self):
31+
user = UserFactory()
32+
# Before the month
33+
self.createNotificationSubscription(
34+
user=user,
35+
created=self.month_start - datetime.timedelta(days=1),
36+
notification_type=NotificationType.Type.NODE_FILES_UPDATED.instance
37+
)
38+
# After the month
39+
self.createNotificationSubscription(
40+
user=user,
41+
created=self.month_end + datetime.timedelta(days=1),
42+
notification_type=NotificationType.Type.NODE_REQUEST_ACCESS_SUBMITTED.instance
43+
)
44+
reports = list_monthly_reports(NotificationCountReporter(self.year_month))
45+
assert len(reports) == 1
46+
report = reports[0]
47+
assert report.notification_subscriptions_count == 0
48+
assert report.notification_subscriptions_users_count == 0
49+
50+
def test_single_subscription_in_month(self):
51+
user = UserFactory()
52+
self.createNotificationSubscription(
53+
user=user,
54+
created=self.month_start + datetime.timedelta(days=1),
55+
notification_type=NotificationType.Type.NODE_REQUEST_ACCESS_SUBMITTED.instance
56+
)
57+
reports = list_monthly_reports(NotificationCountReporter(self.year_month))
58+
assert len(reports) == 1
59+
report = reports[0]
60+
61+
assert report.notification_subscriptions_count == 1
62+
assert report.notification_subscriptions_users_count == 1
63+
64+
def test_multiple_subscriptions_same_user(self):
65+
user = UserFactory()
66+
self.createNotificationSubscription(
67+
user=user,
68+
created=self.month_start + datetime.timedelta(days=2),
69+
notification_type=NotificationType.Type.NODE_REQUEST_ACCESS_SUBMITTED.instance
70+
)
71+
self.createNotificationSubscription(
72+
user=user,
73+
created=self.month_start + datetime.timedelta(days=3),
74+
notification_type=NotificationType.Type.NODE_INSTITUTIONAL_ACCESS_REQUEST.instance
75+
)
76+
reports = list_monthly_reports(NotificationCountReporter(self.year_month))
77+
assert len(reports) == 1
78+
report = reports[0]
79+
assert report.notification_subscriptions_count == 2
80+
assert report.notification_subscriptions_users_count == 1
81+
82+
def test_multiple_users_multiple_subscriptions(self):
83+
user1 = UserFactory()
84+
user2 = UserFactory()
85+
self.createNotificationSubscription(
86+
user=user1,
87+
created=self.month_start + datetime.timedelta(days=2),
88+
notification_type=NotificationType.Type.NODE_AFFILIATION_CHANGED.instance
89+
)
90+
self.createNotificationSubscription(
91+
user=user1,
92+
created=self.month_start + datetime.timedelta(days=3),
93+
notification_type=NotificationType.Type.NODE_FILE_UPDATED.instance
94+
)
95+
self.createNotificationSubscription(
96+
user=user2,
97+
created=self.month_start + datetime.timedelta(days=4),
98+
notification_type=NotificationType.Type.NODE_REQUEST_ACCESS_SUBMITTED.instance
99+
)
100+
reports = list_monthly_reports(NotificationCountReporter(self.year_month))
101+
assert len(reports) == 1
102+
report = reports[0]
103+
assert report.notification_subscriptions_count == 3
104+
assert report.notification_subscriptions_users_count == 2
105+
106+
def test_subscriptions_spanning_months(self):
107+
user1 = UserFactory()
108+
user2 = UserFactory()
109+
# One in previous month, one in current, one in next
110+
self.createNotificationSubscription(
111+
user=user1,
112+
created=self.month_start - datetime.timedelta(days=1),
113+
notification_type=NotificationType.Type.NODE_REQUEST_ACCESS_SUBMITTED.instance
114+
)
115+
self.createNotificationSubscription(
116+
user=user1,
117+
created=self.month_start + datetime.timedelta(days=1),
118+
notification_type=NotificationType.Type.NODE_INSTITUTIONAL_ACCESS_REQUEST.instance
119+
)
120+
self.createNotificationSubscription(
121+
user=user2,
122+
created=self.month_end + datetime.timedelta(days=1),
123+
notification_type=NotificationType.Type.NODE_AFFILIATION_CHANGED.instance
124+
)
125+
reports = list_monthly_reports(NotificationCountReporter(self.year_month))
126+
assert len(reports) == 1
127+
report = reports[0]
128+
assert report.notification_subscriptions_count == 1
129+
assert report.notification_subscriptions_users_count == 1

0 commit comments

Comments
 (0)