summaryrefslogtreecommitdiff
path: root/tests/foreign_object/tests.py
diff options
context:
space:
mode:
authorAntoine Catton <acatton@fusionbox.com>2015-10-09 10:55:19 -0600
committerSimon Charette <charette.s@gmail.com>2015-10-12 18:00:59 -0400
commit80dac8c33e7f6f22577e4346f44e4c5ee89b648c (patch)
tree113d156bf62b84bad2dd758b033a8af864740a32 /tests/foreign_object/tests.py
parent533c10998ad45854208d7e4b89d3c077faf16b8a (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/foreign_object/tests.py')
-rw-r--r--tests/foreign_object/tests.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/foreign_object/tests.py b/tests/foreign_object/tests.py
index 06e2551e4f..559c988edd 100644
--- a/tests/foreign_object/tests.py
+++ b/tests/foreign_object/tests.py
@@ -2,6 +2,8 @@ import datetime
from operator import attrgetter
from django.core.exceptions import FieldError
+from django.db import models
+from django.db.models.fields.related import ForeignObject
from django.test import TestCase, skipUnlessDBFeature
from django.utils import translation
@@ -391,3 +393,61 @@ class MultiColumnFKTests(TestCase):
""" See: https://code.djangoproject.com/ticket/21566 """
objs = [Person(name="abcd_%s" % i, person_country=self.usa) for i in range(0, 5)]
Person.objects.bulk_create(objs, 10)
+
+ def test_check_composite_foreign_object(self):
+ class Parent(models.Model):
+ a = models.PositiveIntegerField()
+ b = models.PositiveIntegerField()
+
+ class Meta:
+ unique_together = (
+ ('a', 'b'),
+ )
+
+ 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)
+
+ self.assertEqual(errors, [])
+
+ def test_check_subset_composite_foreign_object(self):
+ class Parent(models.Model):
+ a = models.PositiveIntegerField()
+ b = models.PositiveIntegerField()
+ c = models.PositiveIntegerField()
+
+ class Meta:
+ unique_together = (
+ ('a', 'b'),
+ )
+
+ class Child(models.Model):
+ a = models.PositiveIntegerField()
+ b = models.PositiveIntegerField()
+ c = models.PositiveIntegerField()
+ d = models.CharField(max_length=255)
+
+ parent = ForeignObject(
+ Parent,
+ on_delete=models.SET_NULL,
+ from_fields=('a', 'b', 'c'),
+ to_fields=('a', 'b', 'c'),
+ related_name='children',
+ )
+
+ field = Child._meta.get_field('parent')
+ errors = field.check(from_model=Child)
+
+ self.assertEqual(errors, [])