Fix problem with to_dict
This commit is contained in:
@@ -438,7 +438,8 @@ cdef _to_dict(msg):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
for field in msg.schema.non_union_fields:
|
for field in msg.schema.non_union_fields:
|
||||||
ret[field] = _to_dict(getattr(msg, field))
|
if msg._has(field):
|
||||||
|
ret[field] = _to_dict(getattr(msg, field))
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ def test_addressbook_resizable(addressbook):
|
|||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def all_types():
|
def all_types():
|
||||||
return capnp.load(os.path.join(this_dir, 'all-types.capnp'))
|
return capnp.load(os.path.join(this_dir, 'all_types.capnp'))
|
||||||
|
|
||||||
# TODO: These tests should be extended to:
|
# TODO: These tests should be extended to:
|
||||||
# - Read each field in Python and assert that it is equal to the expected value.
|
# - Read each field in Python and assert that it is equal to the expected value.
|
||||||
|
|||||||
@@ -2,92 +2,47 @@ import pytest
|
|||||||
import capnp
|
import capnp
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
|
import test_regression
|
||||||
|
|
||||||
this_dir = os.path.dirname(__file__)
|
this_dir = os.path.dirname(__file__)
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def addressbook():
|
def all_types():
|
||||||
return capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
|
return capnp.load(os.path.join(this_dir, 'all_types.capnp'))
|
||||||
|
|
||||||
def build_message(addressbook):
|
def test_roundtrip_file(all_types):
|
||||||
addresses = addressbook.AddressBook.new_message()
|
|
||||||
people = addresses.init('people', 2)
|
|
||||||
|
|
||||||
alice = people[0]
|
|
||||||
alice.id = 123
|
|
||||||
alice.name = 'Alice'
|
|
||||||
alice.email = 'alice@example.com'
|
|
||||||
alicePhones = alice.init('phones', 1)
|
|
||||||
alicePhones[0].number = "555-1212"
|
|
||||||
alicePhones[0].type = 'mobile'
|
|
||||||
alice.employment.school = "MIT"
|
|
||||||
|
|
||||||
bob = people[1]
|
|
||||||
bob.id = 456
|
|
||||||
bob.name = 'Bob'
|
|
||||||
bob.email = 'bob@example.com'
|
|
||||||
bobPhones = bob.init('phones', 2)
|
|
||||||
bobPhones[0].number = "555-4567"
|
|
||||||
bobPhones[0].type = 'home'
|
|
||||||
bobPhones[1].number = "555-7654"
|
|
||||||
bobPhones[1].type = 'work'
|
|
||||||
bob.employment.unemployed = None
|
|
||||||
|
|
||||||
return addresses
|
|
||||||
|
|
||||||
def check_msg(addresses):
|
|
||||||
people = addresses.people
|
|
||||||
|
|
||||||
alice = people[0]
|
|
||||||
assert alice.id == 123
|
|
||||||
assert alice.name == 'Alice'
|
|
||||||
assert alice.email == 'alice@example.com'
|
|
||||||
alicePhones = alice.phones
|
|
||||||
assert alicePhones[0].number == "555-1212"
|
|
||||||
assert alicePhones[0].type == 'mobile'
|
|
||||||
assert alice.employment.school == "MIT"
|
|
||||||
|
|
||||||
bob = people[1]
|
|
||||||
assert bob.id == 456
|
|
||||||
assert bob.name == 'Bob'
|
|
||||||
assert bob.email == 'bob@example.com'
|
|
||||||
bobPhones = bob.phones
|
|
||||||
assert bobPhones[0].number == "555-4567"
|
|
||||||
assert bobPhones[0].type == 'home'
|
|
||||||
assert bobPhones[1].number == "555-7654"
|
|
||||||
assert bobPhones[1].type == 'work'
|
|
||||||
assert bob.employment.unemployed == None
|
|
||||||
|
|
||||||
|
|
||||||
def test_roundtrip_file(addressbook):
|
|
||||||
f = open('example', 'w')
|
f = open('example', 'w')
|
||||||
msg = build_message(addressbook)
|
msg = all_types.TestAllTypes.new_message()
|
||||||
|
test_regression.init_all_types(msg)
|
||||||
msg.write(f)
|
msg.write(f)
|
||||||
|
|
||||||
f = open('example', 'r')
|
f = open('example', 'r')
|
||||||
msg = addressbook.AddressBook.read(f)
|
msg = all_types.TestAllTypes.read(f)
|
||||||
check_msg(msg)
|
test_regression.check_all_types(msg)
|
||||||
|
|
||||||
def test_roundtrip_file_packed(addressbook):
|
def test_roundtrip_file_packed(all_types):
|
||||||
f = open('example', 'w')
|
f = open('example', 'w')
|
||||||
msg = build_message(addressbook)
|
msg = all_types.TestAllTypes.new_message()
|
||||||
|
test_regression.init_all_types(msg)
|
||||||
msg.write_packed(f)
|
msg.write_packed(f)
|
||||||
|
|
||||||
f = open('example', 'r')
|
f = open('example', 'r')
|
||||||
msg = addressbook.AddressBook.read_packed(f)
|
msg = all_types.TestAllTypes.read_packed(f)
|
||||||
check_msg(msg)
|
test_regression.check_all_types(msg)
|
||||||
|
|
||||||
def test_roundtrip_bytes(addressbook):
|
def test_roundtrip_bytes(all_types):
|
||||||
msg = build_message(addressbook)
|
msg = all_types.TestAllTypes.new_message()
|
||||||
|
test_regression.init_all_types(msg)
|
||||||
message_bytes = msg.to_bytes()
|
message_bytes = msg.to_bytes()
|
||||||
|
|
||||||
msg = addressbook.AddressBook.from_bytes(message_bytes)
|
msg = all_types.TestAllTypes.from_bytes(message_bytes)
|
||||||
check_msg(msg)
|
test_regression.check_all_types(msg)
|
||||||
|
|
||||||
@pytest.mark.skipif("platform.python_implementation() == 'PyPy'")
|
@pytest.mark.skipif("platform.python_implementation() == 'PyPy'")
|
||||||
def test_roundtrip_dict(addressbook):
|
def test_roundtrip_dict(all_types):
|
||||||
msg = build_message(addressbook)
|
msg = all_types.TestAllTypes.new_message()
|
||||||
|
test_regression.init_all_types(msg)
|
||||||
d = msg.to_dict()
|
d = msg.to_dict()
|
||||||
|
|
||||||
msg = addressbook.AddressBook.from_dict(d)
|
msg = all_types.TestAllTypes.from_dict(d)
|
||||||
check_msg(msg)
|
test_regression.check_all_types(msg)
|
||||||
|
|||||||
Reference in New Issue
Block a user