Ticket #17463: 17463.diff

File 17463.diff, 6.7 KB (added by Aymeric Augustin, 13 years ago)
  • tests/modeltests/timezones/tests.py

     
    2222from django.test.utils import override_settings
    2323from django.utils import timezone
    2424from django.utils.tzinfo import FixedOffset
    25 from django.utils.unittest import skipIf
     25from django.utils.unittest import skipIf, skipUnless
    2626
    2727from .forms import EventForm, EventSplitForm, EventModelForm
    2828from .models import Event, MaybeEvent, Timestamp
     
    4343TZ_SUPPORT = hasattr(time, 'tzset')
    4444
    4545
     46# On OSes that don't provide tzset (Windows), we can't rely on the timezone
     47# set in the environment. The following decorator will be used to skip tests
     48# that don't enforce their own timezone (with timezone.override or equivalent).
     49requires_tz_support = skipUnless(TZ_SUPPORT,
     50        "This test relies on the ability to run a program in an arbitrary "
     51        "time zone, but your operating system isn't able to do that.")
     52
     53
    4654class BaseDateTimeTests(TestCase):
    4755
    4856    @classmethod
     
    238246#@override_settings(USE_TZ=True)
    239247class NewDatabaseTests(BaseDateTimeTests):
    240248
     249    @requires_tz_support
    241250    @skipIf(sys.version_info < (2, 6), "this test requires Python >= 2.6")
    242251    def test_naive_datetime(self):
    243252        dt = datetime.datetime(2011, 9, 1, 13, 20, 30)
     
    251260        # naive datetimes are interpreted in local time
    252261        self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
    253262
     263    @requires_tz_support
    254264    @skipIf(sys.version_info < (2, 6), "this test requires Python >= 2.6")
    255265    @skipUnlessDBFeature('supports_microsecond_precision')
    256266    def test_naive_datetime_with_microsecond(self):
     
    265275        # naive datetimes are interpreted in local time
    266276        self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
    267277
     278    @requires_tz_support
    268279    @skipIf(sys.version_info < (2, 6), "this test requires Python >= 2.6")
    269280    @skipIfDBFeature('supports_microsecond_precision')
    270281    def test_naive_datetime_with_microsecond_unsupported(self):
     
    347358        self.assertEqual(Event.objects.filter(dt__in=(prev, dt, next)).count(), 1)
    348359        self.assertEqual(Event.objects.filter(dt__range=(prev, next)).count(), 1)
    349360
     361    @requires_tz_support
    350362    @skipIf(sys.version_info < (2, 6), "this test requires Python >= 2.6")
    351363    def test_query_filter_with_naive_datetime(self):
    352364        dt = datetime.datetime(2011, 9, 1, 12, 20, 30, tzinfo=EAT)
     
    567579#@override_settings(DATETIME_FORMAT='c', USE_L10N=False, USE_TZ=True)
    568580class TemplateTests(BaseDateTimeTests):
    569581
     582    @requires_tz_support
    570583    def test_localtime_templatetag_and_filters(self):
    571584        """
    572585        Test the {% localtime %} templatetag and related filters.
     
    686699            ctx = Context({'dt': datetime.datetime(2011, 9, 1, 13, 20, 30), 'tz': 'not a tz'})
    687700            self.assertEqual(tpl.render(ctx), "")
    688701
     702    @requires_tz_support
    689703    def test_timezone_templatetag(self):
    690704        """
    691705        Test the {% timezone %} templatetag.
     
    725739        with self.assertRaises(ValueError if pytz is None else pytz.UnknownTimeZoneError):
    726740            Template("{% load tz %}{% timezone tz %}{% endtimezone %}").render(Context({'tz': 'foobar'}))
    727741
     742    @skipIf(sys.platform.startswith('win'), "Windows uses non-standard and local-dependant time zone names")
    728743    def test_get_current_timezone_templatetag(self):
    729744        """
    730745        Test the {% get_current_timezone %} templatetag.
     
    757772        with self.assertRaises(TemplateSyntaxError):
    758773            Template("{% load tz %}{% get_current_timezone %}").render()
    759774
     775    @skipIf(sys.platform.startswith('win'), "Windows uses non-standard and local-dependant time zone names")
    760776    def test_tz_template_context_processor(self):
    761777        """
    762778        Test the django.core.context_processors.tz template context processor.
     
    765781        self.assertEqual(tpl.render(Context()), "")
    766782        self.assertEqual(tpl.render(RequestContext(HttpRequest())), "Africa/Nairobi" if pytz else "EAT")
    767783
     784    @requires_tz_support
    768785    def test_date_and_time_template_filters(self):
    769786        tpl = Template("{{ dt|date:'Y-m-d' }} at {{ dt|time:'H:i:s' }}")
    770787        ctx = Context({'dt': datetime.datetime(2011, 9, 1, 20, 20, 20, tzinfo=UTC)})
     
    790807            self.assertTrue(tpl.render(ctx).startswith("2011"))
    791808        timezone._localtime = None
    792809
     810    @requires_tz_support
    793811    def test_now_template_tag_uses_current_time_zone(self):
    794812        # Regression for #17343
    795813        tpl = Template("{% now \"O\" %}")
     
    838856#@override_settings(DATETIME_FORMAT='c', USE_L10N=False, USE_TZ=True)
    839857class NewFormsTests(BaseDateTimeTests):
    840858
     859    @requires_tz_support
    841860    def test_form(self):
    842861        form = EventForm({'dt': u'2011-09-01 13:20:30'})
    843862        self.assertTrue(form.is_valid())
     
    867886                [u"2011-10-30 02:30:00 couldn't be interpreted in time zone "
    868887                 u"Europe/Paris; it may be ambiguous or it may not exist."])
    869888
     889    @requires_tz_support
    870890    def test_split_form(self):
    871891        form = EventSplitForm({'dt_0': u'2011-09-01', 'dt_1': u'13:20:30'})
    872892        self.assertTrue(form.is_valid())
    873893        self.assertEqual(form.cleaned_data['dt'], datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC))
    874894
     895    @requires_tz_support
    875896    def test_model_form(self):
    876897        EventModelForm({'dt': u'2011-09-01 13:20:30'}).save()
    877898        e = Event.objects.get()
     
    888909    def setUp(self):
    889910        self.client.login(username='super', password='secret')
    890911
     912    @requires_tz_support
    891913    def test_changelist(self):
    892914        e = Event.objects.create(dt=datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC))
    893915        response = self.client.get(reverse('admin:timezones_event_changelist'))
     
    899921            response = self.client.get(reverse('admin:timezones_event_changelist'))
    900922        self.assertContains(response, e.dt.astimezone(ICT).isoformat())
    901923
     924    @requires_tz_support
    902925    def test_change_editable(self):
    903926        e = Event.objects.create(dt=datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC))
    904927        response = self.client.get(reverse('admin:timezones_event_change', args=(e.pk,)))
     
    912935        self.assertContains(response, e.dt.astimezone(ICT).date().isoformat())
    913936        self.assertContains(response, e.dt.astimezone(ICT).time().isoformat())
    914937
     938    @requires_tz_support
    915939    def test_change_readonly(self):
    916940        Timestamp.objects.create()
    917941        # re-fetch the object for backends that lose microseconds (MySQL)
Back to Top