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
915ac22a
Commit
915ac22a
authored
Oct 04, 2016
by
Alex Kahan
Committed by
Tom Christie
Oct 04, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding tests for rest_framework.py (#4523)
parent
883efbc1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
144 additions
and
1 deletions
+144
-1
tests/test_templatetags.py
+144
-1
No files found.
tests/test_templatetags.py
View file @
915ac22a
...
@@ -3,14 +3,24 @@ from __future__ import unicode_literals
...
@@ -3,14 +3,24 @@ from __future__ import unicode_literals
from
django.test
import
TestCase
from
django.test
import
TestCase
from
rest_framework.relations
import
Hyperlink
from
rest_framework.templatetags.rest_framework
import
(
from
rest_framework.templatetags.rest_framework
import
(
add_
query_param
,
urlize_quoted_links
add_
nested_class
,
add_query_param
,
format_value
,
urlize_quoted_links
)
)
from
rest_framework.test
import
APIRequestFactory
from
rest_framework.test
import
APIRequestFactory
factory
=
APIRequestFactory
()
factory
=
APIRequestFactory
()
def
format_html
(
html
):
"""
Helper function that formats HTML in order for easier comparison
:param html: raw HTML text to be formatted
:return: Cleaned HTML with no newlines or spaces
"""
return
html
.
replace
(
'
\n
'
,
''
)
.
replace
(
' '
,
''
)
class
TemplateTagTests
(
TestCase
):
class
TemplateTagTests
(
TestCase
):
def
test_add_query_param_with_non_latin_character
(
self
):
def
test_add_query_param_with_non_latin_character
(
self
):
...
@@ -22,6 +32,139 @@ class TemplateTagTests(TestCase):
...
@@ -22,6 +32,139 @@ class TemplateTagTests(TestCase):
self
.
assertIn
(
"q=
%
E6
%9
F
%
A5
%
E8
%
AF
%
A2"
,
json_url
)
self
.
assertIn
(
"q=
%
E6
%9
F
%
A5
%
E8
%
AF
%
A2"
,
json_url
)
self
.
assertIn
(
"format=json"
,
json_url
)
self
.
assertIn
(
"format=json"
,
json_url
)
def
test_format_value_boolean_or_none
(
self
):
"""
Tests format_value with booleans and None
"""
self
.
assertEqual
(
format_value
(
True
),
'<code>true</code>'
)
self
.
assertEqual
(
format_value
(
False
),
'<code>false</code>'
)
self
.
assertEqual
(
format_value
(
None
),
'<code>null</code>'
)
def
test_format_value_hyperlink
(
self
):
url
=
'http://url.com'
name
=
'name_of_url'
hyperlink
=
Hyperlink
(
url
,
name
)
self
.
assertEqual
(
format_value
(
hyperlink
),
'<a href=
%
s>
%
s</a>'
%
(
url
,
name
))
def
test_format_value_list
(
self
):
"""
Tests format_value with a list of strings
"""
list_items
=
[
'item1'
,
'item2'
,
'item3'
]
self
.
assertEqual
(
format_value
(
list_items
),
'
\n
item1, item2, item3
\n
'
)
self
.
assertEqual
(
format_value
([]),
'
\n\n
'
)
def
test_format_value_table
(
self
):
"""
Tests format_value with a list of lists/dicts
"""
list_of_lists
=
[[
'list1'
],
[
'list2'
],
[
'list3'
]]
expected_list_format
=
"""
<tableclass="tabletable-striped">
<tbody>
<tr>
<th>0</th>
<td>list1</td>
</tr>
<tr>
<th>1</th>
<td>list2</td>
</tr>
<tr>
<th>2</th>
<td>list3</td>
</tr>
</tbody>
</table>"""
self
.
assertEqual
(
format_html
(
format_value
(
list_of_lists
)),
format_html
(
expected_list_format
)
)
expected_dict_format
=
"""
<tableclass="tabletable-striped">
<tbody>
<tr>
<th>0</th>
<td></td>
</tr>
<tr>
<th>1</th>
<td></td>
</tr>
<tr>
<th>2</th>
<td></td>
</tr>
</tbody>
</table>"""
list_of_dicts
=
[{
'item1'
:
'value1'
},
{
'item2'
:
'value2'
},
{
'item3'
:
'value3'
}]
self
.
assertEqual
(
format_html
(
format_value
(
list_of_dicts
)),
format_html
(
expected_dict_format
)
)
def
test_format_value_simple_string
(
self
):
"""
Tests format_value with a simple string
"""
simple_string
=
'this is an example of a string'
self
.
assertEqual
(
format_value
(
simple_string
),
simple_string
)
def
test_format_value_string_hyperlink
(
self
):
"""
Tests format_value with a url
"""
url
=
'http://www.example.com'
self
.
assertEqual
(
format_value
(
url
),
'<a href="http://www.example.com">http://www.example.com</a>'
)
def
test_format_value_string_email
(
self
):
"""
Tests format_value with an email address
"""
email
=
'something@somewhere.com'
self
.
assertEqual
(
format_value
(
email
),
'<a href="mailto:something@somewhere.com">something@somewhere.com</a>'
)
def
test_format_value_string_newlines
(
self
):
"""
Tests format_value with a string with newline characters
:return:
"""
text
=
'Dear user,
\n
this is a message
\n
from,
\n
someone'
self
.
assertEqual
(
format_value
(
text
),
'<pre>Dear user,
\n
this is a message
\n
from,
\n
someone</pre>'
)
def
test_format_value_object
(
self
):
"""
Tests that format_value with a object returns the object's __str__ method
"""
obj
=
object
()
self
.
assertEqual
(
format_value
(
obj
),
obj
.
__str__
())
def
test_add_nested_class
(
self
):
"""
Tests that add_nested_class returns the proper class
"""
positive_cases
=
[
[[
'item'
]],
[{
'item1'
:
'value1'
}],
{
'item1'
:
'value1'
}
]
negative_cases
=
[
[
'list'
],
''
,
None
,
True
,
False
]
for
case
in
positive_cases
:
self
.
assertEqual
(
add_nested_class
(
case
),
'class=nested'
)
for
case
in
negative_cases
:
self
.
assertEqual
(
add_nested_class
(
case
),
''
)
class
Issue1386Tests
(
TestCase
):
class
Issue1386Tests
(
TestCase
):
"""
"""
...
...
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