python - 序列化twisted.protocols.amp.AmpList以进行测试

标签 python twisted asynchronous-messaging-protocol

我有一个命令如下:

class AddChatMessages(Command):

    arguments = [ 
        ('messages', AmpList([('message', Unicode()), ('type', Integer())]))]

我在 Controller 中有一个响应器:

def add_chat_messages(self, messages):
    for i, m in enumerate(messages):
        messages[i] = (m['message'], m['type'])
        self.main.add_chat_messages(messages)
    return {}
commands.AddChatMessages.responder(add_chat_messages)

我正在为其编写单元测试。这是我的代码:

class AddChatMessagesTest(ProtocolTestMixin, unittest.TestCase):
    command = commands.AddChatMessages
    data = {'messages': [{'message': 'hi', 'type': 'None'}]}

    def assert_callback(self, unused):
        pass

其中ProtocolMixin如下:

class ProtocolTestMixin(object):

    def setUp(self):
        self.protocol = client.CommandProtocol()

    def assert_callback(self, unused):
        raise NotImplementedError("Has to be implemented!")

    def test_responder(self):
        responder = self.protocol.lookupFunction(
            self.command.commandName)
        d = responder(self.data)
        d.addCallback(self.assert_callback)
        return d

如果不涉及AmpList,它就可以工作,但是当它涉及时 - 我收到以下错误:

======================================================================
ERROR: test_responder
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/internet/defer.py", line 139, in maybeDeferred
    result = f(*args, **kw)
  File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/internet/utils.py", line 203, in runWithWarningsSuppressed
    reraise(exc_info[1], exc_info[2])
  File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/internet/utils.py", line 199, in runWithWarningsSuppressed
    result = f(*a, **kw)
  File "/Users/<username>/Projects/space/tests/client_test.py", line 32, in test_responder                                                                             
    d = responder(self.data)
  File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1016, in doit
    kw = command.parseArguments(box, self)
  File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1717, in parseArguments
    return _stringsToObjects(box, cls.arguments, protocol)
  File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py", line 2510, in _stringsToObjects
    argparser.fromBox(argname, myStrings, objects, proto)
  File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1209, in fromBox
    objects[nk] = self.fromStringProto(st, proto)
  File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1465, in fromStringProto
    boxes = parseString(inString)
  File "/Users/<username>/Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py", line 2485, in parseString
    return cls.parse(StringIO(data))
TypeError: must be string or buffer, not list

这是有道理的,但是如何在 AddChatMessagesTest.data 中序列化列表?

最佳答案

响应者希望使用序列化框进行调用。然后,它将反序列化它,将对象分派(dispatch)给应用程序代码,获取应用程序代码返回的对象,将其序列化,然后返回该序列化形式。

对于一些 AMP 类型。最值得注意的是String,序列化形式与反序列化形式相同,因此很容易忽略这一点。

我认为您需要通过 Command.makeArguments 传递数据,以便生成适合传递给响应者的对象。

例如:

>>> from twisted.protocols.amp import Command, Integer
>>> class Foo(Command):
...     arguments = [("bar", Integer())]
... 
>>> Foo.makeArguments({"bar": 17}, None)
AmpBox({'bar': '17'})
>>>

如果您使用使用 AmpListCommand 执行此操作,我想您会发现 makeArguments 返回该值的编码字符串参数,并且响应者很乐意接受并解析这种字符串。

关于python - 序列化twisted.protocols.amp.AmpList以进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21975952/

相关文章:

python - LoopingCall AMP 命令

python - Kinect + OpenCV : Unable to fetch rotational vectors using cv2. 在 python 中解决 PnP

python - SQLAlchemy:混合值对象,查询元组结果

python - Twisted AMP - 如何发送大于 64K 的值

python - 扭曲的 HTTPS 客户端

python - 扭曲有好处吗?

python - 应如何取消 Twisted AMP 延迟?

python - 在python中使用for循环生成mySQL插入语句

python - PySide QPropertyAnimation 未启动

Python,多线程,抓取网页,下载网页