Django测试内存中的SQLlite - 在测试运行期间缺少表

时间:2013-11-13 16:04:26

标签: django sqlite unit-testing testing django-south

我有一个测试套件在使用sqlite文件时传递,但不在内存中。有没有人遇到过这个问题?

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'db.sqlite3',
    }
}

...测试传球。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
    }
}

...测试失败

DatabaseError: no such table: foo_bar

有没有办法在测试运行之前强制创建所有表?完整的堆栈跟踪

    ======================================================================
ERROR: Failure: DatabaseError (no such table: core_industry)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/igniteflow/Projects/foo/src/lib/nose/loader.py", line 413, in loadTestsFromName
    addr.filename, addr.module)
  File "/home/igniteflow/Projects/foo/src/lib/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/home/igniteflow/Projects/foo/src/lib/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/home/igniteflow/Projects/foo/src/app/cms/tests/__init__.py", line 5, in <module>
    from bulk_export import *
  File "/home/igniteflow/Projects/foo/src/app/cms/tests/bulk_export.py", line 8, in <module>
    from cms.input_parsers import SiteProspectInputParser
  File "/home/igniteflow/Projects/foo/src/app/cms/input_parsers.py", line 10, in <module>
    from cms.bulkimport import SiteProspectBulkUploadForm, SiteUserBulkUploadForm
  File "/home/igniteflow/Projects/foo/src/app/cms/bulkimport.py", line 683, in <module>
    class BaseContentBulkUploadForm(BaseBulkUploadForm):
  File "/home/igniteflow/Projects/foo/src/app/cms/bulkimport.py", line 709, in BaseContentBulkUploadForm
    'industries': m2m_column_help_text(Industry),
  File "/home/igniteflow/Projects/foo/src/app/cms/bulkimport.py", line 674, in m2m_column_help_text
    [i[0] for i in ModelClass.objects.all().values_list(field)[:3]]
  File "/home/igniteflow/Projects/foo/src/lib/django/db/models/query.py", line 123, in _result_iter
    self._fill_cache()
  File "/home/igniteflow/Projects/foo/src/lib/django/db/models/query.py", line 927, in _fill_cache
    self._result_cache.append(next(self._iter))
  File "/home/igniteflow/Projects/foo/src/lib/django/db/models/query.py", line 1129, in iterator
    for row in self.query.get_compiler(self.db).results_iter():
  File "/home/igniteflow/Projects/foo/src/lib/django/db/models/sql/compiler.py", line 775, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/igniteflow/Projects/foo/src/lib/django/db/models/sql/compiler.py", line 840, in execute_sql
    cursor.execute(sql, params)
  File "/home/igniteflow/Projects/foo/src/lib/django/db/backends/util.py", line 41, in execute
    return self.cursor.execute(sql, params)
  File "/home/igniteflow/Projects/foo/src/lib/django/db/backends/sqlite3/base.py", line 366, in execute
    six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e.args)), sys.exc_info()[2])
  File "/home/igniteflow/Projects/foo/src/lib/django/db/backends/sqlite3/base.py", line 362, in execute
    return Database.Cursor.execute(self, query, params)
DatabaseError: no such table: core_industry

1 个答案:

答案 0 :(得分:2)

您的cms.bulkimport.BaseContentBulkUploadForm正在尝试在导入时执行数据库查询。在你的跟踪中,Django仍然在发现/构建测试套件,还没有创建测试数据库。您需要找到导入时执行查询的代码(看起来像是m2m_column_help_text)并重构它。