2
2
3
3
from django import forms
4
4
from django .core .exceptions import ValidationError
5
- from django .db import models , IntegrityError
5
+ from django .db import models
6
6
from django .test import TestCase
7
7
8
8
from timezone_field .fields import TimeZoneField
14
14
15
15
16
16
class TestModel (models .Model ):
17
- tz_not_blank = TimeZoneField ()
18
- tz_blank = TimeZoneField (blank = True , null = True )
17
+ tz = TimeZoneField ()
18
+ tz_null = TimeZoneField (null = True )
19
+ tz_blank = TimeZoneField (blank = True )
20
+ tz_blank_null = TimeZoneField (blank = True , null = True )
19
21
20
22
21
23
class TestModelForm (forms .ModelForm ):
@@ -26,95 +28,142 @@ class Meta:
26
28
class TimeZoneFieldModelFormTestCase (TestCase ):
27
29
28
30
def test_valid1 (self ):
29
- form = TestModelForm ({'tz_not_blank ' : PST })
31
+ form = TestModelForm ({'tz ' : PST , 'tz_null' : EST })
30
32
self .assertTrue (form .is_valid ())
31
33
form .save ()
32
34
self .assertEqual (TestModel .objects .count (), 1 )
33
35
34
36
def test_valid2 (self ):
35
37
form = TestModelForm ({
36
- 'tz_not_blank' : PST ,
38
+ 'tz' : PST ,
39
+ 'tz_null' : PST ,
37
40
'tz_blank' : EST ,
41
+ 'tz_blank_null' : EST ,
38
42
})
39
43
self .assertTrue (form .is_valid ())
40
44
form .save ()
41
45
self .assertEqual (TestModel .objects .count (), 1 )
42
46
43
47
def test_invalid_not_blank (self ):
44
- form = TestModelForm ()
45
- self . assertFalse ( form . is_valid () )
46
-
47
- def test_invalid_not_blank2 ( self ):
48
- form = TestModelForm ({ 'tz_blank' : EST } )
49
- self .assertFalse (form .is_valid ())
48
+ form1 = TestModelForm ({ 'tz' : EST } )
49
+ form2 = TestModelForm ({ 'tz_null' : EST } )
50
+ form3 = TestModelForm ()
51
+ self . assertFalse ( form1 . is_valid ())
52
+ self . assertFalse ( form2 . is_valid () )
53
+ self .assertFalse (form3 .is_valid ())
50
54
51
55
def test_invalid_invalid_str (self ):
52
- form = TestModelForm ({'tz_not_blank' : INVALID_TZ })
53
- self .assertFalse (form .is_valid ())
56
+ form1 = TestModelForm ({'tz' : INVALID_TZ })
57
+ form2 = TestModelForm ({'tz_blank_null' : INVALID_TZ })
58
+ self .assertFalse (form1 .is_valid ())
59
+ self .assertFalse (form2 .is_valid ())
60
+
61
+ def test_invalid_type (self ):
62
+ form1 = TestModelForm ({'tz' : 4 })
63
+ form2 = TestModelForm ({'tz_blank_null' : object ()})
64
+ self .assertFalse (form1 .is_valid ())
65
+ self .assertFalse (form2 .is_valid ())
54
66
55
67
56
68
class TimeZoneFieldDBTestCase (TestCase ):
57
69
58
70
def test_valid_strings (self ):
59
71
m = TestModel .objects .create (
60
- tz_not_blank = PST ,
72
+ tz = PST ,
73
+ tz_null = PST ,
61
74
tz_blank = EST ,
75
+ tz_blank_null = EST ,
62
76
)
63
77
m = TestModel .objects .get (pk = m .pk )
64
- self .assertEqual (m .tz_not_blank , pytz .timezone (PST ))
78
+ self .assertEqual (m .tz , pytz .timezone (PST ))
79
+ self .assertEqual (m .tz_null , pytz .timezone (PST ))
65
80
self .assertEqual (m .tz_blank , pytz .timezone (EST ))
81
+ self .assertEqual (m .tz_blank_null , pytz .timezone (EST ))
66
82
67
83
def test_valid_tzinfos (self ):
68
84
m = TestModel .objects .create (
69
- tz_not_blank = pytz .timezone (PST ),
70
- tz_blank = pytz .timezone (EST ),
85
+ tz = pytz .timezone (PST ),
86
+ tz_null = pytz .timezone (EST ),
87
+ tz_blank = pytz .timezone (PST ),
88
+ tz_blank_null = pytz .timezone (EST ),
71
89
)
72
90
m = TestModel .objects .get (pk = m .pk )
73
- self .assertEqual (m .tz_not_blank , pytz .timezone (PST ))
74
- self .assertEqual (m .tz_blank , pytz .timezone (EST ))
91
+ self .assertEqual (m .tz , pytz .timezone (PST ))
92
+ self .assertEqual (m .tz_null , pytz .timezone (EST ))
93
+ self .assertEqual (m .tz_blank , pytz .timezone (PST ))
94
+ self .assertEqual (m .tz_blank_null , pytz .timezone (EST ))
75
95
76
96
def test_valid_blank_str (self ):
77
97
m = TestModel .objects .create (
78
- tz_not_blank = PST ,
98
+ tz = PST ,
99
+ tz_null = EST ,
79
100
tz_blank = '' ,
101
+ tz_blank_null = '' ,
80
102
)
81
103
m = TestModel .objects .get (pk = m .pk )
82
- self .assertEqual (m .tz_not_blank , pytz .timezone (PST ))
104
+ self .assertEqual (m .tz , pytz .timezone (PST ))
105
+ self .assertEqual (m .tz_null , pytz .timezone (EST ))
83
106
self .assertIsNone (m .tz_blank )
107
+ self .assertIsNone (m .tz_blank_null )
84
108
85
109
def test_valid_blank_none (self ):
86
110
m = TestModel .objects .create (
87
- tz_not_blank = PST ,
111
+ tz = PST ,
88
112
tz_blank = None ,
113
+ tz_blank_null = None ,
89
114
)
90
115
m = TestModel .objects .get (pk = m .pk )
91
- self .assertEqual (m .tz_not_blank , pytz .timezone (PST ))
116
+ self .assertEqual (m .tz , pytz .timezone (PST ))
92
117
self .assertIsNone (m .tz_blank )
118
+ self .assertIsNone (m .tz_blank_null )
93
119
94
120
def test_string_value_lookup (self ):
95
- TestModel .objects .create (tz_not_blank = EST )
96
- qs = TestModel .objects .filter (tz_not_blank = EST )
121
+ TestModel .objects .create (tz = EST )
122
+ qs = TestModel .objects .filter (tz = EST )
97
123
self .assertEqual (qs .count (), 1 )
98
124
99
125
def test_tz_value_lookup (self ):
100
- TestModel .objects .create (tz_not_blank = EST )
101
- qs = TestModel .objects .filter (tz_not_blank = pytz .timezone (EST ))
126
+ TestModel .objects .create (tz = EST )
127
+ qs = TestModel .objects .filter (tz = pytz .timezone (EST ))
102
128
self .assertEqual (qs .count (), 1 )
103
129
104
130
def test_invalid_blank_str (self ):
105
- m = TestModel (tz_not_blank = '' )
106
- with self .assertRaises (ValidationError ):
107
- m .full_clean ()
108
- with self .assertRaises (IntegrityError ):
109
- m .save ()
131
+ m1 = TestModel (tz = '' )
132
+ m2 = TestModel (tz_null = '' )
133
+ self .assertRaises (ValidationError , m1 .full_clean )
134
+ self .assertRaises (ValidationError , m2 .full_clean )
110
135
111
136
def test_invalid_blank_none (self ):
112
- m = TestModel (tz_not_blank = None )
137
+ m1 = TestModel (tz = None )
138
+ m2 = TestModel (tz_null = None )
139
+ self .assertRaises (ValidationError , m1 .full_clean )
140
+ self .assertRaises (ValidationError , m2 .full_clean )
141
+
142
+ def test_invalid_type (self ):
113
143
with self .assertRaises (ValidationError ):
114
- m . full_clean ( )
115
- with self .assertRaises (IntegrityError ):
116
- m . save ( )
144
+ TestModel ( tz = 4 )
145
+ with self .assertRaises (ValidationError ):
146
+ TestModel ( tz_null = object () )
117
147
118
148
def test_invalid_string (self ):
119
149
with self .assertRaises (ValidationError ):
120
- TestModel (tz_not_blank = INVALID_TZ )
150
+ TestModel (tz = INVALID_TZ )
151
+ with self .assertRaises (ValidationError ):
152
+ TestModel (tz_null = INVALID_TZ )
153
+ with self .assertRaises (ValidationError ):
154
+ TestModel (tz_blank = INVALID_TZ )
155
+ with self .assertRaises (ValidationError ):
156
+ TestModel (tz_blank_null = INVALID_TZ )
157
+
158
+ def test_invalid_max_length (self ):
159
+ class TestModelML (models .Model ):
160
+ tz = TimeZoneField (max_length = 4 )
161
+ m1 = TestModelML (tz = PST )
162
+ self .assertRaises (ValidationError , m1 .full_clean )
163
+
164
+ def test_invalid_choice (self ):
165
+ class TestModelChoice (models .Model ):
166
+ CHOICES = [(pytz .timezone (tz ), tz ) for tz in pytz .common_timezones ]
167
+ tz = TimeZoneField (choices = CHOICES )
168
+ m1 = TestModelChoice (tz = 'Europe/Nicosia' )
169
+ self .assertRaises (ValidationError , m1 .full_clean )
0 commit comments