【问题标题】:pytest-django run with migrations ignores database triggerspytest-django 使用迁移运行忽略数据库触发器
【发布时间】:2016-12-08 08:03:19
【问题描述】:

我的 Django 应用程序依赖于带有一些触发器设置的数据库。我使用this part of the documentation 在测试数据库中为 pytest 运行器设置触发器。

@pytest.fixture(scope='session')
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        cur = connection.cursor()
        cur.execute([...])  # Set it up

我使用--nomigrations 运行我的测试,它按预期工作。如果没有--nomigrations(首先测试运行迁移),触发器将不起作用。

所以尝试调试这个,我已经确认

  1. 夹具IS运行,所以触发器应该设置
  2. 在我的测试开始时在调试器中暂停执行,我可以确认触发器 ARE 已创建并存在于测试数据库中(通过运行 psql test_<mydb> 并查看 pg_trigger 表)
  3. 在我的夹具中暂停执行,我可以确认迁移是在夹具之前运行的。所以迁移可能会为我设置触发器,他们可能会做错,但夹具会删除所有触发器并重新创建它们
  4. 移除夹具并运行迁移不会提供新结果。所以没有理由认为夹具是问题所在。这似乎只是由于正在运行的迁移

让我再次强调,在没有迁移的情况下运行测试通过并在针对我的开发数据库运行开发服务器时测试功能,我还可以确认它可以工作

所以,我的问题是:是否有任何理由使用迁移运行应该以不同的方式做事?还是我的迁移可能做了一些晦涩的事情导致事情失败,即这是我自己的错?

【问题讨论】:

  • 有什么理由不使用 django 测试框架?
  • @e4c5 太慢了。 pytest 库的许多额外好处

标签: django postgresql pytest database-trigger pytest-django


【解决方案1】:

django_db_setup 中放置触发器对我有用

@pytest.fixture(scope='session')
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        cur = connection.cursor()
        cur.execute('''CREATE TRIGGER search_vector_update BEFORE INSERT OR UPDATE
            ON xml_templates_template FOR EACH ROW EXECUTE PROCEDURE
            tsvector_update_trigger(search_vector, 'pg_catalog.english', name, description, info);
        ''')

pytest (4.4.1) 使用 --nomigrations 运行

【讨论】:

    猜你喜欢
    • 2016-10-08
    • 2017-08-10
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多