def test_inline_task self class TestSpider Spider calls responses def

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def test_inline_task(self):
class TestSpider(Spider):
calls = []
responses = []
def task_generator(self):
yield Task('inline', url=SERVER.BASE_URL)
@inline_task
def task_inline(self, grab, task):
self.calls.append('generator')
for x in xrange(3):
url = SERVER.BASE_URL + '/?foo=%s' % x
grab.setup(url=url)
grab = yield Task(grab=grab)
self.responses.append(SERVER.REQUEST['args'])
self.calls.append('inline%s' % x)
self.add_task(Task('yield', url=SERVER.BASE_URL))
def task_yield(self, grab, task):
self.calls.append('yield')
yield Task('end', url=SERVER.BASE_URL)
def task_end(self, grab, task):
self.calls.append('end')
bot = TestSpider()
bot.run()
self.assertEqual([{'foo': u'0'}, {'foo': u'1'}, {'foo': u'2'}], bot.responses)
self.assertEqual(['generator', 'inline0', 'inline1', 'inline2', 'yield', 'end'],
bot.calls)