diff options
author | INADA Naoki <inada-n@klab.com> | 2012-12-29 11:24:25 +0900 |
---|---|---|
committer | INADA Naoki <inada-n@klab.com> | 2012-12-29 11:24:25 +0900 |
commit | 593c832ab00372b4a44dd47de94e4c2546fc1193 (patch) | |
tree | a24b9b2454f4fe30c2262f7839ec66c286036de6 /test | |
parent | d57e369258a388ee64ad900ee7e975da25ca36ec (diff) | |
download | msgpack-python-593c832ab00372b4a44dd47de94e4c2546fc1193.tar.gz |
Use py.test instead of nosetests.
Diffstat (limited to 'test')
-rw-r--r-- | test/test_buffer.py | 7 | ||||
-rw-r--r-- | test/test_case.py | 15 | ||||
-rw-r--r-- | test/test_except.py | 30 | ||||
-rw-r--r-- | test/test_format.py | 7 | ||||
-rw-r--r-- | test/test_obj.py | 39 | ||||
-rw-r--r-- | test/test_pack.py | 47 | ||||
-rw-r--r-- | test/test_seq.py | 14 | ||||
-rw-r--r-- | test/test_sequnpack.py | 25 | ||||
-rw-r--r-- | test/test_subtype.py | 12 | ||||
-rw-r--r-- | test/test_unpack_raw.py | 17 |
10 files changed, 78 insertions, 135 deletions
diff --git a/test/test_buffer.py b/test/test_buffer.py index 785fb60..04cc02d 100644 --- a/test/test_buffer.py +++ b/test/test_buffer.py @@ -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() diff --git a/test/test_case.py b/test/test_case.py index 9cbf9bd..5a4bb6c 100644 --- a/test/test_case.py +++ b/test/test_case.py @@ -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() diff --git a/test/test_except.py b/test/test_except.py index 35287df..361d4ea 100644 --- a/test/test_except.py +++ b/test/test_except.py @@ -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(ValueError) -def test_invalidvalue(): - unpackb(b'\xd9\x97#DL_') + 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) -if __name__ == '__main__': - from nose import main - main() +def test_invalidvalue(): + with raises(ValueError): + unpackb(b'\xd9\x97#DL_') diff --git a/test/test_format.py b/test/test_format.py index ac08709..5fec0c3 100644 --- a/test/test_format.py +++ b/test/test_format.py @@ -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() diff --git a/test/test_obj.py b/test/test_obj.py index 1d9024b..fbf610c 100644 --- a/test/test_obj.py +++ b/test/test_obj.py @@ -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) diff --git a/test/test_pack.py b/test/test_pack.py index b934dd2..8f4117c 100644 --- a/test/test_pack.py +++ b/test/test_pack.py @@ -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() diff --git a/test/test_seq.py b/test/test_seq.py index 72e935a..af719b0 100644 --- a/test/test_seq.py +++ b/test/test_seq.py @@ -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 diff --git a/test/test_sequnpack.py b/test/test_sequnpack.py index f767726..fc1f712 100644 --- a/test/test_sequnpack.py +++ b/test/test_sequnpack.py @@ -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() diff --git a/test/test_subtype.py b/test/test_subtype.py index 1dfd7da..6807508 100644 --- a/test/test_subtype.py +++ b/test/test_subtype.py @@ -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)) diff --git a/test/test_unpack_raw.py b/test/test_unpack_raw.py index 15d9c93..9f3784c 100644 --- a/test/test_unpack_raw.py +++ b/test/test_unpack_raw.py @@ -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 |