Fix tests to work on all supported configurations.

This commit is contained in:
Tom Christie 2012-01-22 20:19:34 +00:00
parent d1ce9d3914
commit e362344fbd
2 changed files with 23 additions and 29 deletions

View File

@ -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):

View File

@ -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):
@ -22,7 +19,7 @@ class TestDisabledValidations(TestCase):
resource = DisabledFormResource resource = DisabledFormResource
view = MockView() view = MockView()
content = {'qwerty':'uiop'} content = {'qwerty': 'uiop'}
self.assertEqual(FormResource(view).validate_request(content, None), content) self.assertEqual(FormResource(view).validate_request(content, None), content)
def test_disabled_form_validator_get_bound_form_returns_none(self): def test_disabled_form_validator_get_bound_form_returns_none(self):
@ -35,10 +32,9 @@ class TestDisabledValidations(TestCase):
resource = DisabledFormResource resource = DisabledFormResource
view = MockView() view = MockView()
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."""
@ -47,8 +43,8 @@ class TestDisabledValidations(TestCase):
resource = ModelResource resource = ModelResource
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."""
@ -56,9 +52,10 @@ class TestDisabledValidations(TestCase):
resource = ModelResource resource = ModelResource
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)
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,10 +112,9 @@ 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'}
self.assertEqual(validator.validate_request(content, None), content) self.assertEqual(validator.validate_request(content, None), content)
def validation_failure_raises_response_exception(self, validator): def validation_failure_raises_response_exception(self, validator):
@ -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
@ -209,7 +207,7 @@ class TestFormValidation(TestCase):
def test_validation_allows_extra_fields_if_explicitly_set(self): def test_validation_allows_extra_fields_if_explicitly_set(self):
validator = self.MockFormResource(self.MockFormView()) validator = self.MockFormResource(self.MockFormView())
self.validation_allows_extra_fields_if_explicitly_set(validator) self.validation_allows_extra_fields_if_explicitly_set(validator)
def test_validation_allows_unknown_fields_if_explicitly_allowed(self): def test_validation_allows_unknown_fields_if_explicitly_allowed(self):
validator = self.MockFormResource(self.MockFormView()) validator = self.MockFormResource(self.MockFormView())
self.validation_allows_unknown_fields_if_explicitly_allowed(validator) self.validation_allows_unknown_fields_if_explicitly_allowed(validator)
@ -294,22 +292,21 @@ 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'}
self.assertEqual(self.validator.validate_request(content, None), content) self.assertEqual(self.validator.validate_request(content, None), content)
def test_property_fields_are_not_required_on_model_forms(self): def test_property_fields_are_not_required_on_model_forms(self):
"""Validation on ModelForms does not require property fields that exist on the Model to be included in the input.""" """Validation on ModelForms does not require property fields that exist on the Model to be included in the input."""
content = {'qwerty':'example', 'uiop': 'example'} content = {'qwerty': 'example', 'uiop': 'example'}
self.assertEqual(self.validator.validate_request(content, None), content) self.assertEqual(self.validator.validate_request(content, None), content)
def test_extra_fields_not_allowed_on_model_forms(self): def test_extra_fields_not_allowed_on_model_forms(self):
"""If some (otherwise valid) content includes fields that are not in the form then validation should fail. """If some (otherwise valid) content includes fields that are not in the form then validation should fail.
It might be okay on normal form submission, but for Web APIs we oughta get strict, as it'll help show up It might be okay on normal form submission, but for Web APIs we oughta get strict, as it'll help show up
broken clients more easily (eg submitting content with a misnamed field)""" broken clients more easily (eg submitting content with a misnamed field)"""
content = {'qwerty': 'example', 'uiop':'example', 'readonly': 'read only', 'extra': 'extra'} content = {'qwerty': 'example', 'uiop': 'example', 'readonly': 'read only', 'extra': 'extra'}
self.assertRaises(ErrorResponse, self.validator.validate_request, content, None) self.assertRaises(ErrorResponse, self.validator.validate_request, content, None)
def test_validate_requires_fields_on_model_forms(self): def test_validate_requires_fields_on_model_forms(self):
@ -321,10 +318,8 @@ class TestModelFormValidator(TestCase):
def test_validate_does_not_require_blankable_fields_on_model_forms(self): def test_validate_does_not_require_blankable_fields_on_model_forms(self):
"""Test standard ModelForm validation behaviour - fields with blank=True are not required.""" """Test standard ModelForm validation behaviour - fields with blank=True are not required."""
content = {'qwerty':'example', 'readonly': 'read only'} content = {'qwerty': 'example', 'readonly': 'read only'}
self.validator.validate_request(content, None) self.validator.validate_request(content, None)
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))