Python 测试用串行化twisted.protocols.amp.AmpList
我的命令如下:Python 测试用串行化twisted.protocols.amp.AmpList,python,twisted,asynchronous-messaging-protocol,Python,Twisted,Asynchronous Messaging Protocol,我的命令如下: class AddChatMessages(Command): arguments = [ ('messages', AmpList([('message', Unicode()), ('type', Integer())]))] class ProtocolTestMixin(object): def setUp(self): self.protocol = client.CommandProtocol() de
class AddChatMessages(Command):
arguments = [
('messages', AmpList([('message', Unicode()), ('type', Integer())]))]
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
我在控制器中有一个响应器:
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 AddChatMessages(Command):
arguments = [
('messages', AmpList([('message', Unicode()), ('type', Integer())]))]
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
======================================================================
错误:测试响应程序
----------------------------------------------------------------------
回溯(最近一次呼叫最后一次):
文件“/Users//Projects/space/env/lib/python2.7/site packages/twisted/internet/defer.py”,第139行,maybeDeferred
结果=f(*参数,**kw)
文件“/Users//Projects/space/env/lib/python2.7/site packages/twisted/internet/utils.py”,第203行,在runwithwarningsuppressed中
重新发布(exc_信息[1],exc_信息[2])
文件“/Users//Projects/space/env/lib/python2.7/site packages/twisted/internet/utils.py”,第199行,运行时警告被抑制
结果=f(*a,**kw)
文件“/Users//Projects/space/tests/client_test.py”,第32行,在测试响应程序中
d=响应者(自身数据)
doit中的文件“/Users//Projects/space/env/lib/python2.7/site packages/twisted/protocols/amp.py”,第1016行
kw=命令.parseArguments(框,自)
parseArguments中的文件“/Users//Projects/space/env/lib/python2.7/site packages/twisted/protocols/amp.py”,第1717行
返回_stringsToObject(框、cls.arguments、协议)
文件“/Users//Projects/space/env/lib/python2.7/site-packages/twisted/protocols/amp.py”,第2510行,位于
fromBox(argname、mystring、objects、proto)
文件“/Users//Projects/space/env/lib/python2.7/site packages/twisted/protocols/amp.py”,第1209行,在fromBox中
objects[nk]=self.fromStringProto(st,proto)
文件“/Users//Projects/space/env/lib/python2.7/site packages/twisted/protocols/amp.py”,第1465行,fromStringProto
box=parseString(inString)
文件“/Users//Projects/space/env/lib/python2.7/site packages/twisted/protocols/amp.py”,第2485行,格式为parseString
返回cls.parse(StringIO(数据))
TypeError:必须是字符串或缓冲区,而不是列表
这是有道理的,但是如何序列化
addchatmessestest.data
中的列表?响应程序希望使用序列化框调用。然后,它将对其进行反序列化,将对象分派到应用程序代码,获取应用程序代码返回的对象,将其序列化,然后返回该序列化表单
对于一些安培类型。最值得注意的是String
,序列化表单与反序列化表单相同,因此很容易忽略这一点
我认为您需要通过命令.makeArguments
传递数据,以便生成适合传递给响应者的对象
例如:
>>> from twisted.protocols.amp import Command, Integer
>>> class Foo(Command):
... arguments = [("bar", Integer())]
...
>>> Foo.makeArguments({"bar": 17}, None)
AmpBox({'bar': '17'})
>>>
如果您使用使用AmpList
的命令执行此操作,我想您会发现makeArguments
为该参数的值返回一个编码字符串,并且响应程序乐于接受并解析该类型的字符串