- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的问题是下面那个。如果我尝试运行测试,它会说没有数据库权限,我必须添加该 fixture 。问题是我已经将该固定装置添加到我拥有的任何可能的方法中,但仍然没有。所以我假设我不知道在哪里添加这个标记。
不应该在我调用创建这些对象的类的方法中:test_can_get_page_details
吗?
如果我从将用户分配给这些属性的过程中删除 UserFactory,则测试有效。
[pytest]
DJANGO_SETTINGS_MODULE=core.settings_test
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
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
)
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/
我当前正在存储给定产品的上传图像,如下所示: class Product(db.Model): images= db.ListProperty(db.Blob) # More prop
每次对架构或新迁移文件进行更改时,我都会运行以下命令: rake db:drop db:create db:migrate db:seed 是否有预先构建的等效方法来执行此操作? 我从我读到的内容中想
在 android 中使用房间作为数据库。当我试图在 sqlviewer 中查看数据时,在数据库文件中找不到表Myapp.db 文件为空。数据/data/packageName/databases/M
我搜索并尝试了很多次,但没有找到我的答案。我有一些用小 cucumber (在 Rails 项目中)编写的项目的功能文件。所有步骤都已定义,如果我单独启动它们,功能本身运行得很好。我可以将所有场景与我
您必须承认,对于 Rails 和数据库的新手来说,rubyonrails.org 上的官方解释使所有这四个任务听起来完全一样。引用: rake db:test:clone Recreate the
当我尝试运行时: heroku run rake db:drop db:create db:migrate 我得到错误: Running rake db:drop attached to termin
rake db:migrate 和 rake db:reset 之间的区别对我来说非常清楚。我不明白的是 rake db:schema:load 与前两者有何不同。 只是为了确保我在同一页面上: ra
我们都知道,我们可以使用 Azure 函数(使用 out 参数或使用 return)在 cosmos DB 中一次保存一个文档,例如: object outputDocument = new { i
我有一个包含 60 多个表的 mysql 数据库。这是在我将 joomla 版本 2.5.3 从本地灯移植到网络服务器时构建的。 我运行 mysql-db: 移植后我发现我无法登录 amdin 区域。
我想轻松地将现有数据库迁移到 Azure 托管。在我的项目中,我使用 Entity Framework DB First。有什么经验教训或例子可以说明如何做到这一点吗? 最佳答案 您本地使用什么数据库
所以,我一直在使用 MagicalRecord 开发 iPad 应用程序,最近在转移到自动迁移商店后我遇到了一些问题。我需要将我的 .db 文件从一个设备同步到另一个设备,所以我需要所有数据都在 .d
自从我在 Heroku 上部署并希望与生产相匹配后,我最近切换到 postgres 来开发一个 Rails 应用程序。当我将数据库名称设置为“postgres”时,我的应用程序安装了 Postgres
我使用 oledb 提供程序(SQLOLEDB 和 SQL Native OLEDB 提供程序)创建了一个示例应用程序。 案例 1:提供者 = SQLOLEDB hr = ::CoInitialize
我正在为 NodeJs 使用 mongodb 驱动程序,其中有 3 个方法: 1) db.collection.insert 2) 数据库.collection.insertOne 3) db.col
我是 datomic 的新手,我仍在努力弄清楚系统是如何构建的。特别是,我不明白 :db.part/db 扮演什么角色,因为每次安装架构时似乎都需要它。有人可以解释一下这一切意味着什么吗? (需要 '
Berkeley DB 是否有空间索引,例如 R-tree? 最佳答案 有人问the same question on the Oracle forum .还没有甲骨文回答。但答案是否定的,它没有任何
请解释一下这是什么意思 $db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE); 它给了我一个错误 "E
berkeley-db-je 的最新版本是什么? 来自 oracle , 为 7.5。 但来自maven存储库,它是 18.3.12。 有没有人知道更多的细节? 最佳答案 Berkeley DB Ja
我不明白查询构建器的替换和更新之间的区别。尤其是替换文档... This method executes a REPLACE statement, which is basically the SQL
看起来 BerkeleyDB 被 Oracle 收购了,它没有在其网站上发布源代码? 最佳答案 Sleepycat 于 2006 年被 Oracle 收购。该产品继续在原始开源许可下可用,并继续得到增
我是一名优秀的程序员,十分优秀!