在运行 Django 测试之前加载 SQL 转储

发布于 2024-08-17 02:37:55 字数 175 浏览 4 评论 0原文

我有一个相当复杂的 Django 项目,这使得很难/不可能使用固定装置来加载数据。

我想做的是在测试运行程序创建所有表之后和实际测试开始运行之前从生产数据库服务器加载数据库转储。

我在 MyTestCase.setUp() 中尝试了各种“魔法”,但没有运气。

任何建议都将受到欢迎。谢谢。

I have a fairly complex Django project which makes it hard/impossible to use fixtures for loading data.

What I would like to do is to load a database dump from the production database server after all tables has bene created by the testrunner and before the actual tests start running.

I've tried various "magic" in MyTestCase.setUp(), but with no luck.

Any suggestions would be most welcome. Thanks.

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

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

发布评论

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

评论(3

爱要勇敢去追 2024-08-24 02:37:55

Django 支持在执行syncdb、重置或启动测试运行程序时加载 SQL 文件——这正是您所描述的:

http://docs.djangoproject.com/en/dev/howto/initial-data/#providing-initial-sql-data

您需要在应用程序目录中创建一个“sql”目录,然后在该目录中放置一个名为“mymodel.sql”的文件(其中“MyModel”是相应的模型名称)。

myproject/
   |--myapp/
       |--sql/
           |--mymodel.sql

您可以使用数据库转储工具创建此 SQL。

  • SQLite [1]: echo '.dump' | SQLite [1]: echo '.dump' | SQLite [1]: echo '.dump' | sqlite3 yourdbname.sqlite > > myapp/sql/mymodel.sql
  • MySQL [2]: mysqldump yourdbname > myapp/sql/mymodel.sql
  • PostgreSQL [3]: pg_dump yourdbname > myapp/sql/mymodel.sql

转储后,您需要编辑该文件以删除除适当的 INSERT 语句或其他复杂内容之外的所有内容。特别是,您必须删除事务处理、索引创建和表创建 SQL,以避免加载重复的创建语句时出现错误。

我使用这种方法来加载非常非常大的装置——处理 json 需要太长时间,但是直接 sql 导入非常快捷。

请注意,除了为测试运行程序加载数据之外,此方法还将加载任何调用synchdb、重置等的sql——因此您将无法为不同的测试用例提供不同的数据,并且您'如果您不希望文件加载回生产服务器,则必须在重置之前删除文件。

[1] http://www.sqlite.org/sqlite.html

[2] http://dev.mysql.com/doc/refman/5.1/en/mysqldump .html

[3] http://www.postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP" postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP

Django supports loading SQL files when doing syncdb, reset, or starting a test runner -- this does exactly what you describe:

http://docs.djangoproject.com/en/dev/howto/initial-data/#providing-initial-sql-data

You need to create an "sql" directory in your app directory, and then put a file named "mymodel.sql" in that directory (where "MyModel" is the corresponding model name).

myproject/
   |--myapp/
       |--sql/
           |--mymodel.sql

You can create this SQL with dump tools for your database.

  • SQLite [1]: echo '.dump' | sqlite3 yourdbname.sqlite > myapp/sql/mymodel.sql
  • MySQL [2]: mysqldump yourdbname > myapp/sql/mymodel.sql
  • PostgreSQL [3]: pg_dump yourdbname > myapp/sql/mymodel.sql

After dumping, you'll need to edit the file to remove everything but the appropriate INSERT statements, or other complicated stuff. In particular, you must remove transaction handling, index creating, and table creating SQL to avoid errors when loading duplicate create statements.

I use this method for loading really, really big fixtures -- it takes far too long to process the json, but a straight sql import is pretty snappy.

Do be aware that this method will load the sql for any invocation of synchdb, reset, etc. in addition to loading data for the test runner -- so you won't be able to have different data for different test cases, and you'd have to remove files before a reset if you didn't want them loading back onto your production server.

[1] http://www.sqlite.org/sqlite.html

[2] http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html

[3] http://www.postgresql.org/docs/8.1/static/backup.html#BACKUP-DUMP

你爱我像她 2024-08-24 02:37:55

您可能需要考虑定义自定义测试运行程序。这里有一些信息:https://docs。 djangoproject.com/en/dev/topics/testing/advanced/#other-testing-frameworks

基本上我认为你可以从 django.test.simple.run_tests 复制默认的测试运行程序,然后修改它以适合你的需要。

我以前没有这样做过,但根据我的理解,这将是自定义的方式。

You may need to look into defining a custom test runner. There's some info here: https://docs.djangoproject.com/en/dev/topics/testing/advanced/#other-testing-frameworks

Basically I think you can just copy the default test runner from django.test.simple.run_tests and then modify it to suit your needs.

I've not done this before, but from my understanding that would be the way to customize this.

青瓷清茶倾城歌 2024-08-24 02:37:55

固定装置是最好的选择。您是否尝试过使用 ./manage.py dumpdata 从当前数据库创建固定装置?我还没有看到在复杂模型上失败的情况,但我想这是可能的。

假设您使用的是 mysql,您应该能够使用 编写脚本mysqldump

Fixtures are the best option. Have you tried using ./manage.py dumpdata to create a fixture from your current database? I have not seen that fail on complex models, but I guess it's possible.

Assuming you're using mysql, you should be able to script this by using mysqldump.

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