-
Notifications
You must be signed in to change notification settings - Fork 98
/
tests.py
484 lines (390 loc) · 16.4 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
from __future__ import absolute_import
import pytz
from django import forms
from django.core.exceptions import ValidationError
from django.db import models
from django.db.migrations.writer import MigrationWriter
from django.test import TestCase
from rest_framework import serializers
from timezone_field import (
TimeZoneField, TimeZoneFormField, TimeZoneSerializerField,
)
from timezone_field.utils import add_gmt_offset_to_choices
from tests.models import TestModel
PST = 'America/Los_Angeles' # pytz.tzinfo.DstTzInfo
GMT = 'GMT' # pytz.tzinfo.StaticTzInfo
UTC = 'UTC' # pytz.UTC singleton
PST_tz = pytz.timezone(PST)
GMT_tz = pytz.timezone(GMT)
UTC_tz = pytz.timezone(UTC)
INVALID_TZ = 'ogga booga'
UNCOMMON_TZ = 'Singapore'
USA_TZS = [
'US/Alaska',
'US/Arizona',
'US/Central',
'US/Eastern',
'US/Hawaii',
'US/Mountain',
'US/Pacific',
]
class TestForm(forms.Form):
tz = TimeZoneFormField()
tz_opt = TimeZoneFormField(required=False)
class TestModelForm(forms.ModelForm):
class Meta:
model = TestModel
fields = '__all__'
class TimeZoneFormFieldTestCase(TestCase):
def test_valid1(self):
form = TestForm({'tz': PST})
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data['tz'], PST_tz)
self.assertEqual(form.cleaned_data['tz_opt'], None)
def test_valid2(self):
form = TestForm({'tz': GMT, 'tz_opt': UTC})
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data['tz'], GMT_tz)
self.assertEqual(form.cleaned_data['tz_opt'], UTC_tz)
def test_invalid_invalid_str(self):
form = TestForm({'tz': INVALID_TZ})
self.assertFalse(form.is_valid())
def test_invalid_choice(self):
form = TestForm({'tz_invalid_choice': INVALID_TZ})
self.assertFalse(form.is_valid())
def test_invalid_uncommon_tz(self):
form = TestForm({'tz': UNCOMMON_TZ})
self.assertFalse(form.is_valid())
def test_default_human_readable_choices_dont_have_underscores(self):
form = TestForm()
pst_choice = [c for c in form.fields['tz'].choices if c[0] == PST]
self.assertEqual(pst_choice[0][1], 'America/Los Angeles')
class TestFormInvalidChoice(forms.Form):
tz = TimeZoneFormField(
choices=(
[(tz, tz) for tz in pytz.all_timezones]
+ [(INVALID_TZ, pytz.UTC)]
)
)
class TimeZoneFormFieldInvalidChoideTestCase(TestCase):
def test_valid(self):
form = TestFormInvalidChoice({'tz': PST})
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data['tz'], PST_tz)
def test_invalid_choide(self):
form = TestFormInvalidChoice({'tz': INVALID_TZ})
self.assertFalse(form.is_valid())
class TimeZoneFieldModelFormTestCase(TestCase):
def test_valid_with_defaults(self):
# seems there should be a better way to get a form's default values...?
# http://stackoverflow.com/questions/7399490/
data = dict(
(field_name, field.initial)
for field_name, field in TestModelForm().fields.items()
)
data.update({'tz': GMT})
form = TestModelForm(data=data)
self.assertTrue(form.is_valid())
form.save()
self.assertEqual(TestModel.objects.count(), 1)
m = TestModel.objects.get()
self.assertEqual(m.tz, GMT_tz)
self.assertEqual(m.tz_opt, None)
self.assertEqual(m.tz_opt_default, PST_tz)
self.assertEqual(m.tz_gmt_offset, None)
def test_valid_specify_all(self):
form = TestModelForm({
'tz': UTC,
'tz_opt': PST,
'tz_opt_default': GMT,
'tz_gmt_offset': UTC,
})
self.assertTrue(form.is_valid())
form.save()
self.assertEqual(TestModel.objects.count(), 1)
m = TestModel.objects.get()
self.assertEqual(m.tz, UTC_tz)
self.assertEqual(m.tz_opt, PST_tz)
self.assertEqual(m.tz_opt_default, GMT_tz)
self.assertEqual(m.tz_gmt_offset, UTC_tz)
def test_invalid_not_blank(self):
form = TestModelForm({})
self.assertFalse(form.is_valid())
self.assertTrue(any('required' in e for e in form.errors['tz']))
def test_invalid_choice(self):
form = TestModelForm({'tz': INVALID_TZ})
self.assertFalse(form.is_valid())
self.assertTrue(any('choice' in e for e in form.errors['tz']))
def test_invalid_uncommmon_tz(self):
form = TestModelForm({'tz': UNCOMMON_TZ})
self.assertFalse(form.is_valid())
self.assertTrue(any('choice' in e for e in form.errors['tz']))
def test_default_human_readable_choices_dont_have_underscores(self):
form = TestModelForm()
pst_choice = [c for c in form.fields['tz'].choices if c[0] == PST_tz]
self.assertEqual(pst_choice[0][1], 'America/Los Angeles')
def test_display_GMT_offsets(self):
form = TestModelForm({'tz_gmt_offset': PST_tz})
c = [c for c in form.fields['tz_gmt_offset'].choices if c[0] == PST_tz]
self.assertEqual(c[0][1], 'GMT-08:00 America/Los Angeles')
class TimeZoneFieldTestCase(TestCase):
def test_valid_dst_tz_objects(self):
m = TestModel.objects.create(tz=PST_tz, tz_opt=PST_tz,
tz_opt_default=PST_tz)
m.full_clean()
m = TestModel.objects.get(pk=m.pk)
self.assertEqual(m.tz, PST_tz)
self.assertEqual(m.tz_opt, PST_tz)
self.assertEqual(m.tz_opt_default, PST_tz)
def test_valid_dst_tz_strings(self):
m = TestModel.objects.create(tz=PST, tz_opt=PST, tz_opt_default=PST)
m.full_clean()
m = TestModel.objects.get(pk=m.pk)
self.assertEqual(m.tz, PST_tz)
self.assertEqual(m.tz_opt, PST_tz)
self.assertEqual(m.tz_opt_default, PST_tz)
def test_valid_static_tz_objects(self):
m = TestModel.objects.create(tz=GMT_tz, tz_opt=GMT_tz,
tz_opt_default=GMT_tz)
m.full_clean()
m = TestModel.objects.get(pk=m.pk)
self.assertEqual(m.tz, GMT_tz)
self.assertEqual(m.tz_opt, GMT_tz)
self.assertEqual(m.tz_opt_default, GMT_tz)
def test_valid_static_tz_strings(self):
m = TestModel.objects.create(tz=GMT, tz_opt=GMT, tz_opt_default=GMT)
m.full_clean()
m = TestModel.objects.get(pk=m.pk)
self.assertEqual(m.tz, GMT_tz)
self.assertEqual(m.tz_opt, GMT_tz)
self.assertEqual(m.tz_opt_default, GMT_tz)
def test_valid_UTC_object(self):
m = TestModel.objects.create(tz=UTC_tz, tz_opt=UTC_tz,
tz_opt_default=UTC_tz)
m.full_clean()
m = TestModel.objects.get(pk=m.pk)
self.assertEqual(m.tz, UTC_tz)
self.assertEqual(m.tz_opt, UTC_tz)
self.assertEqual(m.tz_opt_default, UTC_tz)
def test_valid_UTC_string(self):
m = TestModel.objects.create(tz=UTC, tz_opt=UTC, tz_opt_default=UTC)
m.full_clean()
m = TestModel.objects.get(pk=m.pk)
self.assertEqual(m.tz, UTC_tz)
self.assertEqual(m.tz_opt, UTC_tz)
self.assertEqual(m.tz_opt_default, UTC_tz)
def test_valid_default_values(self):
m = TestModel.objects.create(tz=UTC_tz)
m.full_clean()
m = TestModel.objects.get(pk=m.pk)
self.assertEqual(m.tz_opt, None)
self.assertEqual(m.tz_opt_default, PST_tz)
def test_valid_default_values_without_saving_to_db(self):
m = TestModel(tz=UTC_tz)
m.full_clean()
self.assertEqual(m.tz_opt, None)
self.assertEqual(m.tz_opt_default, PST_tz)
def test_valid_blank_str(self):
m = TestModel.objects.create(tz=PST, tz_opt='')
m.full_clean()
m = TestModel.objects.get(pk=m.pk)
self.assertEqual(m.tz_opt, None)
def test_valid_blank_none(self):
m = TestModel.objects.create(tz=PST, tz_opt=None)
m.full_clean()
m = TestModel.objects.get(pk=m.pk)
self.assertEqual(m.tz_opt, None)
def test_string_value_lookup(self):
TestModel.objects.create(tz=PST)
qs = TestModel.objects.filter(tz=PST)
self.assertEqual(qs.count(), 1)
def test_tz_value_lookup(self):
TestModel.objects.create(tz=PST)
qs = TestModel.objects.filter(tz=PST_tz)
self.assertEqual(qs.count(), 1)
def test_invalid_blank(self):
m = TestModel()
self.assertRaises(ValidationError, m.full_clean)
def test_invalid_blank_str(self):
m = TestModel(tz='')
self.assertRaises(ValidationError, m.full_clean)
def test_invalid_blank_none(self):
m = TestModel(tz=None)
self.assertRaises(ValidationError, m.full_clean)
def test_invalid_choice(self):
m = TestModel(tz=INVALID_TZ)
self.assertRaises(ValidationError, m.full_clean)
m = TestModel(tz=4)
self.assertRaises(ValidationError, m.full_clean)
m = TestModel(tz=object())
self.assertRaises(ValidationError, m.full_clean)
def test_some_positional_args_ok(self):
TimeZoneField('a verbose name', 'a name', True)
def test_too_many_positional_args_not_ok(self):
def createField():
TimeZoneField('a verbose name', 'a name', True, 42)
self.assertRaises(ValueError, createField)
def test_default_human_readable_choices_dont_have_underscores(self):
m = TestModel(tz=PST_tz)
self.assertEqual(m.get_tz_display(), 'America/Los Angeles')
class TimeZoneFieldLimitedChoicesTestCase(TestCase):
invalid_superset_tz = 'not a tz'
invalid_subset_tz = 'Europe/Brussels'
class TestModelChoice(models.Model):
tz_superset = TimeZoneField(
choices=[(tz, tz) for tz in pytz.all_timezones],
blank=True,
)
tz_subset = TimeZoneField(
choices=[(tz, tz) for tz in USA_TZS],
blank=True,
)
class TestModelOldChoiceFormat(models.Model):
tz_superset = TimeZoneField(
choices=[(pytz.timezone(tz), tz) for tz in pytz.all_timezones],
blank=True,
)
tz_subset = TimeZoneField(
choices=[(pytz.timezone(tz), tz) for tz in USA_TZS],
blank=True,
)
def test_valid_choice(self):
self.TestModelChoice.objects.create(tz_superset=PST, tz_subset=PST)
m = self.TestModelChoice.objects.get()
self.assertEqual(m.tz_superset, PST_tz)
self.assertEqual(m.tz_subset, PST_tz)
def test_invalid_choice(self):
m = self.TestModelChoice(tz_superset=self.invalid_superset_tz)
self.assertRaises(ValidationError, m.full_clean)
m = self.TestModelChoice(tz_subset=self.invalid_subset_tz)
self.assertRaises(ValidationError, m.full_clean)
def test_valid_choice_old_format(self):
self.TestModelOldChoiceFormat.objects.create(
tz_superset=PST, tz_subset=PST,
)
m = self.TestModelOldChoiceFormat.objects.get()
self.assertEqual(m.tz_superset, PST_tz)
self.assertEqual(m.tz_subset, PST_tz)
def test_invalid_choice_old_format(self):
m = self.TestModelOldChoiceFormat(tz_superset=self.invalid_superset_tz)
self.assertRaises(ValidationError, m.full_clean)
m = self.TestModelOldChoiceFormat(tz_subset=self.invalid_subset_tz)
self.assertRaises(ValidationError, m.full_clean)
class TimeZoneFieldDeconstructTestCase(TestCase):
test_fields = (
TimeZoneField(),
TimeZoneField(default='UTC'),
TimeZoneField(max_length=42),
TimeZoneField(choices=[
(pytz.timezone('US/Pacific'), 'US/Pacific'),
(pytz.timezone('US/Eastern'), 'US/Eastern'),
]),
TimeZoneField(choices=[
(pytz.timezone(b'US/Pacific'), b'US/Pacific'),
(pytz.timezone(b'US/Eastern'), b'US/Eastern'),
]),
TimeZoneField(choices=[
('US/Pacific', 'US/Pacific'),
('US/Eastern', 'US/Eastern'),
]),
TimeZoneField(choices=[
(b'US/Pacific', b'US/Pacific'),
(b'US/Eastern', b'US/Eastern'),
]),
)
def test_deconstruct(self):
for org_field in self.test_fields:
name, path, args, kwargs = org_field.deconstruct()
new_field = TimeZoneField(*args, **kwargs)
self.assertEqual(org_field.max_length, new_field.max_length)
self.assertEqual(org_field.choices, new_field.choices)
def test_full_serialization(self):
# ensure the values passed to kwarg arguments can be serialized
# the recommended 'deconstruct' testing by django docs doesn't cut it
# https://docs.djangoproject.com/en/1.7/howto/custom-model-fields/#field-deconstruction
# replicates https://github.com/mfogel/django-timezone-field/issues/12
for field in self.test_fields:
# ensuring the following call doesn't throw an error
MigrationWriter.serialize(field)
def test_from_db_value(self):
"""
Verify that the field can handle data coming back as bytes from the
db.
"""
field = TimeZoneField()
# django 1.11 signuature
value = field.from_db_value(b'UTC', None, None, None)
self.assertEqual(pytz.UTC, value)
# django 2.0+ signuature
value = field.from_db_value(b'UTC', None, None)
self.assertEqual(pytz.UTC, value)
def test_default_kwargs_not_frozen(self):
"""
Ensure the deconstructed representation of the field does not contain
kwargs if they match the default.
Don't want to bloat everyone's migration files.
"""
field = TimeZoneField()
name, path, args, kwargs = field.deconstruct()
self.assertNotIn('choices', kwargs)
self.assertNotIn('max_length', kwargs)
def test_specifying_defaults_not_frozen(self):
"""
If someone's matched the default values with their kwarg args, we
shouldn't bothering freezing those.
"""
field = TimeZoneField(max_length=63)
name, path, args, kwargs = field.deconstruct()
self.assertNotIn('max_length', kwargs)
choices = [
(pytz.timezone(tz), tz.replace('_', ' '))
for tz in pytz.common_timezones
]
field = TimeZoneField(choices=choices)
name, path, args, kwargs = field.deconstruct()
self.assertNotIn('choices', kwargs)
choices = [(tz, tz.replace('_', ' ')) for tz in pytz.common_timezones]
field = TimeZoneField(choices=choices)
name, path, args, kwargs = field.deconstruct()
self.assertNotIn('choices', kwargs)
class GmtOffsetInChoicesTestCase(TestCase):
# test timezones out of order, but they should appear in order in result.
timezones = [
(pytz.timezone('US/Eastern'), 'US/Eastern'),
(pytz.timezone('US/Pacific'), 'US/Pacific'),
(pytz.timezone('Asia/Qatar'), 'Asia/Qatar'),
(pytz.timezone('Pacific/Fiji'), 'Pacific/Fiji'),
(pytz.timezone('Europe/London'), 'Europe/London'),
(pytz.timezone('Pacific/Apia'), 'Pacific/Apia'),
]
def test_add_gmt_offset_to_choices(self):
result = add_gmt_offset_to_choices(self.timezones)
expected = [
"GMT-11:00 Pacific/Apia",
"GMT-08:00 US/Pacific",
"GMT-05:00 US/Eastern",
"GMT+00:00 Europe/London",
"GMT+03:00 Asia/Qatar",
"GMT+13:00 Pacific/Fiji",
]
for i in range(len(expected)):
self.assertEqual(expected[i], result[i][1])
class TimeZoneSerializer(serializers.Serializer):
tz = TimeZoneSerializerField()
class TimeZoneSerializerFieldTestCase(TestCase):
def test_invalid_str(self):
serializer = TimeZoneSerializer(data={'tz': INVALID_TZ})
self.assertFalse(serializer.is_valid())
def test_valid(self):
serializer = TimeZoneSerializer(data={'tz': PST})
self.assertTrue(serializer.is_valid())
self.assertEqual(serializer.validated_data['tz'], PST_tz)
def test_valid_representation(self):
serializer = TimeZoneSerializer(data={'tz': PST})
self.assertTrue(serializer.is_valid())
self.assertEqual(serializer.data['tz'], PST)
def test_valid_with_timezone_object(self):
serializer = TimeZoneSerializer(data={'tz': PST_tz})
self.assertTrue(serializer.is_valid())
self.assertEqual(serializer.data['tz'], PST)
self.assertEqual(serializer.validated_data['tz'], PST_tz)