Adjusted tests that were messing with database connections too heavily.

The previous implementation would result in tests hitting the wrong
database when running tests in parallel on multiple databases.
This commit is contained in:
Aymeric Augustin 2015-06-05 13:49:32 +02:00
parent 442baabd0b
commit e39dd61808
1 changed files with 26 additions and 11 deletions

View File

@ -4,6 +4,7 @@ import datetime
import re import re
import sys import sys
import warnings import warnings
from contextlib import contextmanager
from unittest import SkipTest, skipIf from unittest import SkipTest, skipIf
from xml.dom.minidom import parseString from xml.dom.minidom import parseString
@ -611,27 +612,41 @@ class ForcedTimeZoneDatabaseTests(TransactionTestCase):
raise SkipTest("Database doesn't support feature(s): test_db_allows_multiple_connections") raise SkipTest("Database doesn't support feature(s): test_db_allows_multiple_connections")
super(ForcedTimeZoneDatabaseTests, cls).setUpClass() super(ForcedTimeZoneDatabaseTests, cls).setUpClass()
connections.databases['tz'] = connections.databases['default'].copy()
connections.databases['tz']['TIME_ZONE'] = 'Asia/Bangkok'
@classmethod @contextmanager
def tearDownClass(cls): def override_database_connection_timezone(self, timezone):
connections['tz'].close() try:
del connections['tz'] orig_timezone = connection.settings_dict['TIME_ZONE']
del connections.databases['tz'] connection.settings_dict['TIME_ZONE'] = timezone
super(ForcedTimeZoneDatabaseTests, cls).tearDownClass() # Clear cached properties, after first accessing them to ensure they exist.
connection.timezone
del connection.timezone
connection.timezone_name
del connection.timezone_name
yield
finally:
connection.settings_dict['TIME_ZONE'] = orig_timezone
# Clear cached properties, after first accessing them to ensure they exist.
connection.timezone
del connection.timezone
connection.timezone_name
del connection.timezone_name
def test_read_datetime(self): def test_read_datetime(self):
fake_dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=UTC) fake_dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=UTC)
Event.objects.create(dt=fake_dt) Event.objects.create(dt=fake_dt)
event = Event.objects.using('tz').get() with self.override_database_connection_timezone('Asia/Bangkok'):
dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC) event = Event.objects.get()
dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
self.assertEqual(event.dt, dt) self.assertEqual(event.dt, dt)
def test_write_datetime(self): def test_write_datetime(self):
dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC) dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
Event.objects.using('tz').create(dt=dt) with self.override_database_connection_timezone('Asia/Bangkok'):
Event.objects.create(dt=dt)
event = Event.objects.get() event = Event.objects.get()
fake_dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=UTC) fake_dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=UTC)