Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching from Django UnitTest to pytest-django results in : Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it. #1118

Open
Some7hing0riginal opened this issue Apr 12, 2024 · 1 comment

Comments

@Some7hing0riginal
Copy link

Some7hing0riginal commented Apr 12, 2024

I'm trying to keep my tests the same but wrap them in pytest which would let me use some nice to haves provided by pytest. When I run via pytest, I get the following

Platform/tests/graphql/mutation/test_announcement.py:11: in <module>
    from Platform.tests.graphql.mutation.base import MutationBaseTestCase
Platform/tests/graphql/mutation/base.py:1: in <module>
    from ..base import GraphqlBaseTestCase
Platform/tests/graphql/base.py:17: in <module>
    class GraphqlBaseTestCase(GraphqlMixin, PlatformBaseTestCase):
Platform/tests/graphql/base.py:18: in GraphqlBaseTestCase
    project_content_type = ContentType.objects.get_for_model(ProxyProject)
venv/lib/python3.9/site-packages/django/contrib/contenttypes/models.py:51: in get_for_model
    ct = self.get(app_label=opts.app_label, model=opts.model_name)
venv/lib/python3.9/site-packages/django/db/models/manager.py:85: in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
venv/lib/python3.9/site-packages/cacheops/query.py:351: in get
    return qs._no_monkey.get(qs, *args, **kwargs)
venv/lib/python3.9/site-packages/django/db/models/query.py:492: in get
    num = len(clone)
venv/lib/python3.9/site-packages/django/db/models/query.py:302: in __len__
    self._fetch_all()
venv/lib/python3.9/site-packages/cacheops/query.py:271: in _fetch_all
    return self._no_monkey._fetch_all(self)
venv/lib/python3.9/site-packages/django/db/models/query.py:1507: in _fetch_all
    self._result_cache = list(self._iterable_class(self))
venv/lib/python3.9/site-packages/django/db/models/query.py:57: in __iter__
    results = compiler.execute_sql(
venv/lib/python3.9/site-packages/django/db/models/sql/compiler.py:1359: in execute_sql
    cursor = self.connection.cursor()
venv/lib/python3.9/site-packages/django/utils/asyncio.py:26: in inner
    return func(*args, **kwargs)
venv/lib/python3.9/site-packages/django/db/backends/base/base.py:284: in cursor
    return self._cursor()
venv/lib/python3.9/site-packages/django/db/backends/base/base.py:260: in _cursor
    self.ensure_connection()
E   RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.

This happens on pytest version 8.11 with pytest-django version 4.8.0

Additionally, I attempted to mark every test class involved (even each parent class for the test shown in the stack trace) like so

@pytest.mark.django_db
class GraphqlBaseTestCase(GraphqlMixin, PlatformBaseTestCase):

Running via python manage.py test works fine.

Any idea how I can get this working?

@nbelakovski-mssm
Copy link

Looking at the stack trace, I think what's happening is that as pytest tries to run from Platform.tests.graphql.mutation.base import MutationBaseTestCase in test_announcement.py, something in that import statement is trying access the database (it looks like base.py:18). I'm not entirely sure how you might fix that, perhaps moving the offending line into __init__ for the class, or putting it into a function, so that it's not run at import-time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants