コード例 #1
0
ファイル: test_message.py プロジェクト: qtacore/QT4S
 def test_fix_size_0(self):
     s = BArrayFixSize0()
     item1 = BItem()
     item1.foo = 1
     item2 = BItem()
     item2.foo = 2
     s.arr = [item1, item2]
     buf = s.dumps(BinarySerializer())
     s2 = BArrayFixSize0()
     s2.loads(buf, BinarySerializer())
     self.assertEqual(s.dumps(), s2.dumps())
コード例 #2
0
ファイル: test_message.py プロジェクト: qtacore/QT4S
 def test_size_ref(self):
     s = BArrayRefSize()
     s.size = 3
     item = s.arr.add()
     item.foo = 1
     item = s.arr.add()
     item.foo = 2
     item = s.arr.add()
     item.foo = 3
     buf = s.dumps(BinarySerializer())
     s2 = BArrayRefSize()
     s2.loads(buf, BinarySerializer())
     self.assertEqual(s.dumps(), s2.dumps())
コード例 #3
0
    def test_load_byte_sized(self):
        sized_buff = SizedBuff()
        sized_buff.code = 10
        sized_buff.buff = "test"
        sized_buff.buff1 = "abcdefg"
        sized_buff.buff2 = "1234567"
        data = sized_buff.dumps(BinarySerializer())

        loaded_buff = SizedBuff()
        remain_data = loaded_buff.loads(data, BinarySerializer())
        self.assertEqual(remain_data, None)
        self.assertEqual(loaded_buff.code, 10)
        self.assertEqual(loaded_buff.buff1_len, 7)
        self.assertEqual(loaded_buff.buff1, "abcdefg")
        self.assertEqual(loaded_buff.buff2, "1234567")
コード例 #4
0
ファイル: test_socket_channel.py プロジェクト: qtacore/QT4S
class FooRequest(RequestBase):
    _struct_ = [Field("buffer", Buffer)]
    _serializer_ = BinarySerializer()
    response_class = FooResponse

    def get_sequence_id(self):
        return None
コード例 #5
0
    def test_dumps_and_loads(self):
        foo = FooMessage()
        foo.x = 3
        foo.s = "xxxx"
        foo.jce.id = 1
        foo.jce.name = "foo"
        foo.jce.array = [1, 2, 3]
        foo.jce.mapping = {"a": 1}
        person = foo.pb.people.add()
        person.id = 1
        person.name = "foo"
        person.email = "*****@*****.**"
        phone = person.phones.add()
        phone.type = person.MOBILE
        phone.number = "13312345678"

        foo.fill_size_ref(BinarySerializer())

        foo_json = str(foo)
        foo_json_dict = json.loads(foo_json)
        self.assertEqual(foo_json_dict["unknown"], str(Uninitialized))

        self.assertRaises(ValueError, foo.dumps)
        foo.unknown = "xxx"
        foo_dict = foo.dumps()
        self.assertEqual(foo_dict["x"], 3)
        self.assertEqual(foo_dict["s_len"], len(foo_dict["s"]))
        self.assertEqual(foo_dict["s"], foo.s)
        self.assertEqual(foo_dict["jce_len"], foo.jce_len)
        self.assertEqual(foo_dict["jce"], foo.jce.dumps())
        self.assertEqual(foo_dict["pb_len"], foo.pb_len)
        self.assertEqual(foo_dict["pb"], foo.pb.dumps())
        self.assertTrue("option1" not in foo_dict)
        self.assertTrue("option2" not in foo_dict)
        self.assertEqual(foo_dict["unknown"], "xxx")
コード例 #6
0
ファイル: helloserver.py プロジェクト: qtacore/QT4SDemoProj
 def on_request(self, req):
     rsp = HelloResponse()
     rsp.result = "Hello, %s!" % req.username
     rsp.seq = req.seq
     rsp.len = 8 + len(rsp.result)
     rspbuf = rsp.dumps(BinarySerializer())
     sent_size = 0
     while sent_size < rsp.len:
         sent_size += self.connection.send(rspbuf[sent_size:])
コード例 #7
0
    def test_dump_without_serializer(self):
        foo_buff = FooBuff()
        foo_buff.buf = "xxx"
        foo_buff.a = 1.1111
        self.assertRaises(ValueError, foo_buff.dumps)

        foo_buff.fill_size_ref(BinarySerializer())
        data = foo_buff.dumps()
        self.assertEqual(data, {"buf" : "xxx",
                                "a" : 1.1111,
                                "buf_len" : 7})
コード例 #8
0
ファイル: helloserver.py プロジェクト: qtacore/QT4SDemoProj
class HelloResponse(ResponseBase):
    """hello response definition
    """
    _struct_ = [
        Field('len', Uint32),
        Field('seq', Uint32),
        Field('result', String, byte_size=0)
    ]
    _serializer_ = BinarySerializer()
    _length_field_ = "len"

    def get_sequence_id(self):
        return self.seq
コード例 #9
0
ファイル: httpbody.py プロジェクト: qtacore/QT4S
class Binary(Message, HttpBody):
    _struct_ = [
        Field("data", Buffer)
    ]
    _serializer_ = BinarySerializer()

    @property
    def content_type(self):
        return ""

    def loads(self, value, confs={}, deserializer=None):
        return Message.loads(self, value, deserializer=deserializer)

    def dumps(self, serializer=None):
        return Message.dumps(self, serializer=serializer)
コード例 #10
0
ファイル: helloserver.py プロジェクト: qtacore/QT4SDemoProj
class HelloRequest(RequestBase):
    """hello request definition
    """
    _struct_ = [
        Field('len', Uint32),
        Field('seq', Uint32),
        Field('username', String, byte_size=0)
    ]
    _serializer_ = BinarySerializer()
    _length_field_ = "len"
    response_class = HelloResponse

    def get_sequence_id(self):
        return self.seq

    def pre_process(self, chan):
        self.seq = chan.create_seq()
コード例 #11
0
 def test_load_size_ref(self):
     foo_buff = FooBuff()
     foo_buff.loads(self.data, BinarySerializer())
     self.assertEqual(foo_buff.buf_len, 11)
     self.assertEqual(foo_buff.buf, "abcdefg")
     self.assertTrue((foo_buff.a - 3.1415926) < 0.00001)
コード例 #12
0
 def test_field_size_of_array(self):
     self.assertEqual(field_size_of(self.foo, "uint32_list", BinarySerializer()), 20)
     self.assertEqual(field_size_of(self.foo, "string_list", BinarySerializer()), 14)
コード例 #13
0
 def test_field_size_of_number(self):
     self.assertEqual(field_size_of(self.foo, "version", BinarySerializer()), 4)
     self.assertEqual(field_size_of(self.foo, "timestamp", BinarySerializer()), 4)
     self.assertEqual(field_size_of(self.foo, "double_data", BinarySerializer()), 8)
コード例 #14
0
 def test_field_size_of_string(self):
     self.foo.dumps(BinarySerializer())
     self.assertEqual(field_size_of(self.foo, "cmd", BinarySerializer()), 16)
     self.assertEqual(field_size_of(self.foo, "username", BinarySerializer()), 4)
コード例 #15
0
 def test_field_size_of_map(self):
     foo_map = FooMap()
     foo_map.map["xxx"] = 1
     self.assertRaises(TypeError, size_of, self.foo, "mapping", BinarySerializer())
コード例 #16
0
 def setUpClass(cls):
     cls.foo_buff = FooBuff()
     cls.foo_buff.buf = "abcdefg"
     cls.foo_buff.a = 3.1415926
     cls.data = cls.foo_buff.dumps(BinarySerializer())
コード例 #17
0
 def test_size_of_nested(self):
     self.assertEqual(field_size_of(self.foo, "bar.byte_len_string.len", BinarySerializer()), 1)
     self.assertEqual(field_size_of(self.foo, "bar.byte_len_string.string", BinarySerializer()), 7)
     self.assertEqual(field_size_of(self.foo, "bar.byte_len_string", BinarySerializer()), 8)
コード例 #18
0
 def test_size_offset_of(self):
     self.assertEqual(offset_of(self.foo, "cmd", BinarySerializer()), 0)
     self.assertEqual(offset_of(self.foo, "username", BinarySerializer()), 17)
     self.assertEqual(offset_of(self.foo, "version", BinarySerializer()), 21)