1

I am using Taskqueue stub in google app engine. It is very nice, but there is one thing that is driving me nuts.

During my test, a method I'm testing creates a task, and I want to check if that task has been created correctly. So, one of the things I have to check is that the proper 'params' have been passed. To do that, I do the following to get the task:

tasks = self.taskqueue_stub.GetTasks(queue_name)
self.assertEqual(1, len(tasks))
task = tasks[0]

But this task does not have a 'params' key, which is a shame, because that info is very valuable to assert that everything is fine.

Instead, the task has a 'body' key, which contains a base64 encoded version of the request body, something like this (once base64decoded):

muted=False&class_=Decoder&failures=3&last_orders=Key%28%27ProspectiveSale%27%2C+%27cl1%27%29&last_orderl=Key%28%27ProspectiveSale%27%2C+%27cl2%27%29&hit_per=5.0

I have tried to parse that to get the 'params' dict, but I'm finding it a bit tedious to parse all different items to their corresponding types, etc. Somehow, I feel that is just wrong, there has to be a simpler way to do this.

So, to consider this question as answered, I would need one of the following:

  • A way to read the 'params' dict.
  • A way to reconstruct the 'params' dict from the 'body' value mentioned above.
  • Another solution that I cannot imagine now but that would let me read that frikin 'params' dict ;-)

Cheers!

4

1 回答 1

0

urlparse模块具有将参数字符串转换为 dict: 的功能parse_qs

例如:

import urlparse

params = 'muted=False&class_=Decoder&failures=3&last_orders=Key%28%27ProspectiveSale%27%2C+%27cl1%27%29&last_orderl=Key%28%27ProspectiveSale%27%2C+%27cl2%27%29&hit_per=5.0'
print urlparse.parse_qs(params)

打印以下内容:

{'muted': ['False'], 'class_': ['Decoder'], 'hit_per': ['5.0'], 'last_orders': ["Key(
'ProspectiveSale', 'cl1')"], 'last_orderl': ["Key('ProspectiveSale', 'cl2')"], 'failu
res': ['3']}
于 2013-04-24T22:15:35.040 回答