django - 使用迁移运行的 pytest-django 忽略数据库触发器

标签 django postgresql pytest database-trigger pytest-django

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

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

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

最佳答案

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 运行

关于django - 使用迁移运行的 pytest-django 忽略数据库触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41034549/

相关文章:

python - Django注释,将多个相关值组合到同一个实例上

c - PostgreSQL C 函数获取参数 varchar 类型

python - flask ,flask_login,pytest : How do I set flask_login's current_user?

python - Django 中什么时候需要重启数据库服务器?

python - 获取非测试模块的 pytest 回溯

python - 与 Airflow 的集成测试

在 Bootstrap Modal 中使用 django 清晰表单的 AJAX 反馈表单

css - Django 中的 GUI CSS 选择器

python - 在 Django Web 应用程序中访问 models.py 中的用户 uri 方案

sql - 如何根据规则排除 sql 表中几乎重复的值?