test_pagure_lib_encoding_utils.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 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"):
  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"):
  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. self.assertEqual(result[-1].encoding, "windows-1255")
  66. self.assertListEqual(
  67. [encoding.encoding for encoding in result][:3],
  68. expexted_list,
  69. )
  70. # The values in the middle of the list all have the same confidence
  71. # value and can't be sorted reliably: use sets.
  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 = (
  115. "This is a little longer text for testing Šabata's encoding. "
  116. "With more characters, let's see if it become more clear as to what "
  117. "encoding should be used for this. We'll include from french words "
  118. "in there for non-ascii: français, gagné!"
  119. )
  120. self.assertEqual(data, encoding_utils.decode(data.encode("utf-8")))
  121. if __name__ == "__main__":
  122. unittest.main(verbosity=2)