|
23 | 23 | from patchwork.tests.utils import create_patches |
24 | 24 | from patchwork.tests.utils import create_person |
25 | 25 | from patchwork.tests.utils import create_project |
| 26 | +from patchwork.tests.utils import create_label |
26 | 27 | from patchwork.tests.utils import create_state |
27 | 28 | from patchwork.tests.utils import create_user |
28 | 29 | from patchwork.tests.utils import read_patch |
@@ -203,6 +204,128 @@ def test_utf8_handling(self): |
203 | 204 | self.assertEqual(response.status_code, 200) |
204 | 205 |
|
205 | 206 |
|
| 207 | +class PatchListLabelFilteringTest(TestCase): |
| 208 | + def setUp(self): |
| 209 | + self.project = create_project() |
| 210 | + self.label1 = create_label(project=self.project) |
| 211 | + self.label2 = create_label(project=self.project) |
| 212 | + self.global_label = create_label(project=None) |
| 213 | + |
| 214 | + person = create_person(name='test', email='test@example.com') |
| 215 | + |
| 216 | + self.patches = [ |
| 217 | + create_patch(submitter=person, project=self.project, labels=[]), |
| 218 | + create_patch( |
| 219 | + submitter=person, project=self.project, labels=[self.label1] |
| 220 | + ), |
| 221 | + create_patch( |
| 222 | + submitter=person, |
| 223 | + project=self.project, |
| 224 | + labels=[self.label1, self.label2], |
| 225 | + ), |
| 226 | + create_patch( |
| 227 | + submitter=person, |
| 228 | + project=self.project, |
| 229 | + labels=[self.label1, self.global_label], |
| 230 | + ), |
| 231 | + ] |
| 232 | + |
| 233 | + def _extract_patch_ids(self, response): |
| 234 | + id_re = re.compile(r'<tr id="patch-row:(\d+)"') |
| 235 | + ids = [ |
| 236 | + int(m.group(1)) for m in id_re.finditer(response.content.decode()) |
| 237 | + ] |
| 238 | + |
| 239 | + return ids |
| 240 | + |
| 241 | + def _extract_patches(self, response): |
| 242 | + ids = self._extract_patch_ids(response) |
| 243 | + if not ids: |
| 244 | + return [] |
| 245 | + return [Patch.objects.get(id=i) for i in ids] |
| 246 | + |
| 247 | + def test_no_labels(self): |
| 248 | + url = reverse( |
| 249 | + 'patch-list', kwargs={'project_id': self.project.linkname} |
| 250 | + ) |
| 251 | + response = self.client.get(url + '?labels=') |
| 252 | + |
| 253 | + patches = self._extract_patches(response) |
| 254 | + self.assertEqual(len(patches), len(self.patches)) |
| 255 | + |
| 256 | + def test_one_label(self): |
| 257 | + url = reverse( |
| 258 | + 'patch-list', kwargs={'project_id': self.project.linkname} |
| 259 | + ) |
| 260 | + response = self.client.get(url + '?labels=' + self.label1.name) |
| 261 | + |
| 262 | + patches = self._extract_patches(response) |
| 263 | + self.assertEqual(len(patches), 3) |
| 264 | + self.assertIn(self.patches[1], patches) |
| 265 | + self.assertIn(self.patches[2], patches) |
| 266 | + self.assertIn(self.patches[3], patches) |
| 267 | + |
| 268 | + def test_two_labels(self): |
| 269 | + url = reverse( |
| 270 | + 'patch-list', kwargs={'project_id': self.project.linkname} |
| 271 | + ) |
| 272 | + response = self.client.get( |
| 273 | + url + '?labels=' + self.label1.name + '+' + self.label2.name |
| 274 | + ) |
| 275 | + |
| 276 | + patches = self._extract_patches(response) |
| 277 | + self.assertEqual(len(patches), 1) |
| 278 | + self.assertIn(self.patches[2], patches) |
| 279 | + |
| 280 | + def test_global_label(self): |
| 281 | + url = reverse( |
| 282 | + 'patch-list', kwargs={'project_id': self.project.linkname} |
| 283 | + ) |
| 284 | + response = self.client.get( |
| 285 | + url + '?labels=' + self.label1.name + '+' + self.global_label.name |
| 286 | + ) |
| 287 | + |
| 288 | + patches = self._extract_patches(response) |
| 289 | + self.assertEqual(len(patches), 1) |
| 290 | + self.assertIn(self.patches[3], patches) |
| 291 | + |
| 292 | + def test_negative_label(self): |
| 293 | + url = reverse( |
| 294 | + 'patch-list', kwargs={'project_id': self.project.linkname} |
| 295 | + ) |
| 296 | + response = self.client.get(url + '?labels=-' + self.label2.name) |
| 297 | + |
| 298 | + patches = self._extract_patches(response) |
| 299 | + self.assertEqual(len(patches), 3) |
| 300 | + self.assertIn(self.patches[0], patches) |
| 301 | + self.assertIn(self.patches[1], patches) |
| 302 | + self.assertIn(self.patches[3], patches) |
| 303 | + |
| 304 | + def test_positive_negative_label(self): |
| 305 | + url = reverse( |
| 306 | + 'patch-list', kwargs={'project_id': self.project.linkname} |
| 307 | + ) |
| 308 | + response = self.client.get( |
| 309 | + url + '?labels=-' + self.label2.name + '+' + self.label1.name |
| 310 | + ) |
| 311 | + |
| 312 | + patches = self._extract_patches(response) |
| 313 | + self.assertEqual(len(patches), 2) |
| 314 | + self.assertIn(self.patches[1], patches) |
| 315 | + self.assertIn(self.patches[3], patches) |
| 316 | + |
| 317 | + def test_mutually_exclusive_labels(self): |
| 318 | + url = reverse( |
| 319 | + 'patch-list', kwargs={'project_id': self.project.linkname} |
| 320 | + ) |
| 321 | + response = self.client.get( |
| 322 | + url + '?labels=-' + self.label1.name + '+' + self.label1.name |
| 323 | + ) |
| 324 | + |
| 325 | + patches = self._extract_patches(response) |
| 326 | + self.assertEqual(len(patches), 0) |
| 327 | + |
| 328 | + |
206 | 329 | class PatchViewTest(TestCase): |
207 | 330 | def test_redirect(self): |
208 | 331 | patch = create_patch() |
|
0 commit comments