1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2011 Jeremy Kerr <jk@ozlabs.org>
4 # This file is part of the Patchwork package.
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.
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.
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
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
26 class PatchNotificationModelTest(TestCase):
27 """Tests for the creation & update of the PatchChangeNotification model"""
30 self.project = defaults.project
31 self.project.send_notifications = True
33 self.submitter = defaults.patch_author_person
35 self.patch = Patch(project = self.project, msgid = 'testpatch',
36 name = 'testpatch', content = '',
37 submitter = self.submitter)
41 self.submitter.delete()
44 def testPatchCreation(self):
45 """Ensure we don't get a notification on create"""
47 self.assertEqual(PatchChangeNotification.objects.count(), 0)
49 def testPatchUninterestingChange(self):
50 """Ensure we don't get a notification for "uninteresting" changes"""
52 self.patch.archived = True
54 self.assertEqual(PatchChangeNotification.objects.count(), 0)
56 def testPatchChange(self):
57 """Ensure we get a notification for interesting patch changes"""
59 oldstate = self.patch.state
60 state = State.objects.exclude(pk = oldstate.pk)[0]
62 self.patch.state = state
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)
69 def testNotificationCancelled(self):
70 """Ensure we cancel notifications that are no longer valid"""
72 oldstate = self.patch.state
73 state = State.objects.exclude(pk = oldstate.pk)[0]
75 self.patch.state = state
77 self.assertEqual(PatchChangeNotification.objects.count(), 1)
79 self.patch.state = oldstate
81 self.assertEqual(PatchChangeNotification.objects.count(), 0)
83 def testNotificationUpdated(self):
84 """Ensure we update notifications when the patch has a second change,
85 but keep the original patch details"""
87 oldstate = self.patch.state
88 newstates = State.objects.exclude(pk = oldstate.pk)[:2]
90 self.patch.state = newstates[0]
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
97 self.patch.state = newstates[1]
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)
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
111 oldstate = self.patch.state
112 state = State.objects.exclude(pk = oldstate.pk)[0]
114 self.patch.state = state
116 self.assertEqual(PatchChangeNotification.objects.count(), 0)