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