Code

Ticket #19371: tzbug.py

File tzbug.py, 1.8 KB (added by delinhabit, 20 months ago)

regression tests (only need Django to be installed and $ python tzbug.py)

Line 
1import os
2from unittest import TestCase
3os.environ.setdefault('DJANGO_SETTINGS_MODULE', __name__)
4
5from django import forms
6from django.db import models
7from django.utils import timezone
8from django.test.client import RequestFactory
9from django.test.utils import override_settings
10
11
12class DummyForm(forms.Form):
13    value = forms.DateTimeField()
14
15
16class DummyModel(models.Model):
17    class Meta:
18        app_label = 'tzbug'
19
20    value = models.DateTimeField()
21
22
23class DummyModelForm(forms.ModelForm):
24    class Meta:
25        model = DummyModel
26
27
28class 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
62if __name__ == '__main__':
63    import unittest
64    unittest.main()