Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

httpcache: added tests for custom dbm module.

Rolando Espinoza La fuente6ed44a3a67e6
diff --git a/scrapy/contrib/httpcache.py b/scrapy/contrib/httpcache.py
index 49e82dc8c..be5af1604 100644
--- a/scrapy/contrib/httpcache.py
+++ b/scrapy/contrib/httpcache.py
@@ -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):
diff --git a/scrapy/tests/mocks/__init__.py b/scrapy/tests/mocks/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/scrapy/tests/mocks/dummydbm.py b/scrapy/tests/mocks/dummydbm.py
new file mode 100644
index 000000000..36c2e8637
--- /dev/null
+++ b/scrapy/tests/mocks/dummydbm.py
@@ -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]
diff --git a/scrapy/tests/test_downloadermiddleware_httpcache.py b/scrapy/tests/test_downloadermiddleware_httpcache.py
index 8944ded0a..af4d4bc22 100644
--- a/scrapy/tests/test_downloadermiddleware_httpcache.py
+++ b/scrapy/tests/test_downloadermiddleware_httpcache.py
@@ -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'