mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-18 20:41:04 +03:00
Fix tests to work on all supported configurations.
This commit is contained in:
parent
d1ce9d3914
commit
e362344fbd
|
@ -41,8 +41,7 @@ class TestModelRead(TestModelsTestCase):
|
||||||
mixin = ReadModelMixin()
|
mixin = ReadModelMixin()
|
||||||
mixin.resource = GroupResource
|
mixin.resource = GroupResource
|
||||||
|
|
||||||
with self.assertRaises(ErrorResponse):
|
self.assertRaises(ErrorResponse, mixin.get, request, 12345)
|
||||||
response = mixin.get(request, 12345)
|
|
||||||
|
|
||||||
|
|
||||||
class TestModelCreation(TestModelsTestCase):
|
class TestModelCreation(TestModelsTestCase):
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from djangorestframework.compat import RequestFactory
|
from djangorestframework.resources import FormResource, ModelResource
|
||||||
from djangorestframework.resources import Resource, FormResource, ModelResource
|
|
||||||
from djangorestframework.response import ErrorResponse
|
from djangorestframework.response import ErrorResponse
|
||||||
from djangorestframework.views import View
|
from djangorestframework.views import View
|
||||||
from djangorestframework.resources import Resource
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestDisabledValidations(TestCase):
|
class TestDisabledValidations(TestCase):
|
||||||
|
@ -38,7 +35,6 @@ class TestDisabledValidations(TestCase):
|
||||||
content = {'qwerty': 'uiop'}
|
content = {'qwerty': 'uiop'}
|
||||||
self.assertEqual(FormResource(view).get_bound_form(content), None)
|
self.assertEqual(FormResource(view).get_bound_form(content), None)
|
||||||
|
|
||||||
|
|
||||||
def test_disabled_model_form_validator_returns_content_unchanged(self):
|
def test_disabled_model_form_validator_returns_content_unchanged(self):
|
||||||
"""If the view's form is None and does not have a Resource with a model set then
|
"""If the view's form is None and does not have a Resource with a model set then
|
||||||
ModelFormValidator(view).validate_request(content, None) should just return the content unmodified."""
|
ModelFormValidator(view).validate_request(content, None) should just return the content unmodified."""
|
||||||
|
@ -48,7 +44,7 @@ class TestDisabledValidations(TestCase):
|
||||||
|
|
||||||
view = DisabledModelFormView()
|
view = DisabledModelFormView()
|
||||||
content = {'qwerty': 'uiop'}
|
content = {'qwerty': 'uiop'}
|
||||||
self.assertEqual(ModelResource(view).get_bound_form(content), None)#
|
self.assertEqual(ModelResource(view).get_bound_form(content), None)
|
||||||
|
|
||||||
def test_disabled_model_form_validator_get_bound_form_returns_none(self):
|
def test_disabled_model_form_validator_get_bound_form_returns_none(self):
|
||||||
"""If the form attribute is None on FormValidatorMixin then get_bound_form(content) should just return None."""
|
"""If the form attribute is None on FormValidatorMixin then get_bound_form(content) should just return None."""
|
||||||
|
@ -59,6 +55,7 @@ class TestDisabledValidations(TestCase):
|
||||||
content = {'qwerty': 'uiop'}
|
content = {'qwerty': 'uiop'}
|
||||||
self.assertEqual(ModelResource(view).get_bound_form(content), None)
|
self.assertEqual(ModelResource(view).get_bound_form(content), None)
|
||||||
|
|
||||||
|
|
||||||
class TestNonFieldErrors(TestCase):
|
class TestNonFieldErrors(TestCase):
|
||||||
"""Tests against form validation errors caused by non-field errors. (eg as might be caused by some custom form validation)"""
|
"""Tests against form validation errors caused by non-field errors. (eg as might be caused by some custom form validation)"""
|
||||||
|
|
||||||
|
@ -72,7 +69,7 @@ class TestNonFieldErrors(TestCase):
|
||||||
def clean(self):
|
def clean(self):
|
||||||
if 'field1' in self.cleaned_data and 'field2' in self.cleaned_data:
|
if 'field1' in self.cleaned_data and 'field2' in self.cleaned_data:
|
||||||
raise forms.ValidationError(self.ERROR_TEXT)
|
raise forms.ValidationError(self.ERROR_TEXT)
|
||||||
return self.cleaned_data #pragma: no cover
|
return self.cleaned_data
|
||||||
|
|
||||||
class MockResource(FormResource):
|
class MockResource(FormResource):
|
||||||
form = MockForm
|
form = MockForm
|
||||||
|
@ -87,7 +84,7 @@ class TestNonFieldErrors(TestCase):
|
||||||
except ErrorResponse, exc:
|
except ErrorResponse, exc:
|
||||||
self.assertEqual(exc.response.raw_content, {'errors': [MockForm.ERROR_TEXT]})
|
self.assertEqual(exc.response.raw_content, {'errors': [MockForm.ERROR_TEXT]})
|
||||||
else:
|
else:
|
||||||
self.fail('ErrorResponse was not raised') #pragma: no cover
|
self.fail('ErrorResponse was not raised')
|
||||||
|
|
||||||
|
|
||||||
class TestFormValidation(TestCase):
|
class TestFormValidation(TestCase):
|
||||||
|
@ -115,7 +112,6 @@ class TestFormValidation(TestCase):
|
||||||
self.MockFormView = MockFormView
|
self.MockFormView = MockFormView
|
||||||
self.MockModelFormView = MockModelFormView
|
self.MockModelFormView = MockModelFormView
|
||||||
|
|
||||||
|
|
||||||
def validation_returns_content_unchanged_if_already_valid_and_clean(self, validator):
|
def validation_returns_content_unchanged_if_already_valid_and_clean(self, validator):
|
||||||
"""If the content is already valid and clean then validate(content) should just return the content unmodified."""
|
"""If the content is already valid and clean then validate(content) should just return the content unmodified."""
|
||||||
content = {'qwerty': 'uiop'}
|
content = {'qwerty': 'uiop'}
|
||||||
|
@ -143,7 +139,9 @@ class TestFormValidation(TestCase):
|
||||||
raise errors on unexpected request data"""
|
raise errors on unexpected request data"""
|
||||||
content = {'qwerty': 'uiop', 'extra': 'extra'}
|
content = {'qwerty': 'uiop', 'extra': 'extra'}
|
||||||
validator.allow_unknown_form_fields = True
|
validator.allow_unknown_form_fields = True
|
||||||
self.assertDictEqual({'qwerty': u'uiop'}, validator.validate_request(content, None), "Resource didn't accept unknown fields.")
|
self.assertEqual({'qwerty': u'uiop'},
|
||||||
|
validator.validate_request(content, None),
|
||||||
|
"Resource didn't accept unknown fields.")
|
||||||
validator.allow_unknown_form_fields = False
|
validator.allow_unknown_form_fields = False
|
||||||
|
|
||||||
def validation_does_not_require_extra_fields_if_explicitly_set(self, validator):
|
def validation_does_not_require_extra_fields_if_explicitly_set(self, validator):
|
||||||
|
@ -159,7 +157,7 @@ class TestFormValidation(TestCase):
|
||||||
except ErrorResponse, exc:
|
except ErrorResponse, exc:
|
||||||
self.assertEqual(exc.response.raw_content, {'field_errors': {'qwerty': ['This field is required.']}})
|
self.assertEqual(exc.response.raw_content, {'field_errors': {'qwerty': ['This field is required.']}})
|
||||||
else:
|
else:
|
||||||
self.fail('ResourceException was not raised') #pragma: no cover
|
self.fail('ResourceException was not raised')
|
||||||
|
|
||||||
def validation_failed_due_to_field_error_returns_appropriate_message(self, validator):
|
def validation_failed_due_to_field_error_returns_appropriate_message(self, validator):
|
||||||
"""If validation fails due to a field error, ensure the response contains a single field error"""
|
"""If validation fails due to a field error, ensure the response contains a single field error"""
|
||||||
|
@ -169,7 +167,7 @@ class TestFormValidation(TestCase):
|
||||||
except ErrorResponse, exc:
|
except ErrorResponse, exc:
|
||||||
self.assertEqual(exc.response.raw_content, {'field_errors': {'qwerty': ['This field is required.']}})
|
self.assertEqual(exc.response.raw_content, {'field_errors': {'qwerty': ['This field is required.']}})
|
||||||
else:
|
else:
|
||||||
self.fail('ResourceException was not raised') #pragma: no cover
|
self.fail('ResourceException was not raised')
|
||||||
|
|
||||||
def validation_failed_due_to_invalid_field_returns_appropriate_message(self, validator):
|
def validation_failed_due_to_invalid_field_returns_appropriate_message(self, validator):
|
||||||
"""If validation fails due to an invalid field, ensure the response contains a single field error"""
|
"""If validation fails due to an invalid field, ensure the response contains a single field error"""
|
||||||
|
@ -179,7 +177,7 @@ class TestFormValidation(TestCase):
|
||||||
except ErrorResponse, exc:
|
except ErrorResponse, exc:
|
||||||
self.assertEqual(exc.response.raw_content, {'field_errors': {'extra': ['This field does not exist.']}})
|
self.assertEqual(exc.response.raw_content, {'field_errors': {'extra': ['This field does not exist.']}})
|
||||||
else:
|
else:
|
||||||
self.fail('ResourceException was not raised') #pragma: no cover
|
self.fail('ResourceException was not raised')
|
||||||
|
|
||||||
def validation_failed_due_to_multiple_errors_returns_appropriate_message(self, validator):
|
def validation_failed_due_to_multiple_errors_returns_appropriate_message(self, validator):
|
||||||
"""If validation for multiple reasons, ensure the response contains each error"""
|
"""If validation for multiple reasons, ensure the response contains each error"""
|
||||||
|
@ -190,7 +188,7 @@ class TestFormValidation(TestCase):
|
||||||
self.assertEqual(exc.response.raw_content, {'field_errors': {'qwerty': ['This field is required.'],
|
self.assertEqual(exc.response.raw_content, {'field_errors': {'qwerty': ['This field is required.'],
|
||||||
'extra': ['This field does not exist.']}})
|
'extra': ['This field does not exist.']}})
|
||||||
else:
|
else:
|
||||||
self.fail('ResourceException was not raised') #pragma: no cover
|
self.fail('ResourceException was not raised')
|
||||||
|
|
||||||
# Tests on FormResource
|
# Tests on FormResource
|
||||||
|
|
||||||
|
@ -294,7 +292,6 @@ class TestModelFormValidator(TestCase):
|
||||||
|
|
||||||
self.validator = MockResource(MockView)
|
self.validator = MockResource(MockView)
|
||||||
|
|
||||||
|
|
||||||
def test_property_fields_are_allowed_on_model_forms(self):
|
def test_property_fields_are_allowed_on_model_forms(self):
|
||||||
"""Validation on ModelForms may include property fields that exist on the Model to be included in the input."""
|
"""Validation on ModelForms may include property fields that exist on the Model to be included in the input."""
|
||||||
content = {'qwerty': 'example', 'uiop': 'example', 'readonly': 'read only'}
|
content = {'qwerty': 'example', 'uiop': 'example', 'readonly': 'read only'}
|
||||||
|
@ -326,5 +323,3 @@ class TestModelFormValidator(TestCase):
|
||||||
|
|
||||||
def test_model_form_validator_uses_model_forms(self):
|
def test_model_form_validator_uses_model_forms(self):
|
||||||
self.assertTrue(isinstance(self.validator.get_bound_form(), forms.ModelForm))
|
self.assertTrue(isinstance(self.validator.get_bound_form(), forms.ModelForm))
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user