【发布时间】:2016-06-19 07:04:34
【问题描述】:
代码如下。两个测试。设置的区别:PostgreSQL 和 SQLite。
切换到 SQLite,这些测试通过。但在 PostgreSQL 的情况下会发生此错误:
AssertionError: '/documents/1002/' != '/documents/1/'
- /documents/1002/
? ---
+ /documents/1/
我一直将 SQLite 用于学习目的,并认为在每次测试之前都会清理数据库。但后来我尝试了 PostgreSQL,可以看到这种行为似乎与数据库管理系统有关,而不是与 TestCase 算法有关。
您能对此发表评论吗?
****VARIANT 1****
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'michael',
'PASSWORD': '***',
'HOST': 'localhost',
'PORT': '',
}
}
**VARIANT 2**
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase',
'USER': 'michael',
'PASSWORD': '***',
'HOST': 'localhost',
}
}
from django.test import TestCase
def create_master_document(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
class MasterDocumentTest(TestCase):
def test_0_create_master_document(self):
create_master_document()
number_of_mds = MasterDocument.objects.all().count()
self.assertEqual(number_of_mds, 1)
def test_1_create_multiple_master_documents(self):
for i in range(0, 1000):
create_master_document()
number_of_mds = MasterDocument.objects.all().count()
self.assertEqual(number_of_mds, 1000)
def test_2_get_absolute_url(self):
md = create_master_document()
url = md.get_absolute_url()
self.assertEqual(url, '/documents/1/')
【问题讨论】: