PageRenderTime 33ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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