From 5b97b99a014443b255cf8ab0467864c5874027da Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Tue, 1 Oct 2013 09:27:31 +0200 Subject: [PATCH] [1.6.x] Factorized requires_tz_support decorator in test utils Thanks Aymeric Augustin for the suggestion. Refs #21165. Backport of c1c44b2506 from master. --- django/test/utils.py | 15 ++++++++++++++- tests/generic_views/test_dates.py | 15 +-------------- tests/timezones/tests.py | 16 ++-------------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/django/test/utils.py b/django/test/utils.py index 591c588933..535a70b4f7 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -2,6 +2,7 @@ from contextlib import contextmanager import logging import re import sys +import time import warnings from functools import wraps from xml.dom.minidom import parseString, Node @@ -17,14 +18,16 @@ from django.test.signals import template_rendered, setting_changed from django.utils.encoding import force_str from django.utils import six from django.utils.translation import deactivate +from django.utils.unittest import skipUnless __all__ = ( 'Approximate', 'ContextList', 'get_runner', 'override_settings', - 'setup_test_environment', 'teardown_test_environment', + 'requires_tz_support', 'setup_test_environment', 'teardown_test_environment', ) RESTORE_LOADERS_ATTR = '_original_template_source_loaders' +TZ_SUPPORT = hasattr(time, 'tzset') class Approximate(object): @@ -436,3 +439,13 @@ def patch_logger(logger_name, log_level): yield calls finally: setattr(logger, log_level, orig) + + +# On OSes that don't provide tzset (Windows), we can't set the timezone +# in which the program runs. As a consequence, we must skip tests that +# don't enforce a specific timezone (with timezone.override or equivalent), +# or attempt to interpret naive datetimes in the default timezone. + +requires_tz_support = skipUnless(TZ_SUPPORT, + "This test relies on the ability to run a program in an arbitrary " + "time zone, but your operating system isn't able to do that.") diff --git a/tests/generic_views/test_dates.py b/tests/generic_views/test_dates.py index 263326322e..0841d1a55e 100644 --- a/tests/generic_views/test_dates.py +++ b/tests/generic_views/test_dates.py @@ -1,27 +1,14 @@ from __future__ import absolute_import -import time import datetime from django.core.exceptions import ImproperlyConfigured from django.test import TestCase, skipUnlessDBFeature -from django.test.utils import override_settings +from django.test.utils import override_settings, requires_tz_support from django.utils import timezone -from django.utils.unittest import skipUnless from .models import Book, BookSigning -TZ_SUPPORT = hasattr(time, 'tzset') - -# On OSes that don't provide tzset (Windows), we can't set the timezone -# in which the program runs. As a consequence, we must skip tests that -# don't enforce a specific timezone (with timezone.override or equivalent), -# or attempt to interpret naive datetimes in the default timezone. - -requires_tz_support = skipUnless(TZ_SUPPORT, - "This test relies on the ability to run a program in an arbitrary " - "time zone, but your operating system isn't able to do that.") - def _make_books(n, base_date): for i in range(n): diff --git a/tests/timezones/tests.py b/tests/timezones/tests.py index 0233ea5029..43f41ea7fb 100644 --- a/tests/timezones/tests.py +++ b/tests/timezones/tests.py @@ -4,7 +4,6 @@ import datetime import os import re import sys -import time import warnings from xml.dom.minidom import parseString @@ -21,11 +20,11 @@ from django.db.models import Min, Max from django.http import HttpRequest from django.template import Context, RequestContext, Template, TemplateSyntaxError from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature -from django.test.utils import override_settings +from django.test.utils import override_settings, requires_tz_support from django.utils import six from django.utils import timezone from django.utils.tzinfo import FixedOffset -from django.utils.unittest import skipIf, skipUnless +from django.utils.unittest import skipIf from .forms import EventForm, EventSplitForm, EventLocalizedForm, EventModelForm, EventLocalizedModelForm from .models import Event, MaybeEvent, Session, SessionEvent, Timestamp, AllDayEvent @@ -43,17 +42,6 @@ UTC = timezone.utc EAT = FixedOffset(180) # Africa/Nairobi ICT = FixedOffset(420) # Asia/Bangkok -TZ_SUPPORT = hasattr(time, 'tzset') - -# On OSes that don't provide tzset (Windows), we can't set the timezone -# in which the program runs. As a consequence, we must skip tests that -# don't enforce a specific timezone (with timezone.override or equivalent), -# or attempt to interpret naive datetimes in the default timezone. - -requires_tz_support = skipUnless(TZ_SUPPORT, - "This test relies on the ability to run a program in an arbitrary " - "time zone, but your operating system isn't able to do that.") - @override_settings(TIME_ZONE='Africa/Nairobi', USE_TZ=False) class LegacyDatabaseTests(TestCase):