From aa900c39a8e3134d09e2285cce030c1b33b1e820 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Tue, 13 Mar 2012 06:59:04 +0000 Subject: [PATCH] Fixed #17882 -- Reopened the database connection when a test changes time zone settings. Thanks brodie for the report. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17699 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/test/signals.py | 9 +++++++++ tests/modeltests/timezones/tests.py | 2 -- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/django/test/signals.py b/django/test/signals.py index e29f4704bb..d6797395cf 100644 --- a/django/test/signals.py +++ b/django/test/signals.py @@ -1,5 +1,14 @@ +from django.conf import settings +from django.db import close_connection from django.dispatch import Signal template_rendered = Signal(providing_args=["template", "context"]) setting_changed = Signal(providing_args=["setting", "value"]) + +# Close the database connection to re-establish it with the proper time zone. +def close_connection_on_time_zone_change(**kwargs): + if (kwargs['setting'] == 'USE_TZ' + or (kwargs['setting'] == 'TIME_ZONE' and not settings.USE_TZ)): + close_connection() +setting_changed.connect(close_connection_on_time_zone_change) diff --git a/tests/modeltests/timezones/tests.py b/tests/modeltests/timezones/tests.py index a8d2c0c332..171c2187f2 100644 --- a/tests/modeltests/timezones/tests.py +++ b/tests/modeltests/timezones/tests.py @@ -63,8 +63,6 @@ class BaseDateTimeTests(TestCase): self._old_tz = os.environ.get('TZ') os.environ['TZ'] = 'Africa/Nairobi' time.tzset() - # Create a new cursor, for test cases that change the value of USE_TZ. - connection.close() @classmethod def tearDownClass(self):