PageRenderTime 53ms CodeModel.GetById 47ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/regressiontests/test_utils/tests.py

https://code.google.com/p/mango-py/
Python | 151 lines | 119 code | 20 blank | 12 comment | 1 complexity | 110b39e93101abdbdf7eef183e4ac699 MD5 | raw file
  1import sys
  2
  3from django.test import TestCase, skipUnlessDBFeature, skipIfDBFeature
  4
  5from models import Person
  6
  7if sys.version_info >= (2, 5):
  8    from tests_25 import AssertNumQueriesContextManagerTests
  9
 10
 11class SkippingTestCase(TestCase):
 12    def test_skip_unless_db_feature(self):
 13        "A test that might be skipped is actually called."
 14        # Total hack, but it works, just want an attribute that's always true.
 15        @skipUnlessDBFeature("__class__")
 16        def test_func():
 17            raise ValueError
 18
 19        self.assertRaises(ValueError, test_func)
 20
 21
 22class AssertNumQueriesTests(TestCase):
 23    def test_assert_num_queries(self):
 24        def test_func():
 25            raise ValueError
 26
 27        self.assertRaises(ValueError,
 28            self.assertNumQueries, 2, test_func
 29        )
 30
 31    def test_assert_num_queries_with_client(self):
 32        person = Person.objects.create(name='test')
 33
 34        self.assertNumQueries(
 35            1,
 36            self.client.get,
 37            "/test_utils/get_person/%s/" % person.pk
 38        )
 39
 40        self.assertNumQueries(
 41            1,
 42            self.client.get,
 43            "/test_utils/get_person/%s/" % person.pk
 44        )
 45
 46        def test_func():
 47            self.client.get("/test_utils/get_person/%s/" % person.pk)
 48            self.client.get("/test_utils/get_person/%s/" % person.pk)
 49        self.assertNumQueries(2, test_func)
 50
 51
 52class SaveRestoreWarningState(TestCase):
 53    def test_save_restore_warnings_state(self):
 54        """
 55        Ensure save_warnings_state/restore_warnings_state work correctly.
 56        """
 57        # In reality this test could be satisfied by many broken implementations
 58        # of save_warnings_state/restore_warnings_state (e.g. just
 59        # warnings.resetwarnings()) , but it is difficult to test more.
 60        import warnings
 61        self.save_warnings_state()
 62
 63        class MyWarning(Warning):
 64            pass
 65
 66        # Add a filter that causes an exception to be thrown, so we can catch it
 67        warnings.simplefilter("error", MyWarning)
 68        self.assertRaises(Warning, lambda: warnings.warn("warn", MyWarning))
 69
 70        # Now restore.
 71        self.restore_warnings_state()
 72        # After restoring, we shouldn't get an exception. But we don't want a
 73        # warning printed either, so we have to silence the warning.
 74        warnings.simplefilter("ignore", MyWarning)
 75        warnings.warn("warn", MyWarning)
 76
 77        # Remove the filter we just added.
 78        self.restore_warnings_state()
 79
 80__test__ = {"API_TEST": r"""
 81# Some checks of the doctest output normalizer.
 82# Standard doctests do fairly
 83>>> from django.utils import simplejson
 84>>> from django.utils.xmlutils import SimplerXMLGenerator
 85>>> from StringIO import StringIO
 86
 87>>> def produce_long():
 88...     return 42L
 89
 90>>> def produce_int():
 91...     return 42
 92
 93>>> def produce_json():
 94...     return simplejson.dumps(['foo', {'bar': ('baz', None, 1.0, 2), 'whiz': 42}])
 95
 96>>> def produce_xml():
 97...     stream = StringIO()
 98...     xml = SimplerXMLGenerator(stream, encoding='utf-8')
 99...     xml.startDocument()
100...     xml.startElement("foo", {"aaa" : "1.0", "bbb": "2.0"})
101...     xml.startElement("bar", {"ccc" : "3.0"})
102...     xml.characters("Hello")
103...     xml.endElement("bar")
104...     xml.startElement("whiz", {})
105...     xml.characters("Goodbye")
106...     xml.endElement("whiz")
107...     xml.endElement("foo")
108...     xml.endDocument()
109...     return stream.getvalue()
110
111>>> def produce_xml_fragment():
112...     stream = StringIO()
113...     xml = SimplerXMLGenerator(stream, encoding='utf-8')
114...     xml.startElement("foo", {"aaa": "1.0", "bbb": "2.0"})
115...     xml.characters("Hello")
116...     xml.endElement("foo")
117...     xml.startElement("bar", {"ccc": "3.0", "ddd": "4.0"})
118...     xml.endElement("bar")
119...     return stream.getvalue()
120
121# Long values are normalized and are comparable to normal integers ...
122>>> produce_long()
12342
124
125# ... and vice versa
126>>> produce_int()
12742L
128
129# JSON output is normalized for field order, so it doesn't matter
130# which order json dictionary attributes are listed in output
131>>> produce_json()
132'["foo", {"bar": ["baz", null, 1.0, 2], "whiz": 42}]'
133
134>>> produce_json()
135'["foo", {"whiz": 42, "bar": ["baz", null, 1.0, 2]}]'
136
137# XML output is normalized for attribute order, so it doesn't matter
138# which order XML element attributes are listed in output
139>>> produce_xml()
140'<?xml version="1.0" encoding="UTF-8"?>\n<foo aaa="1.0" bbb="2.0"><bar ccc="3.0">Hello</bar><whiz>Goodbye</whiz></foo>'
141
142>>> produce_xml()
143'<?xml version="1.0" encoding="UTF-8"?>\n<foo bbb="2.0" aaa="1.0"><bar ccc="3.0">Hello</bar><whiz>Goodbye</whiz></foo>'
144
145>>> produce_xml_fragment()
146'<foo aaa="1.0" bbb="2.0">Hello</foo><bar ccc="3.0" ddd="4.0"></bar>'
147
148>>> produce_xml_fragment()
149'<foo bbb="2.0" aaa="1.0">Hello</foo><bar ddd="4.0" ccc="3.0"></bar>'
150
151"""}