From 8a9b0c15a6c0ef60dea3ba3042317520bc201206 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 31 Dec 2014 11:25:11 -0500 Subject: Renamed tests for util -> utils moves; refs #17627. --- tests/forms_tests/tests/test_util.py | 111 ---------------------------------- tests/forms_tests/tests/test_utils.py | 111 ++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 111 deletions(-) delete mode 100644 tests/forms_tests/tests/test_util.py create mode 100644 tests/forms_tests/tests/test_utils.py (limited to 'tests/forms_tests') diff --git a/tests/forms_tests/tests/test_util.py b/tests/forms_tests/tests/test_util.py deleted file mode 100644 index c37affeb31..0000000000 --- a/tests/forms_tests/tests/test_util.py +++ /dev/null @@ -1,111 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -import copy - -from django.core.exceptions import ValidationError -from django.forms.utils import flatatt, ErrorDict, ErrorList -from django.test import TestCase -from django.utils.safestring import mark_safe -from django.utils import six -from django.utils.translation import ugettext_lazy -from django.utils.encoding import python_2_unicode_compatible - - -class FormsUtilTestCase(TestCase): - # Tests for forms/utils.py module. - - def test_flatatt(self): - ########### - # flatatt # - ########### - - self.assertEqual(flatatt({'id': "header"}), ' id="header"') - self.assertEqual(flatatt({'class': "news", 'title': "Read this"}), ' class="news" title="Read this"') - self.assertEqual(flatatt({'class': "news", 'title': "Read this", 'required': "required"}), ' class="news" required="required" title="Read this"') - self.assertEqual(flatatt({'class': "news", 'title': "Read this", 'required': True}), ' class="news" title="Read this" required') - self.assertEqual(flatatt({'class': "news", 'title': "Read this", 'required': False}), ' class="news" title="Read this"') - self.assertEqual(flatatt({}), '') - - def test_flatatt_no_side_effects(self): - """ - Fixes #23883 -- Check that flatatt does not modify the dict passed in - """ - attrs = {'foo': 'bar', 'true': True, 'false': False} - attrs_copy = copy.copy(attrs) - self.assertEqual(attrs, attrs_copy) - - first_run = flatatt(attrs) - self.assertEqual(attrs, attrs_copy) - self.assertEqual(first_run, ' foo="bar" true') - - second_run = flatatt(attrs) - self.assertEqual(attrs, attrs_copy) - - self.assertEqual(first_run, second_run) - - def test_validation_error(self): - ################### - # ValidationError # - ################### - - # Can take a string. - self.assertHTMLEqual(str(ErrorList(ValidationError("There was an error.").messages)), - '') - - # Can take a unicode string. - self.assertHTMLEqual(six.text_type(ErrorList(ValidationError("Not \u03C0.").messages)), - '') - - # Can take a lazy string. - self.assertHTMLEqual(str(ErrorList(ValidationError(ugettext_lazy("Error.")).messages)), - '') - - # Can take a list. - self.assertHTMLEqual(str(ErrorList(ValidationError(["Error one.", "Error two."]).messages)), - '') - - # Can take a mixture in a list. - self.assertHTMLEqual(str(ErrorList(ValidationError(["First error.", "Not \u03C0.", ugettext_lazy("Error.")]).messages)), - '') - - @python_2_unicode_compatible - class VeryBadError: - def __str__(self): - return "A very bad error." - - # Can take a non-string. - self.assertHTMLEqual(str(ErrorList(ValidationError(VeryBadError()).messages)), - '') - - # Escapes non-safe input but not input marked safe. - example = 'Example of link: example' - self.assertHTMLEqual(str(ErrorList([example])), - '') - self.assertHTMLEqual(str(ErrorList([mark_safe(example)])), - '') - self.assertHTMLEqual(str(ErrorDict({'name': example})), - '') - self.assertHTMLEqual(str(ErrorDict({'name': mark_safe(example)})), - '') - - def test_error_dict_copy(self): - e = ErrorDict() - e['__all__'] = ErrorList([ - ValidationError( - message='message %(i)s', - params={'i': 1}, - ), - ValidationError( - message='message %(i)s', - params={'i': 2}, - ), - ]) - - e_copy = copy.copy(e) - self.assertEqual(e, e_copy) - self.assertEqual(e.as_data(), e_copy.as_data()) - - e_deepcopy = copy.deepcopy(e) - self.assertEqual(e, e_deepcopy) - self.assertEqual(e.as_data(), e_copy.as_data()) diff --git a/tests/forms_tests/tests/test_utils.py b/tests/forms_tests/tests/test_utils.py new file mode 100644 index 0000000000..6e40e7565c --- /dev/null +++ b/tests/forms_tests/tests/test_utils.py @@ -0,0 +1,111 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +import copy + +from django.core.exceptions import ValidationError +from django.forms.utils import flatatt, ErrorDict, ErrorList +from django.test import TestCase +from django.utils.safestring import mark_safe +from django.utils import six +from django.utils.translation import ugettext_lazy +from django.utils.encoding import python_2_unicode_compatible + + +class FormsUtilsTestCase(TestCase): + # Tests for forms/utils.py module. + + def test_flatatt(self): + ########### + # flatatt # + ########### + + self.assertEqual(flatatt({'id': "header"}), ' id="header"') + self.assertEqual(flatatt({'class': "news", 'title': "Read this"}), ' class="news" title="Read this"') + self.assertEqual(flatatt({'class': "news", 'title': "Read this", 'required': "required"}), ' class="news" required="required" title="Read this"') + self.assertEqual(flatatt({'class': "news", 'title': "Read this", 'required': True}), ' class="news" title="Read this" required') + self.assertEqual(flatatt({'class': "news", 'title': "Read this", 'required': False}), ' class="news" title="Read this"') + self.assertEqual(flatatt({}), '') + + def test_flatatt_no_side_effects(self): + """ + Fixes #23883 -- Check that flatatt does not modify the dict passed in + """ + attrs = {'foo': 'bar', 'true': True, 'false': False} + attrs_copy = copy.copy(attrs) + self.assertEqual(attrs, attrs_copy) + + first_run = flatatt(attrs) + self.assertEqual(attrs, attrs_copy) + self.assertEqual(first_run, ' foo="bar" true') + + second_run = flatatt(attrs) + self.assertEqual(attrs, attrs_copy) + + self.assertEqual(first_run, second_run) + + def test_validation_error(self): + ################### + # ValidationError # + ################### + + # Can take a string. + self.assertHTMLEqual(str(ErrorList(ValidationError("There was an error.").messages)), + '') + + # Can take a unicode string. + self.assertHTMLEqual(six.text_type(ErrorList(ValidationError("Not \u03C0.").messages)), + '') + + # Can take a lazy string. + self.assertHTMLEqual(str(ErrorList(ValidationError(ugettext_lazy("Error.")).messages)), + '') + + # Can take a list. + self.assertHTMLEqual(str(ErrorList(ValidationError(["Error one.", "Error two."]).messages)), + '') + + # Can take a mixture in a list. + self.assertHTMLEqual(str(ErrorList(ValidationError(["First error.", "Not \u03C0.", ugettext_lazy("Error.")]).messages)), + '') + + @python_2_unicode_compatible + class VeryBadError: + def __str__(self): + return "A very bad error." + + # Can take a non-string. + self.assertHTMLEqual(str(ErrorList(ValidationError(VeryBadError()).messages)), + '') + + # Escapes non-safe input but not input marked safe. + example = 'Example of link: example' + self.assertHTMLEqual(str(ErrorList([example])), + '') + self.assertHTMLEqual(str(ErrorList([mark_safe(example)])), + '') + self.assertHTMLEqual(str(ErrorDict({'name': example})), + '') + self.assertHTMLEqual(str(ErrorDict({'name': mark_safe(example)})), + '') + + def test_error_dict_copy(self): + e = ErrorDict() + e['__all__'] = ErrorList([ + ValidationError( + message='message %(i)s', + params={'i': 1}, + ), + ValidationError( + message='message %(i)s', + params={'i': 2}, + ), + ]) + + e_copy = copy.copy(e) + self.assertEqual(e, e_copy) + self.assertEqual(e.as_data(), e_copy.as_data()) + + e_deepcopy = copy.deepcopy(e) + self.assertEqual(e, e_deepcopy) + self.assertEqual(e.as_data(), e_copy.as_data()) -- cgit v1.3