-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathconftest.py
67 lines (44 loc) · 1.58 KB
/
conftest.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
import pytest
import py
import shutil
import copy
pytest_plugins = 'pytester'
TESTS_DIR = py.path.local(__file__)
@pytest.fixture()
def django_settings():
from django.conf import settings
return settings
from .db_helpers import create_empty_production_database, get_db_engine, DB_NAME
@pytest.fixture(scope='function')
def django_testdir(testdir, monkeypatch, django_settings):
if get_db_engine() in ('mysql', 'postgresql_psycopg2'):
# Django requires the production database to exists..
create_empty_production_database()
db_settings = copy.deepcopy(django_settings.DATABASES)
db_settings['default']['NAME'] = DB_NAME
test_settings = '''
# Pypy compatibility
try:
from psycopg2ct import compat
except ImportError:
pass
else:
compat.register()
DATABASES = %(db_settings)s
INSTALLED_APPS = [
'tpkg.app',
]
SECRET_KEY = 'foobar'
''' % {'db_settings': repr(db_settings)}
tpkg_path = testdir.mkpydir('tpkg')
app_source = TESTS_DIR.dirpath('app')
# Copy the test app to make it available in the new test run
shutil.copytree(py.builtin._totext(app_source), py.builtin._totext(tpkg_path.join('app')))
tpkg_path.join("db_test_settings.py").write(test_settings)
monkeypatch.setenv('DJANGO_SETTINGS_MODULE', 'tpkg.db_test_settings')
return testdir
def create_test_module(testdir, test_code, filename='test_the_test.py'):
tpkg_path = testdir.tmpdir / 'tpkg'
tpkg_path.join(filename).write(test_code)
def create_conftest(testdir, conftest_code):
return create_test_module(testdir, conftest_code, 'conftest.py')