Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it

Ev. picture Ev. · Jul 12, 2017 · Viewed 8.3k times · Source

My problem is the one bellow. If I try to run the test, it says there is no database permission and that I have to add that fixture. The problem is that I have added that fixture to any possible method I have, and still nothing. So I assume I don't know where to add this marker.

Shouldn't it be in the method of the class I am calling the creation of these objects: test_can_get_page_details?

If I remove UserFactory from assigning users to those attributes the test works.

pytest.ini

[pytest]
DJANGO_SETTINGS_MODULE=core.settings_test

test_pages.py

import pytest
from bs4 import BeautifulSoup
import datetime
from django.core.urlresolvers import reverse
from interface.tests import factories


class TestPageDetail:
    @pytest.fixture
    def defaults(self, db):
        page = factories.PageFactory()
        url = page.get_absolute_url()
        return {
            'page': page,
            'url': url
        }

    @pytest.mark.integration
    def test_can_get_page_details(self, defaults, db, admin_client):
        response = admin_client.get(defaults['url'])
        assert 200 == response.status_code

conftest.py

import pytest


@pytest.fixture
def initial_data(db):
    from interface.management.commands.initial_data import Command as InitialData
    InitialData().handle()


@pytest.fixture
def page_and_variable(db, initial_data):
    from interface.tests.factories import PageFactory, VariableFactory

    page = PageFactory()
    variable = VariableFactory()

    return dict(
        page=page,
        variable=variable
    )

factories.py

import factory
import datetime
import pytest

from interface import models


class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = models.User
        django_get_or_create = ('username',)

    username = factory.Sequence(lambda n: "staffman-%d" % n)
    email = factory.LazyAttribute(lambda obj: '%[email protected]' % obj.username)
    first_name = factory.Sequence(lambda n: "John-%d" % n)
    last_name = "Doe"
    password = "1234"

    @classmethod
    def _create(cls, model_class, *args, **kwargs):
        """Override the default ``_create`` with our custom call."""
        manager = cls._get_manager(model_class)
        # default is ``manager.create(*args, **kwargs)``
        user = manager.create_user(*args, **kwargs)
        user.is_staff = True
        user.is_superuser = True
        user.save()
        return user


class PageFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = models.Page

    slug = factory.Sequence(lambda n: "slug-%d" % n)
    title = factory.Sequence(lambda n: "title-%d" % n)
    content = factory.Sequence(lambda n: "Page content with a number: %d" % n)
    created_by = UserFactory()
    updated_by = UserFactory()


class VariableFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = models.Variable

    name = factory.Sequence(lambda n: "some_variable_%d" % n)
    content = factory.Sequence(lambda n: "Variable content with a number: %d" % n)

Error

/documents/envs/atomenv/bin/python /documents/applications/pycharm-2017.1.2/helpers/pycharm/_jb_pytest_runner.py --path test_pages.py -- --nomigrations
Testing started at 1:14 PM ...
Launching py.test with arguments --nomigrations test_pages.py in /documents/projects/atom/interface/tests
============================= test session starts ==============================
platform linux -- Python 3.5.2, pytest-3.1.3, py-1.4.34, pluggy-0.4.0
Django settings: core.settings_test (from environment variable)
rootdir: /documents/projects/atom, inifile: pytest.ini
plugins: mock-1.6.0, django-3.1.2, cov-2.5.1
interface/tests/test_pages.py:None (interface/tests/test_pages.py)
test_pages.py:5: in <module>
    from interface.tests import factories
factories.py:21: in <module>
    class PageFactory(factory.django.DjangoModelFactory):
factories.py:28: in PageFactory
    created_by = UserFactory()
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:69: in __call__
    return cls.create(**kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:623: in create
    return cls._generate(True, attrs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:548: in _generate
    obj = cls._prepare(create, **attrs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:523: in _prepare
    return cls._create(model_class, *args, **kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/django.py:179: in _create
    return cls._get_or_create(model_class, *args, **kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/django.py:170: in _get_or_create
    obj, _created = manager.get_or_create(*args, **key_fields)
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/manager.py:85: in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/query.py:464: in get_or_create
    return self.get(**lookup), False
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/query.py:374: in get
    num = len(clone)
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/query.py:232: in __len__
    self._fetch_all()
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/query.py:1105: in _fetch_all
    self._result_cache = list(self._iterable_class(self))
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/query.py:53: in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/sql/compiler.py:874: in execute_sql
    cursor = self.connection.cursor()
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/backends/base/base.py:254: in cursor
    return self._cursor()
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/backends/base/base.py:229: in _cursor
    self.ensure_connection()
E   Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
collected 0 items / 1 errors
==================================== ERRORS ====================================
________________ ERROR collecting interface/tests/test_pages.py ________________
test_pages.py:5: in <module>
    from interface.tests import factories
factories.py:21: in <module>
    class PageFactory(factory.django.DjangoModelFactory):
factories.py:28: in PageFactory
    created_by = UserFactory()
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:69: in __call__
    return cls.create(**kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:623: in create
    return cls._generate(True, attrs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:548: in _generate
    obj = cls._prepare(create, **attrs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/base.py:523: in _prepare
    return cls._create(model_class, *args, **kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/django.py:179: in _create
    return cls._get_or_create(model_class, *args, **kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/factory/django.py:170: in _get_or_create
    obj, _created = manager.get_or_create(*args, **key_fields)
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/manager.py:85: in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/query.py:464: in get_or_create
    return self.get(**lookup), False
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/query.py:374: in get
    num = len(clone)
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/query.py:232: in __len__
    self._fetch_all()
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/query.py:1105: in _fetch_all
    self._result_cache = list(self._iterable_class(self))
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/query.py:53: in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/models/sql/compiler.py:874: in execute_sql
    cursor = self.connection.cursor()
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/backends/base/base.py:254: in cursor
    return self._cursor()
/documents/envs/atomenv/lib/python3.5/site-packages/django/db/backends/base/base.py:229: in _cursor
    self.ensure_connection()
E   Failed: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.33 seconds ============================
Process finished with exit code 0

Answer

Ev. picture Ev. · Jul 12, 2017

I've found the problem. Instead of manually creating the factory:

created_by = UserFactory()

I have to use the SubFactory

created_by = factory.SubFactory(UserFactory)