pytest django:无法在夹具拆解中访问db

时间:2018-05-24 10:05:49

标签: django pytest pytest-django

我需要在使用后明确删除灯具。我知道pytest-django默认会丢弃所有对象,但在这种特殊情况下我需要手动完成。但是,虽然我的测试标记为pytest.mark.django_db,但我可以创建一个灯具,但无法在yield行后删除它:

import pytest

from tgapps.models import TelegramApp


@pytest.fixture(scope='module')
def some_fixture():

    app = TelegramApp.objects.create(
        session_data=b'\xa2\x8f#',
        app_owner_phone=79856235474,
        app_id=182475,
        app_hash='aad9ab4384fea1af0342b77b606d13b0'
    )
    yield app

    print('deleting object...')
    app.delete()


class TestTelegramServiceObject(object):

    @pytest.mark.django_db
    def test1(self, some_fixture):
        print('Fixture created:')
        print(some_fixture)

这是我的测试输出:

============================= test session starts ==============================
platform darwin -- Python 3.6.4, pytest-3.4.0, py-1.5.2, pluggy-0.6.0
Django settings: inviter.settings.staging (from ini file)
rootdir: /Users/1111/_projects/fasttrack/inviter, inifile: pytest.ini
plugins: mock-1.7.1, dotenv-0.1.0, django-3.1.2
collected 1 item
test_example.py E.Fixture created:
<79856235474 - 182475>
deleting object...

tests/api/test_example.py:25 (TestTelegramServiceObject.test1)
@pytest.fixture(scope='module')
    def some_fixture():

        app = TelegramApp.objects.create(
            session_data=b'\xa2\x8f#',
            app_owner_phone=79856235474,
            app_id=182475,
            app_hash='aad9ab4384fea1af0342b77b606d13b0'
        )
        yield app

        print('deleting object...')
>       app.delete()

test_example.py:21: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/Users/1111/.virtualenvs/inviter-WB5rPISo/lib/python3.6/site-packages/django/db/models/base.py:890: in delete
    collector.collect([self], keep_parents=keep_parents)
/Users/1111/.virtualenvs/inviter-WB5rPISo/lib/python3.6/site-packages/django/db/models/deletion.py:221: in collect
    elif sub_objs:
/Users/1111/.virtualenvs/inviter-WB5rPISo/lib/python3.6/site-packages/django/db/models/query.py:276: in __bool__
    self._fetch_all()
/Users/1111/.virtualenvs/inviter-WB5rPISo/lib/python3.6/site-packages/django/db/models/query.py:1179: in _fetch_all
    self._result_cache = list(self._iterable_class(self))
/Users/1111/.virtualenvs/inviter-WB5rPISo/lib/python3.6/site-packages/django/db/models/query.py:53: in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
/Users/1111/.virtualenvs/inviter-WB5rPISo/lib/python3.6/site-packages/django/db/models/sql/compiler.py:1062: in execute_sql
    cursor = self.connection.cursor()
/Users/1111/.virtualenvs/inviter-WB5rPISo/lib/python3.6/site-packages/django/db/backends/base/base.py:255: in cursor
    return self._cursor()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <django.db.backends.postgresql.base.DatabaseWrapper object at 0x1048bf080>
name = None

    def _cursor(self, name=None):
>       self.ensure_connection()
E       Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.

这是为什么?以及如何在灯具拆卸时启用DB评估?

2 个答案:

答案 0 :(得分:0)

您需要在some_fixture中使用db固定装置:

def some_fixture(db): ...

答案 1 :(得分:0)

安德烈亚斯·普罗夫斯(Andreas Profous)提到的db固定装置具有功能范围,因此在这里不起作用。

您需要做的是:

@pytest.fixture(scope='module')
def some_fixture(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        app = TelegramApp.objects.create(
            session_data=b'\xa2\x8f#',
            app_owner_phone=79856235474,
            app_id=182475,
            app_hash='aad9ab4384fea1af0342b77b606d13b0'
        )
    yield app
    with django_db_blocker.unblock():
        print('deleting object...')
        app.delete()

django_db_setup确保已设置(并准备好)测试数据库以添加其他记录。 django_db_blocker固定装置使用db来实现函数范围的修改。之所以对函数进行范围划分是因为,它将表现出TestCase方法在默认的unittest框架中的行为,该框架在每次测试后都会回滚记录。这是一件好事,它可以确保在一个测试中修改记录不会改变另一个测试中的结果。

因此,在创建不在功能范围内的灯具时要非常小心,因为默认情况下,您的修改不会包装在事务中并回滚。使用django_db_blocker可以吸引您进入django_db_blocker.unblock()方法,该方法从修改数据库中删除了该块。在非功能范围内的灯具中,这是必需的。