-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This introduces the first auto test case for the retrieve API. The implementor provides the actual object under test, and a tuple of (username, password, expected_error). The expected error is what that user should get from the API. If it's `None`, that means the request should be accepted. If the request is accepted, we then use the serializer to figure out what fields the object is going to have. Most of the time, it's pretty straightforward, but sometimes (like for decimal fields, or relational fields), we have to do some transformations. This will (hopefully) make it much easier to add test cases to the API. Or, at the very least, it'll allow us to clean up our fugly existing test cases. Task: #475
- Loading branch information
Showing
2 changed files
with
85 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright (c) 2011-2016 Berkeley Model United Nations. All rights reserved. | ||
# Use of this source code is governed by a BSD License (see LICENSE). | ||
|
||
from django.db import models | ||
from rest_framework import serializers | ||
|
||
from huxley.api.tests import RetrieveAPITestCase | ||
|
||
|
||
class RetrieveAPIAutoTestCase(RetrieveAPITestCase): | ||
NOT_AUTHENTICATED = 'not_authenticated' | ||
|
||
@classmethod | ||
def get_test_object(cls): | ||
raise NotImplementedError('You must provide a test object to retrieve.') | ||
|
||
@classmethod | ||
def get_users(cls, obj): | ||
raise NotImplementedError('You must provide test users.') | ||
|
||
@classmethod | ||
def setUpTestData(cls): | ||
cls.object = cls.get_test_object() | ||
cls.users = cls.get_users(cls.object) | ||
|
||
def test(self): | ||
for user_data in self.users: | ||
username, password, expected_error = user_data | ||
if username and password: | ||
self.client.login(username=username, password=password) | ||
response = self.get_response(self.object.id) | ||
|
||
if expected_error == self.NOT_AUTHENTICATED: | ||
self.assertNotAuthenticated(response) | ||
else: | ||
self.assert_response(response) | ||
|
||
def assert_response(self, response): | ||
serializer = self.view.serializer_class | ||
expected_response = get_expected_response( | ||
serializer, | ||
serializer.Meta.model, | ||
self.object, | ||
) | ||
self.assertEqual(response.data, expected_response) | ||
|
||
|
||
def get_expected_response(serializer, model, test_object): | ||
serializer_fields = serializer._declared_fields | ||
expected = {} | ||
for field_name in serializer.Meta.fields: | ||
field = model._meta.get_field(field_name) | ||
attr = getattr(test_object, field_name) | ||
|
||
if isinstance(field, models.DecimalField): | ||
attr = float(attr) | ||
|
||
serializer_field = serializer_fields.get(field_name, None) | ||
if serializer_field: | ||
if isinstance(serializer_field, serializers.DateTimeField): | ||
attr = attr.isoformat() | ||
elif isinstance(serializer_field, serializers.ListField): | ||
attr = list(getattr(test_object, serializer_field.source)) | ||
elif isinstance(serializer_field, serializers.ManyRelatedField): | ||
attr = list(attr.all()) | ||
|
||
expected[field_name] = attr | ||
|
||
return expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters