jeudi 5 mars 2015

Multiple database unittest in Django

I have multiple databases in my Django project setting but In development setting configuration I have something like this:



DATABASES = {
'default': {
'NAME': 'db',
# Some options
},
}

DATABASES['backup'] = DATABASES['default']


So they are equal.


I tried to write unittest for following method:



def func():
# Somecode
SomeObject.objects.using('backup').all()


So I created a fixture and a UnitTest class. But when I run tests it seems that backup database is empty. When I remove "using" from statement everything works OK So i think Django creates two different test databases for "default" and "backup" although they are equal and fixture only effect on "default" database.


I read Django's "Test and Multiple databases" topic. So I added following code to end of my setting file:



DATABASES['backup']['TEST_MIRROR'] = 'default'


But when I run tests it seems that Django UnitTest uses "db" instead of "test_db" because I got an error in installing fixture indicating "duplicate key value" but my fixture is correct and also I don't have this problem without "TEST_MIRROR".


I would be grateful if someone shows me correct way to test it.


Aucun commentaire:

Enregistrer un commentaire