|
| 1 | +from django.db import IntegrityError |
| 2 | +from django.test import TestCase |
| 3 | + |
| 4 | +from .models import UniqueIntegers |
| 5 | + |
| 6 | + |
| 7 | +class SmallIntegerFieldTests(TestCase): |
| 8 | + max_value = 2**31 - 1 |
| 9 | + min_value = -(2**31) |
| 10 | + |
| 11 | + def test_unique_max_value(self): |
| 12 | + """ |
| 13 | + SmallIntegerField.db_type() is "int" which means unique constraints |
| 14 | + are only enforced up to 32-bit values. |
| 15 | + """ |
| 16 | + UniqueIntegers.objects.create(small=self.max_value + 1) |
| 17 | + UniqueIntegers.objects.create(small=self.max_value + 1) # no IntegrityError |
| 18 | + UniqueIntegers.objects.create(small=self.max_value) |
| 19 | + with self.assertRaises(IntegrityError): |
| 20 | + UniqueIntegers.objects.create(small=self.max_value) |
| 21 | + |
| 22 | + def test_unique_min_value(self): |
| 23 | + """ |
| 24 | + SmallIntegerField.db_type() is "int" which means unique constraints |
| 25 | + are only enforced down to negative 32-bit values. |
| 26 | + """ |
| 27 | + UniqueIntegers.objects.create(small=self.min_value - 1) |
| 28 | + UniqueIntegers.objects.create(small=self.min_value - 1) # no IntegrityError |
| 29 | + UniqueIntegers.objects.create(small=self.min_value) |
| 30 | + with self.assertRaises(IntegrityError): |
| 31 | + UniqueIntegers.objects.create(small=self.min_value) |
| 32 | + |
| 33 | + |
| 34 | +class PositiveSmallIntegerFieldTests(TestCase): |
| 35 | + max_value = 2**31 - 1 |
| 36 | + |
| 37 | + def test_unique_max_value(self): |
| 38 | + """ |
| 39 | + SmallIntegerField.db_type() is "int" which means unique constraints |
| 40 | + are only enforced up to 32-bit values. |
| 41 | + """ |
| 42 | + UniqueIntegers.objects.create(positive_small=self.max_value + 1) |
| 43 | + UniqueIntegers.objects.create(positive_small=self.max_value + 1) # no IntegrityError |
| 44 | + UniqueIntegers.objects.create(positive_small=self.max_value) |
| 45 | + with self.assertRaises(IntegrityError): |
| 46 | + UniqueIntegers.objects.create(positive_small=self.max_value) |
| 47 | + |
| 48 | + # test_unique_min_value isn't needed since PositiveSmallIntegerField has a |
| 49 | + # limit of zero (enforced only in forms and model validation). |
0 commit comments