2013-09-03 00:25:32 -07:00
|
|
|
import pytest
|
|
|
|
import capnp
|
|
|
|
import os
|
2013-12-09 17:16:39 -08:00
|
|
|
import tempfile
|
2013-12-18 12:39:56 -08:00
|
|
|
import sys
|
2013-09-03 00:25:32 -07:00
|
|
|
|
2021-06-01 15:40:35 +08:00
|
|
|
from capnp.lib.capnp import KjException
|
|
|
|
|
2013-09-03 00:25:32 -07:00
|
|
|
this_dir = os.path.dirname(__file__)
|
|
|
|
|
2013-12-18 12:39:56 -08:00
|
|
|
|
2013-09-03 00:25:32 -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-12-18 12:39:56 -08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def all_types():
|
2021-10-01 11:00:22 -07:00
|
|
|
return capnp.load(os.path.join(this_dir, "all_types.capnp"))
|
2013-12-18 12:39:56 -08:00
|
|
|
|
2013-09-03 00:25:32 -07:00
|
|
|
|
|
|
|
def test_which_builder(addressbook):
|
|
|
|
addresses = addressbook.AddressBook.new_message()
|
2021-10-01 11:00:22 -07:00
|
|
|
people = addresses.init("people", 2)
|
2013-09-03 00:25:32 -07:00
|
|
|
|
|
|
|
alice = people[0]
|
|
|
|
alice.employment.school = "MIT"
|
|
|
|
|
2014-02-13 22:08:15 -08:00
|
|
|
assert alice.employment.which == addressbook.Person.Employment.school
|
|
|
|
assert alice.employment.which == "school"
|
2013-09-03 00:25:32 -07:00
|
|
|
|
|
|
|
bob = people[1]
|
|
|
|
|
2014-02-13 22:08:15 -08:00
|
|
|
assert bob.employment.which == addressbook.Person.Employment.unemployed
|
|
|
|
assert bob.employment.which == "unemployed"
|
2013-12-18 12:39:56 -08:00
|
|
|
|
2013-09-03 00:25:32 -07:00
|
|
|
bob.employment.unemployed = None
|
|
|
|
|
2014-02-13 22:08:15 -08:00
|
|
|
assert bob.employment.which == addressbook.Person.Employment.unemployed
|
|
|
|
assert bob.employment.which == "unemployed"
|
2013-09-03 00:25:32 -07:00
|
|
|
|
2021-06-01 15:40:35 +08:00
|
|
|
with pytest.raises(KjException):
|
|
|
|
addresses._which()
|
|
|
|
|
|
|
|
with pytest.raises(KjException):
|
|
|
|
addresses._which_str()
|
|
|
|
|
|
|
|
with pytest.raises(KjException):
|
2014-02-13 22:08:15 -08:00
|
|
|
addresses.which
|
2013-09-03 01:00:52 -07:00
|
|
|
|
2013-12-18 12:39:56 -08:00
|
|
|
|
2013-09-03 00:25:32 -07:00
|
|
|
def test_which_reader(addressbook):
|
|
|
|
def writeAddressBook(fd):
|
|
|
|
message = capnp._MallocMessageBuilder()
|
|
|
|
addressBook = message.init_root(addressbook.AddressBook)
|
2021-10-01 11:00:22 -07:00
|
|
|
people = addressBook.init("people", 2)
|
2013-09-03 00:25:32 -07:00
|
|
|
|
|
|
|
alice = people[0]
|
|
|
|
alice.employment.school = "MIT"
|
|
|
|
|
|
|
|
bob = people[1]
|
|
|
|
bob.employment.unemployed = None
|
|
|
|
|
|
|
|
capnp._write_packed_message_to_fd(fd, message)
|
|
|
|
|
2013-12-09 17:16:39 -08:00
|
|
|
f = tempfile.TemporaryFile()
|
2013-09-03 00:25:32 -07:00
|
|
|
writeAddressBook(f.fileno())
|
2013-12-09 17:16:39 -08:00
|
|
|
f.seek(0)
|
2013-09-03 00:25:32 -07:00
|
|
|
|
|
|
|
addresses = addressbook.AddressBook.read_packed(f)
|
|
|
|
|
|
|
|
people = addresses.people
|
|
|
|
|
|
|
|
alice = people[0]
|
2014-02-13 22:08:15 -08:00
|
|
|
assert alice.employment.which == "school"
|
2013-09-03 00:25:32 -07:00
|
|
|
|
|
|
|
bob = people[1]
|
2014-02-13 22:08:15 -08:00
|
|
|
assert bob.employment.which == "unemployed"
|
2013-09-03 00:25:32 -07:00
|
|
|
|
2021-06-01 15:40:35 +08:00
|
|
|
with pytest.raises(KjException):
|
|
|
|
addresses._which_str()
|
|
|
|
|
|
|
|
with pytest.raises(KjException):
|
|
|
|
addresses._which()
|
|
|
|
|
|
|
|
with pytest.raises(KjException):
|
2014-02-13 22:08:15 -08:00
|
|
|
addresses.which
|
|
|
|
|
|
|
|
|
2019-09-26 22:18:28 -07:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
capnp.version.LIBCAPNP_VERSION < 5000,
|
2021-10-01 11:00:22 -07:00
|
|
|
reason="Using ints as enums requires v0.5.0+ of the C++ capnp library",
|
2019-09-26 22:18:28 -07:00
|
|
|
)
|
2014-02-13 22:08:15 -08:00
|
|
|
def test_enum(addressbook):
|
|
|
|
addresses = addressbook.AddressBook.new_message()
|
2021-10-01 11:00:22 -07:00
|
|
|
people = addresses.init("people", 2)
|
2014-02-13 22:08:15 -08:00
|
|
|
|
|
|
|
alice = people[0]
|
2021-10-01 11:00:22 -07:00
|
|
|
phones = alice.init("phones", 2)
|
2014-02-13 22:08:15 -08:00
|
|
|
|
|
|
|
assert phones[0].type == phones[1].type
|
|
|
|
|
|
|
|
phones[0].type = addressbook.Person.PhoneNumber.Type.home
|
|
|
|
|
|
|
|
assert phones[0].type != phones[1].type
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
phones[1].type = "home"
|
2014-02-13 22:08:15 -08:00
|
|
|
|
|
|
|
assert phones[0].type == phones[1].type
|
2013-09-03 01:00:52 -07:00
|
|
|
|
2013-12-18 12:39:56 -08:00
|
|
|
|
2013-09-03 00:25:32 -07:00
|
|
|
def test_builder_set(addressbook):
|
|
|
|
person = addressbook.Person.new_message()
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
person.name = "test"
|
2013-09-03 00:25:32 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert person.name == "test"
|
2013-09-03 00:25:32 -07:00
|
|
|
|
2014-01-14 09:44:21 -08:00
|
|
|
with pytest.raises(AttributeError):
|
2021-10-01 11:00:22 -07:00
|
|
|
person.foo = "test"
|
2013-12-18 12:39:56 -08:00
|
|
|
|
|
|
|
|
2015-05-30 19:38:40 -04:00
|
|
|
def test_builder_set_from_list(all_types):
|
|
|
|
msg = all_types.TestAllTypes.new_message()
|
|
|
|
|
|
|
|
msg.int32List = [0, 1, 2]
|
|
|
|
|
|
|
|
assert list(msg.int32List) == [0, 1, 2]
|
|
|
|
|
|
|
|
|
2017-04-09 20:57:33 -07:00
|
|
|
def test_builder_set_from_tuple(all_types):
|
|
|
|
msg = all_types.TestAllTypes.new_message()
|
|
|
|
|
|
|
|
msg.int32List = (0, 1, 2)
|
|
|
|
|
|
|
|
assert list(msg.int32List) == [0, 1, 2]
|
|
|
|
|
|
|
|
|
2013-12-18 12:39:56 -08:00
|
|
|
def test_null_str(all_types):
|
|
|
|
msg = all_types.TestAllTypes.new_message()
|
|
|
|
|
|
|
|
msg.textField = "f\x00oo"
|
|
|
|
msg.dataField = b"b\x00ar"
|
|
|
|
|
|
|
|
assert msg.textField == "f\x00oo"
|
|
|
|
assert msg.dataField == b"b\x00ar"
|
|
|
|
|
|
|
|
|
|
|
|
def test_unicode_str(all_types):
|
|
|
|
msg = all_types.TestAllTypes.new_message()
|
|
|
|
|
2013-12-18 13:06:52 -08:00
|
|
|
if sys.version_info[0] == 2:
|
2022-04-06 13:32:52 +10:00
|
|
|
msg.textField = "f\u00e6oo".encode("utf-8")
|
2013-12-18 12:39:56 -08:00
|
|
|
|
2022-04-06 13:32:52 +10:00
|
|
|
assert msg.textField.decode("utf-8") == "f\u00e6oo"
|
2013-12-18 12:39:56 -08:00
|
|
|
else:
|
2013-12-18 13:06:52 -08:00
|
|
|
msg.textField = "f\u00e6oo"
|
|
|
|
|
2013-12-18 13:10:33 -08:00
|
|
|
assert msg.textField == "f\u00e6oo"
|
2014-01-13 16:28:05 -08:00
|
|
|
|
|
|
|
|
|
|
|
def test_new_message(all_types):
|
|
|
|
msg = all_types.TestAllTypes.new_message(int32Field=100)
|
|
|
|
|
|
|
|
assert msg.int32Field == 100
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
msg = all_types.TestAllTypes.new_message(structField={"int32Field": 100})
|
2014-01-13 16:28:05 -08:00
|
|
|
|
|
|
|
assert msg.structField.int32Field == 100
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
msg = all_types.TestAllTypes.new_message(
|
|
|
|
structList=[{"int32Field": 100}, {"int32Field": 101}]
|
|
|
|
)
|
2014-01-13 16:28:05 -08:00
|
|
|
|
|
|
|
assert msg.structList[0].int32Field == 100
|
|
|
|
assert msg.structList[1].int32Field == 101
|
|
|
|
|
2014-01-15 10:37:25 -08:00
|
|
|
msg = all_types.TestAllTypes.new_message(int32Field=100)
|
|
|
|
|
|
|
|
assert msg.int32Field == 100
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
msg = all_types.TestAllTypes.new_message(**{"int32Field": 100, "int64Field": 101})
|
2014-01-15 10:37:25 -08:00
|
|
|
|
|
|
|
assert msg.int32Field == 100
|
|
|
|
assert msg.int64Field == 101
|
|
|
|
|
2014-01-13 16:28:05 -08:00
|
|
|
|
|
|
|
def test_set_dict(all_types):
|
|
|
|
msg = all_types.TestAllTypes.new_message()
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
msg.structField = {"int32Field": 100}
|
2014-01-13 16:28:05 -08:00
|
|
|
|
|
|
|
assert msg.structField.int32Field == 100
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
msg.init("structList", 2)
|
|
|
|
msg.structList[0] = {"int32Field": 102}
|
2014-01-13 16:28:05 -08:00
|
|
|
|
|
|
|
assert msg.structList[0].int32Field == 102
|
2014-02-18 17:34:34 -08:00
|
|
|
|
|
|
|
|
|
|
|
def test_set_dict_union(addressbook):
|
2021-10-01 11:00:22 -07:00
|
|
|
person = addressbook.Person.new_message(
|
|
|
|
**{"employment": {"employer": {"name": "foo"}}}
|
|
|
|
)
|
2014-02-18 17:34:34 -08:00
|
|
|
|
2021-05-31 16:14:21 +08:00
|
|
|
assert person.employment.which == addressbook.Person.Employment.employer
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert person.employment.employer.name == "foo"
|
2014-06-26 00:53:16 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2021-05-31 16:14:21 +08:00
|
|
|
def test_union_enum(all_types):
|
|
|
|
assert all_types.UnionAllTypes.Union.UnionStructField1 == 0
|
|
|
|
assert all_types.UnionAllTypes.Union.UnionStructField2 == 1
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
msg = all_types.UnionAllTypes.new_message(
|
|
|
|
**{"unionStructField1": {"textField": "foo"}}
|
|
|
|
)
|
2021-05-31 16:14:21 +08:00
|
|
|
assert msg.which == all_types.UnionAllTypes.Union.UnionStructField1
|
2021-10-01 11:00:22 -07:00
|
|
|
assert msg.which == "unionStructField1"
|
2021-05-31 16:32:46 +08:00
|
|
|
assert msg.which == 0
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
msg = all_types.UnionAllTypes.new_message(
|
|
|
|
**{"unionStructField2": {"textField": "foo"}}
|
|
|
|
)
|
2021-05-31 16:14:21 +08:00
|
|
|
assert msg.which == all_types.UnionAllTypes.Union.UnionStructField2
|
2021-10-01 11:00:22 -07:00
|
|
|
assert msg.which == "unionStructField2"
|
2021-05-31 16:32:46 +08:00
|
|
|
assert msg.which == 1
|
2021-05-31 16:14:21 +08:00
|
|
|
|
|
|
|
assert all_types.GroupedUnionAllTypes.Union.G1 == 0
|
|
|
|
assert all_types.GroupedUnionAllTypes.Union.G2 == 1
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
msg = all_types.GroupedUnionAllTypes.new_message(
|
|
|
|
**{"g1": {"unionStructField1": {"textField": "foo"}}}
|
|
|
|
)
|
2021-05-31 16:14:21 +08:00
|
|
|
assert msg.which == all_types.GroupedUnionAllTypes.Union.G1
|
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
msg = all_types.GroupedUnionAllTypes.new_message(
|
|
|
|
**{"g2": {"unionStructField2": {"textField": "foo"}}}
|
|
|
|
)
|
2021-05-31 16:14:21 +08:00
|
|
|
assert msg.which == all_types.GroupedUnionAllTypes.Union.G2
|
|
|
|
|
2021-05-31 19:31:53 +08:00
|
|
|
msg = all_types.UnionAllTypes.new_message()
|
|
|
|
msg.unionStructField2 = msg.init(all_types.UnionAllTypes.Union.UnionStructField2)
|
|
|
|
|
2021-05-31 16:14:21 +08:00
|
|
|
|
2019-09-26 22:18:28 -07:00
|
|
|
def isstr(s):
|
|
|
|
return isinstance(s, str)
|
2014-06-26 01:07:16 -07:00
|
|
|
|
2014-06-26 00:53:16 -07:00
|
|
|
|
|
|
|
def test_to_dict_enum(addressbook):
|
2021-10-01 11:00:22 -07:00
|
|
|
person = addressbook.Person.new_message(
|
|
|
|
**{"phones": [{"number": "999-9999", "type": "mobile"}]}
|
|
|
|
)
|
2014-06-26 00:53:16 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
field = person.to_dict()["phones"][0]["type"]
|
2014-06-26 01:07:16 -07:00
|
|
|
assert isstr(field)
|
2021-10-01 11:00:22 -07:00
|
|
|
assert field == "mobile"
|
2014-09-03 15:20:26 -07:00
|
|
|
|
2014-10-27 15:37:38 -07:00
|
|
|
|
2014-09-03 15:20:26 -07:00
|
|
|
def test_explicit_field(addressbook):
|
2021-10-01 11:00:22 -07:00
|
|
|
person = addressbook.Person.new_message(**{"name": "Test"})
|
2014-09-03 15:20:26 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
name_field = addressbook.Person.schema.fields["name"]
|
2014-09-03 15:20:26 -07:00
|
|
|
|
|
|
|
assert person.name == person._get_by_field(name_field)
|
|
|
|
assert person.name == person.as_reader()._get_by_field(name_field)
|
|
|
|
|
2014-10-27 15:37:38 -07:00
|
|
|
|
|
|
|
def test_to_dict_verbose(addressbook):
|
2021-10-01 11:00:22 -07:00
|
|
|
person = addressbook.Person.new_message(**{"name": "Test"})
|
2014-10-27 15:37:38 -07:00
|
|
|
|
2021-10-01 11:00:22 -07:00
|
|
|
assert person.to_dict(verbose=True)["phones"] == []
|
2014-10-27 16:11:19 -07:00
|
|
|
|
|
|
|
if sys.version_info >= (2, 7):
|
2021-10-01 11:00:22 -07:00
|
|
|
assert person.to_dict(verbose=True, ordered=True)["phones"] == []
|
2014-10-27 15:37:38 -07:00
|
|
|
|
|
|
|
with pytest.raises(KeyError):
|
2021-10-01 11:00:22 -07:00
|
|
|
assert person.to_dict()["phones"] == []
|
2014-10-27 15:37:38 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_to_dict_ordered(addressbook):
|
2021-10-01 11:00:22 -07:00
|
|
|
person = addressbook.Person.new_message(
|
|
|
|
**{
|
|
|
|
"name": "Alice",
|
|
|
|
"phones": [{"type": "mobile", "number": "555-1212"}],
|
|
|
|
"id": 123,
|
|
|
|
"employment": {"school": "MIT"},
|
|
|
|
"email": "alice@example.com",
|
|
|
|
}
|
|
|
|
)
|
2014-10-27 15:37:38 -07:00
|
|
|
|
2014-10-27 16:11:19 -07:00
|
|
|
if sys.version_info >= (2, 7):
|
2021-10-01 11:00:22 -07:00
|
|
|
assert list(person.to_dict(ordered=True).keys()) == [
|
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
"email",
|
|
|
|
"phones",
|
|
|
|
"employment",
|
|
|
|
]
|
2014-10-27 16:11:19 -07:00
|
|
|
else:
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
person.to_dict(ordered=True)
|
2015-05-04 11:00:25 -07:00
|
|
|
|
2019-12-11 22:44:44 -08:00
|
|
|
|
2015-05-04 11:00:25 -07:00
|
|
|
def test_nested_list(addressbook):
|
|
|
|
struct = addressbook.NestedList.new_message()
|
2021-10-01 11:00:22 -07:00
|
|
|
struct.init("list", 2)
|
2015-05-04 11:00:25 -07:00
|
|
|
|
|
|
|
struct.list.init(0, 1)
|
|
|
|
struct.list.init(1, 2)
|
|
|
|
|
|
|
|
struct.list[0][0] = 1
|
|
|
|
struct.list[1][0] = 2
|
|
|
|
struct.list[1][1] = 3
|
|
|
|
|
2019-09-26 22:18:28 -07:00
|
|
|
assert struct.to_dict()["list"] == [[1], [2, 3]]
|