From d94d60db8b01bfa9c137ab912a698efd6db872a7 Mon Sep 17 00:00:00 2001 From: Andrey Cizov Date: Sat, 13 Jul 2019 16:36:58 +0100 Subject: [PATCH] get rid of all warnings during tests --- test/test_capability.py | 12 ++++++------ test/test_rpc.py | 1 - test/test_serialization.py | 40 +++++++++++++++++++++++++++----------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/test/test_capability.py b/test/test_capability.py index e37586a..39f5921 100644 --- a/test/test_capability.py +++ b/test/test_capability.py @@ -359,7 +359,7 @@ def test_inheritance(): assert response.x == '26' -class TestPassedCap(capability.TestPassedCap.Server): +class PassedCapTest(capability.TestPassedCap.Server): def foo(self, cap, _context, **kwargs): def set_result(res): _context.results.x = res.x @@ -367,32 +367,32 @@ class TestPassedCap(capability.TestPassedCap.Server): def test_null_cap(): - client = capability.TestPassedCap._new_client(TestPassedCap()) + client = capability.TestPassedCap._new_client(PassedCapTest()) assert client.foo(Server()).wait().x == '26' with pytest.raises(capnp.KjException): client.foo().wait() -class TestStructArg(capability.TestStructArg.Server): +class StructArgTest(capability.TestStructArg.Server): def bar(self, a, b, **kwargs): return a + str(b) def test_struct_args(): - client = capability.TestStructArg._new_client(TestStructArg()) + client = capability.TestStructArg._new_client(StructArgTest()) assert client.bar(a='test', b=1).wait().c == 'test1' with pytest.raises(capnp.KjException): assert client.bar('test', 1).wait().c == 'test1' -class TestGeneric(capability.TestGeneric.Server): +class GenericTest(capability.TestGeneric.Server): def foo(self, a, **kwargs): return a.as_text() + 'test' def test_generic(): - client = capability.TestGeneric._new_client(TestGeneric()) + client = capability.TestGeneric._new_client(GenericTest()) obj = capnp._MallocMessageBuilder().get_root_as_any() obj.set_as_text("anypointer_") diff --git a/test/test_rpc.py b/test/test_rpc.py index ba1c5e8..92e5df9 100644 --- a/test/test_rpc.py +++ b/test/test_rpc.py @@ -32,7 +32,6 @@ def test_simple_rpc_with_options(): remote = cap.foo(i=5) _ = remote.wait() - def test_simple_rpc_bootstrap(): read, write = socket.socketpair() diff --git a/test/test_serialization.py b/test/test_serialization.py index 381b6f8..b1598ee 100644 --- a/test/test_serialization.py +++ b/test/test_serialization.py @@ -1,3 +1,6 @@ +import warnings +from contextlib import contextmanager + import pytest import capnp import os @@ -103,14 +106,23 @@ def test_roundtrip_bytes_packed(all_types): msg = all_types.TestAllTypes.from_bytes_packed(message_bytes) test_regression.check_all_types(msg) +@contextmanager +def _warnings(expected_count=2, expected_text='This message has already been written once.'): + with warnings.catch_warnings(record=True) as w: + yield + + assert len(w) == expected_count + assert all(issubclass(x.category, UserWarning) for x in w), w + assert all(expected_text in str(x.message) for x in w), w def test_roundtrip_file_multiple(all_types): f = tempfile.TemporaryFile() msg = all_types.TestAllTypes.new_message() test_regression.init_all_types(msg) msg.write(f) - msg.write(f) - msg.write(f) + with _warnings(2): + msg.write(f) + msg.write(f) f.seek(0) i = 0 @@ -125,8 +137,9 @@ def test_roundtrip_bytes_multiple(all_types): test_regression.init_all_types(msg) msgs = msg.to_bytes() - msgs += msg.to_bytes() - msgs += msg.to_bytes() + with _warnings(2): + msgs += msg.to_bytes() + msgs += msg.to_bytes() i = 0 for msg in all_types.TestAllTypes.read_multiple_bytes(msgs): @@ -140,8 +153,9 @@ def test_roundtrip_file_multiple_packed(all_types): msg = all_types.TestAllTypes.new_message() test_regression.init_all_types(msg) msg.write_packed(f) - msg.write_packed(f) - msg.write_packed(f) + with _warnings(2): + msg.write_packed(f) + msg.write_packed(f) f.seek(0) i = 0 @@ -156,8 +170,9 @@ def test_roundtrip_bytes_multiple_packed(all_types): test_regression.init_all_types(msg) msgs = msg.to_bytes_packed() - msgs += msg.to_bytes_packed() - msgs += msg.to_bytes_packed() + with _warnings(2): + msgs += msg.to_bytes_packed() + msgs += msg.to_bytes_packed() i = 0 for msg in all_types.TestAllTypes.read_multiple_bytes_packed(msgs): @@ -175,7 +190,8 @@ def test_roundtrip_dict(all_types): test_regression.init_all_types(msg) d = msg.to_dict() - msg = all_types.TestAllTypes.from_dict(d) + with _warnings(1, expected_text="This method is deprecated and will be removed"): + msg = all_types.TestAllTypes.from_dict(d) test_regression.check_all_types(msg) @@ -187,7 +203,8 @@ def test_file_and_bytes(all_types): f.seek(0) - assert f.read() == msg.to_bytes() + with _warnings(1): + assert f.read() == msg.to_bytes() def test_file_and_bytes_packed(all_types): @@ -198,7 +215,8 @@ def test_file_and_bytes_packed(all_types): f.seek(0) - assert f.read() == msg.to_bytes_packed() + with _warnings(1): + assert f.read() == msg.to_bytes_packed() def test_pickle(all_types):