コード例 #1
0
    def test_full_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(protobuf_to_dict(m, extensions=FULL_EXTENSIONS)))
        expected = {u'tests.extDouble': 123.4,
                    u'tests.NestedExtension.extInt': 4,
                    u'tests.NestedExtension.extNested': {u'req': u'nested'},
                    u'tests.extString': u'string'}
        self.assertEqual(expected, res)
        self.assertEqual(set(res.keys()), set([str(f.full_name) for f, _ in m.ListFields() if f.is_extension]))
        for key, value in primitives.items():
            assert res[key.full_name] == value
        assert res[str(NestedExtension.extNested.full_name)]['req'] == 'nested'

        deser = dict_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
コード例 #2
0
    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
コード例 #3
0
    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(protobuf_to_dict(m)))
        expected = {u'___X': {u'100': 123.4,
                              u'101': u'string',
                              u'102': 4,
                              u'103': {u'req': u'nested'}}}
        self.assertEqual(expected, 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 = dict_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
コード例 #4
0
    def test_nested_ignore_none(self):
        m = MessageOfTypes()
        m.nestedMap['123'].req = '42'

        d = protobuf_to_dict(m)
        d['nestedMap']['123']['req'] = None

        m2 = dict_to_protobuf(MessageOfTypes, d, ignore_none=True)
        assert m2.nestedMap['123'].req == ''
コード例 #5
0
    def test_ignore_none(self):
        m = MessageOfTypes()
        d = protobuf_to_dict(m)
        assert d == {}

        for field in m.DESCRIPTOR.fields:
            d[field.name] = None

        m2 = dict_to_protobuf(MessageOfTypes, d, ignore_none=True)
        assert m == m2
コード例 #6
0
    def test_nested_repeated(self):
        m = self.populate_MessageOfTypes()
        m.nestedRepeated.extend([MessageOfTypes.NestedType(req=str(i)) for i in range(10)])

        d = protobuf_to_dict(m)
        self.compare(m, d, exclude=['nestedRepeated'])
        assert d['nestedRepeated'] == [{'req': str(i)} for i in range(10)]

        m2 = dict_to_protobuf(MessageOfTypes, d)
        assert m == m2
コード例 #7
0
    def test_including_default_value_fields(self):
        m = MessageOfTypes()
        d = protobuf_to_dict(m)
        assert d == {}

        d = protobuf_to_dict(m, including_default_value_fields=True)
        for field in m.DESCRIPTOR.fields:
            if field.name != 'nested':
                assert field.name in d, field.name

        m2 = dict_to_protobuf(MessageOfTypes, d)
        assert m == m2
コード例 #8
0
    def test_full_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(protobuf_to_dict(m, extensions=FULL_EXTENSIONS)))
        expected = {
            u'tests.extDouble': 123.4,
            u'tests.NestedExtension.extInt': 4,
            u'tests.NestedExtension.extNested': {
                u'req': u'nested'
            },
            u'tests.extString': u'string'
        }
        self.assertEqual(expected, res)
        self.assertEqual(
            set(res.keys()),
            set([
                str(f.full_name) for f, _ in m.ListFields() if f.is_extension
            ]))
        for key, value in primitives.items():
            assert res[key.full_name] == value
        assert res[str(NestedExtension.extNested.full_name)]['req'] == 'nested'

        deser = dict_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
コード例 #9
0
    def test_nested_repeated(self, add_class_metadata,
                             overwrite_package_with_name):
        m = self.populate_MessageOfTypes()
        m.nestedRepeated.extend(
            [MessageOfTypes.NestedType(req=str(i)) for i in range(10)])

        d = protobuf_to_dict(
            m,
            add_class_metadata=add_class_metadata,
            overwrite_package_with_name=overwrite_package_with_name)
        self.compare(m,
                     d,
                     exclude=['nestedRepeated'],
                     add_class_metadata=add_class_metadata,
                     overwrite_package_with_name=overwrite_package_with_name)
        if not add_class_metadata:
            assert d['nestedRepeated'] == [{'req': str(i)} for i in range(10)]
        else:
            if overwrite_package_with_name is not None:
                assert d['nestedRepeated'] == [{
                    'req':
                    str(i),
                    _CLASS_KEY:
                    'NestedType',
                    _PACKAGE_KEY:
                    overwrite_package_with_name
                } for i in range(10)]
            else:
                assert d['nestedRepeated'] == [{
                    'req':
                    str(i),
                    _CLASS_KEY:
                    'NestedType',
                    _PACKAGE_KEY:
                    'tests.MessageOfTypes'
                } for i in range(10)]

        m2 = dict_to_protobuf(MessageOfTypes, d)
        assert m == m2
コード例 #10
0
 def populate_MessageOfTypes(self):
     m = MessageOfTypes()
     m.dubl = 1.7e+308
     m.flot = 3.4e+038
     m.i32 = 2**31 - 1  # 2147483647 #
     m.i64 = 2**63 - 1  #0x7FFFFFFFFFFFFFFF
     m.ui32 = 2**32 - 1
     m.ui64 = 2**64 - 1
     m.si32 = -1 * m.i32
     m.si64 = -1 * m.i64
     m.f32 = m.i32
     m.f64 = m.i64
     m.sf32 = m.si32
     m.sf64 = m.si64
     m.bol = True
     m.strng = "string"
     m.byts = b'\n\x14\x1e'
     assert len(m.byts) == 3, len(m.byts)
     m.nested.req = "req"
     m.enm = MessageOfTypes.C  #@UndefinedVariable
     m.enmRepeated.extend([MessageOfTypes.A, MessageOfTypes.C])
     m.range.extend(range(10))
     return m
コード例 #11
0
 def populate_MessageOfTypes(self):
     m = MessageOfTypes()
     m.dubl = 1.7e+308
     m.flot = 3.4e+038
     m.i32 = 2 ** 31 - 1 # 2147483647 #
     m.i64 = 2 ** 63 - 1 #0x7FFFFFFFFFFFFFFF
     m.ui32 = 2 ** 32 - 1
     m.ui64 = 2 ** 64 - 1
     m.si32 = -1 * m.i32
     m.si64 = -1 * m.i64
     m.f32 = m.i32
     m.f64 = m.i64
     m.sf32 = m.si32
     m.sf64 = m.si64
     m.bol = True
     m.strng = "string"
     m.byts = b'\n\x14\x1e'
     assert len(m.byts) == 3, len(m.byts)
     m.nested.req = "req"
     m.enm = MessageOfTypes.C #@UndefinedVariable
     m.enmRepeated.extend([MessageOfTypes.A, MessageOfTypes.C])
     m.range.extend(range(10))
     m.optional_string = 'optional'
     return m