def test_pass_instance(self): m = self.populate_MessageOfTypes() d = to_dict(m) d['dubl'] = 1 m2 = to_protobuf(m, d) assert m is m2 assert m.dubl == 1
def test_extensions(self): m = MessageOfTypes() primitives = { extDouble: 123.4, extString: "string", NestedExtension.extInt: 4 } for key, value in primitives.items(): m.Extensions[key] = value m.Extensions[NestedExtension.extNested].req = "nested" # Confirm compatibility with JSON serialization res = json.loads(json.dumps(to_dict(m))) assert '___X' in res exts = res['___X'] assert set(exts.keys()) == set( [str(f.number) for f, _ in m.ListFields() if f.is_extension]) for key, value in primitives.items(): assert exts[str(key.number)] == value assert exts[str(NestedExtension.extNested.number)]['req'] == 'nested' deser = to_protobuf(MessageOfTypes, res) assert deser for key, value in primitives.items(): assert deser.Extensions[key] == m.Extensions[key] assert deser.Extensions[NestedExtension.extNested].req == m.Extensions[ NestedExtension.extNested].req
def test_incomplete(self): m = self.populate_MessageOfTypes() d = to_dict(m) d.pop('dubl') m2 = to_protobuf(MessageOfTypes, d) assert m2.dubl == 0 assert m != m2
def test_basics(self): m = self.populate_MessageOfTypes() d = to_dict(m) self.compare(m, d, ['nestedRepeated']) m2 = to_protobuf(MessageOfTypes, d) assert m == m2
def test_strict(self): m = self.populate_MessageOfTypes() d = to_dict(m) d['meow'] = 1 with nose.tools.assert_raises(KeyError): m2 = to_protobuf(MessageOfTypes, d) m2 = to_protobuf(MessageOfTypes, d, strict=False) assert m == m2
def test_nested_repeated(self): m = self.populate_MessageOfTypes() m.nestedRepeated.extend( [MessageOfTypes.NestedType(req=str(i)) for i in range(10)]) d = to_dict(m) self.compare(m, d, exclude=['nestedRepeated']) assert d['nestedRepeated'] == [{'req': str(i)} for i in range(10)] m2 = to_protobuf(MessageOfTypes, d) assert m == m2
def test_repeated_enum(self): m = self.populate_MessageOfTypes() d = to_dict(m, use_enum_labels=True) self.compare(m, d, ['enm', 'enmRepeated', 'nestedRepeated']) assert d['enmRepeated'] == ['A', 'C'] m2 = to_protobuf(MessageOfTypes, d) assert m == m2 d['enmRepeated'] = ['MEOW'] with nose.tools.assert_raises(KeyError): to_protobuf(MessageOfTypes, d)
def test_reverse(self): m = self.populate_MessageOfTypes() m2 = to_protobuf(MessageOfTypes, to_dict(m)) assert m == m2 m2.dubl = 0 assert m2 != m
def test_to_dict(self): d = to_dict(self.message) assert d['req'] == {}