def test_repeated_enum(m): d = protobuf_to_dict(m) compare(m, d, ['nestedRepeated']) assert d['enmRepeated'] == ['A', 'C'] m2 = dict_to_protobuf(d, MessageOfTypes) assert m == m2 d['enmRepeated'] = ['MEOW'] with pytest.raises(KeyError): dict_to_protobuf(d, MessageOfTypes)
def test_non_strict(m): d = protobuf_to_dict(m) d['non_existing_field'] = 'data' d['temporary_field'] = 'helping_state' with pytest.raises(KeyError): dict_to_protobuf(d, MessageOfTypes) m2 = dict_to_protobuf(d, MessageOfTypes, strict=False) with pytest.raises(AttributeError): m2.temporary_field
def test_repeated_enum(m): d = protobuf_to_dict(m) compare(m, d, ['nestedRepeated']) assert d['enmRepeated'] == ['A', 'C'] m2 = dict_to_protobuf(d, MessageOfTypes) assert m == m2 d['enmRepeated'] = ['MEOW'] with pytest.raises(KeyError): dict_to_protobuf(d, MessageOfTypes)
def test_non_strict(m): d = protobuf_to_dict(m) d['non_existing_field'] = 'data' d['temporary_field'] = 'helping_state' with pytest.raises(KeyError): dict_to_protobuf(d, MessageOfTypes) m2 = dict_to_protobuf(d, MessageOfTypes, strict=False) with pytest.raises(AttributeError): m2.temporary_field
def test_nested_repeated(m): m.nestedRepeated.extend( [MessageOfTypes.NestedType(req=str(i)) for i in range(10)]) d = protobuf_to_dict(m) compare(m, d, exclude=['nestedRepeated']) assert d['nestedRepeated'] == [{'req': str(i)} for i in range(10)] m2 = dict_to_protobuf(d, MessageOfTypes) assert m == m2
def test_nested_repeated(m): m.nestedRepeated.extend( [MessageOfTypes.NestedType(req=str(i)) for i in range(10)]) d = protobuf_to_dict(m) compare(m, d, exclude=['nestedRepeated']) assert d['nestedRepeated'] == [{'req': str(i)} for i in range(10)] m2 = dict_to_protobuf(d, MessageOfTypes) assert m == m2
def test_reverse_type_conversion(m): class String(object): def __init__(self, value): self.value = value def __str__(self): return self.value d = protobuf_to_dict(m) d['strng'] = String('string') m2 = dict_to_protobuf(d, MessageOfTypes) assert m == m2
def test_conditional_container_mapping(m): class truedict(dict): pass class falsedict(dict): pass containers = [(MessageOfTypes(bol=True), truedict), (MessageOfTypes(bol=False), falsedict), (MessageOfTypes.NestedType(), dict)] m.bol = True d = protobuf_to_dict(m, containers=containers) p = dict_to_protobuf(d, containers=containers) assert isinstance(d, truedict) assert isinstance(p, MessageOfTypes) m.bol = False d = protobuf_to_dict(m, containers=containers) p = dict_to_protobuf(d, containers=containers) assert isinstance(d, falsedict) assert isinstance(p, MessageOfTypes)
def test_container_mapping(m): class mapping(dict): pass containers = [(MessageOfTypes.NestedType(), mapping), (MessageOfTypes(), dict)] m.nestedRepeated.extend([MessageOfTypes.NestedType(req='1')]) d = protobuf_to_dict(m, containers=containers) m = dict_to_protobuf(d, containers=containers) assert isinstance(d, dict) assert isinstance(d['nested'], mapping) assert isinstance(m, MessageOfTypes) assert isinstance(m.nested, MessageOfTypes.NestedType)
def test_container_mapping(m): class mapping(dict): pass containers = [(MessageOfTypes.NestedType(), mapping), (MessageOfTypes(), dict)] m.nestedRepeated.extend([MessageOfTypes.NestedType(req='1')]) d = protobuf_to_dict(m, containers=containers) m = dict_to_protobuf(d, containers=containers) assert isinstance(d, dict) assert isinstance(d['nested'], mapping) assert isinstance(m, MessageOfTypes) assert isinstance(m.nested, MessageOfTypes.NestedType)
def test_conditional_container_mapping(m): class truedict(dict): pass class falsedict(dict): pass containers = [(MessageOfTypes(bol=True), truedict), (MessageOfTypes(bol=False), falsedict), (MessageOfTypes.NestedType(), dict)] m.bol = True d = protobuf_to_dict(m, containers=containers) p = dict_to_protobuf(d, containers=containers) assert isinstance(d, truedict) assert isinstance(p, MessageOfTypes) m.bol = False d = protobuf_to_dict(m, containers=containers) p = dict_to_protobuf(d, containers=containers) assert isinstance(d, falsedict) assert isinstance(p, MessageOfTypes)
def test_use_enum_labels(m): d = protobuf_to_dict(m) compare(m, d, ['nestedRepeated']) assert d['enm'] == 'C' assert d['enmRepeated'] == ['A', 'C'] m2 = dict_to_protobuf(d, MessageOfTypes) assert m == m2 d['enm'] = 'MEOW' with pytest.raises(KeyError): dict_to_protobuf(d, MessageOfTypes) d['enm'] = 'A' d['enmRepeated'] = ['B'] dict_to_protobuf(d, MessageOfTypes) d['enmRepeated'] = ['CAT'] with pytest.raises(KeyError): dict_to_protobuf(d, MessageOfTypes)
def test_use_enum_labels(m): d = protobuf_to_dict(m) compare(m, d, ['nestedRepeated']) assert d['enm'] == 'C' assert d['enmRepeated'] == ['A', 'C'] m2 = dict_to_protobuf(d, MessageOfTypes) assert m == m2 d['enm'] = 'MEOW' with pytest.raises(KeyError): dict_to_protobuf(d, MessageOfTypes) d['enm'] = 'A' d['enmRepeated'] = ['B'] dict_to_protobuf(d, MessageOfTypes) d['enmRepeated'] = ['CAT'] with pytest.raises(KeyError): dict_to_protobuf(d, MessageOfTypes)
def test_basics(m): d = protobuf_to_dict(m) compare(m, d, ['nestedRepeated']) m2 = dict_to_protobuf(d, MessageOfTypes) assert m == m2
def test_pass_instance(m): d = protobuf_to_dict(m) d['dubl'] = 1 m2 = dict_to_protobuf(d, m) assert m is m2 assert m.dubl == 1
def test_pass_instance(m): d = protobuf_to_dict(m) d['dubl'] = 1 m2 = dict_to_protobuf(d, m) assert m is m2 assert m.dubl == 1
def test_incomplete(m): d = protobuf_to_dict(m) d.pop('dubl') m2 = dict_to_protobuf(d, MessageOfTypes) assert m2.dubl == 0 assert m != m2
def test_incomplete(m): d = protobuf_to_dict(m) d.pop('dubl') m2 = dict_to_protobuf(d, MessageOfTypes) assert m2.dubl == 0 assert m != m2
def test_basics(m): d = protobuf_to_dict(m) compare(m, d, ['nestedRepeated']) m2 = dict_to_protobuf(d, MessageOfTypes) assert m == m2
def test_reverse(m): m2 = dict_to_protobuf(protobuf_to_dict(m), MessageOfTypes) assert m == m2 m2.dubl = 0 assert m2 != m
def test_reverse(m): m2 = dict_to_protobuf(protobuf_to_dict(m), MessageOfTypes) assert m == m2 m2.dubl = 0 assert m2 != m