1 | import os
|
---|
2 | from unittest import TestCase
|
---|
3 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', __name__)
|
---|
4 |
|
---|
5 | from django import forms
|
---|
6 | from django.db import models
|
---|
7 | from django.utils import timezone
|
---|
8 | from django.test.client import RequestFactory
|
---|
9 | from django.test.utils import override_settings
|
---|
10 |
|
---|
11 |
|
---|
12 | class DummyForm(forms.Form):
|
---|
13 | value = forms.DateTimeField()
|
---|
14 |
|
---|
15 |
|
---|
16 | class DummyModel(models.Model):
|
---|
17 | class Meta:
|
---|
18 | app_label = 'tzbug'
|
---|
19 |
|
---|
20 | value = models.DateTimeField()
|
---|
21 |
|
---|
22 |
|
---|
23 | class DummyModelForm(forms.ModelForm):
|
---|
24 | class Meta:
|
---|
25 | model = DummyModel
|
---|
26 |
|
---|
27 |
|
---|
28 | class TimezonesBugTestCase(TestCase):
|
---|
29 |
|
---|
30 | def setUp(self):
|
---|
31 | self.factory = RequestFactory()
|
---|
32 |
|
---|
33 | @override_settings(USE_TZ=True, TIME_ZONE='Europe/Bucharest')
|
---|
34 | def test_aware_datetime_with_form(self):
|
---|
35 | value = timezone.now()
|
---|
36 | request = self.factory.post('/', data={'value': value.isoformat(' ')})
|
---|
37 | form = DummyForm(request.POST)
|
---|
38 |
|
---|
39 | self.assertTrue(form.is_valid())
|
---|
40 | self.assertEquals(value, form.cleaned_data['value'])
|
---|
41 |
|
---|
42 | @override_settings(USE_TZ=True, TIME_ZONE='Europe/Bucharest')
|
---|
43 | def test_aware_datetime_with_modelform(self):
|
---|
44 | value = timezone.now()
|
---|
45 | request = self.factory.post('/', data={'value': value.isoformat(' ')})
|
---|
46 | form = DummyModelForm(request.POST)
|
---|
47 |
|
---|
48 | self.assertTrue(form.is_valid())
|
---|
49 | self.assertEquals(value, form.cleaned_data.get('value'))
|
---|
50 |
|
---|
51 | @override_settings(USE_TZ=False)
|
---|
52 | def test_works_ok_with_use_tz_off(self):
|
---|
53 | value = timezone.now()
|
---|
54 | request = self.factory.post('/', data={'value': value.isoformat(' ')})
|
---|
55 | form = DummyModelForm(request.POST)
|
---|
56 |
|
---|
57 | self.assertTrue(form.is_valid())
|
---|
58 | self.assertEquals(value, form.cleaned_data.get('value'))
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 | if __name__ == '__main__':
|
---|
63 | import unittest
|
---|
64 | unittest.main()
|
---|