當前位置: 首頁>>代碼示例>>Python>>正文


Python Serializer.load_definitions方法代碼示例

本文整理匯總了Python中serializer.Serializer.load_definitions方法的典型用法代碼示例。如果您正苦於以下問題:Python Serializer.load_definitions方法的具體用法?Python Serializer.load_definitions怎麽用?Python Serializer.load_definitions使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在serializer.Serializer的用法示例。


在下文中一共展示了Serializer.load_definitions方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: TestSerialize

# 需要導入模塊: from serializer import Serializer [as 別名]
# 或者: from serializer.Serializer import load_definitions [as 別名]

#.........這裏部分代碼省略.........

    def test_explicit_call(self):
        enc = self.s.serialize("test.Test", *self.blank_args)

        def f(obj):
            self.assertTrue(obj.IsInitialized())
            for (_, value) in obj.ListFields():
                self.assertIn(value, self.blank_args)

        self.s.add_handler("Test", f)
        self.s.unserialize(enc)

    def test_negative_values(self):
        enc = self.s.serialize("Test", *self.neg_args)

        def f(obj):
            self.assertTrue(obj.IsInitialized())
            for (_, value) in obj.ListFields():
                self.assertIn(value, self.neg_args)

        self.s.add_handler("Test", f)
        self.s.unserialize(enc)

    def test_illegal_length_option(self):
        for args in self.illegal_len_args:
            with self.assertRaises(FieldLengthUnsupportedException):
                self.s.serialize("Test", *args)

    def test_too_long_option(self):
        for args in self.too_long_args:
            with self.assertRaises(FieldTooLongException):
                self.s.serialize("Test", *args)

    def test_unknown_message(self):
        with self.assertRaises(UnknownMessageException):
            self.s.serialize("Unknown")

    def test_unknown_message_explicit(self):
        with self.assertRaises(UnknownMessageException):
            self.s.serialize("test.Unknown")

    def test_unknown_message_explicit2(self):
        with self.assertRaises(UnknownMessageException):
            self.s.serialize("t.Unknown")

    def test_incomplete_definition_empty(self):
        with self.assertRaises(FieldNotDefinedException):
            self.s.serialize("Test")

    def test_incomplete_definition_unnamed(self):
        with self.assertRaises(FieldNotDefinedException):
            self.s.serialize("Test", 0)

    def test_incomplete_definition_named(self):
        with self.assertRaises(FieldNotDefinedException):
            self.s.serialize("Test", boolnolen = 0)

    def test_incomplete_definition_hybrid(self):
        with self.assertRaises(FieldNotDefinedException):
            self.s.serialize("Test", 0, boolnolen = 0)

    def test_wrong_type(self):
        args = self.blank_args[:]
        args[0] = ""
        with self.assertRaises(FieldWrongTypeException):
            self.s.serialize("Test", *args)

    def test_wrong_type_value(self):
        args = self.blank_args[:]
        args[5] = 9999999999999999
        with self.assertRaises(FieldWrongTypeException):
            self.s.serialize("Test", *args)

    def test_hash_name(self):
        name = "test"
        for i in range(1,9):
            self.assertEqual(len(self.s._hash_name(name, i)),
                                i)

    def test_reload_definitions(self):
        class T(logging.Filter):
            def filter(self, record):
                self.called = True
                return 0
        filter = T()
        filter.called = False
        logging.getLogger().addFilter(filter)

        self.assertIn("'test_pb2Test\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'", self.s.messages)
        self.s.load_definitions()
        self.assertIn("'test_pb2Test\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'", self.s.messages)
        self.assertTrue(filter.called)

        logging.getLogger().removeFilter(filter)

    def test_wrong_type_value_check(self):
        args = self.blank_args[:]
        args[1] = 1.10
        with self.assertRaises(FieldLengthUnsupportedException):
            self.s.serialize("Test", *args)
開發者ID:qstokkink,項目名稱:TriblerProtobufSerialization,代碼行數:104,代碼來源:test_serialize.py


注:本文中的serializer.Serializer.load_definitions方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。