Skip to main content
 首页 » 编程设计

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

2024年11月01日10kevingrace

我有一个命令如下:

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'}) 
>>> 

如果您使用使用 AmpList 的 Command 执行此操作,我认为您会发现 makeArguments 返回一个编码字符串作为其值参数并且响应者很乐意接受和解析这种字符串。