Django TestCase未在辅助数据库上使用事务 [英] Django TestCase not using transactions on secondary database
问题描述
我正在使用Django 1.3.1。我有两个数据库,我的某些模型位于一个数据库中,而另一些模型则位于另一个数据库中。这两个数据库都是contrib.gis.db.backends.postgis数据库。
I am using Django 1.3.1. I have two databases, some of my models live in one database, some in the other. Both databases are contrib.gis.db.backends.postgis databases.
令我惊讶的是,Django的TestCase没有回滚我在两次测试之间在二级数据库中所做的更改。
To my surprise, Django's TestCase isn't rolling back changes I made in the secondary database between tests.
在以下代码中,myproject.models.WellOwner是一个非常简单的模型,基本上只有一个字段 name。路由器说它应该在辅助数据库中。第一个测试中的断言成功,第二个测试中的失败:
In the following code, myproject.models.WellOwner is a very simple model that basically only has a field "name". The router says that it should be in the secondary database. The assertion in the first test succeeds, the second test fails:
from django.test import TestCase
from myproject.models import WellOwner
class SimpleTest(TestCase):
def test1(self):
WellOwner.objects.create(name="Remco")
self.assertEquals(1, WellOwner.objects.count()) # Succeeds
class SimpleTest2(TestCase):
def test2(self):
# I would expect to have an empty database at this point
self.assertEquals(0, WellOwner.objects.count()) # Fails!
我假设Django将其包装在默认数据库中的事务中,而不是在辅助数据库中。这是一个已知问题吗?有解决办法吗?在1.4中?我的Google-fu失败了。
I assume that Django wraps this in a transaction on the default database, but not on the secondary database. Is this a known problem? Is there a fix? In 1.4 perhaps? My Google-fu is failing.
(如果我在设置中将DATABASE_ROUTERS更改为[],以便所有内容都进入同一个数据库,问题就消失了)
(if I change DATABASE_ROUTERS to [] in settings so that everything goes into the same database, the problem disappears)
我将添加路由器的整个代码,以防出现问题:
I'll add the whole code of the router, in case it helps:
SECONDARY_MODELS = ('WellOwner', ...)
import logging
logger = logging.getLogger(__name__)
class GmdbRouter(object):
"""Keep some models in a secondary database."""
def db_for_read(self, model, **hints):
if model._meta.app_label == 'gmdb':
if model._meta.object_name in SECONDARY_MODELS:
return 'secondary'
return None
def db_for_write(self, model, **hints):
# Same criteria as for reading
return self.db_for_read(model, **hints)
def allow_syncdb(self, db, model):
if db == 'secondary':
if model._meta.app_label in ('sites', 'south'):
# Hack for bug https://code.djangoproject.com/ticket/16353
# When testing, create django_site and south in both databases
return True
return self.db_for_read(model) == 'secondary'
else:
# Some other db
if model._meta.app_label == 'gmdb':
# Our models go in the other db if they don't go into secondary
return self.db_for_read(model) != 'secondary'
# Some other model in some other db, no opinion
return None
推荐答案
尝试一下:
class MyTestCase(TestCase):
multi_db = True
https://docs.djangoproject.com/zh-CN/1.2/topics/testing/#django.test.Test Case.multi_db
这篇关于Django TestCase未在辅助数据库上使用事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!