为什么在运行单元测试时,django 设置会导入两次,并且测试数据库会创建两次?

发布于 2024-11-19 12:41:21 字数 1719 浏览 4 评论 0原文

姜戈:1.3; PyCharm:1.5.3

我正在为使用 GEOS 保存 Point 对象的 Django 应用程序编写单元测试。对于本地测试,我按照 GeoDjango 文档遵循了自定义 Spatialite 后端的每一步。

每当我尝试使用 Point 对象创建和保存模型实例时,我都会遇到 GEOS_ERROR(GEOS_ERROR:几何图形必须是 Point 或 LineString)。很明显,确实在模型的 get_or_create 函数中传递了一个 Point 对象。并且在shell中可以毫无问题地保存相同的模型。

在没有过多了解导致此错误的代码的情况下,我发现每次运行单元测试时,Django 都会导入设置,创建测试数据库并立即销毁数据库,然后再次创建测试数据库以进行最终的测试会抛出错误。

Testing started at 5:09 PM ...<br />
Importing Django settings module settings
SpatiaLite version ..: 2.4.0    Supported Extensions:
    - 'VirtualShape'    [direct Shapefile access]
    - 'VirtualText'[direct CSV/TXT access]
    - 'VirtualNetwork   [Dijkstra shortest path]
    - 'RTree'       [Spatial Index - R*Tree]
    - 'MbrCache'        [Spatial Index - MBR cache]
    - 'VirtualFDO'      [FDO-OGR interoperability]
    - 'SpatiaLite'      [Spatial SQL - OGC]
PROJ.4 Rel. 4.7.1, 23 September 2009
GEOS version 3.3.0-CAPI-1.7.0Creating test database 'default'...
Destroying old test database 'default'...
SpatiaLite version ..: 2.4.0    Supported Extensions:
(same Spatialite settings again)
cannot start a transaction within a transaction
cannot rollback transaction - SQL statements in progress
Syncing...
Creating tables ...

我怀疑这是 PyCharm 造成的。但是当我从终端 shell 运行“python manage.py test”时,重复相同的过程并抛出相同的错误。

我检查了我的设置文件,但找不到为什么它提示自己导入两次以及为什么测试数据库创建了两次。创建 Spatialite 数据库所需的 init_spatialite-2.*.sql 也在项目路径中。

任何建议将不胜感激!

更新: JetBrains 告知我,可以使用此补丁或 runserver --noreload 修复 runserver 期间两次导入 settings.py 的问题。 http://code.djangoproject.com/changeset/15911

但是,导入错误仍然存​​在测试任务。

Django: 1.3; PyCharm: 1.5.3

I am writing unit-tests for a Django application that uses GEOS to save Point objects. For local testing, I followed every step in customizing a Spatialite backend according to the GeoDjango documentation.

I ran into a GEOS_ERROR (GEOS_ERROR: Geometry must be a Point or LineString) whenever I tried to create and save a model instance with a Point object. It was clear that a Point object was indeed passed in the get_or_create function for the model. And the same model can be saved without a problem in shell.

Without getting too much into the code that caused this error, I found that every time I run a unit test, Django imported the settings, created a test database and destroyed the database immediately, and then created the test database again for the tests that ultimately would throw errors.

Testing started at 5:09 PM ...<br />
Importing Django settings module settings
SpatiaLite version ..: 2.4.0    Supported Extensions:
    - 'VirtualShape'    [direct Shapefile access]
    - 'VirtualText'[direct CSV/TXT access]
    - 'VirtualNetwork   [Dijkstra shortest path]
    - 'RTree'       [Spatial Index - R*Tree]
    - 'MbrCache'        [Spatial Index - MBR cache]
    - 'VirtualFDO'      [FDO-OGR interoperability]
    - 'SpatiaLite'      [Spatial SQL - OGC]
PROJ.4 Rel. 4.7.1, 23 September 2009
GEOS version 3.3.0-CAPI-1.7.0Creating test database 'default'...
Destroying old test database 'default'...
SpatiaLite version ..: 2.4.0    Supported Extensions:
(same Spatialite settings again)
cannot start a transaction within a transaction
cannot rollback transaction - SQL statements in progress
Syncing...
Creating tables ...

I suspected this was caused by PyCharm. But when I run 'python manage.py test' from a Terminal shell, the same process was repeated and the same errors were thrown.

I checked my settings file but couldn't find out why it is prompting itself to be imported twice and why the test database was created twice. The required init_spatialite-2.*.sql for creating Spatialite database is also on the project path.

Any advice would be highly appreciated!

Update:
I was informed by JetBrains that the twice-importing of settings.py during runserver can be fixed with this patch or by runserver --noreload.
http://code.djangoproject.com/changeset/15911

However, the import error still persisted for test tasks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇妻 2024-11-26 12:41:21

这很可能是因为您在导入路径上同时拥有 settings.py 和包含包。因为设置可以在不同的模块(settings 和 myproject.settings)下导入,所以它可以执行两次。

只需确保始终使用 DJANGO_SETTINGS_MODULE=settings 而不是 DJANGO_SETTINGS_MODULE=myproject.settings 或从包含 settings.py 的目录中删除 __init__.py 文件,

或者更好的是,升级到 django 1.4

This is caused most probably because you have both the settings.py and containing package on the import path. Because the settings can be imported under different modules (settings and myproject.settings) it can get executed two times.

Just make sure you always use DJANGO_SETTINGS_MODULE=settings instead of DJANGO_SETTINGS_MODULE=myproject.settings or remove the __init__.py file from the directory that contains settings.py

Or, better, upgrade to django 1.4

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文