Use django test database when running test
I have my database defined as followed in my django settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'USER': 'mydatabaseuser',
'NAME': 'mydatabase',
'TEST': {
'NAME': 'mytestdatabase',
},
},
}
When I run my tests (using python manage.py test) I would expect factory boy to create my fixtures using mytestdatabase but mydatabase is used instead.
This is problematic because classic django ORM operations are using the test database (mytestdatabase) and therefore the following test would fail:
def test_factories(self)
UserFactory.create() # User is created in mydatabase
self.assertEqual(User.objects.count(), 1) # this assertion fails because django can't find any user in mytestdatabase
I have had a look at the database option on the Meta class of the model factory but it doesn't seem to solve my problem. I would have to create a second database alias in my DATABASES dict and django ORM query will still be executed in the test database of that new alias.
Is there a solution to my problem?
My mistake the problem came from a UserFactory.create() called into a test function decorator and was not using the right database.
Not an issue related to factory boy