tests.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import sys
  2. if sys.version_info >= (2, 5):
  3. from python_25 import AssertNumQueriesTests
  4. __test__ = {"API_TEST": r"""
  5. # Some checks of the doctest output normalizer.
  6. # Standard doctests do fairly
  7. >>> from django.utils import simplejson
  8. >>> from django.utils.xmlutils import SimplerXMLGenerator
  9. >>> from StringIO import StringIO
  10. >>> def produce_long():
  11. ... return 42L
  12. >>> def produce_int():
  13. ... return 42
  14. >>> def produce_json():
  15. ... return simplejson.dumps(['foo', {'bar': ('baz', None, 1.0, 2), 'whiz': 42}])
  16. >>> def produce_xml():
  17. ... stream = StringIO()
  18. ... xml = SimplerXMLGenerator(stream, encoding='utf-8')
  19. ... xml.startDocument()
  20. ... xml.startElement("foo", {"aaa" : "1.0", "bbb": "2.0"})
  21. ... xml.startElement("bar", {"ccc" : "3.0"})
  22. ... xml.characters("Hello")
  23. ... xml.endElement("bar")
  24. ... xml.startElement("whiz", {})
  25. ... xml.characters("Goodbye")
  26. ... xml.endElement("whiz")
  27. ... xml.endElement("foo")
  28. ... xml.endDocument()
  29. ... return stream.getvalue()
  30. >>> def produce_xml_fragment():
  31. ... stream = StringIO()
  32. ... xml = SimplerXMLGenerator(stream, encoding='utf-8')
  33. ... xml.startElement("foo", {"aaa": "1.0", "bbb": "2.0"})
  34. ... xml.characters("Hello")
  35. ... xml.endElement("foo")
  36. ... xml.startElement("bar", {"ccc": "3.0", "ddd": "4.0"})
  37. ... xml.endElement("bar")
  38. ... return stream.getvalue()
  39. # Long values are normalized and are comparable to normal integers ...
  40. >>> produce_long()
  41. 42
  42. # ... and vice versa
  43. >>> produce_int()
  44. 42L
  45. # JSON output is normalized for field order, so it doesn't matter
  46. # which order json dictionary attributes are listed in output
  47. >>> produce_json()
  48. '["foo", {"bar": ["baz", null, 1.0, 2], "whiz": 42}]'
  49. >>> produce_json()
  50. '["foo", {"whiz": 42, "bar": ["baz", null, 1.0, 2]}]'
  51. # XML output is normalized for attribute order, so it doesn't matter
  52. # which order XML element attributes are listed in output
  53. >>> produce_xml()
  54. '<?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>'
  55. >>> produce_xml()
  56. '<?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>'
  57. >>> produce_xml_fragment()
  58. '<foo aaa="1.0" bbb="2.0">Hello</foo><bar ccc="3.0" ddd="4.0"></bar>'
  59. >>> produce_xml_fragment()
  60. '<foo bbb="2.0" aaa="1.0">Hello</foo><bar ddd="4.0" ccc="3.0"></bar>'
  61. """}