|
25 | 25 | from patchwork.tests.utils import create_project |
26 | 26 | from patchwork.tests.utils import create_state |
27 | 27 | from patchwork.tests.utils import create_user |
| 28 | +from patchwork.tests.utils import create_label |
28 | 29 | from patchwork.tests.utils import read_patch |
29 | 30 |
|
30 | 31 |
|
@@ -203,6 +204,136 @@ 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.label_RFC = create_label(project=self.project, name='RFC') |
| 213 | + self.label_RFC_global = create_label(project=None, name='RFC') |
| 214 | + self.global_label = create_label(project=None) |
| 215 | + |
| 216 | + self.person = create_person(name='test', email='test@example.com') |
| 217 | + |
| 218 | + self.p_no_labels = self.create_patch([]) |
| 219 | + self.p_label1 = self.create_patch([self.label1]) |
| 220 | + self.p_label1_2 = self.create_patch([self.label1, self.label2]) |
| 221 | + self.p_label1_global = self.create_patch( |
| 222 | + [self.label1, self.global_label] |
| 223 | + ) |
| 224 | + self.p_label_rfc = self.create_patch([self.label_RFC]) |
| 225 | + self.p_label_rfc_global = self.create_patch([self.label_RFC_global]) |
| 226 | + |
| 227 | + self.patches = [ |
| 228 | + self.p_no_labels, |
| 229 | + self.p_label1, |
| 230 | + self.p_label1_2, |
| 231 | + self.p_label1_global, |
| 232 | + self.p_label_rfc, |
| 233 | + self.p_label_rfc_global, |
| 234 | + ] |
| 235 | + |
| 236 | + def _extract_patch_ids(self, response): |
| 237 | + """Extract patch identifiers from patch list HTML.""" |
| 238 | + id_re = re.compile(r'<tr id="patch-row:(\d+)"') |
| 239 | + ids = [ |
| 240 | + int(m.group(1)) for m in id_re.finditer(response.content.decode()) |
| 241 | + ] |
| 242 | + |
| 243 | + return ids |
| 244 | + |
| 245 | + def create_patch(self, labels): |
| 246 | + return create_patch( |
| 247 | + submitter=self.person, project=self.project, labels=labels |
| 248 | + ) |
| 249 | + |
| 250 | + def _extract_patches(self, response): |
| 251 | + """Extract patches from patch list and return Patch models.""" |
| 252 | + ids = self._extract_patch_ids(response) |
| 253 | + if not ids: |
| 254 | + return [] |
| 255 | + return [Patch.objects.get(id=i) for i in ids] |
| 256 | + |
| 257 | + def filter_labels(self, labels, expected): |
| 258 | + """Test if results contain expected patches when filtered by labels.""" |
| 259 | + url = reverse( |
| 260 | + 'patch-list', kwargs={'project_id': self.project.linkname} |
| 261 | + ) |
| 262 | + query = '+'.join(labels) |
| 263 | + response = self.client.get(url + '?labels=' + query) |
| 264 | + |
| 265 | + patches = self._extract_patches(response) |
| 266 | + self.assertEqual(len(patches), len(expected)) |
| 267 | + for patch in patches: |
| 268 | + self.assertIn(patch, expected) |
| 269 | + |
| 270 | + def test_no_labels(self): |
| 271 | + """Test if no filter returns all patches.""" |
| 272 | + self.filter_labels([], self.patches) |
| 273 | + |
| 274 | + def test_one_label(self): |
| 275 | + self.filter_labels( |
| 276 | + [self.label1.name], |
| 277 | + [ |
| 278 | + self.p_label1, |
| 279 | + self.p_label1_2, |
| 280 | + self.p_label1_global, |
| 281 | + ], |
| 282 | + ) |
| 283 | + |
| 284 | + def test_two_labels(self): |
| 285 | + self.filter_labels( |
| 286 | + [self.label1.name, self.label2.name], [self.p_label1_2] |
| 287 | + ) |
| 288 | + |
| 289 | + def test_global_label(self): |
| 290 | + """Test filtering by global labels.""" |
| 291 | + self.filter_labels( |
| 292 | + [self.label1.name, self.global_label.name], [self.p_label1_global] |
| 293 | + ) |
| 294 | + |
| 295 | + def test_negative_label(self): |
| 296 | + """Test filtering with a negative term.""" |
| 297 | + self.filter_labels( |
| 298 | + ['-' + self.label2.name], |
| 299 | + [ |
| 300 | + self.p_no_labels, |
| 301 | + self.p_label1, |
| 302 | + self.p_label1_global, |
| 303 | + self.p_label_rfc, |
| 304 | + self.p_label_rfc_global, |
| 305 | + ], |
| 306 | + ) |
| 307 | + |
| 308 | + def test_positive_negative_label(self): |
| 309 | + """Test filtering with positive and negative term.""" |
| 310 | + self.filter_labels( |
| 311 | + ['-' + self.label2.name, self.label1.name], |
| 312 | + [ |
| 313 | + self.p_label1, |
| 314 | + self.p_label1_global, |
| 315 | + ], |
| 316 | + ) |
| 317 | + |
| 318 | + def test_mutually_exclusive_labels(self): |
| 319 | + """Test filtering by same label as negative and positive filter.""" |
| 320 | + self.filter_labels(['-' + self.label1.name, self.label1.name], []) |
| 321 | + |
| 322 | + def test_label_local_global(self): |
| 323 | + """Test filtering for patches with different labels, one local and |
| 324 | + one global (without a project) that have the same name. |
| 325 | + """ |
| 326 | + self.filter_labels( |
| 327 | + ['RFC'], [self.p_label_rfc, self.p_label_rfc_global] |
| 328 | + ) |
| 329 | + |
| 330 | + def test_label_case_insensitive(self): |
| 331 | + """Test if labels filtering is case-insensitive.""" |
| 332 | + self.filter_labels( |
| 333 | + ['rfc'], [self.p_label_rfc, self.p_label_rfc_global] |
| 334 | + ) |
| 335 | + |
| 336 | + |
206 | 337 | class PatchViewTest(TestCase): |
207 | 338 | def test_redirect(self): |
208 | 339 | patch = create_patch() |
|
0 commit comments