Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django unit testing on multiple databases

I'm working on a django project where all my unit test cases were working perfectly.

Ass soon as I introduced a second database all my test cases that inherit from TestCase are broken. At this stage I haven't build any test case for that second database but my router is working fine.

When I run the tests I get the error,

"KeyError: 'SUPPORTS_TRANSACTIONS'"

It appears to me that is trying to check that that all the databases that I've got setup support transactions but the second database is never created.

Any ideas on how to have the test script to build the second database.

like image 550
user298404 Avatar asked Nov 12 '10 02:11

user298404


People also ask

Can Django support multiple databases?

Django's admin doesn't have any explicit support for multiple databases. If you want to provide an admin interface for a model on a database other than that specified by your router chain, you'll need to write custom ModelAdmin classes that will direct the admin to use a specific database for content.

Where does Django store test database?

from the docs: When using SQLite, the tests will use an in-memory database by default (i.e., the database will be created in memory, bypassing the filesystem entirely!). The TEST dictionary in DATABASES offers a number of settings to configure your test database.

What is RequestFactory in Django?

The request factory The RequestFactory shares the same API as the test client. However, instead of behaving like a browser, the RequestFactory provides a way to generate a request instance that can be used as the first argument to any view.

How do you write unit tests in Django?

To write a test you derive from any of the Django (or unittest) test base classes (SimpleTestCase, TransactionTestCase, TestCase, LiveServerTestCase) and then write separate methods to check that specific functionality works as expected (tests use "assert" methods to test that expressions result in True or False values ...


1 Answers

I realise this is quite an old thread, but I ran into it with the same issue, and my resolve was adding the multi_db = True flag to my testcase, e.g:

class TestThingWithMultipleDatabases(TestCase):
     multi_db = True

     def test_thing(self):
         pass

Source https://github.com/django/django/blob/master/django/test/testcases.py#L861

This causes django to call flush on all databases (or rollback if they support transactions)

I too am using a db router

I'm afraid I cant find this in Django's documentation, so no link for that

like image 126
farridav Avatar answered Sep 21 '22 01:09

farridav