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
ef8092ba
Commit
ef8092ba
authored
Mar 28, 2017
by
Ekluv
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update test case
parent
56fe0e4b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
26 deletions
+28
-26
tests/test_model_serializer.py
+28
-0
tests/test_serializer.py
+0
-26
No files found.
tests/test_model_serializer.py
View file @
ef8092ba
...
...
@@ -99,6 +99,15 @@ class Issue3674ChildModel(models.Model):
value
=
models
.
CharField
(
primary_key
=
True
,
max_length
=
64
)
class
UniqueChoiceModel
(
models
.
Model
):
CHOICES
=
(
(
'choice1'
,
'choice 1'
),
(
'choice2'
,
'choice 1'
),
)
name
=
models
.
CharField
(
max_length
=
254
,
unique
=
True
,
choices
=
CHOICES
)
class
TestModelSerializer
(
TestCase
):
def
test_create_method
(
self
):
class
TestSerializer
(
serializers
.
ModelSerializer
):
...
...
@@ -1080,3 +1089,22 @@ class Issue4897TestCase(TestCase):
with
pytest
.
raises
(
AssertionError
)
as
cm
:
TestSerializer
(
obj
)
.
fields
cm
.
match
(
r'readonly_fields'
)
class
Test5004UniqueChoiceField
(
TestCase
):
def
test_unique_choice_field
(
self
):
class
TestUniqueChoiceSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
UniqueChoiceModel
fields
=
'__all__'
UniqueChoiceModel
.
objects
.
create
(
name
=
'choice1'
)
serializer
=
TestUniqueChoiceSerializer
(
data
=
{
'name'
:
'choice1'
})
expected
=
dedent
(
"""
TestUniqueChoiceSerializer():
id = IntegerField(label='ID', read_only=True)
name = ChoiceField(choices=(('choice1', 'choice 1'), ('choice2', 'choice 1')), validators=[<UniqueValidator(queryset=UniqueChoiceModel.objects.all())>])
"""
)
self
.
assertEqual
(
unicode_repr
(
TestUniqueChoiceSerializer
()),
expected
)
assert
not
serializer
.
is_valid
()
assert
serializer
.
errors
==
{
'name'
:
[
'unique choice model with this name already exists.'
]}
tests/test_serializer.py
View file @
ef8092ba
...
...
@@ -519,29 +519,3 @@ class TestDeclaredFieldInheritance:
assert
len
(
Parent
()
.
get_fields
())
==
2
assert
len
(
Child
()
.
get_fields
())
==
2
assert
len
(
Grandchild
()
.
get_fields
())
==
2
class
Poll
(
models
.
Model
):
CHOICES
=
(
(
'choice1'
,
'choice 1'
),
(
'choice2'
,
'choice 1'
),
)
name
=
models
.
CharField
(
'name'
,
max_length
=
254
,
unique
=
True
,
choices
=
CHOICES
)
@pytest.mark.django_db
class
Test5004UniqueChoiceField
:
def
test_unique_choice_field
(
self
):
Poll
.
objects
.
create
(
name
=
'choice1'
)
class
PollSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
Poll
fields
=
'__all__'
serializer
=
PollSerializer
(
data
=
{
'name'
:
'choice1'
})
assert
not
serializer
.
is_valid
()
assert
serializer
.
errors
==
{
'name'
:
[
'poll with this name already exists.'
]}
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