2
コードは以下の通りです。 2つのテスト。設定の違い:PostgreSQLとSQLite私はTestCaseがSQLiteの場合はDBをクリアし、PostgreSQLの場合はクリアしません
SQLiteに切り替えると、これらのテストは合格になります。しかし、PostgreSQLの場合には、このエラーが発生します。私は常に学習の目的のためのSQLiteを使用して、すべてのテストの前にデータベースが洗浄されると考えられてきた
AssertionError: '/documents/1002/' != '/documents/1/'
- /documents/1002/
? ---
+ /documents/1/
。しかし、私は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/')