PageRenderTime 122ms CodeModel.GetById 80ms app.highlight 6ms RepoModel.GetById 35ms app.codeStats 0ms

/tests/regressiontests/forms/tests/util.py

https://code.google.com/p/mango-py/
Python | 57 lines | 37 code | 10 blank | 10 comment | 0 complexity | 4fa874ec68da96c2f6db4e25a553a6e1 MD5 | raw file
 1# -*- coding: utf-8 -*-
 2from django.core.exceptions import ValidationError
 3from django.forms.util import *
 4from django.utils.translation import ugettext_lazy
 5from django.utils.unittest import TestCase
 6
 7
 8class FormsUtilTestCase(TestCase):
 9        # Tests for forms/util.py module.
10
11    def test_flatatt(self):
12        ###########
13        # flatatt #
14        ###########
15
16        self.assertEqual(flatatt({'id': "header"}), u' id="header"')
17        self.assertEqual(flatatt({'class': "news", 'title': "Read this"}), u' class="news" title="Read this"')
18        self.assertEqual(flatatt({}), u'')
19
20    def test_validation_error(self):
21        ###################
22        # ValidationError #
23        ###################
24
25        # Can take a string.
26        self.assertEqual(str(ErrorList(ValidationError("There was an error.").messages)),
27                         '<ul class="errorlist"><li>There was an error.</li></ul>')
28
29        # Can take a unicode string.
30        self.assertEqual(str(ErrorList(ValidationError(u"Not \u03C0.").messages)),
31                         '<ul class="errorlist"><li>Not ?&#x20AC;.</li></ul>')
32
33        # Can take a lazy string.
34        self.assertEqual(str(ErrorList(ValidationError(ugettext_lazy("Error.")).messages)),
35                         '<ul class="errorlist"><li>Error.</li></ul>')
36
37        # Can take a list.
38        self.assertEqual(str(ErrorList(ValidationError(["Error one.", "Error two."]).messages)),
39                         '<ul class="errorlist"><li>Error one.</li><li>Error two.</li></ul>')
40
41        # Can take a mixture in a list.
42        self.assertEqual(str(ErrorList(ValidationError(["First error.", u"Not \u03C0.", ugettext_lazy("Error.")]).messages)),
43                         '<ul class="errorlist"><li>First error.</li><li>Not ?&#x20AC;.</li><li>Error.</li></ul>')
44
45        class VeryBadError:
46            def __unicode__(self): return u"A very bad error."
47
48        # Can take a non-string.
49        self.assertEqual(str(ErrorList(ValidationError(VeryBadError()).messages)),
50                         '<ul class="errorlist"><li>A very bad error.</li></ul>')
51
52        # Escapes non-safe input but not input marked safe.
53        example = 'Example of link: <a href="http://www.example.com/">example</a>'
54        self.assertEqual(str(ErrorList([example])),
55                         '<ul class="errorlist"><li>Example of link: &lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;</li></ul>')
56        self.assertEqual(str(ErrorList([mark_safe(example)])),
57                         '<ul class="errorlist"><li>Example of link: <a href="http://www.example.com/">example</a></li></ul>')