Development of an internal social media platform with personalised dashboards for students
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.

test_encoding.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. from __future__ import unicode_literals
  4. import sys
  5. from contextlib import contextmanager
  6. from kombu.five import bytes_t, string_t
  7. from kombu.utils.encoding import safe_str, default_encoding
  8. from kombu.tests.case import Case, SkipTest, patch
  9. @contextmanager
  10. def clean_encoding():
  11. old_encoding = sys.modules.pop('kombu.utils.encoding', None)
  12. import kombu.utils.encoding
  13. try:
  14. yield kombu.utils.encoding
  15. finally:
  16. if old_encoding:
  17. sys.modules['kombu.utils.encoding'] = old_encoding
  18. class test_default_encoding(Case):
  19. @patch('sys.getfilesystemencoding')
  20. def test_default(self, getdefaultencoding):
  21. getdefaultencoding.return_value = 'ascii'
  22. with clean_encoding() as encoding:
  23. enc = encoding.default_encoding()
  24. if sys.platform.startswith('java'):
  25. self.assertEqual(enc, 'utf-8')
  26. else:
  27. self.assertEqual(enc, 'ascii')
  28. getdefaultencoding.assert_called_with()
  29. class test_encoding_utils(Case):
  30. def setUp(self):
  31. if sys.version_info >= (3, 0):
  32. raise SkipTest('not relevant on py3k')
  33. def test_str_to_bytes(self):
  34. with clean_encoding() as e:
  35. self.assertIsInstance(e.str_to_bytes('foobar'), bytes_t)
  36. def test_from_utf8(self):
  37. with clean_encoding() as e:
  38. self.assertIsInstance(e.from_utf8('foobar'), bytes_t)
  39. def test_default_encode(self):
  40. with clean_encoding() as e:
  41. self.assertTrue(e.default_encode(b'foo'))
  42. class test_safe_str(Case):
  43. def setUp(self):
  44. self._cencoding = patch('sys.getfilesystemencoding')
  45. self._encoding = self._cencoding.__enter__()
  46. self._encoding.return_value = 'ascii'
  47. def tearDown(self):
  48. self._cencoding.__exit__()
  49. def test_when_bytes(self):
  50. self.assertEqual(safe_str('foo'), 'foo')
  51. def test_when_unicode(self):
  52. self.assertIsInstance(safe_str('foo'), string_t)
  53. def test_when_encoding_utf8(self):
  54. with patch('sys.getfilesystemencoding') as encoding:
  55. encoding.return_value = 'utf-8'
  56. self.assertEqual(default_encoding(), 'utf-8')
  57. s = 'The quiæk fåx jømps øver the lazy dåg'
  58. res = safe_str(s)
  59. self.assertIsInstance(res, str)
  60. def test_when_containing_high_chars(self):
  61. with patch('sys.getfilesystemencoding') as encoding:
  62. encoding.return_value = 'ascii'
  63. s = 'The quiæk fåx jømps øver the lazy dåg'
  64. res = safe_str(s)
  65. self.assertIsInstance(res, str)
  66. self.assertEqual(len(s), len(res))
  67. def test_when_not_string(self):
  68. o = object()
  69. self.assertEqual(safe_str(o), repr(o))
  70. def test_when_unrepresentable(self):
  71. class O(object):
  72. def __repr__(self):
  73. raise KeyError('foo')
  74. self.assertIn('<Unrepresentable', safe_str(O()))