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.2KB

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