summaryrefslogtreecommitdiff
path: root/tests/validation
diff options
context:
space:
mode:
authorBaptiste Mispelon <bmispelon@gmail.com>2013-11-20 10:28:22 +0100
committerBaptiste Mispelon <bmispelon@gmail.com>2013-11-20 17:26:26 +0100
commit331d79a77d48e1a46df0290689d4d865e67221c4 (patch)
tree88ec34b72e3ec4e7722164196c8497dcc26309b5 /tests/validation
parent4cfe6ba6a3d16a16f0561f2f3c5b56d117f8a60d (diff)
downloaddjango-331d79a77d48e1a46df0290689d4d865e67221c4.tar.gz
Fixed #21469 -- Allow set objects in Meta.unique_together.
Thanks to Tim for the review.
Diffstat (limited to 'tests/validation')
-rw-r--r--tests/validation/test_unique.py36
1 files changed, 35 insertions, 1 deletions
diff --git a/tests/validation/test_unique.py b/tests/validation/test_unique.py
index 14ac5a2471..b3d389e4ec 100644
--- a/tests/validation/test_unique.py
+++ b/tests/validation/test_unique.py
@@ -4,6 +4,8 @@ import datetime
import unittest
from django.core.exceptions import ValidationError
+from django.db import models
+from django.db.models.loading import BaseAppCache
from django.test import TestCase
from .models import (CustomPKModel, UniqueTogetherModel, UniqueFieldsModel,
@@ -25,13 +27,45 @@ class GetUniqueCheckTests(unittest.TestCase):
def test_unique_together_gets_picked_up_and_converted_to_tuple(self):
m = UniqueTogetherModel()
self.assertEqual(
- ([(UniqueTogetherModel, ('ifield', 'cfield',)),
+ ([(UniqueTogetherModel, ('ifield', 'cfield')),
(UniqueTogetherModel, ('ifield', 'efield')),
(UniqueTogetherModel, ('id',)), ],
[]),
m._get_unique_checks()
)
+ def test_unique_together_normalization(self):
+ """
+ Test the Meta.unique_together normalization with different sorts of
+ objects.
+ """
+ data = {
+ '2-tuple': (('foo', 'bar'),
+ (('foo', 'bar'),)),
+ 'list': (['foo', 'bar'],
+ (('foo', 'bar'),)),
+ 'already normalized': ((('foo', 'bar'), ('bar', 'baz')),
+ (('foo', 'bar'), ('bar', 'baz'))),
+ 'set': ({('foo', 'bar'), ('bar', 'baz')}, # Ref #21469
+ (('foo', 'bar'), ('bar', 'baz'))),
+ }
+
+ for test_name, (unique_together, normalized) in data.items():
+ class M(models.Model):
+ foo = models.IntegerField()
+ bar = models.IntegerField()
+ baz = models.IntegerField()
+
+ Meta = type(str('Meta'), (), {
+ 'unique_together': unique_together,
+ 'app_cache': BaseAppCache()
+ })
+
+ checks, _ = M()._get_unique_checks()
+ for t in normalized:
+ check = (M, t)
+ self.assertIn(check, checks)
+
def test_primary_key_is_considered_unique(self):
m = CustomPKModel()
self.assertEqual(([(CustomPKModel, ('my_pk_field',))], []), m._get_unique_checks())