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
126cadf2
Commit
126cadf2
authored
Mar 13, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'num-query-checking' of
https://github.com/mjtamlyn/django-rest-framework
parents
1aecd71e
332c9974
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
17 deletions
+33
-17
rest_framework/tests/generics.py
+33
-17
No files found.
rest_framework/tests/generics.py
View file @
126cadf2
...
@@ -60,7 +60,8 @@ class TestRootView(TestCase):
...
@@ -60,7 +60,8 @@ class TestRootView(TestCase):
GET requests to ListCreateAPIView should return list of objects.
GET requests to ListCreateAPIView should return list of objects.
"""
"""
request
=
factory
.
get
(
'/'
)
request
=
factory
.
get
(
'/'
)
response
=
self
.
view
(
request
)
.
render
()
with
self
.
assertNumQueries
(
1
):
response
=
self
.
view
(
request
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
data
,
self
.
data
)
self
.
assertEqual
(
response
.
data
,
self
.
data
)
...
@@ -71,7 +72,8 @@ class TestRootView(TestCase):
...
@@ -71,7 +72,8 @@ class TestRootView(TestCase):
content
=
{
'text'
:
'foobar'
}
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
content_type
=
'application/json'
)
response
=
self
.
view
(
request
)
.
render
()
with
self
.
assertNumQueries
(
1
):
response
=
self
.
view
(
request
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEqual
(
response
.
data
,
{
'id'
:
4
,
'text'
:
'foobar'
})
self
.
assertEqual
(
response
.
data
,
{
'id'
:
4
,
'text'
:
'foobar'
})
created
=
self
.
objects
.
get
(
id
=
4
)
created
=
self
.
objects
.
get
(
id
=
4
)
...
@@ -84,7 +86,8 @@ class TestRootView(TestCase):
...
@@ -84,7 +86,8 @@ class TestRootView(TestCase):
content
=
{
'text'
:
'foobar'
}
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
put
(
'/'
,
json
.
dumps
(
content
),
request
=
factory
.
put
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
content_type
=
'application/json'
)
response
=
self
.
view
(
request
)
.
render
()
with
self
.
assertNumQueries
(
0
):
response
=
self
.
view
(
request
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_405_METHOD_NOT_ALLOWED
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_405_METHOD_NOT_ALLOWED
)
self
.
assertEqual
(
response
.
data
,
{
"detail"
:
"Method 'PUT' not allowed."
})
self
.
assertEqual
(
response
.
data
,
{
"detail"
:
"Method 'PUT' not allowed."
})
...
@@ -93,7 +96,8 @@ class TestRootView(TestCase):
...
@@ -93,7 +96,8 @@ class TestRootView(TestCase):
DELETE requests to ListCreateAPIView should not be allowed
DELETE requests to ListCreateAPIView should not be allowed
"""
"""
request
=
factory
.
delete
(
'/'
)
request
=
factory
.
delete
(
'/'
)
response
=
self
.
view
(
request
)
.
render
()
with
self
.
assertNumQueries
(
0
):
response
=
self
.
view
(
request
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_405_METHOD_NOT_ALLOWED
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_405_METHOD_NOT_ALLOWED
)
self
.
assertEqual
(
response
.
data
,
{
"detail"
:
"Method 'DELETE' not allowed."
})
self
.
assertEqual
(
response
.
data
,
{
"detail"
:
"Method 'DELETE' not allowed."
})
...
@@ -102,7 +106,8 @@ class TestRootView(TestCase):
...
@@ -102,7 +106,8 @@ class TestRootView(TestCase):
OPTIONS requests to ListCreateAPIView should return metadata
OPTIONS requests to ListCreateAPIView should return metadata
"""
"""
request
=
factory
.
options
(
'/'
)
request
=
factory
.
options
(
'/'
)
response
=
self
.
view
(
request
)
.
render
()
with
self
.
assertNumQueries
(
0
):
response
=
self
.
view
(
request
)
.
render
()
expected
=
{
expected
=
{
'parses'
:
[
'parses'
:
[
'application/json'
,
'application/json'
,
...
@@ -126,7 +131,8 @@ class TestRootView(TestCase):
...
@@ -126,7 +131,8 @@ class TestRootView(TestCase):
content
=
{
'id'
:
999
,
'text'
:
'foobar'
}
content
=
{
'id'
:
999
,
'text'
:
'foobar'
}
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
content_type
=
'application/json'
)
response
=
self
.
view
(
request
)
.
render
()
with
self
.
assertNumQueries
(
1
):
response
=
self
.
view
(
request
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEqual
(
response
.
data
,
{
'id'
:
4
,
'text'
:
'foobar'
})
self
.
assertEqual
(
response
.
data
,
{
'id'
:
4
,
'text'
:
'foobar'
})
created
=
self
.
objects
.
get
(
id
=
4
)
created
=
self
.
objects
.
get
(
id
=
4
)
...
@@ -154,7 +160,8 @@ class TestInstanceView(TestCase):
...
@@ -154,7 +160,8 @@ class TestInstanceView(TestCase):
GET requests to RetrieveUpdateDestroyAPIView should return a single object.
GET requests to RetrieveUpdateDestroyAPIView should return a single object.
"""
"""
request
=
factory
.
get
(
'/1'
)
request
=
factory
.
get
(
'/1'
)
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
with
self
.
assertNumQueries
(
1
):
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
data
,
self
.
data
[
0
])
self
.
assertEqual
(
response
.
data
,
self
.
data
[
0
])
...
@@ -165,7 +172,8 @@ class TestInstanceView(TestCase):
...
@@ -165,7 +172,8 @@ class TestInstanceView(TestCase):
content
=
{
'text'
:
'foobar'
}
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
request
=
factory
.
post
(
'/'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
content_type
=
'application/json'
)
response
=
self
.
view
(
request
)
.
render
()
with
self
.
assertNumQueries
(
0
):
response
=
self
.
view
(
request
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_405_METHOD_NOT_ALLOWED
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_405_METHOD_NOT_ALLOWED
)
self
.
assertEqual
(
response
.
data
,
{
"detail"
:
"Method 'POST' not allowed."
})
self
.
assertEqual
(
response
.
data
,
{
"detail"
:
"Method 'POST' not allowed."
})
...
@@ -176,7 +184,8 @@ class TestInstanceView(TestCase):
...
@@ -176,7 +184,8 @@ class TestInstanceView(TestCase):
content
=
{
'text'
:
'foobar'
}
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
content_type
=
'application/json'
)
response
=
self
.
view
(
request
,
pk
=
'1'
)
.
render
()
with
self
.
assertNumQueries
(
3
):
response
=
self
.
view
(
request
,
pk
=
'1'
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
self
.
assertEqual
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
updated
=
self
.
objects
.
get
(
id
=
1
)
updated
=
self
.
objects
.
get
(
id
=
1
)
...
@@ -190,7 +199,8 @@ class TestInstanceView(TestCase):
...
@@ -190,7 +199,8 @@ class TestInstanceView(TestCase):
request
=
factory
.
patch
(
'/1'
,
json
.
dumps
(
content
),
request
=
factory
.
patch
(
'/1'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
content_type
=
'application/json'
)
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
with
self
.
assertNumQueries
(
3
):
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
self
.
assertEqual
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
updated
=
self
.
objects
.
get
(
id
=
1
)
updated
=
self
.
objects
.
get
(
id
=
1
)
...
@@ -201,7 +211,8 @@ class TestInstanceView(TestCase):
...
@@ -201,7 +211,8 @@ class TestInstanceView(TestCase):
DELETE requests to RetrieveUpdateDestroyAPIView should delete an object.
DELETE requests to RetrieveUpdateDestroyAPIView should delete an object.
"""
"""
request
=
factory
.
delete
(
'/1'
)
request
=
factory
.
delete
(
'/1'
)
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
with
self
.
assertNumQueries
(
2
):
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_204_NO_CONTENT
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_204_NO_CONTENT
)
self
.
assertEqual
(
response
.
content
,
six
.
b
(
''
))
self
.
assertEqual
(
response
.
content
,
six
.
b
(
''
))
ids
=
[
obj
.
id
for
obj
in
self
.
objects
.
all
()]
ids
=
[
obj
.
id
for
obj
in
self
.
objects
.
all
()]
...
@@ -212,7 +223,8 @@ class TestInstanceView(TestCase):
...
@@ -212,7 +223,8 @@ class TestInstanceView(TestCase):
OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata
OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata
"""
"""
request
=
factory
.
options
(
'/'
)
request
=
factory
.
options
(
'/'
)
response
=
self
.
view
(
request
)
.
render
()
with
self
.
assertNumQueries
(
0
):
response
=
self
.
view
(
request
)
.
render
()
expected
=
{
expected
=
{
'parses'
:
[
'parses'
:
[
'application/json'
,
'application/json'
,
...
@@ -236,7 +248,8 @@ class TestInstanceView(TestCase):
...
@@ -236,7 +248,8 @@ class TestInstanceView(TestCase):
content
=
{
'id'
:
999
,
'text'
:
'foobar'
}
content
=
{
'id'
:
999
,
'text'
:
'foobar'
}
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
content_type
=
'application/json'
)
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
with
self
.
assertNumQueries
(
3
):
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEqual
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
self
.
assertEqual
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
updated
=
self
.
objects
.
get
(
id
=
1
)
updated
=
self
.
objects
.
get
(
id
=
1
)
...
@@ -251,7 +264,8 @@ class TestInstanceView(TestCase):
...
@@ -251,7 +264,8 @@ class TestInstanceView(TestCase):
content
=
{
'text'
:
'foobar'
}
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
request
=
factory
.
put
(
'/1'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
content_type
=
'application/json'
)
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
with
self
.
assertNumQueries
(
4
):
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEqual
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
self
.
assertEqual
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
updated
=
self
.
objects
.
get
(
id
=
1
)
updated
=
self
.
objects
.
get
(
id
=
1
)
...
@@ -263,10 +277,11 @@ class TestInstanceView(TestCase):
...
@@ -263,10 +277,11 @@ class TestInstanceView(TestCase):
at the requested url if it doesn't exist.
at the requested url if it doesn't exist.
"""
"""
content
=
{
'text'
:
'foobar'
}
content
=
{
'text'
:
'foobar'
}
# pk fields can not be created on demand, only the database can set th pk for a new object
# pk fields can not be created on demand, only the database can set th
e
pk for a new object
request
=
factory
.
put
(
'/5'
,
json
.
dumps
(
content
),
request
=
factory
.
put
(
'/5'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
content_type
=
'application/json'
)
response
=
self
.
view
(
request
,
pk
=
5
)
.
render
()
with
self
.
assertNumQueries
(
4
):
response
=
self
.
view
(
request
,
pk
=
5
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
new_obj
=
self
.
objects
.
get
(
pk
=
5
)
new_obj
=
self
.
objects
.
get
(
pk
=
5
)
self
.
assertEqual
(
new_obj
.
text
,
'foobar'
)
self
.
assertEqual
(
new_obj
.
text
,
'foobar'
)
...
@@ -279,7 +294,8 @@ class TestInstanceView(TestCase):
...
@@ -279,7 +294,8 @@ class TestInstanceView(TestCase):
content
=
{
'text'
:
'foobar'
}
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
put
(
'/test_slug'
,
json
.
dumps
(
content
),
request
=
factory
.
put
(
'/test_slug'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
content_type
=
'application/json'
)
response
=
self
.
slug_based_view
(
request
,
slug
=
'test_slug'
)
.
render
()
with
self
.
assertNumQueries
(
2
):
response
=
self
.
slug_based_view
(
request
,
slug
=
'test_slug'
)
.
render
()
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEqual
(
response
.
status_code
,
status
.
HTTP_201_CREATED
)
self
.
assertEqual
(
response
.
data
,
{
'slug'
:
'test_slug'
,
'text'
:
'foobar'
})
self
.
assertEqual
(
response
.
data
,
{
'slug'
:
'test_slug'
,
'text'
:
'foobar'
})
new_obj
=
SlugBasedModel
.
objects
.
get
(
slug
=
'test_slug'
)
new_obj
=
SlugBasedModel
.
objects
.
get
(
slug
=
'test_slug'
)
...
...
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