From effacb872f223a7144323b4d1bdcae55f89cae3d Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Wed, 4 Sep 2013 11:20:05 -0700 Subject: [PATCH] Fix problem with to_dict --- capnp/capnp.pyx | 3 +- test/{all-types.capnp => all_types.capnp} | 0 test/test_regression.py | 2 +- test/test_serialization.py | 91 ++++++----------------- 4 files changed, 26 insertions(+), 70 deletions(-) rename test/{all-types.capnp => all_types.capnp} (100%) diff --git a/capnp/capnp.pyx b/capnp/capnp.pyx index ac7a60c..e45079e 100644 --- a/capnp/capnp.pyx +++ b/capnp/capnp.pyx @@ -438,7 +438,8 @@ cdef _to_dict(msg): pass 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 diff --git a/test/all-types.capnp b/test/all_types.capnp similarity index 100% rename from test/all-types.capnp rename to test/all_types.capnp diff --git a/test/test_regression.py b/test/test_regression.py index 2d3c775..4b086ae 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -195,7 +195,7 @@ def test_addressbook_resizable(addressbook): @pytest.fixture 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: # - Read each field in Python and assert that it is equal to the expected value. diff --git a/test/test_serialization.py b/test/test_serialization.py index 1af396c..416d54d 100644 --- a/test/test_serialization.py +++ b/test/test_serialization.py @@ -2,92 +2,47 @@ import pytest import capnp import os import platform +import test_regression this_dir = os.path.dirname(__file__) @pytest.fixture -def addressbook(): - return capnp.load(os.path.join(this_dir, 'addressbook.capnp')) +def all_types(): + return capnp.load(os.path.join(this_dir, 'all_types.capnp')) -def build_message(addressbook): - 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): +def test_roundtrip_file(all_types): f = open('example', 'w') - msg = build_message(addressbook) + msg = all_types.TestAllTypes.new_message() + test_regression.init_all_types(msg) msg.write(f) f = open('example', 'r') - msg = addressbook.AddressBook.read(f) - check_msg(msg) + msg = all_types.TestAllTypes.read(f) + test_regression.check_all_types(msg) -def test_roundtrip_file_packed(addressbook): +def test_roundtrip_file_packed(all_types): f = open('example', 'w') - msg = build_message(addressbook) + msg = all_types.TestAllTypes.new_message() + test_regression.init_all_types(msg) msg.write_packed(f) f = open('example', 'r') - msg = addressbook.AddressBook.read_packed(f) - check_msg(msg) + msg = all_types.TestAllTypes.read_packed(f) + test_regression.check_all_types(msg) -def test_roundtrip_bytes(addressbook): - msg = build_message(addressbook) +def test_roundtrip_bytes(all_types): + msg = all_types.TestAllTypes.new_message() + test_regression.init_all_types(msg) message_bytes = msg.to_bytes() - msg = addressbook.AddressBook.from_bytes(message_bytes) - check_msg(msg) + msg = all_types.TestAllTypes.from_bytes(message_bytes) + test_regression.check_all_types(msg) @pytest.mark.skipif("platform.python_implementation() == 'PyPy'") -def test_roundtrip_dict(addressbook): - msg = build_message(addressbook) +def test_roundtrip_dict(all_types): + msg = all_types.TestAllTypes.new_message() + test_regression.init_all_types(msg) d = msg.to_dict() - msg = addressbook.AddressBook.from_dict(d) - check_msg(msg) + msg = all_types.TestAllTypes.from_dict(d) + test_regression.check_all_types(msg)