You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compat_tests.py 2.5KB

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