-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.py
72 lines (57 loc) · 2.43 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# From Kami: https://raw.github.com/Kami/parallel-django-and-twisted-test-runner/master/base.py
from django.conf import settings
from django.core import management
from django.core.management import call_command
from django.db import connection
import logging
log = logging.getLogger(__name__)
from django.test.testcases import disable_transaction_methods
from django.test.testcases import restore_transaction_methods
from django.test.utils import setup_test_environment, teardown_test_environment
from django.db import transaction, DEFAULT_DB_ALIAS
real_commit = transaction.commit
real_rollback = transaction.rollback
real_enter_transaction_management = transaction.enter_transaction_management
real_leave_transaction_management = transaction.leave_transaction_management
real_savepoint_commit = transaction.savepoint_commit
real_savepoint_rollback = transaction.savepoint_rollback
real_managed = transaction.managed
database = DEFAULT_DB_ALIAS
verbosity = 0
interactive = False
old_db_name = settings.DATABASES['default']['NAME']
def create_test_db(test_database_name):
connection.settings_dict['TEST_NAME'] = test_database_name
management.get_commands()
management._commands['syncdb'] = 'django.core'
connection.creation.create_test_db(verbosity, autoclobber=not interactive)
connection.settings_dict['NAME'] = test_database_name
def destroy_test_db(database_name):
connection.creation.destroy_test_db(database_name, verbosity)
def load_db_fixtures(fixtures, commit, database):
call_command('loaddata', *fixtures, **{
'verbosity':verbosity,
'commit': commit,
'database': database
})
def setup_test_db(worker_index, fixtures, fn, *args):
management.get_commands()
management._commands['syncdb'] = 'django.core'
old_name = settings.DATABASES['default']['NAME']
if worker_index is not None:
test_database_name = 'test_%d_%s' % (worker_index, old_name)
else:
test_database_name = 'test_%s' % (old_name)
create_test_db(test_database_name)
if fixtures:
load_db_fixtures(fixtures)
result = None
try:
result = fn(*args)
except Exception, e:
log.err(str(e))
raise
finally:
destroy_test_db(test_database_name)
connection.settings_dict['NAME'] = old_name
return result