|
| 1 | +from django.db import IntegrityError |
| 2 | +from django.test import TestCase |
| 3 | + |
| 4 | +from .models import Integers |
| 5 | + |
| 6 | + |
| 7 | +class SmallUniqueTests(TestCase): |
| 8 | + """ |
| 9 | + Duplicate values < 32 bits are prohibited. This confirms integer field values are |
| 10 | + cast to Int64 so MongoDB stores it as long. Otherwise, the |
| 11 | + partialFilterExpression: {$type: long} unique constraint doesn't work. |
| 12 | + """ |
| 13 | + |
| 14 | + def test_smallintegerfield(self): |
| 15 | + Integers.objects.create(small=123) |
| 16 | + with self.assertRaises(IntegrityError): |
| 17 | + Integers.objects.create(small=123) |
| 18 | + |
| 19 | + def test_integerfield(self): |
| 20 | + Integers.objects.create(normal=123) |
| 21 | + with self.assertRaises(IntegrityError): |
| 22 | + Integers.objects.create(normal=123) |
| 23 | + |
| 24 | + def test_bigintegerfield(self): |
| 25 | + Integers.objects.create(big=123) |
| 26 | + with self.assertRaises(IntegrityError): |
| 27 | + Integers.objects.create(big=123) |
| 28 | + |
| 29 | + def test_positivesmallintegerfield(self): |
| 30 | + Integers.objects.create(positive_small=123) |
| 31 | + with self.assertRaises(IntegrityError): |
| 32 | + Integers.objects.create(positive_small=123) |
| 33 | + |
| 34 | + def test_positiveintegerfield(self): |
| 35 | + Integers.objects.create(positive=123) |
| 36 | + with self.assertRaises(IntegrityError): |
| 37 | + Integers.objects.create(positive=123) |
| 38 | + |
| 39 | + def test_positivebigintegerfield(self): |
| 40 | + Integers.objects.create(positive_big=123) |
| 41 | + with self.assertRaises(IntegrityError): |
| 42 | + Integers.objects.create(positive_big=123) |
| 43 | + |
| 44 | + |
| 45 | +class LargeUniqueTests(TestCase): |
| 46 | + """ |
| 47 | + Duplicate values > 32 bits are prohibited. This confirms each field uses the long |
| 48 | + db_type() rather than the 32 bit int type. |
| 49 | + """ |
| 50 | + |
| 51 | + def test_smallintegerfield(self): |
| 52 | + Integers.objects.create(small=2**31) |
| 53 | + with self.assertRaises(IntegrityError): |
| 54 | + Integers.objects.create(small=2**31) |
| 55 | + |
| 56 | + def test_integerfield(self): |
| 57 | + Integers.objects.create(normal=2**31) |
| 58 | + with self.assertRaises(IntegrityError): |
| 59 | + Integers.objects.create(normal=2**31) |
| 60 | + |
| 61 | + def test_bigintegerfield(self): |
| 62 | + Integers.objects.create(big=2**31) |
| 63 | + with self.assertRaises(IntegrityError): |
| 64 | + Integers.objects.create(big=2**31) |
| 65 | + |
| 66 | + def test_positivesmallintegerfield(self): |
| 67 | + Integers.objects.create(positive_small=2**31) |
| 68 | + with self.assertRaises(IntegrityError): |
| 69 | + Integers.objects.create(positive_small=2**31) |
| 70 | + |
| 71 | + def test_positiveintegerfield(self): |
| 72 | + Integers.objects.create(positive=2**31) |
| 73 | + with self.assertRaises(IntegrityError): |
| 74 | + Integers.objects.create(positive=2**31) |
| 75 | + |
| 76 | + def test_positivebigintegerfield(self): |
| 77 | + Integers.objects.create(positive_big=2**31) |
| 78 | + with self.assertRaises(IntegrityError): |
| 79 | + Integers.objects.create(positive_big=2**31) |
0 commit comments