Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pamella committed May 23, 2024
1 parent f2e10c6 commit 0affe77
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
7 changes: 4 additions & 3 deletions drf_rw_serializers/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 3 additions & 1 deletion test_utils/base_tests.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
49 changes: 21 additions & 28 deletions tests/test_generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,34 @@ 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
write_serializer_class = OrderCreateSerializer

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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0affe77

Please sign in to comment.