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
21166a3a
Commit
21166a3a
authored
Jan 25, 2017
by
Artem Muterko
Committed by
Tom Christie
Jan 25, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add couple of tests for filters (#4849)
parent
48b5aa71
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
0 deletions
+35
-0
tests/test_filters.py
+35
-0
No files found.
tests/test_filters.py
View file @
21166a3a
...
@@ -5,6 +5,7 @@ import unittest
...
@@ -5,6 +5,7 @@ import unittest
import
warnings
import
warnings
from
decimal
import
Decimal
from
decimal
import
Decimal
import
pytest
from
django.conf.urls
import
url
from
django.conf.urls
import
url
from
django.core.exceptions
import
ImproperlyConfigured
from
django.core.exceptions
import
ImproperlyConfigured
from
django.db
import
models
from
django.db
import
models
...
@@ -119,6 +120,27 @@ if django_filters:
...
@@ -119,6 +120,27 @@ if django_filters:
]
]
class
BaseFilterTests
(
TestCase
):
def
setUp
(
self
):
self
.
original_coreapi
=
filters
.
coreapi
filters
.
coreapi
=
True
# mock it, because not None value needed
self
.
filter_backend
=
filters
.
BaseFilterBackend
()
def
tearDown
(
self
):
filters
.
coreapi
=
self
.
original_coreapi
def
test_filter_queryset_raises_error
(
self
):
with
pytest
.
raises
(
NotImplementedError
):
self
.
filter_backend
.
filter_queryset
(
None
,
None
,
None
)
def
test_get_schema_fields_checks_for_coreapi
(
self
):
filters
.
coreapi
=
None
with
pytest
.
raises
(
AssertionError
):
self
.
filter_backend
.
get_schema_fields
({})
filters
.
coreapi
=
True
assert
self
.
filter_backend
.
get_schema_fields
({})
==
[]
class
CommonFilteringTestCase
(
TestCase
):
class
CommonFilteringTestCase
(
TestCase
):
def
_serialize_object
(
self
,
obj
):
def
_serialize_object
(
self
,
obj
):
return
{
'id'
:
obj
.
id
,
'text'
:
obj
.
text
,
'decimal'
:
str
(
obj
.
decimal
),
'date'
:
obj
.
date
.
isoformat
()}
return
{
'id'
:
obj
.
id
,
'text'
:
obj
.
text
,
'decimal'
:
str
(
obj
.
decimal
),
'date'
:
obj
.
date
.
isoformat
()}
...
@@ -429,6 +451,19 @@ class SearchFilterTests(TestCase):
...
@@ -429,6 +451,19 @@ class SearchFilterTests(TestCase):
{
'id'
:
2
,
'title'
:
'zz'
,
'text'
:
'bcd'
}
{
'id'
:
2
,
'title'
:
'zz'
,
'text'
:
'bcd'
}
]
]
def
test_search_returns_same_queryset_if_no_search_fields_or_terms_provided
(
self
):
class
SearchListView
(
generics
.
ListAPIView
):
queryset
=
SearchFilterModel
.
objects
.
all
()
serializer_class
=
SearchFilterSerializer
filter_backends
=
(
filters
.
SearchFilter
,)
view
=
SearchListView
.
as_view
()
request
=
factory
.
get
(
'/'
)
response
=
view
(
request
)
expected
=
SearchFilterSerializer
(
SearchFilterModel
.
objects
.
all
(),
many
=
True
)
.
data
assert
response
.
data
==
expected
def
test_exact_search
(
self
):
def
test_exact_search
(
self
):
class
SearchListView
(
generics
.
ListAPIView
):
class
SearchListView
(
generics
.
ListAPIView
):
queryset
=
SearchFilterModel
.
objects
.
all
()
queryset
=
SearchFilterModel
.
objects
.
all
()
...
...
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