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?
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.
JSONParser. Parses JSON request content. request. data will be populated with a dictionary of data. .media_type: application/json.
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.
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.
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.
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.
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 = { ...
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With