Django 1.9.7 PostgreSQL 9.5.3
你能帮我理解会发生什么吗?测试会影响我的真实数据库。虽然在控制台中它会宣布"为别名创建测试数据库'默认'"。当我运行测试,然后运行服务器并转到http://localhost:8000/admin/auth/user/时,我可以看到两个用户:1)michael,以及2)fe64d759-419c-41d7-b266-acfeb0,电子邮件为lennon@thebeatles.com。
嗯,最后一个肯定是在测试期间创建的。我可以删除它并进行双重检查:这些用户肯定是通过测试创建的。
这里还有另一个问题:表(" auth_user")中没有Key(created_by_id)=(14)。我不知道它是否与标题问题有关。 也许它不是,但在我的真实数据库分离并且安全之后,我会尝试应对这一点。
无论如何,测试会影响真实的数据库。你能帮帮我吗?
/pharchive/pharchive/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'michael',
'PASSWORD': '***',
'HOST': 'localhost',
'PORT': '',
}
}
/pharchive/masterdocument/tests.py
from django.test import TransactionTestCase
today = datetime.datetime.today()
def create_user(name = None):
name = (name or str(uuid.uuid4())[:30])
user = User.objects.create_user(name, 'lennon@thebeatles.com', 'johnpassword')
return user
class MasterDocumentTest(TransactionTestCase):
user = create_user()
def create_master_document(self, title=None,
dated_from=today,
dated_through=today,
created_by=user,
creation_date=today):
title = title or "Some title"
md = MasterDocument.objects.create(title = title,
dated_from = dated_from,
dated_through = dated_through,
created_by = created_by,
creation_date = creation_date)
return md
def test_0_create_master_document(self):
self.create_master_document()
number_of_mds = MasterDocument.objects.all().count()
self.assertEqual(number_of_mds, 1)
回溯
Creating test database for alias 'default'...
E
======================================================================
Destroying test database for alias 'default'...
ERROR: test_0_create_master_document (masterdocument.tests.MasterDocumentTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/michael/workspace/venvs/photoarchive1/lib/python3.5/site-packages/django/db/backends/base/base.py", line 211, in _commit
return self.connection.commit()
psycopg2.IntegrityError: insert or update on table "masterdocument_masterdocument" violates foreign key constraint "masterdocument_masterdoc_created_by_id_5cb1a3d5_fk_auth_user_id"
DETAIL: Key (created_by_id)=(14) is not present in table "auth_user".
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/michael/workspace/pharchive/pharchive/masterdocument/tests.py", line 41, in test_0_create_master_document
self.create_master_document()
File "/home/michael/workspace/pharchive/pharchive/masterdocument/tests.py", line 37, in create_master_document
creation_date = creation_date)
File "/home/michael/workspace/venvs/photoarchive1/lib/python3.5/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/michael/workspace/venvs/photoarchive1/lib/python3.5/site-packages/django/db/models/query.py", line 401, in create
obj.save(force_insert=True, using=self.db)
File "/home/michael/workspace/venvs/photoarchive1/lib/python3.5/site-packages/django/db/models/base.py", line 708, in save
force_update=force_update, update_fields=update_fields)
File "/home/michael/workspace/venvs/photoarchive1/lib/python3.5/site-packages/django/db/models/base.py", line 736, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/michael/workspace/venvs/photoarchive1/lib/python3.5/site-packages/django/db/transaction.py", line 223, in __exit__
connection.commit()
File "/home/michael/workspace/venvs/photoarchive1/lib/python3.5/site-packages/django/db/backends/base/base.py", line 242, in commit
self._commit()
File "/home/michael/workspace/venvs/photoarchive1/lib/python3.5/site-packages/django/db/backends/base/base.py", line 211, in _commit
return self.connection.commit()
File "/home/michael/workspace/venvs/photoarchive1/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/michael/workspace/venvs/photoarchive1/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/michael/workspace/venvs/photoarchive1/lib/python3.5/site-packages/django/db/backends/base/base.py", line 211, in _commit
return self.connection.commit()
django.db.utils.IntegrityError: insert or update on table "masterdocument_masterdocument" violates foreign key constraint "masterdocument_masterdoc_created_by_id_5cb1a3d5_fk_auth_user_id"
DETAIL: Key (created_by_id)=(14) is not present in table "auth_user".
----------------------------------------------------------------------
Ran 1 test in 2.518s
FAILED (errors=1)
Finished "/home/michael/workspace/pharchive/pharchive/manage.py test" execution.
答案 0 :(得分:0)
在user = create_user()
内添加def create_master_document
。
或
self.user = create_user()
def create_master_document(created_by = self.user)