Django Rest Framework Camel Case - testing without using a parser
I have a simple REST API in Django using rest_framework. I added the djangorestframework-camel-case plugin and updated the REST_FRAMEWORK config and the REST API outputs the correct camelCase. However, when I test with unittest ( python manage.py test app.test
), the results show up 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 additional configuration? Is this a bug in djangorestframework? In djangorestframework-camel-case?
source to share
The problem might be in your test file.
Let's say you have some of the lines in your tests:
client = APIClient()
response = client.get('some_url', format='json')
The response object will have a parameter data
which will be snake_case, and content
which will be camelCase.
response.data # will contain snake_case keys
json.loads(response.content) # will contain camelCase keys
Make sure you execute your assertions against the correct answer parameter.
source to share