From a827e63bc424be4134bfbc0f92fecdbbc84a866d Mon Sep 17 00:00:00 2001 From: Alex Kahan Date: Wed, 28 Sep 2016 20:10:30 -0400 Subject: [PATCH 1/4] Adding tests for rest_framework.py --- tests/test_templatetags.py | 95 +++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/tests/test_templatetags.py b/tests/test_templatetags.py index ac218df21..f75dcbadb 100644 --- a/tests/test_templatetags.py +++ b/tests/test_templatetags.py @@ -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), 'true') + self.assertEqual(format_value(False), 'false') + self.assertEqual(format_value(None), 'null') + + 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\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
0\n list1\n
1\n list2\n
2\n list3\n
\n') + + list_of_dicts = [{'item1': 'value1'}, {'item2': 'value2'}, {'item3': 'value3'}] + self.assertEqual(format_value(list_of_dicts), '\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
0
1
2
\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), 'http://www.example.com') + + def test_format_value_string_email(self): + """ + Tests format_value with an email address + """ + email = 'something@somewhere.com' + self.assertEqual(format_value(email), 'something@somewhere.com') + + 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), '
Dear user, \n this is a message \n from,\nsomeone
') + + 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): """ From 93d81b5b74314797939d4ee70db2ddd37e9d8872 Mon Sep 17 00:00:00 2001 From: Alex Kahan Date: Wed, 28 Sep 2016 21:12:48 -0400 Subject: [PATCH 2/4] Adding test for hyperlink --- tests/test_templatetags.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_templatetags.py b/tests/test_templatetags.py index f75dcbadb..ee9513ca0 100644 --- a/tests/test_templatetags.py +++ b/tests/test_templatetags.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals from django.test import TestCase +from rest_framework.relations import Hyperlink from rest_framework.templatetags.rest_framework import ( add_nested_class, add_query_param, format_value, urlize_quoted_links ) @@ -30,6 +31,12 @@ class TemplateTagTests(TestCase): self.assertEqual(format_value(False), 'false') self.assertEqual(format_value(None), 'null') + def test_format_value_hyperlink(self): + url = 'http://url.com' + name = 'name_of_url' + hyperlink = Hyperlink(url, name) + self.assertEqual(format_value(hyperlink), '%s' % (url, name)) + def test_format_value_list(self): """ Tests format_value with a list of strings From 567371bb2e9928086c272a83219e249c53c1abc4 Mon Sep 17 00:00:00 2001 From: Alex Kahan Date: Mon, 3 Oct 2016 18:32:40 -0400 Subject: [PATCH 3/4] Cleaning up whitespace in tests --- tests/test_templatetags.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/tests/test_templatetags.py b/tests/test_templatetags.py index ee9513ca0..acfc8ab7c 100644 --- a/tests/test_templatetags.py +++ b/tests/test_templatetags.py @@ -50,17 +50,16 @@ class TemplateTagTests(TestCase): Tests format_value with a list of lists/dicts """ list_of_lists = [['list1'], ['list2'], ['list3']] - self.assertEqual(format_value(list_of_lists), '\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
0\n list1\n
1\n list2\n
2\n list3\n
\n') + self.assertEqual( + format_value(list_of_lists).replace(' ', ''), + '\n\n\n\n\n0\n\nlist1\n\n\n\n\n1\n\nlist2\n\n\n\n\n2\n\nlist3\n\n\n\n\n\n' + ) list_of_dicts = [{'item1': 'value1'}, {'item2': 'value2'}, {'item3': 'value3'}] - self.assertEqual(format_value(list_of_dicts), '\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
0
1
2
\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), '') + self.assertEqual( + format_value(list_of_dicts).replace(' ', ''), + '\n\n\n\n\n0\n\n\n\n\n1\n\n\n\n\n2\n\n\n\n\n\n' + ) def test_format_value_simple_string(self): """ From 2d376c487fab9ba0263f87e2478b5dc7750254f7 Mon Sep 17 00:00:00 2001 From: Alex Kahan Date: Mon, 3 Oct 2016 21:35:37 -0400 Subject: [PATCH 4/4] Adding tests for encoders.py --- tests/test_encoders.py | 81 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 tests/test_encoders.py diff --git a/tests/test_encoders.py b/tests/test_encoders.py new file mode 100644 index 000000000..d6f681932 --- /dev/null +++ b/tests/test_encoders.py @@ -0,0 +1,81 @@ +from datetime import date, datetime, timedelta, tzinfo +from decimal import Decimal +from uuid import uuid4 + +from django.test import TestCase + +from rest_framework.utils.encoders import JSONEncoder + + +class JSONEncoderTests(TestCase): + """ + Tests the JSONEncoder method + """ + + def setUp(self): + self.encoder = JSONEncoder() + + def test_encode_decimal(self): + """ + Tests encoding a decimal + """ + d = Decimal(3.14) + self.assertEqual(d, float(d)) + + def test_encode_datetime(self): + """ + Tests encoding a datetime object + """ + current_time = datetime.now() + self.assertEqual(self.encoder.default(current_time), current_time.isoformat()) + + def test_encode_time(self): + """ + Tests encoding a timezone + """ + current_time = datetime.now().time() + self.assertEqual(self.encoder.default(current_time), current_time.isoformat()[:12]) + + def test_encode_time_tz(self): + """ + Tests encoding a timezone aware timestamp + """ + + class UTC(tzinfo): + """ + Class extending tzinfo to mimic UTC time + """ + def utcoffset(self, dt): + return timedelta(0) + + def tzname(self, dt): + return "UTC" + + def dst(self, dt): + return timedelta(0) + + current_time = datetime.now().time() + current_time = current_time.replace(tzinfo=UTC()) + with self.assertRaises(ValueError): + self.encoder.default(current_time) + + def test_encode_date(self): + """ + Tests encoding a date object + """ + current_date = date.today() + self.assertEqual(self.encoder.default(current_date), current_date.isoformat()) + + def test_encode_timedelta(self): + """ + Tests encoding a timedelta object + """ + delta = timedelta(hours=1) + self.assertEqual(self.encoder.default(delta), str(delta.total_seconds())) + + def test_encode_uuid(self): + """ + Tests encoding a UUID object + """ + unique_id = uuid4() + self.assertEqual(self.encoder.default(unique_id), str(unique_id))