-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
56 lines (44 loc) · 2.03 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# coding: utf8
from __future__ import unicode_literals
import os
import tempfile
import shutil
from unittest import TestCase
class ConvertEncodingTestCase(TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.convert_encoding = __import__("convert-encoding")
def tearDown(self):
shutil.rmtree(self.tempdir, ignore_errors=True)
def assertEncodedTextEqual(self, input_text, input_encoding, output_text, output_encoding):
tmp_file = tempfile.NamedTemporaryFile(dir=self.tempdir, suffix='.txt', delete=False)
new_file_name = None
try:
with tmp_file:
tmp_file.write(input_text)
success = self.convert_encoding.convert_to(tmp_file.name,
input_encoding=input_encoding,
output_encoding=output_encoding)
new_file_name = tmp_file.name[:-4] + '.' + output_encoding + '.txt'
self.assertTrue(success)
self.assertTrue(os.path.isfile(new_file_name))
with open(new_file_name, 'rb') as new_file:
self.assertEqual(new_file.read(), output_text)
finally:
os.unlink(tmp_file.name)
if new_file_name is not None:
os.unlink(new_file_name)
def test_convert_utf8_to_windows1251(self):
test_text = '\u201cТест\u201d\u2122\u2026'
self.assertEncodedTextEqual(
test_text.encode('utf8'), 'utf8',
test_text.encode('windows-1251'), 'windows-1251')
def test_convert_windows_1251_to_iso_8859_5_with_fallback(self):
input_text = '\u201cТест\u201d\u2122\u2026'.encode('windows-1251')
# iso-8859-5 dows not have some special characters available in
# windows 1251 and for that reason we use fallback
expected_text = '"Тест"TM...'.encode('iso-8859-5')
self.assertEncodedTextEqual(
input_text, 'windows-1251',
expected_text, 'iso-8859-5',
)