1+ from django .core .exceptions import ValidationError
12from django .db import IntegrityError
23from django .test import TestCase
34
4- from .models import Integers
5+ from .models import UniqueIntegers
56
67
78class SmallIntegerFieldTests (TestCase ):
@@ -13,37 +14,62 @@ def test_unique_max_value(self):
1314 SmallIntegerField.db_type() is "int" which means unique constraints
1415 are only enforced up to 32-bit values.
1516 """
16- Integers .objects .create (small = self .max_value + 1 )
17- Integers .objects .create (small = self .max_value + 1 ) # no IntegrityError
18- Integers .objects .create (small = self .max_value )
17+ UniqueIntegers .objects .create (small = self .max_value + 1 )
18+ UniqueIntegers .objects .create (small = self .max_value + 1 ) # no IntegrityError
19+ UniqueIntegers .objects .create (small = self .max_value )
1920 with self .assertRaises (IntegrityError ):
20- Integers .objects .create (small = self .max_value )
21+ UniqueIntegers .objects .create (small = self .max_value )
2122
2223 def test_unique_min_value (self ):
2324 """
2425 SmallIntegerField.db_type() is "int" which means unique constraints
2526 are only enforced down to negative 32-bit values.
2627 """
27- Integers .objects .create (small = self .min_value - 1 )
28- Integers .objects .create (small = self .min_value - 1 ) # no IntegrityError
29- Integers .objects .create (small = self .min_value )
28+ UniqueIntegers .objects .create (small = self .min_value - 1 )
29+ UniqueIntegers .objects .create (small = self .min_value - 1 ) # no IntegrityError
30+ UniqueIntegers .objects .create (small = self .min_value )
3031 with self .assertRaises (IntegrityError ):
31- Integers .objects .create (small = self .min_value )
32+ UniqueIntegers .objects .create (small = self .min_value )
33+
34+ def test_validate_max_value (self ):
35+ UniqueIntegers (small = self .max_value ).full_clean () # no error
36+ msg = "{'small': ['Ensure this value is less than or equal to 2147483647.']"
37+ with self .assertRaisesMessage (ValidationError , msg ):
38+ UniqueIntegers (small = self .max_value + 1 ).full_clean ()
39+
40+ def test_validate_min_value (self ):
41+ UniqueIntegers (small = self .min_value ).full_clean () # no error
42+ msg = "{'small': ['Ensure this value is greater than or equal to -2147483648.']"
43+ with self .assertRaisesMessage (ValidationError , msg ):
44+ UniqueIntegers (small = self .min_value - 1 ).full_clean ()
3245
3346
3447class PositiveSmallIntegerFieldTests (TestCase ):
3548 max_value = 2 ** 31 - 1
49+ min_value = 0
3650
3751 def test_unique_max_value (self ):
3852 """
3953 SmallIntegerField.db_type() is "int" which means unique constraints
4054 are only enforced up to 32-bit values.
4155 """
42- Integers .objects .create (positive_small = self .max_value + 1 )
43- Integers .objects .create (positive_small = self .max_value + 1 ) # no IntegrityError
44- Integers .objects .create (positive_small = self .max_value )
56+ UniqueIntegers .objects .create (positive_small = self .max_value + 1 )
57+ UniqueIntegers .objects .create (positive_small = self .max_value + 1 ) # no IntegrityError
58+ UniqueIntegers .objects .create (positive_small = self .max_value )
4559 with self .assertRaises (IntegrityError ):
46- Integers .objects .create (positive_small = self .max_value )
60+ UniqueIntegers .objects .create (positive_small = self .max_value )
4761
4862 # test_unique_min_value isn't needed since PositiveSmallIntegerField has a
4963 # limit of zero (enforced only in forms and model validation).
64+
65+ def test_validate_max_value (self ):
66+ UniqueIntegers (positive_small = self .max_value ).full_clean () # no error
67+ msg = "{'positive_small': ['Ensure this value is less than or equal to 2147483647.']"
68+ with self .assertRaisesMessage (ValidationError , msg ):
69+ UniqueIntegers (positive_small = self .max_value + 1 ).full_clean ()
70+
71+ def test_validate_min_value (self ):
72+ UniqueIntegers (positive_small = self .min_value ).full_clean () # no error
73+ msg = "{'positive_small': ['Ensure this value is greater than or equal to 0.']"
74+ with self .assertRaisesMessage (ValidationError , msg ):
75+ UniqueIntegers (positive_small = self .min_value - 1 ).full_clean ()
0 commit comments