Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
e362344f
Commit
e362344f
authored
Jan 22, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix tests to work on all supported configurations.
parent
d1ce9d39
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
29 deletions
+23
-29
djangorestframework/tests/mixins.py
+1
-2
djangorestframework/tests/validators.py
+22
-27
No files found.
djangorestframework/tests/mixins.py
View file @
e362344f
...
...
@@ -41,8 +41,7 @@ class TestModelRead(TestModelsTestCase):
mixin
=
ReadModelMixin
()
mixin
.
resource
=
GroupResource
with
self
.
assertRaises
(
ErrorResponse
):
response
=
mixin
.
get
(
request
,
12345
)
self
.
assertRaises
(
ErrorResponse
,
mixin
.
get
,
request
,
12345
)
class
TestModelCreation
(
TestModelsTestCase
):
...
...
djangorestframework/tests/validators.py
View file @
e362344f
from
django
import
forms
from
django.db
import
models
from
django.test
import
TestCase
from
djangorestframework.compat
import
RequestFactory
from
djangorestframework.resources
import
Resource
,
FormResource
,
ModelResource
from
djangorestframework.resources
import
FormResource
,
ModelResource
from
djangorestframework.response
import
ErrorResponse
from
djangorestframework.views
import
View
from
djangorestframework.resources
import
Resource
class
TestDisabledValidations
(
TestCase
):
...
...
@@ -22,7 +19,7 @@ class TestDisabledValidations(TestCase):
resource
=
DisabledFormResource
view
=
MockView
()
content
=
{
'qwerty'
:
'uiop'
}
content
=
{
'qwerty'
:
'uiop'
}
self
.
assertEqual
(
FormResource
(
view
)
.
validate_request
(
content
,
None
),
content
)
def
test_disabled_form_validator_get_bound_form_returns_none
(
self
):
...
...
@@ -35,10 +32,9 @@ class TestDisabledValidations(TestCase):
resource
=
DisabledFormResource
view
=
MockView
()
content
=
{
'qwerty'
:
'uiop'
}
content
=
{
'qwerty'
:
'uiop'
}
self
.
assertEqual
(
FormResource
(
view
)
.
get_bound_form
(
content
),
None
)
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
ModelFormValidator(view).validate_request(content, None) should just return the content unmodified."""
...
...
@@ -47,8 +43,8 @@ class TestDisabledValidations(TestCase):
resource
=
ModelResource
view
=
DisabledModelFormView
()
content
=
{
'qwerty'
:
'uiop'
}
self
.
assertEqual
(
ModelResource
(
view
)
.
get_bound_form
(
content
),
None
)
#
content
=
{
'qwerty'
:
'uiop'
}
self
.
assertEqual
(
ModelResource
(
view
)
.
get_bound_form
(
content
),
None
)
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."""
...
...
@@ -56,9 +52,10 @@ class TestDisabledValidations(TestCase):
resource
=
ModelResource
view
=
DisabledModelFormView
()
content
=
{
'qwerty'
:
'uiop'
}
content
=
{
'qwerty'
:
'uiop'
}
self
.
assertEqual
(
ModelResource
(
view
)
.
get_bound_form
(
content
),
None
)
class
TestNonFieldErrors
(
TestCase
):
"""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
):
if
'field1'
in
self
.
cleaned_data
and
'field2'
in
self
.
cleaned_data
:
raise
forms
.
ValidationError
(
self
.
ERROR_TEXT
)
return
self
.
cleaned_data
#pragma: no cover
return
self
.
cleaned_data
class
MockResource
(
FormResource
):
form
=
MockForm
...
...
@@ -87,7 +84,7 @@ class TestNonFieldErrors(TestCase):
except
ErrorResponse
,
exc
:
self
.
assertEqual
(
exc
.
response
.
raw_content
,
{
'errors'
:
[
MockForm
.
ERROR_TEXT
]})
else
:
self
.
fail
(
'ErrorResponse was not raised'
)
#pragma: no cover
self
.
fail
(
'ErrorResponse was not raised'
)
class
TestFormValidation
(
TestCase
):
...
...
@@ -115,10 +112,9 @@ class TestFormValidation(TestCase):
self
.
MockFormView
=
MockFormView
self
.
MockModelFormView
=
MockModelFormView
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."""
content
=
{
'qwerty'
:
'uiop'
}
content
=
{
'qwerty'
:
'uiop'
}
self
.
assertEqual
(
validator
.
validate_request
(
content
,
None
),
content
)
def
validation_failure_raises_response_exception
(
self
,
validator
):
...
...
@@ -143,7 +139,9 @@ class TestFormValidation(TestCase):
raise errors on unexpected request data"""
content
=
{
'qwerty'
:
'uiop'
,
'extra'
:
'extra'
}
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
def
validation_does_not_require_extra_fields_if_explicitly_set
(
self
,
validator
):
...
...
@@ -159,7 +157,7 @@ class TestFormValidation(TestCase):
except
ErrorResponse
,
exc
:
self
.
assertEqual
(
exc
.
response
.
raw_content
,
{
'field_errors'
:
{
'qwerty'
:
[
'This field is required.'
]}})
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
):
"""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
:
self
.
assertEqual
(
exc
.
response
.
raw_content
,
{
'field_errors'
:
{
'qwerty'
:
[
'This field is required.'
]}})
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
):
"""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
:
self
.
assertEqual
(
exc
.
response
.
raw_content
,
{
'field_errors'
:
{
'extra'
:
[
'This field does not exist.'
]}})
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
):
"""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.'
],
'extra'
:
[
'This field does not exist.'
]}})
else
:
self
.
fail
(
'ResourceException was not raised'
)
#pragma: no cover
self
.
fail
(
'ResourceException was not raised'
)
# Tests on FormResource
...
...
@@ -209,7 +207,7 @@ class TestFormValidation(TestCase):
def
test_validation_allows_extra_fields_if_explicitly_set
(
self
):
validator
=
self
.
MockFormResource
(
self
.
MockFormView
())
self
.
validation_allows_extra_fields_if_explicitly_set
(
validator
)
def
test_validation_allows_unknown_fields_if_explicitly_allowed
(
self
):
validator
=
self
.
MockFormResource
(
self
.
MockFormView
())
self
.
validation_allows_unknown_fields_if_explicitly_allowed
(
validator
)
...
...
@@ -294,22 +292,21 @@ class TestModelFormValidator(TestCase):
self
.
validator
=
MockResource
(
MockView
)
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."""
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
)
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."""
content
=
{
'qwerty'
:
'example'
,
'uiop'
:
'example'
}
content
=
{
'qwerty'
:
'example'
,
'uiop'
:
'example'
}
self
.
assertEqual
(
self
.
validator
.
validate_request
(
content
,
None
),
content
)
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.
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)"""
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
)
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
):
"""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
)
def
test_model_form_validator_uses_model_forms
(
self
):
self
.
assertTrue
(
isinstance
(
self
.
validator
.
get_bound_form
(),
forms
.
ModelForm
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment