@@ -73,3 +73,62 @@ def test_validate_min_value(self):
73
73
msg = "{'positive_small': ['Ensure this value is greater than or equal to 0.']"
74
74
with self .assertRaisesMessage (ValidationError , msg ):
75
75
UniqueIntegers (positive_small = self .min_value - 1 ).full_clean ()
76
+
77
+
78
+ class SmallUniqueTests (TestCase ):
79
+ """
80
+ Duplicate values < 32 bits are prohibited. This confirms integer field
81
+ values are cast to Int64 so MongoDB stores it as long. Otherwise, the
82
+ partialFilterExpression: {$type: long} unique constraint doesn't work.
83
+ """
84
+
85
+ test_value = 123
86
+
87
+ def test_integerfield (self ):
88
+ UniqueIntegers .objects .create (plain = self .test_value )
89
+ with self .assertRaises (IntegrityError ):
90
+ UniqueIntegers .objects .create (plain = self .test_value )
91
+
92
+ def test_bigintegerfield (self ):
93
+ UniqueIntegers .objects .create (big = self .test_value )
94
+ with self .assertRaises (IntegrityError ):
95
+ UniqueIntegers .objects .create (big = self .test_value )
96
+
97
+ def test_positiveintegerfield (self ):
98
+ UniqueIntegers .objects .create (positive = self .test_value )
99
+ with self .assertRaises (IntegrityError ):
100
+ UniqueIntegers .objects .create (positive = self .test_value )
101
+
102
+ def test_positivebigintegerfield (self ):
103
+ UniqueIntegers .objects .create (positive_big = self .test_value )
104
+ with self .assertRaises (IntegrityError ):
105
+ UniqueIntegers .objects .create (positive_big = self .test_value )
106
+
107
+
108
+ class LargeUniqueTests (TestCase ):
109
+ """
110
+ Duplicate values > 32 bits are prohibited. This confirms each field uses
111
+ the long db_type() rather than the 32 bit int type.
112
+ """
113
+
114
+ test_value = 2 ** 63 - 1
115
+
116
+ def test_integerfield (self ):
117
+ UniqueIntegers .objects .create (plain = self .test_value )
118
+ with self .assertRaises (IntegrityError ):
119
+ UniqueIntegers .objects .create (plain = self .test_value )
120
+
121
+ def test_bigintegerfield (self ):
122
+ UniqueIntegers .objects .create (big = self .test_value )
123
+ with self .assertRaises (IntegrityError ):
124
+ UniqueIntegers .objects .create (big = self .test_value )
125
+
126
+ def test_positiveintegerfield (self ):
127
+ UniqueIntegers .objects .create (positive = self .test_value )
128
+ with self .assertRaises (IntegrityError ):
129
+ UniqueIntegers .objects .create (positive = self .test_value )
130
+
131
+ def test_positivebigintegerfield (self ):
132
+ UniqueIntegers .objects .create (positive_big = self .test_value )
133
+ with self .assertRaises (IntegrityError ):
134
+ UniqueIntegers .objects .create (positive_big = self .test_value )
0 commit comments