test_pagure_lib_encoding_utils.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for :module:`pagure.lib.encoding_utils`.
  4. """
  5. from __future__ import unicode_literals, absolute_import
  6. import os
  7. import unittest
  8. import sys
  9. cchardet = None
  10. try:
  11. import cchardet
  12. except ImportError:
  13. pass
  14. import chardet
  15. sys.path.insert(
  16. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  17. )
  18. from pagure.lib import encoding_utils
  19. class TestGuessEncoding(unittest.TestCase):
  20. def test_guess_encoding_ascii(self):
  21. """
  22. Assert when ascii-only data is provided ascii is the guessed encoding.
  23. """
  24. data = "Twas bryllyg, and the slythy toves did gyre and gymble"
  25. result = encoding_utils.guess_encoding(data.encode("ascii"))
  26. if cchardet is not None:
  27. self.assertEqual(result, "utf-8")
  28. else:
  29. self.assertEqual(result, "ascii")
  30. def test_guess_encoding_favor_utf_8(self):
  31. """
  32. Test that strings that could be UTF-8 or ISO-8859-* result in UTF-8.
  33. python-chardet-3.0.4-2.fc27.noarch detects it as ISO-8859-9
  34. python-chardet-2.2.1-1.el7_1.noarch detects it as ISO-8859-2
  35. """
  36. data = "Šabata".encode("utf-8")
  37. result = encoding_utils.guess_encoding(data)
  38. chardet_result = chardet.detect(data)
  39. self.assertEqual(result, "utf-8")
  40. if chardet.__version__[0] == "3":
  41. self.assertEqual(chardet_result["encoding"], "ISO-8859-9")
  42. else:
  43. self.assertEqual(chardet_result["encoding"], "ISO-8859-2")
  44. def test_guess_encoding_no_data(self):
  45. """ Test encoding_utils.guess_encoding() with an empty string """
  46. result = encoding_utils.guess_encoding("".encode("utf-8"))
  47. self.assertEqual(result, "ascii")
  48. class TestGuessEncodings(unittest.TestCase):
  49. def test_guess_encodings(self):
  50. """ Test the encoding_utils.guess_encodings() method. """
  51. data = "Šabata".encode("utf-8")
  52. result = encoding_utils.guess_encodings(data)
  53. chardet_result = chardet.detect(data)
  54. if chardet.__version__[0] == "3":
  55. # The first three have different confidence values
  56. if cchardet is not None:
  57. expexted_list = ["utf-8"]
  58. # The last one in the list (which apparently has only one)
  59. self.assertEqual(result[-1].encoding, "utf-8")
  60. else:
  61. expexted_list = ["utf-8", "ISO-8859-9", "ISO-8859-1"]
  62. # This is the one with the least confidence
  63. self.assertEqual(result[-1].encoding, "windows-1255")
  64. self.assertListEqual(
  65. [encoding.encoding for encoding in result][:3], expexted_list
  66. )
  67. # The values in the middle of the list all have the same confidence
  68. # value and can't be sorted reliably: use sets.
  69. if cchardet is not None:
  70. expected_list = sorted(["utf-8"])
  71. else:
  72. expected_list = sorted(
  73. [
  74. "utf-8",
  75. "ISO-8859-9",
  76. "ISO-8859-1",
  77. "MacCyrillic",
  78. "IBM866",
  79. "TIS-620",
  80. "EUC-JP",
  81. "EUC-KR",
  82. "GB2312",
  83. "KOI8-R",
  84. "Big5",
  85. "IBM855",
  86. "ISO-8859-7",
  87. "SHIFT_JIS",
  88. "windows-1253",
  89. "CP949",
  90. "EUC-TW",
  91. "ISO-8859-5",
  92. "windows-1251",
  93. "windows-1255",
  94. ]
  95. )
  96. self.assertListEqual(
  97. sorted(set([encoding.encoding for encoding in result])),
  98. expected_list,
  99. )
  100. self.assertEqual(chardet_result["encoding"], "ISO-8859-9")
  101. else:
  102. self.assertListEqual(
  103. [encoding.encoding for encoding in result],
  104. ["utf-8", "ISO-8859-2", "windows-1252"],
  105. )
  106. self.assertEqual(chardet_result["encoding"], "ISO-8859-2")
  107. def test_guess_encodings_no_data(self):
  108. """ Test encoding_utils.guess_encodings() with an emtpy string """
  109. result = encoding_utils.guess_encodings("".encode("utf-8"))
  110. self.assertEqual([encoding.encoding for encoding in result], ["ascii"])
  111. class TestDecode(unittest.TestCase):
  112. def test_decode(self):
  113. """ Test encoding_utils.decode() """
  114. data = "Šabata"
  115. self.assertEqual(data, encoding_utils.decode(data.encode("utf-8")))
  116. if __name__ == "__main__":
  117. unittest.main(verbosity=2)