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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# encoding: utf-8
# Authors:
# Pavel Zuna <pzuna@redhat.com>
#
# Copyright (C) 2009 Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
Test the `ipalib.encoder` module.
"""
import string
from tests.util import ClassChecker, assert_equal
from ipalib.encoder import Encoder, EncoderSettings, encode_args, decode_retval
_encoder_settings = EncoderSettings()
_test_str_d = u'ěščřžýáíé'
_test_str_e = u'ěščřžýáíé'.encode(_encoder_settings.encode_to)
class test_Encoder(ClassChecker):
"""
Test the `ipalib.encoder.Encoder` class.
"""
_cls = Encoder
def test_encode(self):
"""
Test the `ipalib.encoder.Encoder.encode` method.
"""
o = self.cls()
encode_to = o.encoder_settings.encode_to
o.encoder_settings.encode_postprocessor = lambda x: x
# strings
assert_equal(o.encode('ahoj'), 'ahoj'.encode(encode_to))
assert_equal(o.encode(_test_str_d), _test_str_e)
# bool, float, int, long
assert_equal(o.encode(True), str(True).encode(encode_to))
assert_equal(o.encode(1.01), str(1.01).encode(encode_to))
assert_equal(o.encode(1000), str(1000).encode(encode_to))
assert_equal(o.encode(long(1)), str(long(1)).encode(encode_to))
# lists
expected = [_test_str_e, '1']
assert_equal(o.encode([_test_str_d, 1]), expected)
expected = ['1', ['1', '2', '3']]
assert_equal(o.encode([1, [1, 2, 3]]), expected)
# tuples
expected = (_test_str_e, '1')
assert_equal(o.encode((_test_str_d, 1)), expected)
expected = ('1', ('1', '2', '3'))
assert_equal(o.encode((1, (1, 2, 3))), expected)
# dicts: only values, no postprocessing
o.encoder_settings.encode_dict_keys = False
o.encoder_settings.encode_dict_keys_postprocess = False
o.encoder_settings.encode_dict_vals = True
o.encoder_settings.encode_dict_vals_postprocess = False
expected = {_test_str_d: _test_str_e}
assert_equal(o.encode({_test_str_d: _test_str_d}), expected)
# dicts: only keys, no postprocessing
o.encoder_settings.encode_dict_keys = True
o.encoder_settings.encode_dict_vals = False
expected = {_test_str_e: _test_str_d}
assert_equal(o.encode({_test_str_d: _test_str_d}), expected)
# dicts: both keys and values, no postprocessing
o.encoder_settings.encode_dict_vals = True
expected = {_test_str_e: _test_str_e}
assert_equal(o.encode({_test_str_d: _test_str_d}), expected)
# dicts: both keys and values, postprocessing on keys only
o.encoder_settings.encode_dict_keys = True
o.encoder_settings.encode_dict_keys_postprocess = True
o.encoder_settings.encode_postprocessor = string.upper
expected = {_test_str_e.upper(): _test_str_e}
assert_equal(o.encode({u'ěščřžýáíé': u'ěščřžýáíé'}), expected)
# None
o.encoder_settings.encode_postprocessor = lambda x: x
o.encoder_settings.encode_none = False
assert_equal(o.encode(None), None)
o.encoder_settings.encode_none = True
assert_equal(o.encode(None), str(None).encode(encode_to))
def test_decode(self):
"""
Test the `ipalib.encoder.Encoder.decode` method.
"""
o = self.cls()
decode_from = o.encoder_settings.decode_from
o.encoder_settings.decode_postprocessor = lambda x: x
# strings
assert_equal(o.decode('ahoj'), 'ahoj'.decode(decode_from))
assert_equal(o.decode(_test_str_e), _test_str_d)
# bool, float, int, long
assert_equal(o.decode('True'), str(True).decode(decode_from))
assert_equal(o.decode('1.01'), str(1.01).decode(decode_from))
assert_equal(o.decode('1000'), str(1000).decode(decode_from))
assert_equal(o.decode('1'), str(long(1)).decode(decode_from))
# lists
expected = [_test_str_d, '1']
assert_equal(o.decode([_test_str_e, '1']), expected)
expected = [u'1', [u'1', u'2', u'3']]
assert_equal(o.decode(['1', ['1', '2', '3']]), expected)
# tuples
expected = (_test_str_d, 1)
assert_equal(o.decode((_test_str_e, 1)), expected)
expected = (u'1', (u'1', u'2', u'3'))
assert_equal(o.decode(('1', ('1', '2', '3'))), expected)
# dicts: only values, no postprocessing
o.encoder_settings.decode_dict_keys = False
o.encoder_settings.decode_dict_keys_postprocess = False
o.encoder_settings.decode_dict_vals = True
o.encoder_settings.decode_dict_vals_postprocess = False
expected = {_test_str_e: _test_str_d}
assert_equal(o.decode({_test_str_e: _test_str_e}), expected)
# dicts: only keys, no postprocessing
o.encoder_settings.decode_dict_keys = True
o.encoder_settings.decode_dict_vals = False
expected = {_test_str_d: _test_str_e}
assert_equal(o.decode({_test_str_e: _test_str_e}), expected)
# dicts: both keys and values, no postprocessing
o.encoder_settings.decode_dict_vals = True
expected = {_test_str_d: _test_str_d}
assert_equal(o.decode({_test_str_e: _test_str_e}), expected)
# dicts: both keys and values, postprocessing on keys only
o.encoder_settings.decode_dict_keys = True
o.encoder_settings.decode_dict_keys_postprocess = True
o.encoder_settings.decode_postprocessor = string.upper
expected = {_test_str_d.upper(): _test_str_d}
assert_equal(o.decode({_test_str_e: _test_str_e}), expected)
# TODO: dict decoding using a table
# None
o.encoder_settings.decode_postprocessor = lambda x: x
o.encoder_settings.decode_none = False
assert_equal(o.decode(None), None)
o.encoder_settings.decode_none = True
assert_equal(o.decode(None), str(None).decode(decode_from))
|