summaryrefslogtreecommitdiff
path: root/tests/invalid_models_tests
diff options
context:
space:
mode:
authorSergey Fedoseev <fedoseev.sergey@gmail.com>2016-06-07 13:55:27 +0500
committerTim Graham <timograham@gmail.com>2016-06-09 10:19:09 -0400
commit21130ce1a9c4fcbfce4c614be9e5408b43092bf0 (patch)
tree9e4a89131523daf9a3409aff1cc485f36da631d2 /tests/invalid_models_tests
parentf6681393d3f53a67b4e0645e8d02f95579d8ae2d (diff)
Fixed #26718 -- Added system check for existence of the fields specified by ForeignKey.to_field.
Diffstat (limited to 'tests/invalid_models_tests')
-rw-r--r--tests/invalid_models_tests/test_relative_fields.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/invalid_models_tests/test_relative_fields.py b/tests/invalid_models_tests/test_relative_fields.py
index 8b95cf3e18..2534b345c2 100644
--- a/tests/invalid_models_tests/test_relative_fields.py
+++ b/tests/invalid_models_tests/test_relative_fields.py
@@ -763,6 +763,56 @@ class RelativeFieldTests(SimpleTestCase):
errors = Child.check()
self.assertFalse(errors)
+ def test_to_fields_exist(self):
+ class Parent(models.Model):
+ pass
+
+ class Child(models.Model):
+ a = models.PositiveIntegerField()
+ b = models.PositiveIntegerField()
+ parent = ForeignObject(
+ Parent,
+ on_delete=models.SET_NULL,
+ from_fields=('a', 'b'),
+ to_fields=('a', 'b'),
+ )
+
+ field = Child._meta.get_field('parent')
+ expected = [
+ Error(
+ "The to_field 'a' doesn't exist on the related model 'invalid_models_tests.Parent'.",
+ obj=field,
+ id='fields.E312',
+ ),
+ Error(
+ "The to_field 'b' doesn't exist on the related model 'invalid_models_tests.Parent'.",
+ obj=field,
+ id='fields.E312',
+ ),
+ ]
+ self.assertEqual(field.check(), expected)
+
+ def test_to_fields_not_checked_if_related_model_doesnt_exist(self):
+ class Child(models.Model):
+ a = models.PositiveIntegerField()
+ b = models.PositiveIntegerField()
+ parent = ForeignObject(
+ 'invalid_models_tests.Parent',
+ on_delete=models.SET_NULL,
+ from_fields=('a', 'b'),
+ to_fields=('a', 'b'),
+ )
+
+ field = Child._meta.get_field('parent')
+ self.assertEqual(field.check(), [
+ Error(
+ "Field defines a relation with model 'invalid_models_tests.Parent', "
+ "which is either not installed, or is abstract.",
+ id='fields.E300',
+ obj=field,
+ ),
+ ])
+
@isolate_apps('invalid_models_tests')
class AccessorClashTests(SimpleTestCase):