1
+ from django .core .exceptions import ValidationError
1
2
from django .db import IntegrityError
2
3
from django .test import TestCase
3
4
4
- from .models import Integers
5
+ from .models import UniqueIntegers
5
6
6
7
7
8
class SmallIntegerFieldTests (TestCase ):
@@ -13,37 +14,62 @@ def test_unique_max_value(self):
13
14
SmallIntegerField.db_type() is "int" which means unique constraints
14
15
are only enforced up to 32-bit values.
15
16
"""
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 )
19
20
with self .assertRaises (IntegrityError ):
20
- Integers .objects .create (small = self .max_value )
21
+ UniqueIntegers .objects .create (small = self .max_value )
21
22
22
23
def test_unique_min_value (self ):
23
24
"""
24
25
SmallIntegerField.db_type() is "int" which means unique constraints
25
26
are only enforced down to negative 32-bit values.
26
27
"""
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 )
30
31
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 ()
32
45
33
46
34
47
class PositiveSmallIntegerFieldTests (TestCase ):
35
48
max_value = 2 ** 31 - 1
49
+ min_value = 0
36
50
37
51
def test_unique_max_value (self ):
38
52
"""
39
53
SmallIntegerField.db_type() is "int" which means unique constraints
40
54
are only enforced up to 32-bit values.
41
55
"""
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 )
45
59
with self .assertRaises (IntegrityError ):
46
- Integers .objects .create (positive_small = self .max_value )
60
+ UniqueIntegers .objects .create (positive_small = self .max_value )
47
61
48
62
# test_unique_min_value isn't needed since PositiveSmallIntegerField has a
49
63
# 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