mirror of
https://github.com/django/django.git
synced 2025-08-02 18:13:02 +00:00
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:
parent
442baabd0b
commit
e39dd61808
1 changed files with 26 additions and 11 deletions
|
@ -4,6 +4,7 @@ import datetime
|
|||
import re
|
||||
import sys
|
||||
import warnings
|
||||
from contextlib import contextmanager
|
||||
from unittest import SkipTest, skipIf
|
||||
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")
|
||||
|
||||
super(ForcedTimeZoneDatabaseTests, cls).setUpClass()
|
||||
connections.databases['tz'] = connections.databases['default'].copy()
|
||||
connections.databases['tz']['TIME_ZONE'] = 'Asia/Bangkok'
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
connections['tz'].close()
|
||||
del connections['tz']
|
||||
del connections.databases['tz']
|
||||
super(ForcedTimeZoneDatabaseTests, cls).tearDownClass()
|
||||
@contextmanager
|
||||
def override_database_connection_timezone(self, timezone):
|
||||
try:
|
||||
orig_timezone = connection.settings_dict['TIME_ZONE']
|
||||
connection.settings_dict['TIME_ZONE'] = timezone
|
||||
# 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):
|
||||
fake_dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=UTC)
|
||||
Event.objects.create(dt=fake_dt)
|
||||
|
||||
event = Event.objects.using('tz').get()
|
||||
dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
|
||||
with self.override_database_connection_timezone('Asia/Bangkok'):
|
||||
event = Event.objects.get()
|
||||
dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
|
||||
self.assertEqual(event.dt, dt)
|
||||
|
||||
def test_write_datetime(self):
|
||||
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()
|
||||
fake_dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=UTC)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue