httpcache: added tests for custom dbm module.

And removed the break in the line to improve readability as there
is no strict 80-chars line width convention.
This commit is contained in:
Rolando Espinoza La fuente 2013-02-01 12:41:52 -04:00
parent a2602df90b
commit 6ed44a3a67
4 changed files with 38 additions and 2 deletions

View File

@ -163,8 +163,7 @@ class DbmCacheStorage(object):
def __init__(self, settings):
self.cachedir = data_path(settings['HTTPCACHE_DIR'], createdir=True)
self.expiration_secs = settings.getint('HTTPCACHE_EXPIRATION_SECS')
self.dbmodule = __import__(settings['HTTPCACHE_DBM_MODULE'],
{}, {}, [''])
self.dbmodule = __import__(settings['HTTPCACHE_DBM_MODULE'], {}, {}, [''])
self.db = None
def open_spider(self, spider):

View File

View File

@ -0,0 +1,23 @@
"""DBM-like dummy module"""
import collections
class DummyDB(dict):
"""Provide dummy DBM-like interface."""
def close(self):
pass
class error(Exception):
pass
_DATABASES = collections.defaultdict(DummyDB)
def open(file, flag='r', mode=0666):
"""Open or create a dummy database compatible.
Arguments `flag` and `mode` are ignored.
"""
# return same instance for same file argument
return _DATABASES[file]

View File

@ -116,6 +116,20 @@ class DbmStorageTest(DefaultStorageTest):
storage_class = 'scrapy.contrib.httpcache.DbmCacheStorage'
class DbmStorageWithCustomDbmModuleTest(DbmStorageTest):
dbm_module = 'scrapy.tests.mocks.dummydbm'
def _get_settings(self, **new_settings):
new_settings.setdefault('HTTPCACHE_DBM_MODULE', self.dbm_module)
return super(DbmStorageWithCustomDbmModuleTest, self)._get_settings(**new_settings)
def test_custom_dbm_module_loaded(self):
# make sure our dbm module has been loaded
with self._storage() as storage:
self.assertEqual(storage.dbmodule.__name__, self.dbm_module)
class FilesystemStorageTest(DefaultStorageTest):
storage_class = 'scrapy.contrib.httpcache.FilesystemCacheStorage'