Django does not test mirror database

I am trying to configure some tests in my Django application. I use a database mirror for some readings made throughout the application. When I try to test these parts by creating data mockups in the database and then try to read them, it seems that the data is not in the mirror database, despite the fact that they are configured as TEST_MIRROR .

The database configuration for testing is as follows:

 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'maindb', 'HOST': 'localhost' }, 'mirror1': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'maindb', 'HOST': 'localhost', 'TEST_MIRROR': 'default' } } 

And then in my test I do something like this (Foo - model)

 Foo.objects.create(name='bar') self.assertTrue(Foo.objects.filter(name='bar').exists()) # passes self.assertTrue(Foo.objects.using('mirror1').filter(name='bar').exists()) # fails 

This is confusing for me, since I thought that the point of TEST_MIRROR was to make calls to the mirror right to the default value?

+6
source share
2 answers

If your installation contains several databases, and you have a test that each database requires, you can use the multi_db attribute on to request a full flash.

For instance:

 class TestMyViews(TestCase): multi_db = True def testIndexPageView(self): call_some_test_code() 

This documentation ( testing support for multiple databases ) is not accurate, since the multi_db condition (indirectly in _databases_names) is not only used for tearDown in the Django source, but also for '_fixture_setup'. (Django-1.5.1 / django / test / testcases.py: 834) Therefore, this is apparently the main condition, independent of the master / slave settings.

+4
source

I think the answer may be here :

When the test environment is configured, the test version of the slave will not be created . Instead, the connection to the subordinate will be redirected to the default point.

Since the slave device does not really exist in testing, it makes sense that an attempt to call it directly is not performed.

+1
source

Source: https://habr.com/ru/post/954259/


All Articles