From 435a79fad47df5b0069a2c4fbca46a577ddd4796 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Mon, 24 Jan 2022 14:03:18 +0900 Subject: [PATCH] Remove repetitive code from test cases --- backend/api/tests/test_category.py | 55 ++++++++++-------------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/backend/api/tests/test_category.py b/backend/api/tests/test_category.py index 9d007a80..cd059727 100644 --- a/backend/api/tests/test_category.py +++ b/backend/api/tests/test_category.py @@ -35,14 +35,7 @@ class TestCategoryAnnotation(abc.ABC, TestCase): self.assertTrue(can_annotate) -class TestExclusiveCategoryAnnotation(TestCategoryAnnotation): - exclusive = True - collaborative = False - - def test_cannot_annotate_different_category_to_annotated_data(self): - mommy.make('Category', example=self.example, user=self.user) - can_annotate = Category.objects.can_annotate(self.category, self.project.item) - self.assertFalse(can_annotate) +class NonCollaborativeMixin: def test_cannot_annotate_same_category_to_annotated_data(self): mommy.make('Category', example=self.example, label=self.label_type, user=self.user) @@ -60,34 +53,27 @@ class TestExclusiveCategoryAnnotation(TestCategoryAnnotation): self.assertTrue(can_annotate) -class TestNonExclusiveCategoryAnnotation(TestCategoryAnnotation): - exclusive = False +class TestExclusiveCategoryAnnotation(TestCategoryAnnotation, NonCollaborativeMixin): + exclusive = True collaborative = False - def test_can_annotate_different_category_to_annotated_data(self): + def test_cannot_annotate_different_category_to_annotated_data(self): mommy.make('Category', example=self.example, user=self.user) can_annotate = Category.objects.can_annotate(self.category, self.project.item) - self.assertTrue(can_annotate) - - def test_cannot_annotate_same_category_to_annotated_data(self): - mommy.make('Category', example=self.example, label=self.label_type, user=self.user) - can_annotate = Category.objects.can_annotate(self.category, self.project.item) self.assertFalse(can_annotate) - def test_allow_another_user_to_annotate_same_category(self): - mommy.make( - 'Category', - example=self.example, - label=self.label_type, - user=self.another_user - ) + +class TestNonExclusiveCategoryAnnotation(TestCategoryAnnotation, NonCollaborativeMixin): + exclusive = False + collaborative = False + + def test_can_annotate_different_category_to_annotated_data(self): + mommy.make('Category', example=self.example, user=self.user) can_annotate = Category.objects.can_annotate(self.category, self.project.item) self.assertTrue(can_annotate) -class TestCollaborativeExclusiveCategoryAnnotation(TestCategoryAnnotation): - exclusive = True - collaborative = True +class CollaborativeMixin: def test_deny_another_user_to_annotate_same_category(self): mommy.make( @@ -99,6 +85,11 @@ class TestCollaborativeExclusiveCategoryAnnotation(TestCategoryAnnotation): can_annotate = Category.objects.can_annotate(self.category, self.project.item) self.assertFalse(can_annotate) + +class TestCollaborativeExclusiveCategoryAnnotation(TestCategoryAnnotation, CollaborativeMixin): + exclusive = True + collaborative = True + def test_deny_another_user_to_annotate_different_category(self): mommy.make( 'Category', @@ -109,20 +100,10 @@ class TestCollaborativeExclusiveCategoryAnnotation(TestCategoryAnnotation): self.assertFalse(can_annotate) -class TestCollaborativeNonExclusiveCategoryAnnotation(TestCategoryAnnotation): +class TestCollaborativeNonExclusiveCategoryAnnotation(TestCategoryAnnotation, CollaborativeMixin): exclusive = False collaborative = True - def test_deny_another_user_to_annotate_same_category(self): - mommy.make( - 'Category', - example=self.example, - label=self.label_type, - user=self.another_user - ) - can_annotate = Category.objects.can_annotate(self.category, self.project.item) - self.assertFalse(can_annotate) - def test_allow_another_user_to_annotate_different_category(self): mommy.make( 'Category',