]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/notifications.py
c4df1b0ff376897bfca5dec7eb919f979ce1a9c0
[patchwork] / apps / patchwork / tests / notifications.py
1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2011 Jeremy Kerr <jk@ozlabs.org>
3 #
4 # This file is part of the Patchwork package.
5 #
6 # Patchwork is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # Patchwork is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Patchwork; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 from django.test import TestCase
21 from django.core.urlresolvers import reverse
22 from django.db.utils import IntegrityError
23 from patchwork.models import Patch, State, PatchChangeNotification
24 from patchwork.tests.utils import defaults, create_maintainer
25
26 class PatchNotificationModelTest(TestCase):
27     """Tests for the creation & update of the PatchChangeNotification model"""
28
29     def setUp(self):
30         self.project = defaults.project
31         self.project.send_notifications = True
32         self.project.save()
33         self.submitter = defaults.patch_author_person
34         self.submitter.save()
35         self.patch = Patch(project = self.project, msgid = 'testpatch',
36                         name = 'testpatch', content = '',
37                         submitter = self.submitter)
38
39     def tearDown(self):
40         self.patch.delete()
41         self.submitter.delete()
42         self.project.delete()
43
44     def testPatchCreation(self):
45         """Ensure we don't get a notification on create"""
46         self.patch.save()
47         self.assertEqual(PatchChangeNotification.objects.count(), 0)
48
49     def testPatchUninterestingChange(self):
50         """Ensure we don't get a notification for "uninteresting" changes"""
51         self.patch.save()
52         self.patch.archived = True
53         self.patch.save()
54         self.assertEqual(PatchChangeNotification.objects.count(), 0)
55
56     def testPatchChange(self):
57         """Ensure we get a notification for interesting patch changes"""
58         self.patch.save()
59         oldstate = self.patch.state
60         state = State.objects.exclude(pk = oldstate.pk)[0]
61
62         self.patch.state = state
63         self.patch.save()
64         self.assertEqual(PatchChangeNotification.objects.count(), 1)
65         notification = PatchChangeNotification.objects.all()[0]
66         self.assertEqual(notification.patch, self.patch)
67         self.assertEqual(notification.orig_state, oldstate)
68
69     def testNotificationCancelled(self):
70         """Ensure we cancel notifications that are no longer valid"""
71         self.patch.save()
72         oldstate = self.patch.state
73         state = State.objects.exclude(pk = oldstate.pk)[0]
74
75         self.patch.state = state
76         self.patch.save()
77         self.assertEqual(PatchChangeNotification.objects.count(), 1)
78
79         self.patch.state = oldstate
80         self.patch.save()
81         self.assertEqual(PatchChangeNotification.objects.count(), 0)
82
83     def testNotificationUpdated(self):
84         """Ensure we update notifications when the patch has a second change,
85            but keep the original patch details"""
86         self.patch.save()
87         oldstate = self.patch.state
88         newstates = State.objects.exclude(pk = oldstate.pk)[:2]
89
90         self.patch.state = newstates[0]
91         self.patch.save()
92         self.assertEqual(PatchChangeNotification.objects.count(), 1)
93         notification = PatchChangeNotification.objects.all()[0]
94         self.assertEqual(notification.orig_state, oldstate)
95         orig_timestamp = notification.last_modified
96                          
97         self.patch.state = newstates[1]
98         self.patch.save()
99         self.assertEqual(PatchChangeNotification.objects.count(), 1)
100         notification = PatchChangeNotification.objects.all()[0]
101         self.assertEqual(notification.orig_state, oldstate)
102         self.assertTrue(notification.last_modified > orig_timestamp)
103
104     def testProjectNotificationsDisabled(self):
105         """Ensure we don't see notifications created when a project is
106            configured not to send them"""
107         self.project.send_notifications = False
108         self.project.save()
109
110         self.patch.save()
111         oldstate = self.patch.state
112         state = State.objects.exclude(pk = oldstate.pk)[0]
113
114         self.patch.state = state
115         self.patch.save()
116         self.assertEqual(PatchChangeNotification.objects.count(), 0)
117