-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
123 lines (97 loc) · 4.48 KB
/
tests.py
File metadata and controls
123 lines (97 loc) · 4.48 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from unittest2 import TestCase
from methods import encode_dictionary, convert_to_unicode
class WhenEncodingDictionaries(TestCase):
def test_should_require_arguments(self):
self.assertRaises(TypeError, encode_dictionary)
def test_should_argument_be_a_dictionary(self):
self.assertRaises(TypeError, encode_dictionary, '')
def test_should_return_new_dictionary(self):
params = {'key': 'value'}
result = encode_dictionary(params)
self.assertTrue(isinstance(result, dict))
self.assertTrue(result is not params)
def test_should_return_new_dictionary_with_unicode_values(self):
params = {'key': 'value'}
result = encode_dictionary(params)
self.assertDictEqual(result, {'key': u'value'})
def test_should_find_charset_in_params(self):
default_data = {
'txn_type': 'express_checkout',
'payment_status': 'Completed',
'first_name': 'Bob',
'last_name': 'Smith',
'payer_email': 'bob@example.com',
'item_name1': 'not-a-rule',
'item_number': 123,
'charset': 'ascii'
}
result = encode_dictionary(default_data)
assert result['charset'] == u'ascii'
def test_return_dictionary_values_converted_to_unicode(self):
default_data = {
'txn_type': 'express_checkout',
'payment_status': 'Completed',
'first_name': 'Bob',
'last_name': 'Smith',
'payer_email': 'bob@example.com',
'item_name1': 'not-a-rule',
'item_number': 123,
'charset': 'ascii'
}
result = encode_dictionary(default_data)
for key, val in result.items():
if not isinstance(val, basestring):
continue
self.assertIsInstance(val, unicode)
class WhenEncodingStrings(TestCase):
def test_raise_err_if_original_parm_is_None(self):
self.assertRaises(TypeError, convert_to_unicode, None)
def test_raise_err_if_original_param_is_not_string(self):
self.assertRaises(TypeError, convert_to_unicode, 123)
self.assertRaises(TypeError, convert_to_unicode, dict())
self.assertRaises(TypeError, convert_to_unicode, list())
def test_return_unicode_if_original_param_is_unicode(self):
mystring = u'This is a unicode string'
result = convert_to_unicode(mystring, 'utf8')
self.assertIs(result, mystring)
def test_convert_ascii_to_unicode(self):
result = convert_to_unicode('This is an ascii string', 'ascii')
self.assertEqual(result, u'This is an ascii string')
def test_convert_capital_thorn_in_latin1_to_unicode(self):
# DE is capital Thorn in latin1
result = convert_to_unicode('\xde', 'latin1')
self.assertEqual(result, u'\u00de')
def test_convert_capital_thorn_in_cp850_to_unicode(self):
# E8 is capital Thorn in cp850
result = convert_to_unicode('\xe8', 'cp850')
self.assertEqual(result, u'\u00de')
def test_convert_utf8_chars_to_unicode(self):
# C3 9E: capital Thorn in UTF-8
result = convert_to_unicode('\xc3\x9e', 'utf-8')
self.assertEqual(result, u'\u00de')
def test_default_to_utf8_if_charset_omitted(self):
# C3 9E: capital Thorn in UTF-8
result = convert_to_unicode('\xc3\x9e')
self.assertEqual(result, u'\u00de')
def test_default_to_utf8_if_charset_is_none(self):
# C3 9E: capital Thorn in UTF-8
result = convert_to_unicode('\xc3\x9e', None)
self.assertEqual(result, u'\u00de')
def test_default_to_utf8_if_charset_is_empty_string(self):
# C3 9E: capital Thorn in UTF-8
result = convert_to_unicode('\xc3\x9e', '')
self.assertEqual(result, u'\u00de')
def test_return_utf8_if_unknown_charset(self):
# C3 9E: capital Thorn in UTF-8
result = convert_to_unicode('\xc3\x9e', 'DoesNotExist')
self.assertEqual(result, u'\u00de')
def test_replace_chars_not_in_charset_with_replacement_char(self):
# E0 is not valid in the ascii character set
result = convert_to_unicode('before \xe0 after', 'ascii')
assert result == u'before \ufffd after'
def test_replace_chars_when_assuming_utf8_with_replacement_char(self):
# C0 00 is not valid in UTF-8
#result = convert_to_unicode('before \xe0 after')
result = convert_to_unicode('before \xc0\x00 after')
print result
assert result == u'before \ufffd after'