summaryrefslogtreecommitdiff
path: root/tests/invalid_models_tests
diff options
context:
space:
mode:
authorXavier Francisco <xavier.n.francisco@gmail.com>2020-02-13 11:46:15 +0000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-02-17 13:53:45 +0100
commit86908785076b2bbc31b908781da6b6ad1779b18b (patch)
tree765d274ec3d1a4b20a9ceace7c1c853d2982f47c /tests/invalid_models_tests
parent98f23a8af0be7e87535426c5c83058e2682bfdf8 (diff)
Fixed #31277 -- Relaxed system check of m2m intermediary tables for db_table collision when database routers are installed.
Turned the error into a warning when database routers are installed.
Diffstat (limited to 'tests/invalid_models_tests')
-rw-r--r--tests/invalid_models_tests/test_models.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/tests/invalid_models_tests/test_models.py b/tests/invalid_models_tests/test_models.py
index 0df41c2952..438c327003 100644
--- a/tests/invalid_models_tests/test_models.py
+++ b/tests/invalid_models_tests/test_models.py
@@ -10,6 +10,10 @@ from django.test import SimpleTestCase, TestCase
from django.test.utils import isolate_apps, override_settings, register_lookup
+class EmptyRouter:
+ pass
+
+
def get_max_column_name_length():
allowed_len = None
db_alias = None
@@ -1044,6 +1048,32 @@ class OtherModelTests(SimpleTestCase):
)
])
+ @override_settings(DATABASE_ROUTERS=['invalid_models_tests.test_models.EmptyRouter'])
+ def test_m2m_table_name_clash_database_routers_installed(self):
+ class Foo(models.Model):
+ bar = models.ManyToManyField('Bar', db_table='myapp_bar')
+
+ class Meta:
+ db_table = 'myapp_foo'
+
+ class Bar(models.Model):
+ class Meta:
+ db_table = 'myapp_bar'
+
+ self.assertEqual(Foo.check(), [
+ Warning(
+ "The field's intermediary table 'myapp_bar' clashes with the "
+ "table name of 'invalid_models_tests.Bar'.",
+ obj=Foo._meta.get_field('bar'),
+ hint=(
+ "You have configured settings.DATABASE_ROUTERS. Verify "
+ "that the table of 'invalid_models_tests.Bar' is "
+ "correctly routed to a separate database."
+ ),
+ id='fields.W344',
+ ),
+ ])
+
def test_m2m_field_table_name_clash(self):
class Foo(models.Model):
pass
@@ -1069,6 +1099,32 @@ class OtherModelTests(SimpleTestCase):
)
])
+ @override_settings(DATABASE_ROUTERS=['invalid_models_tests.test_models.EmptyRouter'])
+ def test_m2m_field_table_name_clash_database_routers_installed(self):
+ class Foo(models.Model):
+ pass
+
+ class Bar(models.Model):
+ foos = models.ManyToManyField(Foo, db_table='clash')
+
+ class Baz(models.Model):
+ foos = models.ManyToManyField(Foo, db_table='clash')
+
+ self.assertEqual(Bar.check() + Baz.check(), [
+ Warning(
+ "The field's intermediary table 'clash' clashes with the "
+ "table name of 'invalid_models_tests.%s.foos'."
+ % clashing_model,
+ obj=model_cls._meta.get_field('foos'),
+ hint=(
+ "You have configured settings.DATABASE_ROUTERS. Verify "
+ "that the table of 'invalid_models_tests.%s.foos' is "
+ "correctly routed to a separate database." % clashing_model
+ ),
+ id='fields.W344',
+ ) for model_cls, clashing_model in [(Bar, 'Baz'), (Baz, 'Bar')]
+ ])
+
def test_m2m_autogenerated_table_name_clash(self):
class Foo(models.Model):
class Meta:
@@ -1090,6 +1146,33 @@ class OtherModelTests(SimpleTestCase):
)
])
+ @override_settings(DATABASE_ROUTERS=['invalid_models_tests.test_models.EmptyRouter'])
+ def test_m2m_autogenerated_table_name_clash_database_routers_installed(self):
+ class Foo(models.Model):
+ class Meta:
+ db_table = 'bar_foos'
+
+ class Bar(models.Model):
+ # The autogenerated db_table is bar_foos.
+ foos = models.ManyToManyField(Foo)
+
+ class Meta:
+ db_table = 'bar'
+
+ self.assertEqual(Bar.check(), [
+ Warning(
+ "The field's intermediary table 'bar_foos' clashes with the "
+ "table name of 'invalid_models_tests.Foo'.",
+ obj=Bar._meta.get_field('foos'),
+ hint=(
+ "You have configured settings.DATABASE_ROUTERS. Verify "
+ "that the table of 'invalid_models_tests.Foo' is "
+ "correctly routed to a separate database."
+ ),
+ id='fields.W344',
+ ),
+ ])
+
def test_m2m_unmanaged_shadow_models_not_checked(self):
class A1(models.Model):
pass