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
bbd44ae9
Commit
bbd44ae9
authored
Jul 25, 2015
by
Ion Scerbatiuc
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated the test cases based on the CR comments
parent
085c3e8a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
55 deletions
+26
-55
tests/test_serializer_nested.py
+26
-55
No files found.
tests/test_serializer_nested.py
View file @
bbd44ae9
...
@@ -73,84 +73,55 @@ class TestNotRequiredNestedSerializer:
...
@@ -73,84 +73,55 @@ class TestNotRequiredNestedSerializer:
class
TestNestedSerializerWithMany
:
class
TestNestedSerializerWithMany
:
def
setup
(
self
):
def
setup
(
self
):
class
PaymentInfoSerializer
(
serializers
.
Serializer
):
url
=
serializers
.
URLField
()
amount
=
serializers
.
DecimalField
(
max_digits
=
6
,
decimal_places
=
2
)
class
NestedSerializer
(
serializers
.
Serializer
):
class
NestedSerializer
(
serializers
.
Serializer
):
foo
=
serializers
.
CharField
()
example
=
serializers
.
IntegerField
(
max_value
=
10
)
class
AcceptRequestSerializer
(
serializers
.
Serializer
):
ERROR_MESSAGE
=
'`payment_info` is required under this condition'
nested
=
NestedSerializer
(
many
=
True
)
payment_info
=
PaymentInfoSerializer
(
many
=
True
,
allow_null
=
True
)
def
validate_payment_info
(
self
,
value
):
class
TestSerializer
(
serializers
.
Serializer
):
if
value
is
None
and
not
self
.
context
[
'condition_to_allow_null'
]:
allow_null
=
NestedSerializer
(
many
=
True
,
allow_null
=
True
)
raise
serializers
.
ValidationError
(
self
.
ERROR_MESSAGE
)
not_allow_null
=
NestedSerializer
(
many
=
True
)
return
value
self
.
Serializer
=
AcceptRequestSerializer
def
get_serializer
(
self
,
data
,
condition_to_allow_null
=
True
):
self
.
Serializer
=
TestSerializer
return
self
.
Serializer
(
data
=
data
,
context
=
{
'condition_to_allow_null'
:
condition_to_allow_null
})
def
test_null_allowed_if_allow_null_is_set
(
self
):
def
test_null_allowed_if_allow_null_is_set
(
self
):
input_data
=
{
input_data
=
{
'
nested'
:
[{
'foo'
:
'bar'
}]
,
'
allow_null'
:
None
,
'
payment_info'
:
None
'
not_allow_null'
:
[{
'example'
:
'2'
},
{
'example'
:
'3'
}]
}
}
expected_data
=
{
expected_data
=
{
'
nested'
:
[{
'foo'
:
'bar'
}]
,
'
allow_null'
:
None
,
'
payment_info'
:
None
'
not_allow_null'
:
[{
'example'
:
2
},
{
'example'
:
3
}]
}
}
serializer
=
self
.
get_serializer
(
input_data
)
serializer
=
self
.
Serializer
(
data
=
input_data
)
assert
serializer
.
is_valid
(),
serializer
.
errors
assert
serializer
.
is_valid
(),
serializer
.
errors
assert
serializer
.
validated_data
==
expected_data
assert
serializer
.
validated_data
==
expected_data
def
test_null_is_not_allowed_if_allow_null_is_not_set
(
self
):
def
test_null_is_not_allowed_if_allow_null_is_not_set
(
self
):
input_data
=
{
input_data
=
{
'
nested
'
:
None
,
'
allow_null
'
:
None
,
'
payment_info
'
:
None
'
not_allow_null
'
:
None
}
}
serializer
=
self
.
get_serializer
(
input_data
)
serializer
=
self
.
Serializer
(
data
=
input_data
)
assert
not
serializer
.
is_valid
()
assert
not
serializer
.
is_valid
()
assert
set
(
serializer
.
errors
)
==
set
([
'nested'
])
assert
serializer
.
errors
[
'nested'
][
0
]
==
serializer
.
error_messages
[
'null'
]
expected_errors
=
{
'not_allow_null'
:
[
serializer
.
error_messages
[
'null'
]]}
assert
serializer
.
errors
==
expected_errors
def
test_run_the_field_validation_even_if_the_field_is_null
(
self
):
def
test_run_the_field_validation_even_if_the_field_is_null
(
self
):
input_data
=
{
class
TestSerializer
(
self
.
Serializer
):
'nested'
:
[{
'foo'
:
'bar'
}],
validation_was_run
=
False
'payment_info'
:
None
,
}
serializer
=
self
.
get_serializer
(
input_data
,
condition_to_allow_null
=
False
)
assert
not
serializer
.
is_valid
()
def
validate_allow_null
(
self
,
value
):
assert
set
(
serializer
.
errors
)
==
set
([
'payment_info'
])
TestSerializer
.
validation_was_run
=
True
assert
serializer
.
errors
[
'payment_info'
][
0
]
==
serializer
.
ERROR_MESSAGE
return
value
def
test_expected_results_if_not_null
(
self
):
input_data
=
{
input_data
=
{
'nested'
:
[{
'foo'
:
'bar'
}],
'allow_null'
:
None
,
'payment_info'
:
[
'not_allow_null'
:
[{
'example'
:
2
}]
{
'url'
:
'https://domain.org/api/payment-method/1/'
,
'amount'
:
'22'
},
{
'url'
:
'https://domain.org/api/payment-method/2/'
,
'amount'
:
'33'
},
]
}
expected_data
=
{
'nested'
:
[{
'foo'
:
'bar'
}],
'payment_info'
:
[
{
'url'
:
'https://domain.org/api/payment-method/1/'
,
'amount'
:
22
},
{
'url'
:
'https://domain.org/api/payment-method/2/'
,
'amount'
:
33
},
]
}
}
serializer
=
self
.
get_serializer
(
input_data
)
serializer
=
TestSerializer
(
data
=
input_data
)
assert
serializer
.
is_valid
()
assert
serializer
.
is_valid
()
assert
serializer
.
validated_data
==
expected_data
assert
serializer
.
validated_data
==
input_data
assert
TestSerializer
.
validation_was_run
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