Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

test_compat.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # -*- coding: utf-8 -*-
  2. import codecs
  3. import csv
  4. import datetime
  5. import os
  6. from tempfile import NamedTemporaryFile
  7. from django.utils import six
  8. from django.utils.encoding import smart_text
  9. from django.utils.six.moves import cStringIO
  10. from django.test import TestCase, override_settings
  11. from oscar.core.compat import UnicodeCSVWriter, existing_user_fields
  12. class unicodeobj(object):
  13. def __init__(self, s):
  14. self.s = s
  15. def __str__(self):
  16. return self.s
  17. def __unicode__(self):
  18. return self.s
  19. class TestExistingUserFields(TestCase):
  20. def test_order(self):
  21. fields = existing_user_fields(['email', 'first_name', 'last_name'])
  22. self.assertEqual(fields, ['email', 'first_name', 'last_name'])
  23. class TestUnicodeCSVWriter(TestCase):
  24. def test_can_write_different_values(self):
  25. writer = UnicodeCSVWriter(open_file=cStringIO())
  26. s = u'ünįcodē'
  27. rows = [[s, unicodeobj(s), 123, datetime.date.today()], ]
  28. writer.writerows(rows)
  29. self.assertRaises(TypeError, writer.writerows, [object()])
  30. def test_context_manager(self):
  31. tmp_file = NamedTemporaryFile()
  32. with UnicodeCSVWriter(filename=tmp_file.name) as writer:
  33. s = u'ünįcodē'
  34. rows = [[s, unicodeobj(s), 123, datetime.date.today()], ]
  35. writer.writerows(rows)
  36. def test_csv_write_output(self):
  37. tmp_file = NamedTemporaryFile(delete=False)
  38. with UnicodeCSVWriter(filename=tmp_file.name) as writer:
  39. s = u'ünįcodē'
  40. row = [s, 123, 'foo-bar']
  41. writer.writerows([row])
  42. with open(tmp_file.name, 'r') as read_file:
  43. content = smart_text(read_file.read(), encoding='utf-8').strip()
  44. self.assertEqual(content, u'ünįcodē,123,foo-bar')
  45. # Clean up
  46. os.unlink(tmp_file.name)
  47. @override_settings(OSCAR_CSV_INCLUDE_BOM=True)
  48. def test_bom_write_with_open_file(self):
  49. csv_file = NamedTemporaryFile(delete=False)
  50. with open(csv_file.name, 'w') as open_file:
  51. writer = UnicodeCSVWriter(open_file=open_file, encoding="utf-8")
  52. s = u'ünįcodē'
  53. row = [s, 123, datetime.date.today()]
  54. writer.writerows([row])
  55. with open(csv_file.name, 'rb') as read_file:
  56. self.assertTrue(read_file.read().startswith(codecs.BOM_UTF8))
  57. # Clean up
  58. os.unlink(csv_file.name)
  59. @override_settings(OSCAR_CSV_INCLUDE_BOM=True)
  60. def test_bom_write_with_filename(self):
  61. csv_file = NamedTemporaryFile(delete=False)
  62. with UnicodeCSVWriter(filename=csv_file.name, encoding="utf-8") as writer:
  63. s = u'ünįcodē'
  64. row = [s, 123, datetime.date.today()]
  65. writer.writerows([row])
  66. with open(csv_file.name, 'rb') as read_file:
  67. self.assertTrue(read_file.read().startswith(codecs.BOM_UTF8))
  68. # Clean up
  69. os.unlink(csv_file.name)
  70. @override_settings(OSCAR_CSV_INCLUDE_BOM=True)
  71. def test_bom_not_written_for_other_encodings(self):
  72. csv_file = NamedTemporaryFile(delete=False)
  73. with UnicodeCSVWriter(filename=csv_file.name, encoding="ascii") as writer:
  74. s = 'boring ascii'
  75. row = [s, 123, datetime.date.today()]
  76. writer.writerows([row])
  77. with open(csv_file.name, 'rb') as read_file:
  78. self.assertFalse(read_file.read().startswith(codecs.BOM_UTF8))
  79. # Clean up
  80. os.unlink(csv_file.name)
  81. class TestPython3Compatibility(TestCase):
  82. def test_models_define_python_3_compatible_representation(self):
  83. """
  84. In Python 2, models can define __unicode__ to get a text representation,
  85. in Python 3 this is achieved by defining __str__. The
  86. python_2_unicode_compatible decorator helps with that. We must use it
  87. every time we define a text representation; this test checks that it's
  88. used correctly.
  89. """
  90. from django.apps import apps
  91. models = [
  92. model for model in apps.get_models() if 'oscar' in repr(model)]
  93. invalid_models = []
  94. for model in models:
  95. # Use abstract model if it exists
  96. if 'oscar' in repr(model.__base__):
  97. model = model.__base__
  98. dict_ = model.__dict__
  99. if '__str__' in dict_:
  100. if six.PY2:
  101. valid = '__unicode__' in dict_
  102. else:
  103. valid = '__unicode__' not in dict_
  104. else:
  105. valid = '__unicode__' not in dict_
  106. if not valid:
  107. invalid_models.append(model)
  108. if invalid_models:
  109. self.fail(
  110. "Those models don't use the python_2_compatible decorator or define __unicode__: %s" % invalid_models)