コード例 #1
0
    def test_wrongTypeForMessage(self):
        """
		If a non-L{Message} is passed as the second argument to
		L{PbLiteSerializer.deserialize}, it raises L{AttributeError}.
		"""
        serializer = pbliteserializer.PbLiteSerializer()
        for messageDecoded in [None, 3, 4L, 0.5, {}, set()]:
            self.assertRaises(
                AttributeError,
                lambda: serializer.deserialize(messageDecoded, []))
コード例 #2
0
    def test_wrongTypeForData(self):
        """
		If a non-indexable object is passed as the second argument to
		L{PbLiteSerializer.deserialize}, it raises L{TypeError}.
		"""
        serializer = pbliteserializer.PbLiteSerializer()
        for pblite in [None, 3, 4L, 0.5]:
            messageDecoded = alltypes_pb2.TestAllTypes()
            self.assertRaises(
                TypeError,
                lambda: serializer.deserialize(messageDecoded, pblite))
コード例 #3
0
    def test_deserializeSerializeRepeatedMessage(self):
        """
		Deserializing a repeated Message works.  When serialized, it
		matches the original serialized data.
		"""
        serializer = pbliteserializer.PbLiteSerializer()
        pblite = _getExpectedDefaults()
        # Set the repeated_nested_message
        pblite[48] = [[None, 100], [None, 200]]
        messageDecoded = alltypes_pb2.TestAllTypes()
        serializer.deserialize(messageDecoded, pblite)

        pbliteReencoded = serializer.serialize(messageDecoded)
        self.assertEqual([[None, 100], [None, 200]], pbliteReencoded[48])
コード例 #4
0
    def test_serializeDeserialize(self):
        """
		This is a port of Closure Library's closure/goog/proto2/pbserializer_test.html
		testSerializationAndDeserialization.
		"""
        message = alltypes_pb2.TestAllTypes()

        # Set the fields.
        # Singular.
        message.optional_int32 = 101
        message.optional_int64 = 102
        message.optional_uint32 = 103
        message.optional_uint64 = 104
        message.optional_sint32 = 105
        message.optional_sint64 = 106
        message.optional_fixed32 = 107
        message.optional_fixed64 = 108
        message.optional_sfixed32 = 109
        message.optional_sfixed64 = 110
        message.optional_float = 111.5
        message.optional_double = 112.5
        message.optional_bool = True
        message.optional_string = 'test'
        message.optional_bytes = 'abcd'

        # Note: setting OptionGroup.a is wrong and leads to disaster.
        message.optionalgroup.a = 111

        message.optional_nested_message.b = 112

        message.optional_nested_enum = alltypes_pb2.TestAllTypes.FOO

        # Repeated.
        message.repeated_int32.append(201)
        message.repeated_int32.append(202)

        # Skip a few repeated fields so we can test how null array values are
        # handled.
        message.repeated_string.append('foo')
        message.repeated_string.append('bar')

        message.required_int32 = 1

        # Serialize.
        serializer = pbliteserializer.PbLiteSerializer()
        pblite = serializer.serialize(message)

        self.assertTrue(isinstance(pblite, list))

        # Assert that everything serialized properly.
        self.assertEqual(101, pblite[1])
        self.assertEqual(102, pblite[2])
        self.assertEqual(103, pblite[3])
        self.assertEqual(104, pblite[4])
        self.assertEqual(105, pblite[5])
        self.assertEqual(106, pblite[6])
        self.assertEqual(107, pblite[7])
        self.assertEqual(108, pblite[8])
        self.assertEqual(109, pblite[9])
        self.assertEqual(110, pblite[10])
        self.assertEqual(111.5, pblite[11])
        self.assertEqual(112.5, pblite[12])
        self.assertEqual(1, pblite[13])  # True is serialized as 1
        self.assertEqual('test', pblite[14])
        self.assertEqual('abcd', pblite[15])

        self.assertEqual(111, pblite[16][17])
        self.assertEqual(112, pblite[18][1])

        self.assertEqual(None, pblite[19])
        self.assertEqual(None, pblite[20])

        self.assertEqual(alltypes_pb2.TestAllTypes.FOO, pblite[21])

        self.assertEqual(201, pblite[31][0])
        self.assertEqual(202, pblite[31][1])
        self.assertEqual('foo', pblite[44][0])
        self.assertEqual('bar', pblite[44][1])
        self.assertEqual(1, pblite[50])

        messageDecoded = alltypes_pb2.TestAllTypes()
        serializer.deserialize(messageDecoded, pblite)
        ##print "\n\n", message
        ##print "\n\n", messageDecoded
        self.assertEqual(
            messageDecoded, message, "Messages do not match:\n" +
            str(messageDecoded) + "\n!=\n\n" + str(message))
コード例 #5
0
    def test_defaults(self):
        message = alltypes_pb2.TestAllTypes()
        serializer = pbliteserializer.PbLiteSerializer()
        ser = serializer.serialize(message)

        self.assertEqual(_getExpectedDefaults(), ser)
コード例 #6
0
 def setUp(self):
     self.serializer = pbliteserializer.PbLiteSerializer()