Beispiel #1
0
 def testOneofNestedMergeFrom(self):
     m = unittest_pb2.NestedTestAllTypes()
     m.payload.oneof_uint32 = 11
     m2 = unittest_pb2.NestedTestAllTypes()
     m2.payload.oneof_bytes = b'bb'
     m2.child.payload.oneof_bytes = b'bb'
     m2.MergeFrom(m)
     self.assertEqual('oneof_uint32', m2.payload.WhichOneof('oneof_field'))
     self.assertEqual('oneof_bytes',
                      m2.child.payload.WhichOneof('oneof_field'))
 def testPrintFloatFormat(self):
   # Check that float_format argument is passed to sub-message formatting.
   message = unittest_pb2.NestedTestAllTypes()
   # We use 1.25 as it is a round number in binary.  The proto 32-bit float
   # will not gain additional imprecise digits as a 64-bit Python float and
   # show up in its str.  32-bit 1.2 is noisy when extended to 64-bit:
   #  >>> struct.unpack('f', struct.pack('f', 1.2))[0]
   #  1.2000000476837158
   #  >>> struct.unpack('f', struct.pack('f', 1.25))[0]
   #  1.25
   message.payload.optional_float = 1.25
   # Check rounding at 15 significant digits
   message.payload.optional_double = -.000003456789012345678
   # Check no decimal point.
   message.payload.repeated_float.append(-5642)
   # Check no trailing zeros.
   message.payload.repeated_double.append(.000078900)
   formatted_fields = ['optional_float: 1.25',
                       'optional_double: -3.45678901234568e-6',
                       'repeated_float: -5642',
                       'repeated_double: 7.89e-5']
   text_message = text_format.MessageToString(message, float_format='.15g')
   self.CompareToGoldenText(
       self.RemoveRedundantZeros(text_message),
       'payload {{\n  {}\n  {}\n  {}\n  {}\n}}\n'.format(*formatted_fields))
   # as_one_line=True is a separate code branch where float_format is passed.
   text_message = text_format.MessageToString(message, as_one_line=True,
                                              float_format='.15g')
   self.CompareToGoldenText(
       self.RemoveRedundantZeros(text_message),
       'payload {{ {} {} {} {} }}'.format(*formatted_fields))
Beispiel #3
0
 def testSubUnknownFields(self):
     message = unittest_pb2.TestAllTypes()
     message.optionalgroup.a = 123
     destination = unittest_pb2.TestEmptyMessage()
     destination.ParseFromString(message.SerializeToString())
     sub_unknown_fields = destination.UnknownFields()[0].data
     self.assertEqual(1, len(sub_unknown_fields))
     self.assertEqual(sub_unknown_fields[0].data, 123)
     destination.Clear()
     with self.assertRaises(ValueError) as context:
         len(sub_unknown_fields)
     self.assertIn('UnknownFields does not exist.', str(context.exception))
     with self.assertRaises(ValueError) as context:
         # pylint: disable=pointless-statement
         sub_unknown_fields[0]
     self.assertIn('UnknownFields does not exist.', str(context.exception))
     message.Clear()
     message.optional_uint32 = 456
     nested_message = unittest_pb2.NestedTestAllTypes()
     nested_message.payload.optional_nested_message.ParseFromString(
         message.SerializeToString())
     unknown_fields = (
         nested_message.payload.optional_nested_message.UnknownFields())
     self.assertEqual(unknown_fields[0].data, 456)
     nested_message.ClearField('payload')
     self.assertEqual(unknown_fields[0].data, 456)
     unknown_fields = (
         nested_message.payload.optional_nested_message.UnknownFields())
     self.assertEqual(0, len(unknown_fields))
 def testSubUnknownFields(self):
     message = unittest_pb2.TestAllTypes()
     message.optionalgroup.a = 123
     destination = unittest_pb2.TestEmptyMessage()
     destination.ParseFromString(message.SerializeToString())
     sub_unknown_fields = unknown_fields.UnknownFieldSet(
         destination)[0].data
     self.assertEqual(1, len(sub_unknown_fields))
     self.assertEqual(sub_unknown_fields[0].data, 123)
     destination.Clear()
     self.assertEqual(1, len(sub_unknown_fields))
     self.assertEqual(sub_unknown_fields[0].data, 123)
     message.Clear()
     message.optional_uint32 = 456
     nested_message = unittest_pb2.NestedTestAllTypes()
     nested_message.payload.optional_nested_message.ParseFromString(
         message.SerializeToString())
     unknown_field_set = unknown_fields.UnknownFieldSet(
         nested_message.payload.optional_nested_message)
     self.assertEqual(unknown_field_set[0].data, 456)
     nested_message.ClearField('payload')
     self.assertEqual(unknown_field_set[0].data, 456)
     unknown_field_set = unknown_fields.UnknownFieldSet(
         nested_message.payload.optional_nested_message)
     self.assertEqual(0, len(unknown_field_set))
Beispiel #5
0
 def testAssertOversizeProto(self):
     from google.protobuf.pyext._message import SetAllowOversizeProtos
     SetAllowOversizeProtos(False)
     q = unittest_pb2.NestedTestAllTypes()
     with self.assertRaises(message.DecodeError):
         q.ParseFromString(self.p_serialized)
         print(q)
Beispiel #6
0
 def setUp(self):
     msg = unittest_pb2.NestedTestAllTypes()
     m = msg
     for i in range(101):
         m = m.child
     m.Clear()
     self.p_serialized = msg.SerializeToString()
    def testMergeMessage(self):
        # Test merge one field.
        src = unittest_pb2.TestAllTypes()
        test_util.SetAllFields(src)
        for field in src.DESCRIPTOR.fields:
            if field.containing_oneof:
                continue
            field_name = field.name
            dst = unittest_pb2.TestAllTypes()
            # Only set one path to mask.
            mask = field_mask_pb2.FieldMask()
            mask.paths.append(field_name)
            mask.MergeMessage(src, dst)
            # The expected result message.
            msg = unittest_pb2.TestAllTypes()
            if field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
                repeated_src = getattr(src, field_name)
                repeated_msg = getattr(msg, field_name)
                if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
                    for item in repeated_src:
                        repeated_msg.add().CopyFrom(item)
                else:
                    repeated_msg.extend(repeated_src)
            elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
                getattr(msg, field_name).CopyFrom(getattr(src, field_name))
            else:
                setattr(msg, field_name, getattr(src, field_name))
            # Only field specified in mask is merged.
            self.assertEqual(msg, dst)

        # Test merge nested fields.
        nested_src = unittest_pb2.NestedTestAllTypes()
        nested_dst = unittest_pb2.NestedTestAllTypes()
        nested_src.child.payload.optional_int32 = 1234
        nested_src.child.child.payload.optional_int32 = 5678
        mask = field_mask_pb2.FieldMask()
        mask.FromJsonString('child.payload')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertEqual(1234, nested_dst.child.payload.optional_int32)
        self.assertEqual(0, nested_dst.child.child.payload.optional_int32)

        mask.FromJsonString('child.child.payload')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertEqual(1234, nested_dst.child.payload.optional_int32)
        self.assertEqual(5678, nested_dst.child.child.payload.optional_int32)

        nested_dst.Clear()
        mask.FromJsonString('child.child.payload')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertEqual(0, nested_dst.child.payload.optional_int32)
        self.assertEqual(5678, nested_dst.child.child.payload.optional_int32)

        nested_dst.Clear()
        mask.FromJsonString('child')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertEqual(1234, nested_dst.child.payload.optional_int32)
        self.assertEqual(5678, nested_dst.child.child.payload.optional_int32)

        # Test MergeOptions.
        nested_dst.Clear()
        nested_dst.child.payload.optional_int64 = 4321
        # Message fields will be merged by default.
        mask.FromJsonString('child.payload')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertEqual(1234, nested_dst.child.payload.optional_int32)
        self.assertEqual(4321, nested_dst.child.payload.optional_int64)
        # Change the behavior to replace message fields.
        mask.FromJsonString('child.payload')
        mask.MergeMessage(nested_src, nested_dst, True, False)
        self.assertEqual(1234, nested_dst.child.payload.optional_int32)
        self.assertEqual(0, nested_dst.child.payload.optional_int64)

        # By default, fields missing in source are not cleared in destination.
        nested_dst.payload.optional_int32 = 1234
        self.assertTrue(nested_dst.HasField('payload'))
        mask.FromJsonString('payload')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertTrue(nested_dst.HasField('payload'))
        # But they are cleared when replacing message fields.
        nested_dst.Clear()
        nested_dst.payload.optional_int32 = 1234
        mask.FromJsonString('payload')
        mask.MergeMessage(nested_src, nested_dst, True, False)
        self.assertFalse(nested_dst.HasField('payload'))

        nested_src.payload.repeated_int32.append(1234)
        nested_dst.payload.repeated_int32.append(5678)
        # Repeated fields will be appended by default.
        mask.FromJsonString('payload.repeatedInt32')
        mask.MergeMessage(nested_src, nested_dst)
        self.assertEqual(2, len(nested_dst.payload.repeated_int32))
        self.assertEqual(5678, nested_dst.payload.repeated_int32[0])
        self.assertEqual(1234, nested_dst.payload.repeated_int32[1])
        # Change the behavior to replace repeated fields.
        mask.FromJsonString('payload.repeatedInt32')
        mask.MergeMessage(nested_src, nested_dst, False, True)
        self.assertEqual(1, len(nested_dst.payload.repeated_int32))
        self.assertEqual(1234, nested_dst.payload.repeated_int32[0])
Beispiel #8
0
 def testClearStubRepeatedField(self):
     msg = unittest_pb2.NestedTestAllTypes()
     int32_array = msg.payload.repeated_int32
     msg.payload.ClearField("repeated_int32")
     int32_array.append(123)
     self.assertEqual(0, msg.payload.ByteSize())
Beispiel #9
0
 def testSucceedOversizeProto(self):
     from google.protobuf.pyext._message import SetAllowOversizeProtos
     SetAllowOversizeProtos(True)
     q = unittest_pb2.NestedTestAllTypes()
     q.ParseFromString(self.p_serialized)