From 0affe772264506384fa6c7e0e09019e6795bda12 Mon Sep 17 00:00:00 2001 From: Pamella Bezerra Date: Thu, 23 May 2024 16:21:07 -0300 Subject: [PATCH] Add more tests --- drf_rw_serializers/generics.py | 7 ++--- test_utils/base_tests.py | 4 ++- tests/test_generics.py | 49 +++++++++++++++------------------- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/drf_rw_serializers/generics.py b/drf_rw_serializers/generics.py index 95239b5..604ece2 100644 --- a/drf_rw_serializers/generics.py +++ b/drf_rw_serializers/generics.py @@ -34,13 +34,14 @@ def get_serializer_class(self): """ Return the class to use for the serializer. Defaults to using `self.serializer_class`. + If the request method is GET, it tries to use `self.read_serializer_class`. + If the request method is not GET, it tries to use `self.write_serializer_class`. + If the specific serializer class for the request method is not set, it falls back to + `self.serializer_class`. You may want to override this if you need to provide different serializations depending on the incoming request. (Eg. admins get full serialization, others get basic serialization) """ - if self.serializer_class is not None: - return self.serializer_class - if hasattr(self, "request"): if self.request.method in ["GET"]: assert ( diff --git a/test_utils/base_tests.py b/test_utils/base_tests.py index 2147964..a49ad12 100644 --- a/test_utils/base_tests.py +++ b/test_utils/base_tests.py @@ -1,7 +1,9 @@ from django.contrib.auth import get_user_model -from model_bakery import baker + from rest_framework.test import APIClient, APITestCase +from model_bakery import baker + from example_app.models import Order from example_app.serializers import OrderedMealDetailsSerializer diff --git a/tests/test_generics.py b/tests/test_generics.py index 3dba9d5..6deaa79 100644 --- a/tests/test_generics.py +++ b/tests/test_generics.py @@ -41,7 +41,7 @@ class NoSerializerClass(generics.GenericAPIView): ), ) - def test_return_serializer_class_over_rw(self): + def test_no_request_provided_return_serializer_class_over_rw(self): class SerializerClass(generics.GenericAPIView): serializer_class = OrderListSerializer read_serializer_class = OrderListSerializer @@ -49,6 +49,26 @@ class SerializerClass(generics.GenericAPIView): self.assertEqual(SerializerClass().get_serializer_class(), OrderListSerializer) + def test_get_request_provided(self): + class RWSerializerClass(generics.GenericAPIView): + read_serializer_class = OrderListSerializer + write_serializer_class = OrderCreateSerializer + + RWSerializerClass.request = mock.Mock(method="GET") + + self.assertEqual(RWSerializerClass().get_serializer_class(), OrderListSerializer) + + def test_non_get_request_provided(self): + class RWSerializerClass(generics.GenericAPIView): + read_serializer_class = OrderListSerializer + write_serializer_class = OrderCreateSerializer + + non_get_methods = ["POST", "PUT", "PATCH", "DELETE"] + + for method in non_get_methods: + RWSerializerClass.request = mock.Mock(method=method) + self.assertEqual(RWSerializerClass().get_serializer_class(), OrderCreateSerializer) + class GenericAPIViewGetReadSerializerClassTests(BaseTestCase): def test_read_serializer_class_not_provided(self): @@ -103,33 +123,6 @@ def setUp(self): self.create_in_serializer_class = OrderCreateSerializer self.create_out_serializer_class = OrderListSerializer - # def test_get_serializer_class(self): - # # GET request - # response = self.auth_client.get(self.view_url, format="json") - # view = response.renderer_context["view"] - # self.assertEqual(view.get_serializer_class(), OrderListSerializer) - - # # POST request - # response = self.auth_client.post( - # self.view_url, - # { - # "table_number": 100, - # "ordered_meals": [ - # { - # "quantity": 1, - # "meal": self.meals[0].id, - # }, - # { - # "quantity": 2, - # "meal": self.meals[1].id, - # }, - # ], - # }, - # format="json", - # ) - # view = response.renderer_context["view"] - # self.assertEqual(view.get_serializer_class(), OrderCreateSerializer) - class OrderRetrieveUpdateDestroyEndpointTests( BaseTestCase, TestRetrieveRequestSuccess, TestUpdateRequestSuccess