test_pagure_lib_encoding_utils.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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, "ASCII")
  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 and above 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. if cchardet:
  40. self.assertEqual(result, "WINDOWS-1250")
  41. else:
  42. self.assertEqual(result, "utf-8")
  43. if chardet.__version__[0] in ("3", "4", "5"):
  44. self.assertEqual(chardet_result["encoding"], "ISO-8859-9")
  45. else:
  46. self.assertEqual(chardet_result["encoding"], "ISO-8859-2")
  47. def test_guess_encoding_no_data(self):
  48. """Test encoding_utils.guess_encoding() with an empty string"""
  49. result = encoding_utils.guess_encoding("".encode("utf-8"))
  50. self.assertEqual(result, "ascii")
  51. class TestGuessEncodings(unittest.TestCase):
  52. def test_guess_encodings(self):
  53. """Test the encoding_utils.guess_encodings() method."""
  54. data = "Šabata".encode("utf-8")
  55. result = encoding_utils.guess_encodings(data)
  56. chardet_result = chardet.detect(data)
  57. if cchardet is not None:
  58. # The last one in the list (which apparently has only one)
  59. self.assertEqual(result[-1].encoding, "WINDOWS-1250")
  60. else:
  61. if chardet.__version__[0] in ("3", "4", "5"):
  62. # The first three have different confidence values
  63. expexted_list = ["utf-8", "ISO-8859-9", "ISO-8859-1"]
  64. # This is the one with the least confidence
  65. print(result)
  66. if chardet.__version__ >= "5.1.0":
  67. self.assertEqual(result[-1].encoding, "TIS-620")
  68. else:
  69. self.assertEqual(result[-1].encoding, "windows-1255")
  70. self.assertListEqual(
  71. [encoding.encoding for encoding in result][:3],
  72. expexted_list,
  73. )
  74. # The values in the middle of the list all have the same confidence
  75. # value and can't be sorted reliably: use sets.
  76. if chardet.__version__ >= "5.1.0":
  77. expected_list = sorted(
  78. [
  79. "utf-8",
  80. "ISO-8859-9",
  81. "ISO-8859-1",
  82. "MacCyrillic",
  83. "IBM866",
  84. "TIS-620",
  85. "EUC-JP",
  86. "EUC-KR",
  87. "GB2312",
  88. "KOI8-R",
  89. "Big5",
  90. "IBM855",
  91. "ISO-8859-7",
  92. "SHIFT_JIS",
  93. "windows-1253",
  94. "CP949",
  95. "EUC-TW",
  96. "ISO-8859-5",
  97. "windows-1251",
  98. "windows-1255",
  99. "Johab", # Added in 5.0.0
  100. "MacRoman", # Added in 5.1.0
  101. ]
  102. )
  103. else:
  104. expected_list = sorted(
  105. [
  106. "utf-8",
  107. "ISO-8859-9",
  108. "ISO-8859-1",
  109. "MacCyrillic",
  110. "IBM866",
  111. "TIS-620",
  112. "EUC-JP",
  113. "EUC-KR",
  114. "GB2312",
  115. "KOI8-R",
  116. "Big5",
  117. "IBM855",
  118. "ISO-8859-7",
  119. "SHIFT_JIS",
  120. "windows-1253",
  121. "CP949",
  122. "EUC-TW",
  123. "ISO-8859-5",
  124. "windows-1251",
  125. "windows-1255",
  126. ]
  127. )
  128. self.assertListEqual(
  129. sorted(set([encoding.encoding for encoding in result])),
  130. expected_list,
  131. )
  132. self.assertEqual(chardet_result["encoding"], "ISO-8859-9")
  133. else:
  134. self.assertListEqual(
  135. [encoding.encoding for encoding in result],
  136. ["utf-8", "ISO-8859-2", "windows-1252"],
  137. )
  138. self.assertEqual(chardet_result["encoding"], "ISO-8859-2")
  139. def test_guess_encodings_no_data(self):
  140. """Test encoding_utils.guess_encodings() with an emtpy string"""
  141. result = encoding_utils.guess_encodings("".encode("utf-8"))
  142. self.assertEqual([encoding.encoding for encoding in result], ["ascii"])
  143. class TestDecode(unittest.TestCase):
  144. def test_decode(self):
  145. """Test encoding_utils.decode()"""
  146. data = (
  147. "This is a little longer text for testing Šabata's encoding. "
  148. "With more characters, let's see if it become more clear as to what "
  149. "encoding should be used for this. We'll include from french words "
  150. "in there for non-ascii: français, gagné!"
  151. )
  152. self.assertEqual(data, encoding_utils.decode(data.encode("utf-8")))
  153. if __name__ == "__main__":
  154. unittest.main(verbosity=2)