mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-19 20:03:16 +00:00
Use py.test instead of nosetests.
This commit is contained in:
parent
d57e369258
commit
593c832ab0
10 changed files with 77 additions and 134 deletions
|
@ -1,16 +1,13 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
from nose import main
|
||||
from nose.tools import *
|
||||
from msgpack import packb, unpackb
|
||||
|
||||
|
||||
def test_unpack_buffer():
|
||||
from array import array
|
||||
buf = array('b')
|
||||
buf.fromstring(packb(('foo', 'bar')))
|
||||
obj = unpackb(buf, use_list=1)
|
||||
assert_equal([b'foo', b'bar'], obj)
|
||||
assert [b'foo', b'bar'] == obj
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
from nose import main
|
||||
from nose.tools import *
|
||||
from msgpack import packb, unpackb
|
||||
|
||||
|
||||
def check(length, obj):
|
||||
v = packb(obj)
|
||||
assert_equal(len(v), length, "%r length should be %r but get %r" % (obj, length, len(v)))
|
||||
assert_equal(unpackb(v, use_list=0), obj)
|
||||
assert len(v) == length, \
|
||||
"%r length should be %r but get %r" % (obj, length, len(v))
|
||||
assert unpackb(v, use_list=0) == obj
|
||||
|
||||
def test_1():
|
||||
for o in [None, True, False, 0, 1, (1 << 6), (1 << 7) - 1, -1,
|
||||
|
@ -70,8 +69,8 @@ def test_array32():
|
|||
|
||||
|
||||
def match(obj, buf):
|
||||
assert_equal(packb(obj), buf)
|
||||
assert_equal(unpackb(buf, use_list=0), obj)
|
||||
assert packb(obj) == buf
|
||||
assert unpackb(buf, use_list=0) == obj
|
||||
|
||||
def test_match():
|
||||
cases = [
|
||||
|
@ -99,7 +98,5 @@ def test_match():
|
|||
match(v, p)
|
||||
|
||||
def test_unicode():
|
||||
assert_equal(b'foobar', unpackb(packb('foobar'), use_list=1))
|
||||
assert unpackb(packb('foobar'), use_list=1) == b'foobar'
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
from nose.tools import *
|
||||
from pytest import raises
|
||||
from msgpack import packb, unpackb
|
||||
|
||||
import datetime
|
||||
|
@ -12,28 +12,20 @@ class DummyException(Exception):
|
|||
|
||||
|
||||
def test_raise_on_find_unsupported_value():
|
||||
assert_raises(TypeError, packb, datetime.datetime.now())
|
||||
with raises(TypeError):
|
||||
packb(datetime.datetime.now())
|
||||
|
||||
|
||||
def test_raise_from_object_hook():
|
||||
def hook(obj):
|
||||
raise DummyException
|
||||
assert_raises(DummyException, unpackb, packb({}), object_hook=hook)
|
||||
assert_raises(DummyException, unpackb, packb({'fizz': 'buzz'}),
|
||||
object_hook=hook)
|
||||
assert_raises(DummyException, unpackb, packb({'fizz': 'buzz'}),
|
||||
object_pairs_hook=hook)
|
||||
assert_raises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}),
|
||||
object_hook=hook)
|
||||
assert_raises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}),
|
||||
object_pairs_hook=hook)
|
||||
raises(DummyException, unpackb, packb({}), object_hook=hook)
|
||||
raises(DummyException, unpackb, packb({'fizz': 'buzz'}), object_hook=hook)
|
||||
raises(DummyException, unpackb, packb({'fizz': 'buzz'}), object_pairs_hook=hook)
|
||||
raises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}), object_hook=hook)
|
||||
raises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}), object_pairs_hook=hook)
|
||||
|
||||
|
||||
@raises(ValueError)
|
||||
def test_invalidvalue():
|
||||
unpackb(b'\xd9\x97#DL_')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from nose import main
|
||||
main()
|
||||
with raises(ValueError):
|
||||
unpackb(b'\xd9\x97#DL_')
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
from nose import main
|
||||
from nose.tools import *
|
||||
from msgpack import unpackb
|
||||
|
||||
def check(src, should, use_list=0):
|
||||
assert_equal(unpackb(src, use_list=use_list), should)
|
||||
assert unpackb(src, use_list=use_list) == should
|
||||
|
||||
def testSimpleValue():
|
||||
check(b"\x93\xc0\xc2\xc3",
|
||||
|
@ -70,6 +68,3 @@ def testMap():
|
|||
b"\xdf\x00\x00\x00\x02\xc0\xc2\xc3\xc2",
|
||||
({}, {None: False}, {True: False, None: False}, {},
|
||||
{None: False}, {True: False, None: False}))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
from nose import main
|
||||
from nose.tools import *
|
||||
|
||||
from pytest import raises
|
||||
from msgpack import packb, unpackb
|
||||
|
||||
def _decode_complex(obj):
|
||||
|
@ -19,27 +17,27 @@ def _encode_complex(obj):
|
|||
def test_encode_hook():
|
||||
packed = packb([3, 1+2j], default=_encode_complex)
|
||||
unpacked = unpackb(packed, use_list=1)
|
||||
eq_(unpacked[1], {b'__complex__': True, b'real': 1, b'imag': 2})
|
||||
assert unpacked[1] == {b'__complex__': True, b'real': 1, b'imag': 2}
|
||||
|
||||
def test_decode_hook():
|
||||
packed = packb([3, {b'__complex__': True, b'real': 1, b'imag': 2}])
|
||||
unpacked = unpackb(packed, object_hook=_decode_complex, use_list=1)
|
||||
eq_(unpacked[1], 1+2j)
|
||||
assert unpacked[1] == 1+2j
|
||||
|
||||
def test_decode_pairs_hook():
|
||||
packed = packb([3, {1: 2, 3: 4}])
|
||||
prod_sum = 1 * 2 + 3 * 4
|
||||
unpacked = unpackb(packed, object_pairs_hook=lambda l: sum(k * v for k, v in l), use_list=1)
|
||||
eq_(unpacked[1], prod_sum)
|
||||
assert unpacked[1] == prod_sum
|
||||
|
||||
@raises(ValueError)
|
||||
def test_only_one_obj_hook():
|
||||
unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x)
|
||||
with raises(ValueError):
|
||||
unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x)
|
||||
|
||||
@raises(ValueError)
|
||||
def test_bad_hook():
|
||||
packed = packb([3, 1+2j], default=lambda o: o)
|
||||
unpacked = unpackb(packed, use_list=1)
|
||||
with raises(ValueError):
|
||||
packed = packb([3, 1+2j], default=lambda o: o)
|
||||
unpacked = unpackb(packed, use_list=1)
|
||||
|
||||
def _arr_to_str(arr):
|
||||
return ''.join(str(c) for c in arr)
|
||||
|
@ -47,7 +45,7 @@ def _arr_to_str(arr):
|
|||
def test_array_hook():
|
||||
packed = packb([1,2,3])
|
||||
unpacked = unpackb(packed, list_hook=_arr_to_str, use_list=1)
|
||||
eq_(unpacked, '123')
|
||||
assert unpacked == '123'
|
||||
|
||||
|
||||
class DecodeError(Exception):
|
||||
|
@ -57,18 +55,13 @@ def bad_complex_decoder(o):
|
|||
raise DecodeError("Ooops!")
|
||||
|
||||
|
||||
@raises(DecodeError)
|
||||
def test_an_exception_in_objecthook1():
|
||||
packed = packb({1: {'__complex__': True, 'real': 1, 'imag': 2}})
|
||||
unpackb(packed, object_hook=bad_complex_decoder)
|
||||
with raises(DecodeError):
|
||||
packed = packb({1: {'__complex__': True, 'real': 1, 'imag': 2}})
|
||||
unpackb(packed, object_hook=bad_complex_decoder)
|
||||
|
||||
|
||||
@raises(DecodeError)
|
||||
def test_an_exception_in_objecthook2():
|
||||
packed = packb({1: [{'__complex__': True, 'real': 1, 'imag': 2}]})
|
||||
unpackb(packed, list_hook=bad_complex_decoder, use_list=1)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
with raises(DecodeError):
|
||||
packed = packb({1: [{'__complex__': True, 'real': 1, 'imag': 2}]})
|
||||
unpackb(packed, list_hook=bad_complex_decoder, use_list=1)
|
||||
|
|
|
@ -3,9 +3,7 @@
|
|||
|
||||
import six
|
||||
import struct
|
||||
from nose import main
|
||||
from nose.tools import *
|
||||
from nose.plugins.skip import SkipTest
|
||||
from pytest import raises, xfail
|
||||
|
||||
from msgpack import packb, unpackb, Unpacker, Packer
|
||||
|
||||
|
@ -13,7 +11,7 @@ from io import BytesIO
|
|||
|
||||
def check(data, use_list=False):
|
||||
re = unpackb(packb(data), use_list=use_list)
|
||||
assert_equal(re, data)
|
||||
assert re == data
|
||||
|
||||
def testPack():
|
||||
test_data = [
|
||||
|
@ -35,11 +33,11 @@ def testPackUnicode():
|
|||
]
|
||||
for td in test_data:
|
||||
re = unpackb(packb(td, encoding='utf-8'), use_list=1, encoding='utf-8')
|
||||
assert_equal(re, td)
|
||||
assert re == td
|
||||
packer = Packer(encoding='utf-8')
|
||||
data = packer.pack(td)
|
||||
re = Unpacker(BytesIO(data), encoding='utf-8', use_list=1).unpack()
|
||||
assert_equal(re, td)
|
||||
assert re == td
|
||||
|
||||
def testPackUTF32():
|
||||
try:
|
||||
|
@ -51,9 +49,9 @@ def testPackUTF32():
|
|||
]
|
||||
for td in test_data:
|
||||
re = unpackb(packb(td, encoding='utf-32'), use_list=1, encoding='utf-32')
|
||||
assert_equal(re, td)
|
||||
except LookupError:
|
||||
raise SkipTest
|
||||
assert re == td
|
||||
except LookupError as e:
|
||||
xfail(e)
|
||||
|
||||
def testPackBytes():
|
||||
test_data = [
|
||||
|
@ -64,31 +62,31 @@ def testPackBytes():
|
|||
|
||||
def testIgnoreUnicodeErrors():
|
||||
re = unpackb(packb(b'abc\xeddef'), encoding='utf-8', unicode_errors='ignore', use_list=1)
|
||||
assert_equal(re, "abcdef")
|
||||
assert re == "abcdef"
|
||||
|
||||
@raises(UnicodeDecodeError)
|
||||
def testStrictUnicodeUnpack():
|
||||
unpackb(packb(b'abc\xeddef'), encoding='utf-8', use_list=1)
|
||||
with raises(UnicodeDecodeError):
|
||||
unpackb(packb(b'abc\xeddef'), encoding='utf-8', use_list=1)
|
||||
|
||||
@raises(UnicodeEncodeError)
|
||||
def testStrictUnicodePack():
|
||||
packb(six.u("abc\xeddef"), encoding='ascii', unicode_errors='strict')
|
||||
with raises(UnicodeEncodeError):
|
||||
packb(six.u("abc\xeddef"), encoding='ascii', unicode_errors='strict')
|
||||
|
||||
def testIgnoreErrorsPack():
|
||||
re = unpackb(packb(six.u("abcФФФdef"), encoding='ascii', unicode_errors='ignore'), encoding='utf-8', use_list=1)
|
||||
assert_equal(re, six.u("abcdef"))
|
||||
assert re == six.u("abcdef")
|
||||
|
||||
@raises(TypeError)
|
||||
def testNoEncoding():
|
||||
packb(six.u("abc"), encoding=None)
|
||||
with raises(TypeError):
|
||||
packb(six.u("abc"), encoding=None)
|
||||
|
||||
def testDecodeBinary():
|
||||
re = unpackb(packb("abc"), encoding=None, use_list=1)
|
||||
assert_equal(re, b"abc")
|
||||
assert re == b"abc"
|
||||
|
||||
def testPackFloat():
|
||||
assert_equal(packb(1.0, use_single_float=True), b'\xca' + struct.pack('>f', 1.0))
|
||||
assert_equal(packb(1.0, use_single_float=False), b'\xcb' + struct.pack('>d', 1.0))
|
||||
assert packb(1.0, use_single_float=True) == b'\xca' + struct.pack('>f', 1.0)
|
||||
assert packb(1.0, use_single_float=False) == b'\xcb' + struct.pack('>d', 1.0)
|
||||
|
||||
def testArraySize(sizes=[0, 5, 50, 1000]):
|
||||
bio = six.BytesIO()
|
||||
|
@ -151,10 +149,10 @@ class odict(dict):
|
|||
def test_odict():
|
||||
seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)]
|
||||
od = odict(seq)
|
||||
assert_equal(unpackb(packb(od), use_list=1), dict(seq))
|
||||
assert unpackb(packb(od), use_list=1) == dict(seq)
|
||||
def pair_hook(seq):
|
||||
return seq
|
||||
assert_equal(unpackb(packb(od), object_pairs_hook=pair_hook, use_list=1), seq)
|
||||
assert unpackb(packb(od), object_pairs_hook=pair_hook, use_list=1) == seq
|
||||
|
||||
|
||||
def test_pairlist():
|
||||
|
@ -163,8 +161,3 @@ def test_pairlist():
|
|||
packed = packer.pack_map_pairs(pairlist)
|
||||
unpacked = unpackb(packed, object_pairs_hook=list)
|
||||
assert pairlist == unpacked
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
|
@ -2,9 +2,6 @@
|
|||
# coding: utf-8
|
||||
|
||||
import six
|
||||
from nose import main
|
||||
from nose.tools import *
|
||||
|
||||
import io
|
||||
import msgpack
|
||||
|
||||
|
@ -38,13 +35,8 @@ def test_exceeding_unpacker_read_size():
|
|||
|
||||
read_count = 0
|
||||
for idx, o in enumerate(unpacker):
|
||||
assert_equal(type(o), bytes)
|
||||
assert_equal(o, gen_binary_data(idx))
|
||||
assert type(o) == bytes
|
||||
assert o == gen_binary_data(idx)
|
||||
read_count += 1
|
||||
|
||||
assert_equal(read_count, NUMBER_OF_STRINGS)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
#test_exceeding_unpacker_read_size()
|
||||
assert read_count == NUMBER_OF_STRINGS
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
import six
|
||||
from msgpack import Unpacker, BufferFull
|
||||
from msgpack.exceptions import OutOfData
|
||||
import nose
|
||||
from pytest import raises
|
||||
|
||||
|
||||
def test_foobar():
|
||||
unpacker = Unpacker(read_size=3, use_list=1)
|
||||
|
@ -15,11 +16,8 @@ def test_foobar():
|
|||
assert unpacker.unpack() == ord(b'b')
|
||||
assert unpacker.unpack() == ord(b'a')
|
||||
assert unpacker.unpack() == ord(b'r')
|
||||
try:
|
||||
o = unpacker.unpack()
|
||||
assert 0, "should raise exception"
|
||||
except OutOfData:
|
||||
assert 1, "ok"
|
||||
with raises(OutOfData):
|
||||
unpacker.unpack()
|
||||
|
||||
unpacker.feed(b'foo')
|
||||
unpacker.feed(b'bar')
|
||||
|
@ -39,17 +37,16 @@ def test_foobar_skip():
|
|||
unpacker.skip()
|
||||
assert unpacker.unpack() == ord(b'a')
|
||||
unpacker.skip()
|
||||
try:
|
||||
o = unpacker.unpack()
|
||||
assert 0, "should raise exception"
|
||||
except OutOfData:
|
||||
assert 1, "ok"
|
||||
with raises(OutOfData):
|
||||
unpacker.unpack()
|
||||
|
||||
def test_maxbuffersize():
|
||||
nose.tools.assert_raises(ValueError, Unpacker, read_size=5, max_buffer_size=3)
|
||||
with raises(ValueError):
|
||||
Unpacker(read_size=5, max_buffer_size=3)
|
||||
unpacker = Unpacker(read_size=3, max_buffer_size=3, use_list=1)
|
||||
unpacker.feed(b'fo')
|
||||
nose.tools.assert_raises(BufferFull, unpacker.feed, b'ob')
|
||||
with raises(BufferFull):
|
||||
unpacker.feed(b'ob')
|
||||
unpacker.feed(b'o')
|
||||
assert ord('f') == next(unpacker)
|
||||
unpacker.feed(b'b')
|
||||
|
@ -73,5 +70,3 @@ def test_readbytes():
|
|||
assert unpacker.unpack() == ord(b'a')
|
||||
assert unpacker.unpack() == ord(b'r')
|
||||
|
||||
if __name__ == '__main__':
|
||||
nose.main()
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
from nose import main
|
||||
from nose.tools import *
|
||||
from msgpack import packb, unpackb
|
||||
from collections import namedtuple
|
||||
|
||||
|
@ -18,10 +16,6 @@ class MyTuple(tuple):
|
|||
MyNamedTuple = namedtuple('MyNamedTuple', 'x y')
|
||||
|
||||
def test_types():
|
||||
assert_equal(packb(dict()), packb(MyDict()))
|
||||
assert_equal(packb(list()), packb(MyList()))
|
||||
assert_equal(packb(MyNamedTuple(1,2)), packb((1,2)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
assert packb(MyDict()) == packb(dict())
|
||||
assert packb(MyList()) == packb(list())
|
||||
assert packb(MyNamedTuple(1, 2)) == packb((1, 2))
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
"""Tests for cases where the user seeks to obtain packed msgpack objects"""
|
||||
|
||||
from nose import main
|
||||
from nose.tools import *
|
||||
import six
|
||||
from msgpack import Unpacker, packb
|
||||
|
||||
|
@ -10,14 +8,14 @@ def test_write_bytes():
|
|||
unpacker = Unpacker()
|
||||
unpacker.feed(b'abc')
|
||||
f = six.BytesIO()
|
||||
assert_equal(unpacker.unpack(f.write), ord('a'))
|
||||
assert_equal(f.getvalue(), b'a')
|
||||
assert unpacker.unpack(f.write) == ord('a')
|
||||
assert f.getvalue() == b'a'
|
||||
f = six.BytesIO()
|
||||
assert unpacker.skip(f.write) is None
|
||||
assert_equal(f.getvalue(), b'b')
|
||||
assert f.getvalue() == b'b'
|
||||
f = six.BytesIO()
|
||||
assert unpacker.skip() is None
|
||||
assert_equal(f.getvalue(), b'')
|
||||
assert f.getvalue() == b''
|
||||
|
||||
|
||||
def test_write_bytes_multi_buffer():
|
||||
|
@ -27,8 +25,5 @@ def test_write_bytes_multi_buffer():
|
|||
|
||||
f = six.BytesIO()
|
||||
unpacked = unpacker.unpack(f.write)
|
||||
assert_equal(unpacked, long_val)
|
||||
assert_equal(f.getvalue(), expected)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
assert unpacked == long_val
|
||||
assert f.getvalue() == expected
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue