Skip to content

Use Django's --keepdb with Django 1.8+ #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pytest_django/db_reuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def create_test_db_with_reuse(self, verbosity=1, autoclobber=False,
This method is a monkey patched version of create_test_db that
will not actually create a new database, but just reuse the
existing.

This is only used with Django < 1.8.
"""
test_database_name = self._get_test_db_name()
self.connection.settings_dict['NAME'] = test_database_name
Expand Down
11 changes: 7 additions & 4 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ def _django_db_setup(request,
if request.config.getvalue('nomigrations'):
_disable_native_migrations()

db_args = {}
with _django_cursor_wrapper:
# Monkey patch Django's setup code to support database re-use
if request.config.getvalue('reuse_db'):
if not request.config.getvalue('create_db'):
if (request.config.getvalue('reuse_db') and
not request.config.getvalue('create_db')):
if get_django_version() >= (1, 8):
db_args['keepdb'] = True
else:
monkey_patch_creation_for_db_reuse()

# Create the database
db_cfg = setup_databases(verbosity=pytest.config.option.verbose,
interactive=False)
interactive=False, **db_args)

def teardown_database():
with _django_cursor_wrapper:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ def test_more_verbose_with_vv(self, testdir):
"*PASSED*Destroying test database for alias 'default' ('*')...*"])

def test_more_verbose_with_vv_and_reusedb(self, testdir):
"""More verbose output with '-v -v', and --reuse-db."""
result = testdir.runpytest_subprocess('-s', '-v', '-v', '--reuse-db')
"""More verbose output with '-v -v', and --create-db."""
result = testdir.runpytest_subprocess('-s', '-v', '-v', '--create-db')
result.stdout.fnmatch_lines([
"tpkg/test_the_test.py:*Creating test database for alias*",
"*PASSED*"])
Expand Down