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
6b6f0e68
Commit
6b6f0e68
authored
Oct 29, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #331 from markotibold/test_put_as_create
Test put as create
parents
ee8ab283
1a16289e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
2 deletions
+43
-2
rest_framework/tests/generics.py
+38
-2
rest_framework/tests/models.py
+5
-0
No files found.
rest_framework/tests/generics.py
View file @
6b6f0e68
...
...
@@ -2,7 +2,7 @@ from django.test import TestCase
from
django.test.client
import
RequestFactory
from
django.utils
import
simplejson
as
json
from
rest_framework
import
generics
,
serializers
,
status
from
rest_framework.tests.models
import
BasicModel
,
Comment
from
rest_framework.tests.models
import
BasicModel
,
Comment
,
SlugBasedModel
factory
=
RequestFactory
()
...
...
@@ -22,6 +22,13 @@ class InstanceView(generics.RetrieveUpdateDestroyAPIView):
model
=
BasicModel
class
SlugBasedInstanceView
(
InstanceView
):
"""
A model with a slug-field.
"""
model
=
SlugBasedModel
class
TestRootView
(
TestCase
):
def
setUp
(
self
):
"""
...
...
@@ -129,6 +136,7 @@ class TestInstanceView(TestCase):
for
obj
in
self
.
objects
.
all
()
]
self
.
view
=
InstanceView
.
as_view
()
self
.
slug_based_view
=
SlugBasedInstanceView
.
as_view
()
def
test_get_instance_view
(
self
):
"""
...
...
@@ -198,7 +206,7 @@ class TestInstanceView(TestCase):
def
test_put_cannot_set_id
(
self
):
"""
P
OS
T requests to create a new object should not be able to set the id.
P
U
T requests to create a new object should not be able to set the id.
"""
content
=
{
'id'
:
999
,
'text'
:
'foobar'
}
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
...
...
@@ -224,6 +232,34 @@ class TestInstanceView(TestCase):
updated
=
self
.
objects
.
get
(
id
=
1
)
self
.
assertEquals
(
updated
.
text
,
'foobar'
)
def
test_put_as_create_on_id_based_url
(
self
):
"""
PUT requests to RetrieveUpdateDestroyAPIView should create an object
at the requested url if it doesn't exist.
"""
content
=
{
'text'
:
'foobar'
}
# pk fields can not be created on demand, only the database can set th pk for a new object
request
=
factory
.
put
(
'/5'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
response
=
self
.
view
(
request
,
pk
=
5
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
new_obj
=
self
.
objects
.
get
(
pk
=
5
)
self
.
assertEquals
(
new_obj
.
text
,
'foobar'
)
def
test_put_as_create_on_slug_based_url
(
self
):
"""
PUT requests to RetrieveUpdateDestroyAPIView should create an object
at the requested url if possible, else return HTTP_403_FORBIDDEN error-response.
"""
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
put
(
'/test_slug'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
response
=
self
.
slug_based_view
(
request
,
pk
=
'test_slug'
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEquals
(
response
.
data
,
{
'slug'
:
'test_slug'
,
'text'
:
'foobar'
})
new_obj
=
self
.
objects
.
get
(
slug
=
'test_slug'
)
self
.
assertEquals
(
new_obj
.
text
,
'foobar'
)
# Regression test for #285
...
...
rest_framework/tests/models.py
View file @
6b6f0e68
...
...
@@ -52,6 +52,11 @@ class BasicModel(RESTFrameworkModel):
text
=
models
.
CharField
(
max_length
=
100
)
class
SlugBasedModel
(
RESTFrameworkModel
):
text
=
models
.
CharField
(
max_length
=
100
)
slug
=
models
.
SlugField
(
max_length
=
32
)
class
DefaultValueModel
(
RESTFrameworkModel
):
text
=
models
.
CharField
(
default
=
'foobar'
,
max_length
=
100
)
...
...
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