简体   繁体   English

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

[英]pytest-django run with migrations ignores database triggers

My Django app depends on a database with some triggers setup. 我的Django应用程序依赖于具有某些触发器设置的数据库。 I use this part of the documentation to set up the triggers in the test database for the pytest runner. 我使用文档的这一部分在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

I run my tests with --nomigrations and it works as expected. 我使用--nomigrations运行测试,它可以按预期运行。 Without --nomigrations (test runs migrations first), the triggers are not working. 如果没有--nomigrations (测试首先运行迁移),则触发器不起作用。

So trying to debug this, I've confirmed 因此,我尝试调试

  1. The fixture IS run, so the triggers SHOULD be setup 夹具运行 ,因此,触发器
  2. Pausing execution in the debugger at the start of my tests, I can confirm that the triggers ARE created and present in the test database (by running psql test_<mydb> and looking in the pg_trigger table) 在我的测试开始暂停在调试器执行,我可以证实, 创建触发器和存在于测试数据库(通过运行psql test_<mydb>并在寻找pg_trigger表)
  3. Pausing execution inside my fixture, I can confirm that migrations are run before the fixture. 暂停夹具内部的执行,我可以确认迁移是在夹具之前进行的。 So the migrations may setup the triggers for me, and they may do it incorrectly, but the fixture will drop all the triggers and recreate them 因此,迁移可能会为我设置触发器,并且可能做错了,但是固定装置会删除所有触发器并重新创建它们
  4. Removing the fixture and running with migrations provide no new results. 卸下固定装置并进行迁移无法提供任何新结果。 So there is no reason to think that the fixture is the problem. 因此,没有理由认为固定装置是问题所在。 It seems to be only due to the migrations being run 这似乎仅是由于正在运行迁移

Let me just stress once more that the tests pass when run without migrations and testing the functionality when running the dev server against my dev db I can also confirm that it works 让我再次强调一下, 在不进行迁移的情况下运行时测试通过了,在对我的dev db运行dev服务器时测试了功能,我也可以确认它是否有效

So, my question is: Is there any reason that running with migrations should do things differently? 因此,我的问题是:使用迁移进行操作应该有什么不同的理由吗? Or is it likely that my migrations do something obscure which makes things fail, ie it is my own fault? 还是我的迁移所做的事情晦涩难懂,导致事情失败,即是我自己的错?

Placing trigger in django_db_setup worked for me 将触发器放在django_db_setup对我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) is run with --nomigrations pytest(4.4.1)使用--nomigrations运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM