|
| 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 |
| 10 | + values are 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_integerfield(self): |
| 15 | + Integers.objects.create(normal=123) |
| 16 | + with self.assertRaises(IntegrityError): |
| 17 | + Integers.objects.create(normal=123) |
| 18 | + |
| 19 | + def test_bigintegerfield(self): |
| 20 | + Integers.objects.create(big=123) |
| 21 | + with self.assertRaises(IntegrityError): |
| 22 | + Integers.objects.create(big=123) |
| 23 | + |
| 24 | + def test_positiveintegerfield(self): |
| 25 | + Integers.objects.create(positive=123) |
| 26 | + with self.assertRaises(IntegrityError): |
| 27 | + Integers.objects.create(positive=123) |
| 28 | + |
| 29 | + def test_positivebigintegerfield(self): |
| 30 | + Integers.objects.create(positive_big=123) |
| 31 | + with self.assertRaises(IntegrityError): |
| 32 | + Integers.objects.create(positive_big=123) |
| 33 | + |
| 34 | + |
| 35 | +class LargeUniqueTests(TestCase): |
| 36 | + """ |
| 37 | + Duplicate values > 32 bits are prohibited. This confirms each field uses |
| 38 | + the long db_type() rather than the 32 bit int type. |
| 39 | + """ |
| 40 | + |
| 41 | + def test_integerfield(self): |
| 42 | + Integers.objects.create(normal=2**31) |
| 43 | + with self.assertRaises(IntegrityError): |
| 44 | + Integers.objects.create(normal=2**31) |
| 45 | + |
| 46 | + def test_bigintegerfield(self): |
| 47 | + Integers.objects.create(big=2**31) |
| 48 | + with self.assertRaises(IntegrityError): |
| 49 | + Integers.objects.create(big=2**31) |
| 50 | + |
| 51 | + def test_positiveintegerfield(self): |
| 52 | + Integers.objects.create(positive=2**31) |
| 53 | + with self.assertRaises(IntegrityError): |
| 54 | + Integers.objects.create(positive=2**31) |
| 55 | + |
| 56 | + def test_positivebigintegerfield(self): |
| 57 | + Integers.objects.create(positive_big=2**31) |
| 58 | + with self.assertRaises(IntegrityError): |
| 59 | + Integers.objects.create(positive_big=2**31) |
0 commit comments