Repository: watchdogpolska/small_eod

Issue:
udostępnienie id w API dla tags i documentType 
Id dla tags i documetType, jest niezbędne dla zbudowania listy tych elementów w reakcie. 


Discussion:

Resolving patch:
diff --git a/backend-project/small_eod/letters/serializers.py b/backend-project/small_eod/letters/serializers.py
--- a/backend-project/small_eod/letters/serializers.py
+++ b/backend-project/small_eod/letters/serializers.py
@@ -13,7 +13,7 @@
 class DocumentTypeSerializer(serializers.ModelSerializer):
     class Meta:
         model = DocumentType
-        fields = ["name"]
+        fields = ["id", "name"]
 
 
 class LetterSerializer(UserLogModelSerializer):
diff --git a/backend-project/small_eod/tags/serializers.py b/backend-project/small_eod/tags/serializers.py
--- a/backend-project/small_eod/tags/serializers.py
+++ b/backend-project/small_eod/tags/serializers.py
@@ -6,5 +6,6 @@ class TagSerializer(serializers.ModelSerializer):
     class Meta:
         model = Tag
         fields = [
+            "id",
             "name",
         ]


Test patch:
diff --git a/backend-project/small_eod/cases/tests/test_serializers.py b/backend-project/small_eod/cases/tests/test_serializers.py
--- a/backend-project/small_eod/cases/tests/test_serializers.py
+++ b/backend-project/small_eod/cases/tests/test_serializers.py
@@ -1,15 +1,23 @@
 from django.test import TestCase
 
 from ..models import Case
-from ..serializers import CaseSerializer, CaseCountSerializer
+from ..serializers import CaseCountSerializer
 from ...features.factories import FeatureOptionFactory, FeatureFactory
 from ...notes.factories import NoteFactory
 from ...tags.models import Tag
 from ...users.factories import UserFactory
 from ...generic.mixins import AuthRequiredMixin
+from ...generic.tests.test_serializers import ResourceSerializerMixin
+from ..factories import CaseFactory
 
 
-class CaseCountSerializerTestCase(AuthRequiredMixin, TestCase):
+class CaseCountSerializerTestCase(ResourceSerializerMixin, AuthRequiredMixin, TestCase):
+    serializer_class = CaseCountSerializer
+    factory_class = CaseFactory
+
+    def get_serializer_context(self):
+        return {"request": self.request}
+
     def get_default_data(self, new_data=None, skip=None):
         new_data = new_data or {}
         skip = skip or []
@@ -31,25 +39,25 @@ def get_default_data(self, new_data=None, skip=None):
 
     def test_tag_field(self):
         self.login_required()
-        serializer = CaseSerializer(
-            data=self.get_default_data(), context={"request": self.request}
+        serializer = self.serializer_class(
+            data=self.get_default_data(), context=self.get_serializer_context()
         )
         self.assertTrue(serializer.is_valid(), serializer.errors)
         obj = serializer.save()
         self.assertTrue(Tag.objects.count(), 1)
         self.assertEqual(obj.tags.all()[0].name, "rejestr umów")
-        data = CaseSerializer(Case.objects.get()).data
+        data = self.serializer_class(Case.objects.get()).data
         self.assertTrue(data["tags"], ["rejestr umów"])
 
     def test_raise_for_over_maximum_feature(self):
         self.login_required()
         feature = FeatureFactory(max_options=3)
         options = FeatureOptionFactory.create_batch(size=5, feature=feature)
-        serializer = CaseCountSerializer(
+        serializer = self.serializer_class(
             data=self.get_default_data(
                 {"featureoptions": [x.id for x in options], "tag": []}
             ),
-            context={"request": self.request},
+            context=self.get_serializer_context(),
         )
         self.assertFalse(serializer.is_valid())
         self.assertEqual(set(serializer.errors.keys()), {"featureoptions"})
@@ -60,15 +68,15 @@ def test_serializer_counters(self):
         case_counted = Case.objects.with_counter().get()
         self.assertEqual(case_counted.letter_count, 0)
         self.assertEqual(case_counted.note_count, 1)
-        data = CaseCountSerializer(case_counted).data
+        data = self.serializer_class(case_counted).data
         self.assertEqual(data["letter_count"], 0)
         self.assertEqual(data["note_count"], 1)
 
     def test_default_for_related_user(self):
         self.login_required()
-        serializer = CaseCountSerializer(
+        serializer = self.serializer_class(
             data=self.get_default_data(skip=["responsible_user", "notified_user"]),
-            context={"request": self.request},
+            context=self.get_serializer_context(),
         )
         self.assertTrue(serializer.is_valid(), serializer.errors)
         obj = serializer.save()
@@ -78,14 +86,14 @@ def test_default_for_related_user(self):
     def test_save_related_user(self):
         self.login_required()
         [responsible_users, notified_users] = UserFactory.create_batch(size=2)
-        serializer = CaseCountSerializer(
+        serializer = self.serializer_class(
             data=self.get_default_data(
                 new_data={
                     "responsible_users": [responsible_users.pk],
                     "notified_users": [notified_users.pk],
                 }
             ),
-            context={"request": self.request},
+            context=self.get_serializer_context(),
         )
         self.assertTrue(serializer.is_valid(), serializer.errors)
         obj = serializer.save()
diff --git a/backend-project/small_eod/features/tests/test_serializers.py b/backend-project/small_eod/features/tests/test_serializers.py
--- a/backend-project/small_eod/features/tests/test_serializers.py
+++ b/backend-project/small_eod/features/tests/test_serializers.py
@@ -1,20 +1,21 @@
 from django.test import TestCase
-from rest_framework.test import APIRequestFactory, force_authenticate
 
 from ..models import FeatureOption
 from ..serializers import FeatureSerializer
-from ...users.factories import UserFactory
+from ...generic.mixins import AuthRequiredMixin
+from ...generic.tests.test_serializers import ResourceSerializerMixin
+from ..factories import FeatureFactory
 
 
-class FeatureSerializerTestCase(TestCase):
-    def setUp(self):
-        self.user = UserFactory()
-        factory = APIRequestFactory()
-        self.request = factory.get("/")
-        force_authenticate(self.request, user=self.user)
-        self.request.user = self.user
+class FeatureSerializerTestCase(ResourceSerializerMixin, AuthRequiredMixin, TestCase):
+    serializer_class = FeatureSerializer
+    factory_class = FeatureFactory
+
+    def get_serializer_context(self):
+        return {"request": self.request}
 
     def test_save_nested_values(self):
+        self.login_required()
         serializer = FeatureSerializer(
             data={
                 "name": "Czyja sprawa",
@@ -22,7 +23,7 @@ def test_save_nested_values(self):
                 "max_options": 2,
                 "featureoptions": [{"name": "SO-WP"}, {"name": "Klienci"}],
             },
-            context={"request": self.request},
+            context=self.get_serializer_context(),
         )
         self.assertTrue(serializer.is_valid(), serializer.errors)
         feature = serializer.save()
diff --git a/backend-project/small_eod/generic/tests/test_serializers.py b/backend-project/small_eod/generic/tests/test_serializers.py
new file mode 100644
--- /dev/null
+++ b/backend-project/small_eod/generic/tests/test_serializers.py
@@ -0,0 +1,15 @@
+class ResourceSerializerMixin:
+    serializer_class = None
+    factory_class = None
+
+    def get_serializer_context(self):
+        return {}
+
+    def test_should_serialize_successfully(self):
+        self.serializer_class(self.factory_class()).data
+
+    def test_have_id_field(self):
+        """
+        All serializers of resource MUST have "id" field for management purpose
+        """
+        self.assertIn("id", self.serializer_class(self.factory_class()).data)
diff --git a/backend-project/small_eod/letters/tests/test_serializers.py b/backend-project/small_eod/letters/tests/test_serializers.py
--- a/backend-project/small_eod/letters/tests/test_serializers.py
+++ b/backend-project/small_eod/letters/tests/test_serializers.py
@@ -4,6 +4,7 @@
 from ..serializers import LetterSerializer, DocumentTypeSerializer
 
 from ...generic.mixins import AuthRequiredMixin
+from ...generic.tests.test_serializers import ResourceSerializerMixin
 from ..factories import LetterFactory, DocumentTypeFactory
 from ...files.factories import FileFactory
 from ...channels.factories import ChannelFactory
@@ -13,7 +14,7 @@
 from ...cases.factories import CaseFactory
 
 
-class DocumentTypeSerializerTestCase(TestCase):
+class DocumentTypeSerializerTestCase(ResourceSerializerMixin, TestCase):
     serializer_class = DocumentTypeSerializer
     factory_class = DocumentTypeFactory
 
@@ -41,7 +42,7 @@ def test_update_name(self):
         self.assertEqual(obj.name, "Not so important letter")
 
 
-class LetterSerializerTestCase(AuthRequiredMixin, TestCase):
+class LetterSerializerTestCase(ResourceSerializerMixin, AuthRequiredMixin, TestCase):
     serializer_class = LetterSerializer
     factory_class = LetterFactory
 
diff --git a/backend-project/small_eod/tags/tests/test_serializers.py b/backend-project/small_eod/tags/tests/test_serializers.py
new file mode 100644
--- /dev/null
+++ b/backend-project/small_eod/tags/tests/test_serializers.py
@@ -0,0 +1,10 @@
+from django.test import TestCase
+
+from ...generic.tests.test_serializers import ResourceSerializerMixin
+from ..factories import TagFactory
+from ..serializers import TagSerializer
+
+
+class TagSerializerTestCase(ResourceSerializerMixin, TestCase):
+    serializer_class = TagSerializer
+    factory_class = TagFactory