get rid of all warnings during tests

This commit is contained in:
Andrey Cizov
2019-07-13 16:36:58 +01:00
committed by Jacob Alexander
parent 7bce2f751a
commit d94d60db8b
3 changed files with 35 additions and 18 deletions

View File

@@ -359,7 +359,7 @@ def test_inheritance():
assert response.x == '26' assert response.x == '26'
class TestPassedCap(capability.TestPassedCap.Server): class PassedCapTest(capability.TestPassedCap.Server):
def foo(self, cap, _context, **kwargs): def foo(self, cap, _context, **kwargs):
def set_result(res): def set_result(res):
_context.results.x = res.x _context.results.x = res.x
@@ -367,32 +367,32 @@ class TestPassedCap(capability.TestPassedCap.Server):
def test_null_cap(): def test_null_cap():
client = capability.TestPassedCap._new_client(TestPassedCap()) client = capability.TestPassedCap._new_client(PassedCapTest())
assert client.foo(Server()).wait().x == '26' assert client.foo(Server()).wait().x == '26'
with pytest.raises(capnp.KjException): with pytest.raises(capnp.KjException):
client.foo().wait() client.foo().wait()
class TestStructArg(capability.TestStructArg.Server): class StructArgTest(capability.TestStructArg.Server):
def bar(self, a, b, **kwargs): def bar(self, a, b, **kwargs):
return a + str(b) return a + str(b)
def test_struct_args(): 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' assert client.bar(a='test', b=1).wait().c == 'test1'
with pytest.raises(capnp.KjException): with pytest.raises(capnp.KjException):
assert client.bar('test', 1).wait().c == 'test1' assert client.bar('test', 1).wait().c == 'test1'
class TestGeneric(capability.TestGeneric.Server): class GenericTest(capability.TestGeneric.Server):
def foo(self, a, **kwargs): def foo(self, a, **kwargs):
return a.as_text() + 'test' return a.as_text() + 'test'
def test_generic(): def test_generic():
client = capability.TestGeneric._new_client(TestGeneric()) client = capability.TestGeneric._new_client(GenericTest())
obj = capnp._MallocMessageBuilder().get_root_as_any() obj = capnp._MallocMessageBuilder().get_root_as_any()
obj.set_as_text("anypointer_") obj.set_as_text("anypointer_")

View File

@@ -32,7 +32,6 @@ def test_simple_rpc_with_options():
remote = cap.foo(i=5) remote = cap.foo(i=5)
_ = remote.wait() _ = remote.wait()
def test_simple_rpc_bootstrap(): def test_simple_rpc_bootstrap():
read, write = socket.socketpair() read, write = socket.socketpair()

View File

@@ -1,3 +1,6 @@
import warnings
from contextlib import contextmanager
import pytest import pytest
import capnp import capnp
import os import os
@@ -103,12 +106,21 @@ def test_roundtrip_bytes_packed(all_types):
msg = all_types.TestAllTypes.from_bytes_packed(message_bytes) msg = all_types.TestAllTypes.from_bytes_packed(message_bytes)
test_regression.check_all_types(msg) 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): def test_roundtrip_file_multiple(all_types):
f = tempfile.TemporaryFile() f = tempfile.TemporaryFile()
msg = all_types.TestAllTypes.new_message() msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg) test_regression.init_all_types(msg)
msg.write(f) msg.write(f)
with _warnings(2):
msg.write(f) msg.write(f)
msg.write(f) msg.write(f)
@@ -125,6 +137,7 @@ def test_roundtrip_bytes_multiple(all_types):
test_regression.init_all_types(msg) test_regression.init_all_types(msg)
msgs = msg.to_bytes() msgs = msg.to_bytes()
with _warnings(2):
msgs += msg.to_bytes() msgs += msg.to_bytes()
msgs += msg.to_bytes() msgs += msg.to_bytes()
@@ -140,6 +153,7 @@ def test_roundtrip_file_multiple_packed(all_types):
msg = all_types.TestAllTypes.new_message() msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg) test_regression.init_all_types(msg)
msg.write_packed(f) msg.write_packed(f)
with _warnings(2):
msg.write_packed(f) msg.write_packed(f)
msg.write_packed(f) msg.write_packed(f)
@@ -156,6 +170,7 @@ def test_roundtrip_bytes_multiple_packed(all_types):
test_regression.init_all_types(msg) test_regression.init_all_types(msg)
msgs = msg.to_bytes_packed() msgs = msg.to_bytes_packed()
with _warnings(2):
msgs += msg.to_bytes_packed() msgs += msg.to_bytes_packed()
msgs += msg.to_bytes_packed() msgs += msg.to_bytes_packed()
@@ -175,6 +190,7 @@ def test_roundtrip_dict(all_types):
test_regression.init_all_types(msg) test_regression.init_all_types(msg)
d = msg.to_dict() d = msg.to_dict()
with _warnings(1, expected_text="This method is deprecated and will be removed"):
msg = all_types.TestAllTypes.from_dict(d) msg = all_types.TestAllTypes.from_dict(d)
test_regression.check_all_types(msg) test_regression.check_all_types(msg)
@@ -187,6 +203,7 @@ def test_file_and_bytes(all_types):
f.seek(0) f.seek(0)
with _warnings(1):
assert f.read() == msg.to_bytes() assert f.read() == msg.to_bytes()
@@ -198,6 +215,7 @@ def test_file_and_bytes_packed(all_types):
f.seek(0) f.seek(0)
with _warnings(1):
assert f.read() == msg.to_bytes_packed() assert f.read() == msg.to_bytes_packed()