summaryrefslogtreecommitdiff
path: root/tests/check_framework/test_multi_db.py
diff options
context:
space:
mode:
authorIon Scerbatiuc <ion@rover.com>2015-08-01 07:46:25 -0700
committerTim Graham <timograham@gmail.com>2015-08-12 18:00:26 -0400
commit0cc059cd104cdb70340bd08e597d403d80dc42a6 (patch)
treee9015bb84e07c0a4c407c465faf7e672ebe4c05e /tests/check_framework/test_multi_db.py
parentd0bd5330432e1dda519ebd89606bd0980a36dcb4 (diff)
Fixed #25172 -- Fixed check framework to work with multiple databases.
Diffstat (limited to 'tests/check_framework/test_multi_db.py')
-rw-r--r--tests/check_framework/test_multi_db.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/check_framework/test_multi_db.py b/tests/check_framework/test_multi_db.py
new file mode 100644
index 0000000000..168b2ea33f
--- /dev/null
+++ b/tests/check_framework/test_multi_db.py
@@ -0,0 +1,43 @@
+from django.db import connections, models
+from django.test import TestCase, mock
+from django.test.utils import override_settings
+
+from .tests import IsolateModelsMixin
+
+
+class TestRouter(object):
+ """
+ Routes to the 'other' database if the model name starts with 'Other'.
+ """
+ def allow_migrate(self, db, app_label, model=None, **hints):
+ return db == ('other' if model._meta.verbose_name.startswith('other') else 'default')
+
+
+@override_settings(DATABASE_ROUTERS=[TestRouter()])
+class TestMultiDBChecks(IsolateModelsMixin, TestCase):
+ multi_db = True
+
+ 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:
+ model.check()
+ 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:
+ model.check()
+ self.assertTrue(mock_check_field_other.called)
+ self.assertFalse(mock_check_field_default.called)