我对吗,TestCase 在 SQLite 的情况下清除数据库,而在 PostgreSQL 的情况下不清除它
Am I right that TestCase clears DB in case of SQLite and doesn't clear it in case of PostgreSQL
代码如下。两个测试。设置上的区别: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/')
没有。数据库总是在测试用例之间被清除。不同之处在于数据库如何管理其自动增量序列; sqlite 似乎重置了它们,但 Postgres 没有。您不应在测试中依赖任何一种行为,而应根据相关文档的 ID 进行具体检查。
代码如下。两个测试。设置上的区别: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/')
没有。数据库总是在测试用例之间被清除。不同之处在于数据库如何管理其自动增量序列; sqlite 似乎重置了它们,但 Postgres 没有。您不应在测试中依赖任何一种行为,而应根据相关文档的 ID 进行具体检查。