Adding tests for rest_framework.py

This commit is contained in:
Alex Kahan 2016-09-28 20:10:30 -04:00
parent 12576275c4
commit a827e63bc4

View File

@ -4,7 +4,7 @@ from __future__ import unicode_literals
from django.test import TestCase
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
@ -22,6 +22,99 @@ class TemplateTagTests(TestCase):
self.assertIn("q=%E6%9F%A5%E8%AF%A2", 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_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']]
self.assertEqual(format_value(list_of_lists), '\n<table class="table table-striped">\n <tbody>\n \n <tr>\n <th>0</th>\n <td>\n list1\n</td>\n </tr>\n \n <tr>\n <th>1</th>\n <td>\n list2\n</td>\n </tr>\n \n <tr>\n <th>2</th>\n <td>\n list3\n</td>\n </tr>\n \n </tbody>\n</table>\n')
list_of_dicts = [{'item1': 'value1'}, {'item2': 'value2'}, {'item3': 'value3'}]
self.assertEqual(format_value(list_of_dicts), '\n<table class="table table-striped">\n <tbody>\n \n <tr>\n <th>0</th>\n <td></td>\n </tr>\n \n <tr>\n <th>1</th>\n <td></td>\n </tr>\n \n <tr>\n <th>2</th>\n <td></td>\n </tr>\n \n </tbody>\n</table>\n')
def test_format_value_dict(self):
"""
Tests format_value with a dict of strings
"""
dict_items = {'item1': 'value1', 'item2': 'value2', 'item3': 'value3'}
self.assertEqual(format_value(dict_items), '')
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,\nsomeone'
self.assertEqual(format_value(text), '<pre>Dear user, \n this is a message \n from,\nsomeone</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):
"""