diff options
| author | Antoine Catton <acatton@fusionbox.com> | 2015-10-09 10:55:19 -0600 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2015-10-12 18:00:59 -0400 |
| commit | 80dac8c33e7f6f22577e4346f44e4c5ee89b648c (patch) | |
| tree | 113d156bf62b84bad2dd758b033a8af864740a32 /tests/invalid_models_tests | |
| parent | 533c10998ad45854208d7e4b89d3c077faf16b8a (diff) | |
Fixed #25535 -- Made ForeignObject checks less strict.
Check that the foreign object `from_fields` are a subset of any unique
constraints on the foreign model.
Diffstat (limited to 'tests/invalid_models_tests')
| -rw-r--r-- | tests/invalid_models_tests/test_relative_fields.py | 83 |
1 files changed, 80 insertions, 3 deletions
diff --git a/tests/invalid_models_tests/test_relative_fields.py b/tests/invalid_models_tests/test_relative_fields.py index ad1c25da52..545aa87548 100644 --- a/tests/invalid_models_tests/test_relative_fields.py +++ b/tests/invalid_models_tests/test_relative_fields.py @@ -5,6 +5,7 @@ import warnings from django.core.checks import Error, Warning as DjangoWarning from django.db import models +from django.db.models.fields.related import ForeignObject from django.test import ignore_warnings from django.test.testcases import skipIfDBFeature from django.test.utils import override_settings @@ -507,9 +508,9 @@ class RelativeFieldTests(IsolatedModelsTestCase): errors = field.check() expected = [ Error( - "None of the fields 'country_id', 'city_id' on model 'Person' " - "have a unique=True constraint.", - hint=None, + "No subset of the fields 'country_id', 'city_id' on model 'Person' is unique", + hint="Add a unique=True on any of the fields 'country_id', 'city_id' of 'Person', " + "or add them all or a subset to a unique_together constraint.", obj=field, id='fields.E310', ) @@ -1395,3 +1396,79 @@ class M2mThroughFieldsTests(IsolatedModelsTestCase): obj=field, id='fields.E337')] self.assertEqual(expected, errors) + + def test_superset_foreign_object(self): + class Parent(models.Model): + a = models.PositiveIntegerField() + b = models.PositiveIntegerField() + c = models.PositiveIntegerField() + + class Meta: + unique_together = ( + ('a', 'b', 'c'), + ) + + class Child(models.Model): + a = models.PositiveIntegerField() + b = models.PositiveIntegerField() + value = models.CharField(max_length=255) + + parent = ForeignObject( + Parent, + on_delete=models.SET_NULL, + from_fields=('a', 'b'), + to_fields=('a', 'b'), + related_name='children', + ) + + field = Child._meta.get_field('parent') + errors = field.check(from_model=Child) + expected = [ + Error( + "No subset of the fields 'a', 'b' on model 'Parent' is unique", + hint="Add a unique=True on any of the fields 'a', 'b' of 'Parent', or add them " + "all or a subset to a unique_together constraint.", + obj=field, + id='fields.E310', + ), + ] + self.assertEqual(expected, errors) + + def test_insersection_foreign_object(self): + class Parent(models.Model): + a = models.PositiveIntegerField() + b = models.PositiveIntegerField() + c = models.PositiveIntegerField() + d = models.PositiveIntegerField() + + class Meta: + unique_together = ( + ('a', 'b', 'c'), + ) + + class Child(models.Model): + a = models.PositiveIntegerField() + b = models.PositiveIntegerField() + d = models.PositiveIntegerField() + value = models.CharField(max_length=255) + + parent = ForeignObject( + Parent, + on_delete=models.SET_NULL, + from_fields=('a', 'b', 'd'), + to_fields=('a', 'b', 'd'), + related_name='children', + ) + + field = Child._meta.get_field('parent') + errors = field.check(from_model=Child) + expected = [ + Error( + "No subset of the fields 'a', 'b', 'd' on model 'Parent' is unique", + hint="Add a unique=True on any of the fields 'a', 'b', 'd' of 'Parent', or add " + "them all or a subset to a unique_together constraint.", + obj=field, + id='fields.E310', + ), + ] + self.assertEqual(expected, errors) |
