]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/bundles.py
[tests] Add initial bundle tests
[patchwork] / apps / patchwork / tests / bundles.py
1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2009 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 import unittest
21 from django.test import TestCase
22 from django.test.client import Client
23 from patchwork.models import Patch, Bundle, BundlePatch, Person
24 from patchwork.tests.utils import defaults, create_user, find_in_context
25
26 class BundleListTest(TestCase):
27     def setUp(self):
28         self.user = create_user()
29         self.client.login(username = self.user.username,
30                 password = self.user.username)
31
32     def testNoBundles(self):
33         response = self.client.get('/user/bundles/')
34         self.failUnlessEqual(response.status_code, 200)
35         self.failUnlessEqual(
36                 len(find_in_context(response.context, 'bundles')), 0)
37
38     def testSingleBundle(self):
39         defaults.project.save()
40         bundle = Bundle(owner = self.user, project = defaults.project)
41         bundle.save()
42         response = self.client.get('/user/bundles/')
43         self.failUnlessEqual(response.status_code, 200)
44         self.failUnlessEqual(
45                 len(find_in_context(response.context, 'bundles')), 1)
46
47     def tearDown(self):
48         self.user.delete()
49
50 class BundleTestBase(TestCase):
51     def setUp(self):
52         patch_names = ['testpatch1', 'testpatch2', 'testpatch3']
53         self.user = create_user()
54         self.client.login(username = self.user.username,
55                 password = self.user.username)
56         defaults.project.save()
57         self.bundle = Bundle(owner = self.user, project = defaults.project,
58                 name = 'testbundle')
59         self.bundle.save()
60         self.patches = []
61
62         for patch_name in patch_names:
63             patch = Patch(project = defaults.project,
64                                msgid = patch_name, name = patch_name,
65                                submitter = Person.objects.get(user = self.user),
66                                content = '')
67             patch.save()
68             self.patches.append(patch)
69
70     def tearDown(self):
71         for patch in self.patches:
72             patch.delete()
73         self.bundle.delete()
74         self.user.delete()
75
76 class BundleViewTest(BundleTestBase):
77
78     def testEmptyBundle(self):
79         response = self.client.get('/user/bundle/%d/' % self.bundle.id)
80         self.failUnlessEqual(response.status_code, 200)
81         page = find_in_context(response.context, 'page')
82         self.failUnlessEqual(len(page.object_list), 0)
83
84     def testNonEmptyBundle(self):
85         self.bundle.append_patch(self.patches[0])
86
87         response = self.client.get('/user/bundle/%d/' % self.bundle.id)
88         self.failUnlessEqual(response.status_code, 200)
89         page = find_in_context(response.context, 'page')
90         self.failUnlessEqual(len(page.object_list), 1)
91
92     def testBundleOrder(self):
93         for patch in self.patches:
94             self.bundle.append_patch(patch)
95
96         response = self.client.get('/user/bundle/%d/' % self.bundle.id)
97
98         pos = 0
99         for patch in self.patches:
100             next_pos = response.content.find(patch.name)
101             # ensure that this patch is after the previous
102             self.failUnless(next_pos > pos)
103             pos = next_pos
104
105         # reorder and recheck
106         i = 0
107         for patch in self.patches.__reversed__():
108             bundlepatch = BundlePatch.objects.get(bundle = self.bundle,
109                     patch = patch)
110             bundlepatch.order = i
111             bundlepatch.save()
112             i += 1
113
114         response = self.client.get('/user/bundle/%d/' % self.bundle.id)
115         pos = len(response.content)
116         for patch in self.patches:
117             next_pos = response.content.find(patch.name)
118             # ensure that this patch is now *before* the previous
119             self.failUnless(next_pos < pos)
120             pos = next_pos
121
122 class BundleCreateFromListTest(BundleTestBase):
123     def testCreateEmptyBundle(self):
124         newbundlename = 'testbundle-new'
125         params = {'form': 'patchlistform',
126                   'bundle_name': newbundlename,
127                   'action': 'Create',
128                   'project': defaults.project.id}
129
130         response = self.client.post(
131                 '/project/%s/list/' % defaults.project.linkname,
132                 params)
133
134         self.assertContains(response, 'Bundle %s created' % newbundlename)
135
136     def testCreateNonEmptyBundle(self):
137         newbundlename = 'testbundle-new'
138         patch = self.patches[0]
139
140         params = {'form': 'patchlistform',
141                   'bundle_name': newbundlename,
142                   'action': 'Create',
143                   'project': defaults.project.id,
144                   'patch_id:%d' % patch.id: 'checked'}
145
146         response = self.client.post(
147                 '/project/%s/list/' % defaults.project.linkname,
148                 params)
149
150         self.assertContains(response, 'Bundle %s created' % newbundlename)
151         self.assertContains(response, 'added to bundle %s' % newbundlename,
152             count = 1)
153
154         bundle = Bundle.objects.get(name = newbundlename)
155         self.failUnlessEqual(bundle.patches.count(), 1)
156         self.failUnlessEqual(bundle.patches.all()[0], patch)
157
158 class BundleCreateFromPatchTest(BundleTestBase):
159     def testCreateNonEmptyBundle(self):
160         newbundlename = 'testbundle-new'
161         patch = self.patches[0]
162
163         params = {'name': newbundlename,
164                   'action': 'createbundle'}
165
166         response = self.client.post('/patch/%d/' % patch.id, params)
167
168         self.assertContains(response,
169                 'Bundle %s created' % newbundlename)
170
171         bundle = Bundle.objects.get(name = newbundlename)
172         self.failUnlessEqual(bundle.patches.count(), 1)
173         self.failUnlessEqual(bundle.patches.all()[0], patch)
174
175
176 class BundleAddFromListTest(BundleTestBase):
177     def testAddToEmptyBundle(self):
178         patch = self.patches[0]
179         params = {'form': 'patchlistform',
180                   'action': 'Add',
181                   'project': defaults.project.id,
182                   'bundle_id': self.bundle.id,
183                   'patch_id:%d' % patch.id: 'checked'}
184
185         response = self.client.post(
186                 '/project/%s/list/' % defaults.project.linkname,
187                 params)
188
189         self.assertContains(response, 'added to bundle %s' % self.bundle.name,
190             count = 1)
191
192         self.failUnlessEqual(self.bundle.patches.count(), 1)
193         self.failUnlessEqual(self.bundle.patches.all()[0], patch)
194
195     def testAddToNonEmptyBundle(self):
196         self.bundle.append_patch(self.patches[0])
197         patch = self.patches[1]
198         params = {'form': 'patchlistform',
199                   'action': 'Add',
200                   'project': defaults.project.id,
201                   'bundle_id': self.bundle.id,
202                   'patch_id:%d' % patch.id: 'checked'}
203
204         response = self.client.post(
205                 '/project/%s/list/' % defaults.project.linkname,
206                 params)
207
208         self.assertContains(response, 'added to bundle %s' % self.bundle.name,
209             count = 1)
210
211         self.failUnlessEqual(self.bundle.patches.count(), 2)
212         self.failUnless(self.patches[0] in self.bundle.patches.all())
213         self.failUnless(self.patches[1] in self.bundle.patches.all())
214
215         # check order
216         bps = [ BundlePatch.objects.get(bundle = self.bundle,
217                                         patch = self.patches[i]) \
218                 for i in [0, 1] ]
219         self.failUnless(bps[0].order < bps[1].order)
220
221 class BundleAddFromPatchTest(BundleTestBase):
222     def testAddToEmptyBundle(self):
223         patch = self.patches[0]
224         params = {'action': 'addtobundle',
225                   'bundle_id': self.bundle.id}
226
227         response = self.client.post('/patch/%d/' % patch.id, params)
228
229         self.assertContains(response,
230                 'added to bundle &quot;%s&quot;' % self.bundle.name,
231                 count = 1)
232
233         self.failUnlessEqual(self.bundle.patches.count(), 1)
234         self.failUnlessEqual(self.bundle.patches.all()[0], patch)
235
236     def testAddToNonEmptyBundle(self):
237         self.bundle.append_patch(self.patches[0])
238         patch = self.patches[1]
239         params = {'action': 'addtobundle',
240                   'bundle_id': self.bundle.id}
241
242         response = self.client.post('/patch/%d/' % patch.id, params)
243
244         self.assertContains(response,
245                 'added to bundle &quot;%s&quot;' % self.bundle.name,
246                 count = 1)
247
248         self.failUnlessEqual(self.bundle.patches.count(), 2)
249         self.failUnless(self.patches[0] in self.bundle.patches.all())
250         self.failUnless(self.patches[1] in self.bundle.patches.all())
251
252         # check order
253         bps = [ BundlePatch.objects.get(bundle = self.bundle,
254                                         patch = self.patches[i]) \
255                 for i in [0, 1] ]
256         self.failUnless(bps[0].order < bps[1].order)
257
258 class BundleReorderTest(BundleTestBase):
259     def setUp(self):
260         super(BundleReorderTest, self).setUp()
261         self.bundle.append_patch(self.patches[0])
262         self.bundle.append_patch(self.patches[1])
263         self.bundle.append_patch(self.patches[2])
264
265     def testBundleReorder(self):
266         bundlepatch = BundlePatch.objects.get(bundle = self.bundle,
267                 patch = self.patches[0])
268
269         neworder = [self.patches[2], self.patches[0], self.patches[1]]
270         neworder_ids = [ p.id for p in neworder ]
271
272         params = {'form': 'reorderform',
273                   'order_start': bundlepatch.order,
274                   'neworder': neworder_ids}
275
276         response = self.client.post('/user/bundle/%d/' % self.bundle.id,
277                                     params)
278
279         self.failUnlessEqual(response.status_code, 200)
280
281         bundle_ids = [ bp.patch.id for bp in \
282                         BundlePatch.objects.filter(bundle = self.bundle) \
283                         .order_by('order') ]
284
285         self.failUnlessEqual(neworder_ids, bundle_ids)
286
287