Pytest-django: django_db ν‘œμ‹œλ₯Ό μ‚¬μš©ν•˜μ§€ μ•Šκ³  μ „μ—­μ μœΌλ‘œ λ°μ΄ν„°λ² μ΄μŠ€ μ•‘μ„ΈμŠ€λ₯Ό ν—ˆμš©ν•˜λŠ” 방법은 λ¬΄μ—‡μž…λ‹ˆκΉŒ?

에 λ§Œλ“  2016λ…„ 09μ›” 20일  Β·  8μ½”λ©˜νŠΈ  Β·  좜처: pytest-dev/pytest-django

Django ν…ŒμŠ€νŠΈ λŸ¬λ„ˆ 기반 ν…ŒμŠ€νŠΈλ₯Ό pytest둜 λ³€ν™˜ν•˜λŠ” 데 어렀움을 κ²ͺκ³  μžˆμŠ΅λ‹ˆλ‹€.
λ‚˜λŠ” κ·Έ νΌνŒ…μ„ κΈ°λŒ€ν•  것이닀

import pytest
pytestmark = pytest.mark.django_db

λ‚΄ μ½”λ“œμ— μ†ν•œ μŠ€νƒ μΆ”μ μ˜ λ§ˆμ§€λ§‰ λͺ¨λ“ˆ(lib/frontend/decorators.py)
μ™„ν™”ν•  수 μžˆμ§€λ§Œ 그렇지 μ•ŠμŠ΅λ‹ˆλ‹€. μŠ€νƒ 좔적에 μžˆλŠ” λ‚΄ λͺ¨λ“ˆ 4개 λͺ¨λ‘μ— 넣어도 도움이 λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

pytest-djangoμ—μ„œ λ°μ΄ν„°λ² μ΄μŠ€ μ•‘μ„ΈμŠ€λ₯Ό μ „μ—­μ μœΌλ‘œ ν—ˆμš©ν•  수 μžˆλŠ” 방법이 μžˆμŠ΅λ‹ˆκΉŒ?

vagrant@vagrant-ubuntu-trusty-64:/vagrant$ pytest 
== test session starts ==
platform linux2 -- Python 2.7.6, pytest-3.0.2, py-1.4.31, pluggy-0.3.1
Django settings: patient.settings (from ini file)
rootdir: /vagrant, inifile: pytest.ini
plugins: django-3.0.0, pythonpath-0.7.1
collected 265 items / 2 errors 

[...]

_ ERROR collecting lib/tests/report/testfunc.py _
lib/tests/report/testfunc.py:6: in <module>
    from report.func import generate_report_filename
lib/report/func.py:29: in <module>
    from frontend import chart
lib/frontend/chart.py:294: in <module>
    @pytest.mark.django_db
lib/frontend/decorators.py:20: in decorator
    if Site.objects.get_current().id in (allowed_site_ids):
/usr/local/lib/python2.7/dist-packages/django/contrib/sites/models.py:60: in get_current
    return self._get_site_by_id(site_id)
/usr/local/lib/python2.7/dist-packages/django/contrib/sites/models.py:39: in _get_site_by_id
    site = self.get(pk=site_id)
/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py:127: in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
/usr/local/lib/python2.7/dist-packages/django/db/models/query.py:328: in get
    num = len(clone)
/usr/local/lib/python2.7/dist-packages/django/db/models/query.py:144: in __len__
    self._fetch_all()
/usr/local/lib/python2.7/dist-packages/django/db/models/query.py:965: in _fetch_all
    self._result_cache = list(self.iterator())
/usr/local/lib/python2.7/dist-packages/django/db/models/query.py:238: in iterator
    results = compiler.execute_sql()
/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py:838: in execute_sql
    cursor = self.connection.cursor()
/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py:162: in cursor
    cursor = self.make_debug_cursor(self._cursor())
/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py:135: in _cursor
    self.ensure_connection()
E   Failed: Database access not allowed, use the "django_db" mark to enable it.

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

κΈ€μŽ„, 당신은 항상 autouse μ‘°λͺ…κΈ°λ₯Ό λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

@pytest.fixture(autouse=True)
def enable_db_access(db):
    pass

λ‹Ήμ‹ μ˜ λŒ€νšŒμ—μ„œ

λͺ¨λ“  8 λŒ“κΈ€

κΈ€μŽ„, 당신은 항상 autouse μ‘°λͺ…κΈ°λ₯Ό λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

@pytest.fixture(autouse=True)
def enable_db_access(db):
    pass

λ‹Ήμ‹ μ˜ λŒ€νšŒμ—μ„œ

@enkore 의 λŒ€λ‹΅μ€ 갈 κΈΈμž…λ‹ˆλ‹€. μ„€λͺ…μ„œμ— FAQ도 μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€.

http://pytest-django.readthedocs.io/en/latest/faq.html#how -can-i-give-database-access-to-all-my-tests-without-the-django-db-marker

ν…ŒμŠ€νŠΈμ—μ„œ κ°€μ Έμ˜€λŠ” λͺ¨λ“ˆμ€ κ°€μ Έμ˜¬ λ•Œ DB에 μ—°κ²°λ©λ‹ˆλ‹€. ν…ŒμŠ€νŠΈ μˆ˜μ§‘ 쀑에 이미 ν—ˆμš©λ˜λ„λ‘ μ „μ—­μ μœΌλ‘œ DB μ•‘μ„ΈμŠ€λ₯Ό ν™œμ„±ν™”ν•˜λ €λ©΄ μ–΄λ–»κ²Œ ν•΄μ•Ό ν•©λ‹ˆκΉŒ? 감사 ν•΄μš”!

@Telofy κ°€μ Έμ˜€κΈ°λ₯Ό db μ•‘μ„ΈμŠ€λ‘œ ν‘œμ‹œν•˜λŠ” 문제λ₯Ό ν•΄κ²°ν•œ 적이 μžˆμŠ΅λ‹ˆκΉŒ? 같은 λ¬Έμ œκ°€...

μ£„μ†‘ν•˜μ§€λ§Œ Django(및 pytest-django)κ°€ μž‘λ™ν•˜λŠ” 방식은 μ‹ λ’°ν•  수 μžˆλŠ” λ°©μ‹μœΌλ‘œ λΆˆκ°€λŠ₯ν•©λ‹ˆλ‹€. μœ μΌν•œ μ μ ˆν•œ μ†”λ£¨μ…˜μ€ κ°€μ Έμ˜€κΈ° μ‹œκ°„ λ™μ•ˆ λ°μ΄ν„°λ² μ΄μŠ€ 쿼리λ₯Ό μˆ˜ν–‰ν•˜μ§€ μ•ŠλŠ” κ²ƒμž…λ‹ˆλ‹€. pytest-djangoλŠ” pytests ν”½μŠ€μ²˜ μ‹œμŠ€ν…œμ„ 기반으둜 κ΅¬μΆ•λ˜μ—ˆμœΌλ©° pytest-djangoκ°€ ν”½μŠ€μ²˜κ°€ κ°€μ Έμ˜€κΈ° 전에 μ‹€ν–‰λ˜λ„λ‘ ν•  수 μžˆλŠ” 방법은 μ—†μŠ΅λ‹ˆλ‹€.

이 λ¬Έμ œμ— λŒ€ν•œ μžμ„Έν•œ μ„€λͺ…은 μ—¬κΈ° λ‚΄ μ˜κ²¬μ„ μ°Έμ‘°ν•˜μ‹­μ‹œμ˜€.
https://github.com/pytest-dev/pytest-django/issues/499#issuecomment -322056315

μ΄λŸ¬ν•œ 쿼리λ₯Ό ν”Όν•˜κΈ° μœ„ν•΄ νšŒμ‚¬μ˜ 전체 ν”Œλž«νΌμ„ λ‹€μ‹œ μž‘μ„±ν•˜λŠ” 것은 제 λŠ₯λ ₯μ΄λ‚˜ μ‹œκ°„ μ˜ˆμ‚°μ΄ μ•„λ‹™λ‹ˆλ‹€. λ‚΄ μ†”λ£¨μ…˜μ€ conftest.pyμ—μ„œ 맀우 λͺ»μƒκΈ΄ μ›μˆ­μ΄ νŒ¨μΉ˜μž…λ‹ˆλ‹€.

from pytest_django.plugin import _blocking_manager
from django.db.backends.base.base import BaseDatabaseWrapper

_blocking_manager.unblock()
_blocking_manager._blocking_wrapper = BaseDatabaseWrapper.ensure_connection

λ‚˜λŠ” λ˜ν•œ 이 λ°μ½”λ ˆμ΄ν„°( @pytest.mark.django_db 와 ν•¨κ»˜)λ₯Ό μ‚¬μš©ν•˜μ—¬ ν…ŒμŠ€νŠΈκ°€ 잘λͺ»λœ λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ μ‹€ν–‰λ˜κ³  μžˆμ§€ μ•Šμ€μ§€ ν™•μΈν•©λ‹ˆλ‹€. μ˜ˆμΈ‘ν•˜κΈ°κ°€ μ•½κ°„ λ³΅μž‘ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.

def assert_test_db(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        assert connection.settings_dict['NAME'] == 'test_kf_server', connection.settings_dict
        return func(*args, **kwargs)
    return wrapper

@Telofy μ˜ˆμ œμ— κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€. 정말 μœ μš©ν•©λ‹ˆλ‹€. λ˜ν•œ 전체 μ½”λ“œλ² μ΄μŠ€λ₯Ό λ‹€μ‹œ μž‘μ„±ν•˜λŠ” 것을 ν”Όν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€!

κ°€μ Έμ˜¬ λ•Œ db 쿼리λ₯Ό λ§Œλ“œλŠ” 것은 λ‚˜μœ pytest둜 μ΄μ–΄μ§ˆ 뿐만 μ•„λ‹ˆλΌ μΆ”μ ν•˜κΈ° μ–΄λ €μš΄ λ‹€λ₯Έ λΆ€λΆ„μ—μ„œ 문제λ₯Ό μΌμœΌν‚¬ κ²ƒμž…λ‹ˆλ‹€. 당신은 λ‚˜μœ μ‹œκ°„μ„ 보낼거야.

전체 μ½”λ“œλ² μ΄μŠ€λ₯Ό λ‹€μ‹œ μž‘μ„±ν•  ν•„μš”κ°€ μ—†μŠ΅λ‹ˆλ‹€. 지연 λ©”μ„œλ“œ/속성을 μ‚¬μš©ν•˜μ—¬ ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 값을 μš”μ²­ν•  λ•ŒκΉŒμ§€ μ—°κ²°ν•˜μ§€ μ•ŠλŠ” μ½”λ“œμž…λ‹ˆλ‹€. λ˜ν•œ ν‰κ°€ν•˜μ§€ μ•Šκ³  쿼리 μ„ΈνŠΈλ§Œ 전달해도 μž‘λ™ν•©λ‹ˆλ‹€. count λ“±κ³Ό 같은 것을 ν”Όν•˜μ‹­μ‹œμ˜€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰