diff --git a/pytest_django/db_reuse.py b/pytest_django/db_reuse.py index da1017003..f933525a6 100644 --- a/pytest_django/db_reuse.py +++ b/pytest_django/db_reuse.py @@ -93,13 +93,35 @@ def create_test_db_with_reuse(self, verbosity=1, autoclobber=False): # See https://code.djangoproject.com/ticket/17760 if hasattr(self.connection.features, 'confirm'): self.connection.features.confirm() + + self.connection.settings_dict['TEST_REUSE']=True #to check if destroy or not return test_database_name -def monkey_patch_creation_for_db_reuse(): +def monkey_patch_creation_for_db_reuse(reuse_db,create_db): from django.db import connections for connection in connections.all(): - if test_database_exists_from_previous_run(connection): - _monkeypatch(connection.creation, 'create_test_db', create_test_db_with_reuse) + if reuse_db and not create_db or connection.settings_dict.get('TEST_REUSE',False): + if test_database_exists_from_previous_run(connection): + _monkeypatch(connection.creation, 'create_test_db', create_test_db_with_reuse) + +def monkey_patch_create_with_template(): + """ + Monkeypatch create sql to do 'CREATE DATABASE test_xxx WITH TEMPLATE xxx' instead of just 'CREATE DATABASE test_xxx' + Useful when testing with PostgreSQL and barebone of existing database + """ + def _sql_table_creation_suffix(self): + try: + return " WITH TEMPLATE=%s"%self.connection.settings_dict['TEST_WITH_TEMPLATE'] + except KeyError: + return "" + + + from django.db import connections + + for connection in connections.all(): + if "TEST_WITH_TEMPLATE" in connection.settings_dict: + _monkeypatch(connection.creation,'sql_table_creation_suffix',_sql_table_creation_suffix) + diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index e434d9a46..07c72826c 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -8,7 +8,8 @@ from . import live_server_helper from .db_reuse import (monkey_patch_creation_for_db_reuse, - monkey_patch_creation_for_db_suffix) + monkey_patch_creation_for_db_suffix, + monkey_patch_create_with_template) from .django_compat import is_django_unittest from .lazy_django import skip_if_no_django @@ -34,6 +35,7 @@ def _django_db_setup(request, _django_runner, _django_cursor_wrapper): db_suffix = None monkey_patch_creation_for_db_suffix(db_suffix) + monkey_patch_create_with_template() # Disable south's syncdb command commands = management.get_commands() @@ -42,13 +44,18 @@ def _django_db_setup(request, _django_runner, _django_cursor_wrapper): 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'): - monkey_patch_creation_for_db_reuse() - _django_runner.teardown_databases = lambda db_cfg: None - + monkey_patch_creation_for_db_reuse(request.config.getvalue('reuse_db'), + request.config.getvalue('create_db')) # Create the database db_cfg = _django_runner.setup_databases() + #rewrite db_cfg - remove those with REUSE + _old_dbs,mirror = db_cfg + _new_dbs = [] + for connection,old_name,destroy in _old_dbs: + if not connection.settings_dict.get('TEST_REUSE'): + _new_dbs.append((connection,old_name,destroy)) + db_cfg = _new_dbs,mirror + def teardown_database(): with _django_cursor_wrapper: