Show More
@@ -1,189 +1,191 | |||
|
1 | 1 | import os |
|
2 | 2 | import unittest |
|
3 | 3 | from rhodecode.tests import * |
|
4 | 4 | |
|
5 | 5 | from rhodecode.model.db import User, Notification, UserNotification |
|
6 | 6 | from rhodecode.model.user import UserModel |
|
7 | 7 | |
|
8 | 8 | from rhodecode.model.meta import Session |
|
9 | 9 | from rhodecode.model.notification import NotificationModel |
|
10 | 10 | |
|
11 | 11 | |
|
12 | 12 | class TestNotifications(unittest.TestCase): |
|
13 | 13 | |
|
14 | 14 | def __init__(self, methodName='runTest'): |
|
15 | 15 | Session.remove() |
|
16 | 16 | self.u1 = UserModel().create_or_update(username=u'u1', |
|
17 | 17 | password=u'qweqwe', |
|
18 | 18 | email=u'u1@rhodecode.org', |
|
19 | 19 | firstname=u'u1', lastname=u'u1') |
|
20 | 20 | Session().commit() |
|
21 | 21 | self.u1 = self.u1.user_id |
|
22 | 22 | |
|
23 | 23 | self.u2 = UserModel().create_or_update(username=u'u2', |
|
24 | 24 | password=u'qweqwe', |
|
25 | 25 | email=u'u2@rhodecode.org', |
|
26 | 26 | firstname=u'u2', lastname=u'u3') |
|
27 | 27 | Session().commit() |
|
28 | 28 | self.u2 = self.u2.user_id |
|
29 | 29 | |
|
30 | 30 | self.u3 = UserModel().create_or_update(username=u'u3', |
|
31 | 31 | password=u'qweqwe', |
|
32 | 32 | email=u'u3@rhodecode.org', |
|
33 | 33 | firstname=u'u3', lastname=u'u3') |
|
34 | 34 | Session().commit() |
|
35 | 35 | self.u3 = self.u3.user_id |
|
36 | 36 | |
|
37 | 37 | super(TestNotifications, self).__init__(methodName=methodName) |
|
38 | 38 | |
|
39 | 39 | def _clean_notifications(self): |
|
40 | 40 | for n in Notification.query().all(): |
|
41 | 41 | Session().delete(n) |
|
42 | 42 | |
|
43 | 43 | Session().commit() |
|
44 | 44 | self.assertEqual(Notification.query().all(), []) |
|
45 | 45 | |
|
46 | 46 | def tearDown(self): |
|
47 | 47 | self._clean_notifications() |
|
48 | 48 | |
|
49 | 49 | def test_create_notification(self): |
|
50 | 50 | self.assertEqual([], Notification.query().all()) |
|
51 | 51 | self.assertEqual([], UserNotification.query().all()) |
|
52 | 52 | |
|
53 | 53 | usrs = [self.u1, self.u2] |
|
54 | 54 | notification = NotificationModel().create(created_by=self.u1, |
|
55 | 55 | subject=u'subj', body=u'hi there', |
|
56 | 56 | recipients=usrs) |
|
57 | 57 | Session().commit() |
|
58 | 58 | u1 = User.get(self.u1) |
|
59 | 59 | u2 = User.get(self.u2) |
|
60 | 60 | u3 = User.get(self.u3) |
|
61 | 61 | notifications = Notification.query().all() |
|
62 | 62 | self.assertEqual(len(notifications), 1) |
|
63 | 63 | |
|
64 | self.assertEqual(notifications[0].recipients, [u1, u2]) | |
|
65 | self.assertEqual(notification.notification_id, | |
|
66 | notifications[0].notification_id) | |
|
67 | ||
|
64 | 68 | unotification = UserNotification.query()\ |
|
65 | 69 | .filter(UserNotification.notification == notification).all() |
|
66 | 70 | |
|
67 | self.assertEqual(notifications[0].recipients, [u1, u2]) | |
|
68 | self.assertEqual(notification.notification_id, | |
|
69 | notifications[0].notification_id) | |
|
70 | 71 | self.assertEqual(len(unotification), len(usrs)) |
|
71 |
self.assertEqual([x.user.user_id for x in unotification], |
|
|
72 | self.assertEqual(set([x.user.user_id for x in unotification]), | |
|
73 | set(usrs)) | |
|
72 | 74 | |
|
73 | 75 | def test_user_notifications(self): |
|
74 | 76 | self.assertEqual([], Notification.query().all()) |
|
75 | 77 | self.assertEqual([], UserNotification.query().all()) |
|
76 | 78 | |
|
77 | 79 | notification1 = NotificationModel().create(created_by=self.u1, |
|
78 | 80 | subject=u'subj', body=u'hi there1', |
|
79 | 81 | recipients=[self.u3]) |
|
80 | 82 | Session().commit() |
|
81 | 83 | notification2 = NotificationModel().create(created_by=self.u1, |
|
82 | 84 | subject=u'subj', body=u'hi there2', |
|
83 | 85 | recipients=[self.u3]) |
|
84 | 86 | Session().commit() |
|
85 | 87 | u3 = Session().query(User).get(self.u3) |
|
86 | 88 | |
|
87 | 89 | self.assertEqual(sorted([x.notification for x in u3.notifications]), |
|
88 | 90 | sorted([notification2, notification1])) |
|
89 | 91 | |
|
90 | 92 | def test_delete_notifications(self): |
|
91 | 93 | self.assertEqual([], Notification.query().all()) |
|
92 | 94 | self.assertEqual([], UserNotification.query().all()) |
|
93 | 95 | |
|
94 | 96 | notification = NotificationModel().create(created_by=self.u1, |
|
95 | 97 | subject=u'title', body=u'hi there3', |
|
96 | 98 | recipients=[self.u3, self.u1, self.u2]) |
|
97 | 99 | Session().commit() |
|
98 | 100 | notifications = Notification.query().all() |
|
99 | 101 | self.assertTrue(notification in notifications) |
|
100 | 102 | |
|
101 | 103 | Notification.delete(notification.notification_id) |
|
102 | 104 | Session().commit() |
|
103 | 105 | |
|
104 | 106 | notifications = Notification.query().all() |
|
105 | 107 | self.assertFalse(notification in notifications) |
|
106 | 108 | |
|
107 | 109 | un = UserNotification.query().filter(UserNotification.notification |
|
108 | 110 | == notification).all() |
|
109 | 111 | self.assertEqual(un, []) |
|
110 | 112 | |
|
111 | 113 | def test_delete_association(self): |
|
112 | 114 | |
|
113 | 115 | self.assertEqual([], Notification.query().all()) |
|
114 | 116 | self.assertEqual([], UserNotification.query().all()) |
|
115 | 117 | |
|
116 | 118 | notification = NotificationModel().create(created_by=self.u1, |
|
117 | 119 | subject=u'title', body=u'hi there3', |
|
118 | 120 | recipients=[self.u3, self.u1, self.u2]) |
|
119 | 121 | Session().commit() |
|
120 | 122 | |
|
121 | 123 | unotification = UserNotification.query()\ |
|
122 | 124 | .filter(UserNotification.notification == |
|
123 | 125 | notification)\ |
|
124 | 126 | .filter(UserNotification.user_id == self.u3)\ |
|
125 | 127 | .scalar() |
|
126 | 128 | |
|
127 | 129 | self.assertEqual(unotification.user_id, self.u3) |
|
128 | 130 | |
|
129 | 131 | NotificationModel().delete(self.u3, |
|
130 | 132 | notification.notification_id) |
|
131 | 133 | Session().commit() |
|
132 | 134 | |
|
133 | 135 | u3notification = UserNotification.query()\ |
|
134 | 136 | .filter(UserNotification.notification == |
|
135 | 137 | notification)\ |
|
136 | 138 | .filter(UserNotification.user_id == self.u3)\ |
|
137 | 139 | .scalar() |
|
138 | 140 | |
|
139 | 141 | self.assertEqual(u3notification, None) |
|
140 | 142 | |
|
141 | 143 | # notification object is still there |
|
142 | 144 | self.assertEqual(Notification.query().all(), [notification]) |
|
143 | 145 | |
|
144 | 146 | #u1 and u2 still have assignments |
|
145 | 147 | u1notification = UserNotification.query()\ |
|
146 | 148 | .filter(UserNotification.notification == |
|
147 | 149 | notification)\ |
|
148 | 150 | .filter(UserNotification.user_id == self.u1)\ |
|
149 | 151 | .scalar() |
|
150 | 152 | self.assertNotEqual(u1notification, None) |
|
151 | 153 | u2notification = UserNotification.query()\ |
|
152 | 154 | .filter(UserNotification.notification == |
|
153 | 155 | notification)\ |
|
154 | 156 | .filter(UserNotification.user_id == self.u2)\ |
|
155 | 157 | .scalar() |
|
156 | 158 | self.assertNotEqual(u2notification, None) |
|
157 | 159 | |
|
158 | 160 | def test_notification_counter(self): |
|
159 | 161 | self._clean_notifications() |
|
160 | 162 | self.assertEqual([], Notification.query().all()) |
|
161 | 163 | self.assertEqual([], UserNotification.query().all()) |
|
162 | 164 | |
|
163 | 165 | NotificationModel().create(created_by=self.u1, |
|
164 | 166 | subject=u'title', body=u'hi there_delete', |
|
165 | 167 | recipients=[self.u3, self.u1]) |
|
166 | 168 | Session().commit() |
|
167 | 169 | |
|
168 | 170 | self.assertEqual(NotificationModel() |
|
169 | 171 | .get_unread_cnt_for_user(self.u1), 1) |
|
170 | 172 | self.assertEqual(NotificationModel() |
|
171 | 173 | .get_unread_cnt_for_user(self.u2), 0) |
|
172 | 174 | self.assertEqual(NotificationModel() |
|
173 | 175 | .get_unread_cnt_for_user(self.u3), 1) |
|
174 | 176 | |
|
175 | 177 | notification = NotificationModel().create(created_by=self.u1, |
|
176 | 178 | subject=u'title', body=u'hi there3', |
|
177 | 179 | recipients=[self.u3, self.u1, self.u2]) |
|
178 | 180 | Session().commit() |
|
179 | 181 | |
|
180 | 182 | self.assertEqual(NotificationModel() |
|
181 | 183 | .get_unread_cnt_for_user(self.u1), 2) |
|
182 | 184 | self.assertEqual(NotificationModel() |
|
183 | 185 | .get_unread_cnt_for_user(self.u2), 1) |
|
184 | 186 | self.assertEqual(NotificationModel() |
|
185 | 187 | .get_unread_cnt_for_user(self.u3), 2) |
|
186 | 188 | |
|
187 | 189 | |
|
188 | 190 | |
|
189 | 191 |
General Comments 0
You need to be logged in to leave comments.
Login now