]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/test_list.py
a795a5f4825f43de19aa115fc2773f67e5167935
[patchwork] / apps / patchwork / tests / test_list.py
1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2012 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 import random
22 import datetime
23 import string
24 import re
25 from django.test import TestCase
26 from django.test.client import Client
27 from patchwork.tests.utils import defaults, create_user, find_in_context
28 from patchwork.models import Person, Patch
29 from django.core.urlresolvers import reverse
30
31 class EmptyPatchListTest(TestCase):
32
33     def testEmptyPatchList(self):
34         """test that we don't output an empty table when there are no
35            patches present"""
36         project = defaults.project
37         defaults.project.save()
38         url = reverse('patchwork.views.patch.list',
39                 kwargs={'project_id': project.linkname})
40         response = self.client.get(url)
41         self.assertContains(response, 'No patches to display')
42         self.assertNotContains(response, 'tbody')
43
44 class PatchOrderTest(TestCase):
45
46     d = datetime.datetime
47     patchmeta = [
48         ('AlCMyjOsx', 'AlxMyjOsx@nRbqkQV.wBw', d(2014,3,16,13, 4,50, 155643)), 
49         ('MMZnrcDjT', 'MMmnrcDjT@qGaIfOl.tbk', d(2014,1,25,13, 4,50, 162814)), 
50         ('WGirwRXgK', 'WGSrwRXgK@TriIETY.GhE', d(2014,2,14,13, 4,50, 169305)), 
51         ('isjNIuiAc', 'issNIuiAc@OsEirYx.EJh', d(2014,3,15,13, 4,50, 176264)), 
52         ('XkAQpYGws', 'XkFQpYGws@hzntTcm.JSE', d(2014,1,18,13, 4,50, 182493)), 
53         ('uJuCPWMvi', 'uJACPWMvi@AVRBOBl.ecy', d(2014,3,12,13, 4,50, 189554)), 
54         ('TyQmWtcbg', 'TylmWtcbg@DzrNeNH.JuB', d(2014,2, 3,13, 4,50, 195685)), 
55         ('FpvAhWRdX', 'FpKAhWRdX@agxnCAI.wFO', d(2014,3,15,13, 4,50, 201398)), 
56         ('bmoYvnyWa', 'bmdYvnyWa@aeoPnlX.juy', d(2014,3, 4,13, 4,50, 206800)), 
57         ('CiReUQsAq', 'CiieUQsAq@DnOYRuf.TTI', d(2014,3,28,13, 4,50, 212169)),
58     ]
59
60     def setUp(self):
61         defaults.project.save()
62
63         for (name, email, date) in self.patchmeta:
64             patch_name = 'testpatch' + name
65             person = Person(name = name, email = email)
66             person.save()
67             patch = Patch(project = defaults.project, msgid = patch_name,
68                         submitter = person, content = '', date = date)
69             patch.save()
70
71     def _extract_patch_ids(self, response):
72         id_re = re.compile('<tr id="patch_row:(\d+)" ')
73         ids = [ int(m.group(1)) for m in id_re.finditer(response.content) ]
74         return ids
75
76     def _test_sequence(self, response, test_fn):
77         ids = self._extract_patch_ids(response)
78         self.assertTrue(bool(ids))
79         patches = [ Patch.objects.get(id = i) for i in ids ]
80         pairs = zip(patches, patches[1:])
81         [ test_fn(p1, p2) for (p1, p2) in pairs ]
82
83     def testDateOrder(self):
84         url = reverse('patchwork.views.patch.list',
85                 kwargs={'project_id': defaults.project.linkname})
86         response = self.client.get(url + '?order=date')
87         def test_fn(p1, p2):
88             self.assertLessEqual(p1.date, p2.date)
89         self._test_sequence(response, test_fn)
90
91     def testDateReverseOrder(self):
92         url = reverse('patchwork.views.patch.list',
93                 kwargs={'project_id': defaults.project.linkname})
94         response = self.client.get(url + '?order=-date')
95         def test_fn(p1, p2):
96             self.assertGreaterEqual(p1.date, p2.date)
97         self._test_sequence(response, test_fn)
98
99     def testSubmitterOrder(self):
100         url = reverse('patchwork.views.patch.list',
101                 kwargs={'project_id': defaults.project.linkname})
102         response = self.client.get(url + '?order=submitter')
103         def test_fn(p1, p2):
104             self.assertLessEqual(p1.submitter.name.lower(),
105                                  p2.submitter.name.lower())
106         self._test_sequence(response, test_fn)
107
108     def testSubmitterReverseOrder(self):
109         url = reverse('patchwork.views.patch.list',
110                 kwargs={'project_id': defaults.project.linkname})
111         response = self.client.get(url + '?order=-submitter')
112         def test_fn(p1, p2):
113             self.assertGreaterEqual(p1.submitter.name.lower(),
114                                     p2.submitter.name.lower())
115         self._test_sequence(response, test_fn)
116