3
3
4
4
from django .test import TestCase
5
5
6
+ from rest_framework .relations import Hyperlink
6
7
from rest_framework .templatetags .rest_framework import (
7
- add_query_param , urlize_quoted_links
8
+ add_nested_class , add_query_param , format_value , urlize_quoted_links
8
9
)
9
10
from rest_framework .test import APIRequestFactory
10
11
11
12
factory = APIRequestFactory ()
12
13
13
14
15
+ def format_html (html ):
16
+ """
17
+ Helper function that formats HTML in order for easier comparison
18
+ :param html: raw HTML text to be formatted
19
+ :return: Cleaned HTML with no newlines or spaces
20
+ """
21
+ return html .replace ('\n ' , '' ).replace (' ' , '' )
22
+
23
+
14
24
class TemplateTagTests (TestCase ):
15
25
16
26
def test_add_query_param_with_non_latin_character (self ):
@@ -22,6 +32,139 @@ def test_add_query_param_with_non_latin_character(self):
22
32
self .assertIn ("q=%E6%9F%A5%E8%AF%A2" , json_url )
23
33
self .assertIn ("format=json" , json_url )
24
34
35
+ def test_format_value_boolean_or_none (self ):
36
+ """
37
+ Tests format_value with booleans and None
38
+ """
39
+ self .assertEqual (format_value (True ), '<code>true</code>' )
40
+ self .assertEqual (format_value (False ), '<code>false</code>' )
41
+ self .assertEqual (format_value (None ), '<code>null</code>' )
42
+
43
+ def test_format_value_hyperlink (self ):
44
+ url = 'http://url.com'
45
+ name = 'name_of_url'
46
+ hyperlink = Hyperlink (url , name )
47
+ self .assertEqual (format_value (hyperlink ), '<a href=%s>%s</a>' % (url , name ))
48
+
49
+ def test_format_value_list (self ):
50
+ """
51
+ Tests format_value with a list of strings
52
+ """
53
+ list_items = ['item1' , 'item2' , 'item3' ]
54
+ self .assertEqual (format_value (list_items ), '\n item1, item2, item3\n ' )
55
+ self .assertEqual (format_value ([]), '\n \n ' )
56
+
57
+ def test_format_value_table (self ):
58
+ """
59
+ Tests format_value with a list of lists/dicts
60
+ """
61
+ list_of_lists = [['list1' ], ['list2' ], ['list3' ]]
62
+ expected_list_format = """
63
+ <tableclass="tabletable-striped">
64
+ <tbody>
65
+ <tr>
66
+ <th>0</th>
67
+ <td>list1</td>
68
+ </tr>
69
+ <tr>
70
+ <th>1</th>
71
+ <td>list2</td>
72
+ </tr>
73
+ <tr>
74
+ <th>2</th>
75
+ <td>list3</td>
76
+ </tr>
77
+ </tbody>
78
+ </table>"""
79
+ self .assertEqual (
80
+ format_html (format_value (list_of_lists )),
81
+ format_html (expected_list_format )
82
+ )
83
+
84
+ expected_dict_format = """
85
+ <tableclass="tabletable-striped">
86
+ <tbody>
87
+ <tr>
88
+ <th>0</th>
89
+ <td></td>
90
+ </tr>
91
+ <tr>
92
+ <th>1</th>
93
+ <td></td>
94
+ </tr>
95
+ <tr>
96
+ <th>2</th>
97
+ <td></td>
98
+ </tr>
99
+ </tbody>
100
+ </table>"""
101
+
102
+ list_of_dicts = [{'item1' : 'value1' }, {'item2' : 'value2' }, {'item3' : 'value3' }]
103
+ self .assertEqual (
104
+ format_html (format_value (list_of_dicts )),
105
+ format_html (expected_dict_format )
106
+ )
107
+
108
+ def test_format_value_simple_string (self ):
109
+ """
110
+ Tests format_value with a simple string
111
+ """
112
+ simple_string = 'this is an example of a string'
113
+ self .assertEqual (format_value (simple_string ), simple_string )
114
+
115
+ def test_format_value_string_hyperlink (self ):
116
+ """
117
+ Tests format_value with a url
118
+ """
119
+ url = 'http://www.example.com'
120
+ self .assertEqual (format_value (url ), '<a href="http://www.example.com">http://www.example.com</a>' )
121
+
122
+ def test_format_value_string_email (self ):
123
+ """
124
+ Tests format_value with an email address
125
+ """
126
+ email = 'something@somewhere.com'
127
+ self .assertEqual (format_value (email ), '<a href="mailto:something@somewhere.com">something@somewhere.com</a>' )
128
+
129
+ def test_format_value_string_newlines (self ):
130
+ """
131
+ Tests format_value with a string with newline characters
132
+ :return:
133
+ """
134
+ text = 'Dear user, \n this is a message \n from,\n someone'
135
+ self .assertEqual (format_value (text ), '<pre>Dear user, \n this is a message \n from,\n someone</pre>' )
136
+
137
+ def test_format_value_object (self ):
138
+ """
139
+ Tests that format_value with a object returns the object's __str__ method
140
+ """
141
+ obj = object ()
142
+ self .assertEqual (format_value (obj ), obj .__str__ ())
143
+
144
+ def test_add_nested_class (self ):
145
+ """
146
+ Tests that add_nested_class returns the proper class
147
+ """
148
+ positive_cases = [
149
+ [['item' ]],
150
+ [{'item1' : 'value1' }],
151
+ {'item1' : 'value1' }
152
+ ]
153
+
154
+ negative_cases = [
155
+ ['list' ],
156
+ '' ,
157
+ None ,
158
+ True ,
159
+ False
160
+ ]
161
+
162
+ for case in positive_cases :
163
+ self .assertEqual (add_nested_class (case ), 'class=nested' )
164
+
165
+ for case in negative_cases :
166
+ self .assertEqual (add_nested_class (case ), '' )
167
+
25
168
26
169
class Issue1386Tests (TestCase ):
27
170
"""
0 commit comments