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

compat_tests.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. from django.utils import six
  4. from django.utils.six.moves import cStringIO
  5. from django.test import TestCase
  6. from oscar.core.compat import UnicodeCSVWriter, existing_user_fields
  7. class TestExistingUserFields(TestCase):
  8. def test_order(self):
  9. fields = existing_user_fields(['email', 'first_name', 'last_name'])
  10. self.assertEqual(fields, ['email', 'first_name', 'last_name'])
  11. class TestUnicodeCSVWriter(TestCase):
  12. def test_can_write_different_values(self):
  13. writer = UnicodeCSVWriter(open_file=cStringIO())
  14. s = u'ünįcodē'
  15. class unicodeobj(object):
  16. def __str__(self):
  17. return s
  18. def __unicode__(self):
  19. return s
  20. rows = [[s, unicodeobj(), 123, datetime.date.today()], ]
  21. writer.writerows(rows)
  22. self.assertRaises(TypeError, writer.writerows, [object()])
  23. class TestPython3Compatibility(TestCase):
  24. def test_models_define_python_3_compatible_representation(self):
  25. """
  26. In Python 2, models can define __unicode__ to get a text representation,
  27. in Python 3 this is achieved by defining __str__. The
  28. python_2_unicode_compatible decorator helps with that. We must use it
  29. every time we define a text representation; this test checks that it's
  30. used correctly.
  31. """
  32. from django.apps import apps
  33. models = [
  34. model for model in apps.get_models() if 'oscar' in repr(model)]
  35. invalid_models = []
  36. for model in models:
  37. # Use abstract model if it exists
  38. if 'oscar' in repr(model.__base__):
  39. model = model.__base__
  40. dict_ = model.__dict__
  41. if '__str__' in dict_:
  42. if six.PY2:
  43. str_method_module = dict_['__str__'].__module__
  44. valid = ('django.utils.encoding' == str_method_module and
  45. '__unicode__' in dict_)
  46. else:
  47. valid = '__unicode__' not in dict_
  48. else:
  49. valid = '__unicode__' not in dict_
  50. if not valid:
  51. invalid_models.append(model)
  52. if invalid_models:
  53. self.fail(
  54. "Those models don't use the python_2_compatible decorator or define __unicode__: %s" % invalid_models)