python - Test isolation broken with multiple databases in Django. How to fix it? -
django’s testcase class wraps each test in transaction , rolls transaction after each test, in order provide test isolation.
apparently, however, operations in default
database within scope of transaction. have multiple database setup router directs orm calls on models second database. means in following example, test2
fails:
class mytestcase(testcase): def test1(self): # let's foo model orm calls routed different db foo = foo.objects.create(...) assert foo.is_ok() def test2(self): assert not foo.objects.exists()
the immediate solution problem override teardown
method in mytestcase
, manually make sure delete foo
objects. bit annoying because it's sort of hack , database sequences (e.g. autoincrement columns) won't reset example, after test suite done , database destroyed.
is there way fix this, making sure database operations default made inside transaction , rolled @ end of each test?
[update]
here's router:
class foorouter(object): def db_for_read(self, model, **hints): if model._meta.app_label == 'foo': return 'foo_db' return none def db_for_write(self, model, **hints): if model._meta.app_label == 'foo': return 'foo_db' return none def allow_migrate(self, db, app_label, model_name=none, **hints): if app_label == 'foo': return db == 'foo_db' return none
Comments
Post a Comment