2013-08-28 23:13:38 -07:00
|
|
|
import pytest
|
|
|
|
import capnp
|
|
|
|
import os
|
2013-09-01 23:55:29 -07:00
|
|
|
import sys
|
2013-08-28 23:13:38 -07:00
|
|
|
|
|
|
|
this_dir = os.path.dirname(__file__)
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-28 23:13:38 -07:00
|
|
|
@pytest.fixture
|
|
|
|
def addressbook():
|
2021-10-01 11:00:22 -07:00
|
|
|
return capnp.load(os.path.join(this_dir, "addressbook.capnp"))
|
2013-08-28 23:13:38 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-29 21:03:00 -07:00
|
|
|
@pytest.fixture
|
|
|
|
def foo():
|
2021-10-01 11:00:22 -07:00
|
|
|
return capnp.load(os.path.join(this_dir, "foo.capnp"))
|
2013-08-29 21:03:00 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-29 21:03:00 -07:00
|
|
|
@pytest.fixture
|
|
|
|
def bar():
|
2021-10-01 11:00:22 -07:00
|
|
|
return capnp.load(os.path.join(this_dir, "bar.capnp"))
|
2013-08-29 21:03:00 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-28 23:13:38 -07:00
|
|
|
def test_basic_load():
|
2021-10-01 11:00:22 -07:00
|
|
|
capnp.load(os.path.join(this_dir, "addressbook.capnp"))
|
2013-08-28 23:13:38 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-28 23:13:38 -07:00
|
|
|
def test_constants(addressbook):
|
|
|
|
assert addressbook.qux == 123
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-28 23:13:38 -07:00
|
|
|
def test_classes(addressbook):
|
|
|
|
assert addressbook.AddressBook
|
|
|
|
assert addressbook.Person
|
2013-08-29 21:03:00 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-29 21:03:00 -07:00
|
|
|
def test_import(foo, bar):
|
2013-09-01 20:10:57 -07:00
|
|
|
m = capnp._MallocMessageBuilder()
|
|
|
|
foo = m.init_root(foo.Foo)
|
|
|
|
m2 = capnp._MallocMessageBuilder()
|
|
|
|
bar = m2.init_root(bar.Bar)
|
2013-08-29 21:03:00 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
foo.name = "foo"
|
2013-08-29 21:03:00 -07:00
|
|
|
bar.foo = foo
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert bar.foo.name == "foo"
|
2013-08-29 21:03:00 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-08-29 21:03:00 -07:00
|
|
|
def test_failed_import():
|
|
|
|
s = capnp.SchemaParser()
|
|
|
|
s2 = capnp.SchemaParser()
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
foo = s.load(os.path.join(this_dir, "foo.capnp"))
|
|
|
|
bar = s2.load(os.path.join(this_dir, "bar.capnp"))
|
2013-08-29 21:03:00 -07:00
|
|
|
|
2013-09-01 20:10:57 -07:00
|
|
|
m = capnp._MallocMessageBuilder()
|
|
|
|
foo = m.init_root(foo.Foo)
|
|
|
|
m2 = capnp._MallocMessageBuilder()
|
|
|
|
bar = m2.init_root(bar.Bar)
|
2013-08-29 21:03:00 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
foo.name = "foo"
|
2013-08-29 21:03:00 -07:00
|
|
|
|
2014-11-30 14:27:10 -08:00
|
|
|
with pytest.raises(Exception):
|
2013-08-29 21:03:00 -07:00
|
|
|
bar.foo = foo
|
2013-09-01 21:31:39 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-09-03 00:25:32 -07:00
|
|
|
def test_defualt_import_hook():
|
2019-09-27 01:09:28 -07:00
|
|
|
# Make sure any previous imports of addressbook_capnp are gone
|
|
|
|
capnp.cleanup_global_schema_parser()
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
import addressbook_capnp # noqa: F401
|
|
|
|
|
2013-09-03 00:25:32 -07:00
|
|
|
|
2014-06-12 16:37:06 -07:00
|
|
|
def test_dash_import():
|
2019-12-11 22:44:44 -08:00
|
|
|
import addressbook_with_dashes_capnp # noqa: F401
|
|
|
|
|
2014-06-12 16:37:06 -07:00
|
|
|
|
|
|
|
def test_spaces_import():
|
2019-12-11 22:44:44 -08:00
|
|
|
import addressbook_with_spaces_capnp # noqa: F401
|
|
|
|
|
2014-06-12 16:37:06 -07:00
|
|
|
|
2013-09-01 21:31:39 -07:00
|
|
|
def test_add_import_hook():
|
2023-11-24 23:34:27 +01:00
|
|
|
capnp.add_import_hook()
|
2013-09-01 22:24:46 -07:00
|
|
|
|
2019-09-27 01:09:28 -07:00
|
|
|
# Make sure any previous imports of addressbook_capnp are gone
|
|
|
|
capnp.cleanup_global_schema_parser()
|
|
|
|
|
2013-09-01 23:55:29 -07:00
|
|
|
import addressbook_capnp
|
2021-10-01 11:00:22 -07:00
|
|
|
|
2013-09-01 23:55:29 -07:00
|
|
|
addressbook_capnp.AddressBook.new_message()
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-09-01 23:55:29 -07:00
|
|
|
def test_multiple_add_import_hook():
|
|
|
|
capnp.add_import_hook()
|
|
|
|
capnp.add_import_hook()
|
|
|
|
|
2019-09-27 01:09:28 -07:00
|
|
|
# Make sure any previous imports of addressbook_capnp are gone
|
|
|
|
capnp.cleanup_global_schema_parser()
|
|
|
|
|
2013-09-01 23:55:29 -07:00
|
|
|
import addressbook_capnp
|
2021-10-01 11:00:22 -07:00
|
|
|
|
2013-09-01 23:55:29 -07:00
|
|
|
addressbook_capnp.AddressBook.new_message()
|
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2013-09-01 23:55:29 -07:00
|
|
|
def test_remove_import_hook():
|
2023-11-24 23:34:27 +01:00
|
|
|
capnp.add_import_hook()
|
2013-09-01 23:55:29 -07:00
|
|
|
capnp.remove_import_hook()
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
if "addressbook_capnp" in sys.modules:
|
2019-12-11 22:44:44 -08:00
|
|
|
# hack to deal with it being imported already
|
2021-10-01 11:00:22 -07:00
|
|
|
del sys.modules["addressbook_capnp"]
|
2013-09-01 23:55:29 -07:00
|
|
|
|
|
|
|
with pytest.raises(ImportError):
|
2019-12-11 22:44:44 -08:00
|
|
|
import addressbook_capnp # noqa: F401
|
2020-06-03 22:49:52 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_bundled_import_hook():
|
|
|
|
# stream.capnp should be bundled, or provided by the system capnproto
|
|
|
|
capnp.add_import_hook()
|
2023-11-24 23:34:27 +01:00
|
|
|
from capnp import stream_capnp # noqa: F401
|
|
|
|
|
|
|
|
|
|
|
|
def test_nested_import():
|
|
|
|
import schemas.parent_capnp # noqa: F401
|
|
|
|
import schemas.child_capnp # noqa: F401
|
2023-02-21 08:31:12 +02:00
|
|
|
|
|
|
|
|
2023-06-19 12:21:38 +02:00
|
|
|
async def test_load_capnp(foo):
|
2023-02-21 08:31:12 +02:00
|
|
|
# test dynamically loading
|
|
|
|
loader = capnp.SchemaLoader()
|
|
|
|
loader.load(foo.Baz.schema.get_proto())
|
|
|
|
loader.load_dynamic(foo.Qux.schema.get_proto().node)
|
|
|
|
|
|
|
|
schema = loader.get(foo.Baz.schema.get_proto().node.id).as_struct()
|
|
|
|
assert "text" in schema.fieldnames
|
|
|
|
assert "qux" in schema.fieldnames
|
|
|
|
assert schema.fields["qux"].proto.slot.type.which == "struct"
|
|
|
|
|
|
|
|
class Wrapper(foo.Wrapper.Server):
|
2023-06-19 12:21:38 +02:00
|
|
|
async def wrapped(self, object, **kwargs):
|
2023-02-21 08:31:12 +02:00
|
|
|
assert isinstance(object, capnp.lib.capnp._DynamicObjectReader)
|
|
|
|
baz_ = object.as_struct(schema)
|
|
|
|
assert baz_.text == "test"
|
|
|
|
assert baz_.qux.id == 2
|
|
|
|
|
|
|
|
# test calling into the wrapper with a Baz message.
|
|
|
|
baz_ = foo.Baz.new_message()
|
|
|
|
baz_.text = "test"
|
|
|
|
baz_.qux.id = 2
|
|
|
|
|
2023-10-11 20:28:24 +02:00
|
|
|
async with capnp.kj_loop():
|
|
|
|
wrapper = foo.Wrapper._new_client(Wrapper())
|
|
|
|
remote = wrapper.wrapped(baz_)
|
|
|
|
await remote
|