2017-01-19 17:16:04 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
2015-08-01 14:46:25 +00:00
|
|
|
from django.db import connections, models
|
2018-11-26 19:05:02 +00:00
|
|
|
from django.test import SimpleTestCase
|
2015-11-17 05:39:28 +00:00
|
|
|
from django.test.utils import isolate_apps, override_settings
|
2015-08-01 14:46:25 +00:00
|
|
|
|
|
|
|
|
2017-01-19 07:39:46 +00:00
|
|
|
class TestRouter:
|
2015-08-01 14:46:25 +00:00
|
|
|
"""
|
|
|
|
Routes to the 'other' database if the model name starts with 'Other'.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2016-02-10 04:44:04 +00:00
|
|
|
def allow_migrate(self, db, app_label, model_name=None, **hints):
|
|
|
|
return db == ("other" if model_name.startswith("other") else "default")
|
2015-08-01 14:46:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
@override_settings(DATABASE_ROUTERS=[TestRouter()])
|
2015-11-17 05:39:28 +00:00
|
|
|
@isolate_apps("check_framework")
|
2018-11-26 19:05:02 +00:00
|
|
|
class TestMultiDBChecks(SimpleTestCase):
|
2015-08-01 14:46:25 +00:00
|
|
|
def _patch_check_field_on(self, db):
|
|
|
|
return mock.patch.object(connections[db].validation, "check_field")
|
|
|
|
|
|
|
|
def test_checks_called_on_the_default_database(self):
|
|
|
|
class Model(models.Model):
|
|
|
|
field = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
model = Model()
|
|
|
|
with self._patch_check_field_on("default") as mock_check_field_default:
|
|
|
|
with self._patch_check_field_on("other") as mock_check_field_other:
|
2020-02-19 11:14:54 +00:00
|
|
|
model.check(databases={"default", "other"})
|
2015-08-01 14:46:25 +00:00
|
|
|
self.assertTrue(mock_check_field_default.called)
|
|
|
|
self.assertFalse(mock_check_field_other.called)
|
|
|
|
|
|
|
|
def test_checks_called_on_the_other_database(self):
|
|
|
|
class OtherModel(models.Model):
|
|
|
|
field = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
model = OtherModel()
|
|
|
|
with self._patch_check_field_on("other") as mock_check_field_other:
|
|
|
|
with self._patch_check_field_on("default") as mock_check_field_default:
|
2020-02-19 11:14:54 +00:00
|
|
|
model.check(databases={"default", "other"})
|
2015-08-01 14:46:25 +00:00
|
|
|
self.assertTrue(mock_check_field_other.called)
|
|
|
|
self.assertFalse(mock_check_field_default.called)
|