Pytest errors connecting to test database
Asked Answered
E

2

5

I am running into strange errors I have never seen before running pytest where I am running my tests and nearly all are erroring out due to not being allowed to access the database.

This is a new error that did not occur last week so it isn't a local code change, but I am not sure exactly what happened.

Can anybody point me in the right direction?

Here is the error:

conftest.py:52: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/local/lib/python3.8/site-packages/django/db/models/query.py:287: in __iter__
    self._fetch_all()
/usr/local/lib/python3.8/site-packages/cacheops/query.py:271: in _fetch_all
    return self._no_monkey._fetch_all(self)
/usr/local/lib/python3.8/site-packages/django/db/models/query.py:1308: in _fetch_all
    self._result_cache = list(self._iterable_class(self))
/usr/local/lib/python3.8/site-packages/django/db/models/query.py:53: in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
/usr/local/lib/python3.8/site-packages/django/db/models/sql/compiler.py:1154: in execute_sql
    cursor = self.connection.cursor()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <django.test.testcases._DatabaseFailure object at 0x7fb361b2d2b0>

    def __call__(self):
>       raise AssertionError(self.message)
E       AssertionError: Database queries to 'test' are not allowed in this test. Add 'test' to pytest_django.fixtures._django_db_fixture_helper.<locals>.PytestDjangoTestCase.databases to ensure proper test isolation and silence this failure.

/usr/local/lib/python3.8/site-packages/django/test/testcases.py:146: AssertionError
_ ERROR at setup of TestAccountViews.test_can_edit_account_info_with_backticks[admin] _

db = None

    @pytest.fixture
    def slug(db):
        # delete these guys because there should only ever be 1 of each
        from apps.payments.models.ledger_models import TransactionCode
>       for code_object in TransactionCode.objects.all():

Extort answered 17/5, 2021 at 12:38 Comment(0)
E
6

Well it appears this was a versioning issue.

pytest-django https://pypi.org/project/pytest-django/4.3.0/ updated a few days ago and apparently this is a new error because of that. I don't see anything in their changelog: https://pytest-django.readthedocs.io/en/latest/changelog.html but dropping from version 4.3.0 to 4.2.0 did fix the issue.

Another fix to the issue was to alter pytest_sessionstart() to allow access to all databases in conftest.py like so:

def pytest_sessionstart(session):
    from django.test import TestCase
    TestCase.multi_db = True
    TestCase.databases = '__all__'      # here
Extort answered 17/5, 2021 at 12:59 Comment(2)
Make sure to lock your dependencies and breaking updates won't happen behind your back...Sonjasonnet
@Sonjasonnet yeah, I have it for everything else just didn't think it mattered for tests. Have already changed this in requirements.Extort
A
4

As per Tests requiring multiple databases in the pytest-django docs, it may now be possible (this worked for me on pytest-django version 4.3.0) to fix this by using the following pytest mark:

@pytest.mark.django_db(databases=['default', 'test'])
def test_my_code():
    ...

Where the list passed to databases= is a list of keys of databases you have defined in settings.DATABASES.

However it seems this area is subject to some change and may only partially be implemented, so YMMV.

Ashy answered 7/6, 2021 at 14:41 Comment(2)
I don't want to use default DB, I wat to use a different DB for testing how can i do that?Syck
The list of databases passed to databases= is a list of keys in your DATABASES dictionary in your django settings.py file, so you should just pass the key of the database you want to use... if you want to use a different DB for all your tests then the simplest thing is to configure DATABASES['default'] to be different when testing, which you can detect by looking at sys.argv or by using an environment variableAshy

© 2022 - 2024 — McMap. All rights reserved.