gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:31:41 25 4
gpt4 key购买 nike

我有一个测试套件在使用 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

最佳答案

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

关于Django 测试内存中的 SQLlite - 测试运行期间缺少表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19958587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com