tests.test_ipalib.test_rpc now imports constants from tests.data

This commit is contained in:
Jason Gerard DeRose 2008-12-08 15:10:01 -07:00
parent 9862cc404b
commit f4f010ae8d

View File

@ -22,26 +22,10 @@ Test the `ipalib.rpc` module.
"""
from xmlrpclib import Binary, dumps, loads
import struct
from tests.util import raises
from tests.data import binary_bytes, utf8_bytes, unicode_str
from ipalib import rpc
# FIXME: These constants should be imported from tests.data
# A string that should have bytes 'x\00' through '\xff':
BINARY_BYTES = ''.join(struct.pack('B', d) for d in xrange(256))
assert '\x00' in BINARY_BYTES and '\xff' in BINARY_BYTES
assert type(BINARY_BYTES) is str and len(BINARY_BYTES) == 256
# A UTF-8 encoded str:
UTF8_BYTES = '\xd0\x9f\xd0\xb0\xd0\xb2\xd0\xb5\xd0\xbb'
# The same UTF-8 data decoded (a unicode instance):
UNICODE_CHARS = u'\u041f\u0430\u0432\u0435\u043b'
assert UTF8_BYTES.decode('UTF-8') == UNICODE_CHARS
assert UNICODE_CHARS.encode('UTF-8') == UTF8_BYTES
def dump_n_load(value):
(param, method) = loads(
@ -65,10 +49,10 @@ def test_round_trip():
"""
# We first test that our assumptions about xmlrpclib module in the Python
# standard library are correct:
assert dump_n_load(UTF8_BYTES) == UNICODE_CHARS
assert dump_n_load(UNICODE_CHARS) == UNICODE_CHARS
assert dump_n_load(Binary(BINARY_BYTES)).data == BINARY_BYTES
assert isinstance(dump_n_load(Binary(BINARY_BYTES)), Binary)
assert dump_n_load(utf8_bytes) == unicode_str
assert dump_n_load(unicode_str) == unicode_str
assert dump_n_load(Binary(binary_bytes)).data == binary_bytes
assert isinstance(dump_n_load(Binary(binary_bytes)), Binary)
assert type(dump_n_load('hello')) is str
assert type(dump_n_load(u'hello')) is str
@ -77,15 +61,15 @@ def test_round_trip():
# xmlrpclib.Binary(). All unicode should come back unicode because str
# explicity get decoded by rpc.xmlrpc_unwrap() if they weren't already
# decoded by xmlrpclib.loads().
assert round_trip(UTF8_BYTES) == UTF8_BYTES
assert round_trip(UNICODE_CHARS) == UNICODE_CHARS
assert round_trip(BINARY_BYTES) == BINARY_BYTES
assert round_trip(utf8_bytes) == utf8_bytes
assert round_trip(unicode_str) == unicode_str
assert round_trip(binary_bytes) == binary_bytes
assert type(round_trip('hello')) is str
assert type(round_trip(u'hello')) is unicode
assert round_trip('') == ''
assert round_trip(u'') == u''
compound = [UTF8_BYTES, UNICODE_CHARS, BINARY_BYTES,
dict(utf8=UTF8_BYTES, chars=UNICODE_CHARS, data=BINARY_BYTES)
compound = [utf8_bytes, unicode_str, binary_bytes,
dict(utf8=utf8_bytes, chars=unicode_str, data=binary_bytes)
]
assert round_trip(compound) == tuple(compound)
@ -113,12 +97,12 @@ def test_xmlrpc_unwrap():
f = rpc.xmlrpc_unwrap
assert f([]) == tuple()
assert f({}) == dict()
value = f(Binary(UTF8_BYTES))
value = f(Binary(utf8_bytes))
assert type(value) is str
assert value == UTF8_BYTES
assert f(UTF8_BYTES) == UNICODE_CHARS
assert f(UNICODE_CHARS) == UNICODE_CHARS
value = f([True, Binary('hello'), dict(one=1, two=UTF8_BYTES, three=None)])
assert value == (True, 'hello', dict(one=1, two=UNICODE_CHARS, three=None))
assert value == utf8_bytes
assert f(utf8_bytes) == unicode_str
assert f(unicode_str) == unicode_str
value = f([True, Binary('hello'), dict(one=1, two=utf8_bytes, three=None)])
assert value == (True, 'hello', dict(one=1, two=unicode_str, three=None))
assert type(value[1]) is str
assert type(value[2]['two']) is unicode