Вопрос

I have a command as follows:

class AddChatMessages(Command):

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

And I have a responder for it in a 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)

I am writing a unit test for it. This is my code:

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

    def assert_callback(self, unused):
        pass

Where ProtocolMixin is as follows:

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

It works if AmpList is not involved, but when it is - I get following error:

======================================================================
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

Which makes sense, but how do I serialize a list in AddChatMessagesTest.data?

Это было полезно?

Решение

The responder expects to be called with a serialized box. It will then deserialize it, dispatch the objects to application code, take the object the application code returns, serialize it, and then return that serialized form.

For a few AMP types. most notably String, the serialized form is the same as the deserialized form, so it's easy to overlook this.

I think that you'll want to pass your data through Command.makeArguments in order to produce an object suitable to pass to a responder.

For example:

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

If you do this with a Command that uses AmpList I think you'll find makeArguments returns an encoded string for the value of that argument and that the responder is happy to accept and parse that kind of string.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top