2010-12-27 16:22:32 -02:00
|
|
|
from __future__ import with_statement
|
2010-11-29 17:19:05 -02:00
|
|
|
|
2010-12-27 16:22:32 -02:00
|
|
|
import os
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
|
|
|
from twisted.trial import unittest
|
|
|
|
|
|
|
|
from scrapy.utils.py26 import get_data
|
|
|
|
from scrapyd.interfaces import IEggStorage
|
|
|
|
from scrapyd.utils import get_crawl_args, get_spider_list
|
|
|
|
from scrapyd import get_application
|
|
|
|
|
|
|
|
__package__ = 'scrapyd.tests' # required for compatibility with python 2.5
|
2010-11-29 17:19:05 -02:00
|
|
|
|
|
|
|
class UtilsTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_get_crawl_args(self):
|
2010-11-30 13:03:20 -02:00
|
|
|
msg = {'_project': 'lolo', '_spider': 'lala'}
|
2010-11-29 17:19:05 -02:00
|
|
|
self.assertEqual(get_crawl_args(msg), ['lala'])
|
2010-11-30 13:03:20 -02:00
|
|
|
msg = {'_project': 'lolo', '_spider': 'lala', 'arg1': u'val1'}
|
2010-11-29 17:19:05 -02:00
|
|
|
cargs = get_crawl_args(msg)
|
|
|
|
self.assertEqual(cargs, ['lala', '-a', 'arg1=val1'])
|
|
|
|
assert all(isinstance(x, str) for x in cargs), cargs
|
2010-12-27 16:22:32 -02:00
|
|
|
|
|
|
|
class GetSpiderListTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_get_spider_list(self):
|
2011-01-05 15:59:43 -02:00
|
|
|
path = os.path.abspath(self.mktemp())
|
2010-12-27 16:22:32 -02:00
|
|
|
j = os.path.join
|
|
|
|
eggs_dir = j(path, 'eggs')
|
|
|
|
os.makedirs(eggs_dir)
|
|
|
|
dbs_dir = j(path, 'dbs')
|
|
|
|
os.makedirs(dbs_dir)
|
|
|
|
logs_dir = j(path, 'logs')
|
|
|
|
os.makedirs(logs_dir)
|
|
|
|
os.chdir(path)
|
|
|
|
with open('scrapyd.conf', 'w') as f:
|
|
|
|
f.write("[scrapyd]\n")
|
|
|
|
f.write("eggs_dir = %s\n" % eggs_dir)
|
|
|
|
f.write("dbs_dir = %s\n" % dbs_dir)
|
|
|
|
f.write("logs_dir = %s\n" % logs_dir)
|
|
|
|
app = get_application()
|
|
|
|
eggstorage = app.getComponent(IEggStorage)
|
|
|
|
eggfile = StringIO(get_data(__package__, 'mybot.egg'))
|
|
|
|
eggstorage.put(eggfile, 'mybot', 'r1')
|
|
|
|
self.assertEqual(sorted(get_spider_list('mybot')), ['spider1', 'spider2'])
|
|
|
|
|