pytest-dev / pytest-dev/pytest-django

DB setup fails with KeyError when OPTIONS is used in DATABASES

Open
#724 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

When setting a timeout value in DATABASES (which is required to avoid TransactionManagementError with sqlite), a KeyError is raise inside pytest-django. More specifically, I set

DATABASES = {"default": env.db()}
DATABASES["OPTIONS"] = {"timeout": 10}

which results in

{'default': {'NAME': ':memory:', 'USER': '', 'PASSWORD': '', 'HOST': '', 'PORT': '', 'ENGINE': 'django.db.backends.sqlite3'}, 'OPTIONS': {'timeout': 10}}

All tests then fail in django_db_modify_db_settings_xdist_suffix, because OPTIONS has no ENGINE key:

https://github.com/pytest-dev/pytest-django/blob/787bbec7c459120866f786fe5f68bb6a7af6cb25/pytest_django/fixtures.py#L48-L50

Skipping entries without the ENGINE key fixes the problem for me, however I do not fully understand how that code is supposed to work:

        if not test_name:
            if not "ENGINE" in db_settings or db_settings["ENGINE"] == "django.db.backends.sqlite3":
                continue
Complete error
_________________________________________________________ ERROR at setup of TestDocumentAccess.test_delete_document _________________________________________________________

request = <SubRequest '_django_setup_unittest' for <TestCaseFunction test_delete_document>>
django_db_blocker = <pytest_django.plugin._DatabaseBlocker object at 0x7f3d9100af60>

    @pytest.fixture(autouse=True, scope="class")
    def _django_setup_unittest(request, django_db_blocker):
        """Setup a django unittest, internal to pytest-django."""
        if django_settings_is_configured() and is_django_unittest(request):
            request.getfixturevalue("django_test_environment")
>           request.getfixturevalue("django_db_setup")

.venv/lib/python3.6/site-packages/pytest_django/plugin.py:493: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

request = <SubRequest 'django_db_modify_db_settings_xdist_suffix' for <TestCaseFunction test_delete_document>>

    @pytest.fixture(scope="session")
    def django_db_modify_db_settings_xdist_suffix(request):
        skip_if_no_django()
    
        from django.conf import settings
    
        for db_settings in settings.DATABASES.values():
    
            try:
                test_name = db_settings["TEST"]["NAME"]
            except KeyError:
                test_name = None
    
            if not test_name:
>               if db_settings["ENGINE"] == "django.db.backends.sqlite3":
E               KeyError: 'ENGINE'

.venv/lib/python3.6/site-packages/pytest_django/fixtures.py:49: KeyError
========================================================================== 1 error in 1.04 seconds ==========================================================================

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in pytest_django/fixtures.py at django_db_modify_db_settings_xdist_suffix, then reproduce with the DATABASES configuration shown in the issue. Check how entries in settings.DATABASES are handled when an OPTIONS entry lacks ENGINE, and verify the fixture completes without the reported KeyError while preserving SQLite handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
backend, databases, testing-qa
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.