Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework Camel Case - Testing not using parser

I have a simple REST API in Django using rest_framework. I added the djangorestframework-camel-case plugin and updated my REST_FRAMEWORK configuration and the REST API outputs proper camelCase. However, when I test using unittest (python manage.py test app.test), the results are in snake_case instead of camelCase and cause my assertions to fail.

Using this fork: https://github.com/rense/djangorestframework-camel-case

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.DjangoModelPermissions',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_FILTER_BACKENDS': ('rest_framework.filters.DjangoFilterBackend', 'rest_framework.filters.OrderingFilter'),
    'DEFAULT_RENDERER_CLASSES': ('djangorestframework_camel_case.render.CamelCaseJSONRenderer',),
    'DEFAULT_PARSER_CLASSES': ('djangorestframework_camel_case.parser.CamelCaseJSONParser',),
    'TEST_REQUEST_RENDERER_CLASSES': ('djangorestframework_camel_case.render.CamelCaseJSONRenderer',),
    'TEST_REQUEST_PARSER_CLASSES': ('djangorestframework_camel_case.parser.CamelCaseJSONParser',),
    'TEST_REQUEST_DEFAULT_FORMAT': 'json',
}

Do I need to add some additional configuration? Is this a bug in djangorestframework? In djangorestframework-camel-case?

like image 805
Denise Mauldin Avatar asked Mar 30 '17 17:03

Denise Mauldin


People also ask

How do I run test cases in Django REST Framework?

Once you have coded a couple of tests, you can execute them with this command in the main folder of the project (the one that has the manage.py file): python manage.py test This will search for all of your tests files under that folder and then execute them, showing the error or success of each unit test.

What is JSON parser in Django?

JSONParser. Parses JSON request content. request. data will be populated with a dictionary of data. .media_type: application/json.

What is renderers in Django REST Framework?

The rendering process takes the intermediate representation of template and context, and turns it into the final byte stream that can be served to the client. REST framework includes a number of built in Renderer classes, that allow you to return responses with various media types.

What is djangorestframework-camel case?

djangorestframework-camel-case provides camel case JSON renderers and parsers for REST framework. This allows serializers to use Python-style underscored field names, but be exposed in the API as Javascript-style camel case field names. It is maintained by Vitaly Babiy.

What is camel-case in Django?

djangorestframework-camel-case provides camel case JSON renderers and parsers for REST framework. This allows serializers to use Python-style underscored field names, but be exposed in the API as Javascript-style camel case field names.

Can I use REST framework as a test case class?

You can use any of REST framework's test case classes as you would for the regular Django test case classes. The self.client attribute will be an APIClient instance. REST framework also provides a test case class for isolating urlpatterns on a per-class basis.

Why is my test object snake_case and not camel case?

The problem might be in your test file. The response object will have a data parameter which will be snake_case, and a content which will be camelCase. Make sure you are running your assertions against the right response parameter. Show activity on this post. REST_FRAMEWORK = { ...


2 Answers

The problem might be in your test file.

Let's say you have in your tests something among the lines of:

client = APIClient() response = client.get('some_url', format='json')

The response object will have a data parameter which will be snake_case, and a content which will be camelCase.

response.data # will contain snake_case keys json.loads(response.content) # will contain camelCase keys

Make sure you are running your assertions against the right response parameter.

like image 66
alemangui Avatar answered Oct 17 '22 14:10

alemangui


I just learned you can also set this globally, which may be what you want:

REST_FRAMEWORK = {
    ...
    'TEST_REQUEST_DEFAULT_FORMAT': 'json'
}

as per docs: https://www.django-rest-framework.org/api-guide/testing/#configuration

like image 31
jordancooperman Avatar answered Oct 17 '22 14:10

jordancooperman