gpt4 book ai didi

python - 失败 : Database access not allowed, 使用 "django_db"标记,或 "db"或 "transactional_db"装置启用它

转载 作者:太空狗 更新时间:2023-10-29 22:24:46 46 4
gpt4 key购买 nike

我的问题是下面那个。如果我尝试运行测试,它会说没有数据库权限,我必须添加该 fixture 。问题是我已经将该固定装置添加到我拥有的任何可能的方法中,但仍然没有。所以我假设我不知道在哪里添加这个标记。

不应该在我调用创建这些对象的类的方法中:test_can_get_page_details吗?

如果我从将用户分配给这些属性的过程中删除 UserFactory,则测试有效。

pytest.ini

[pytest]
DJANGO_SETTINGS_MODULE=core.settings_test

测试页面.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
)

工厂.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: '%s@example.com' % 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)

错误

/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

最佳答案

我发现了问题。而不是手动创建工厂:

created_by = UserFactory()

我必须使用 SubFactory

created_by = factory.SubFactory(UserFactory) 

关于python - 失败 : Database access not allowed, 使用 "django_db"标记,或 "db"或 "transactional_db"装置启用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45056387/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com