From 0086366609ba0042cdb4333a44d16102479c3032 Mon Sep 17 00:00:00 2001 From: maho Date: Sun, 23 Feb 2014 18:43:39 +0100 Subject: [PATCH 1/4] - let postgresql db use "CREATE DATABASE test_XXX WITH TEMPLATE=XXX" --- pytest_django/db_reuse.py | 18 ++++++++++++++++++ pytest_django/fixtures.py | 4 +++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pytest_django/db_reuse.py b/pytest_django/db_reuse.py index da1017003..20a62e1aa 100644 --- a/pytest_django/db_reuse.py +++ b/pytest_django/db_reuse.py @@ -103,3 +103,21 @@ def monkey_patch_creation_for_db_reuse(): for connection in connections.all(): 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(): + _monkeypatch(connection.creation,'sql_table_creation_suffix',_sql_table_creation_suffix) + \ No newline at end of file diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index e434d9a46..48355c868 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() From 932b64803a00366c3e55512d2cc84a803c2859d3 Mon Sep 17 00:00:00 2001 From: maho Date: Sun, 23 Feb 2014 18:45:02 +0100 Subject: [PATCH 2/4] - monkeypatch only connections with TEST_WITH_TEMPLATE setting set --- pytest_django/db_reuse.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pytest_django/db_reuse.py b/pytest_django/db_reuse.py index 20a62e1aa..ab50a61f2 100644 --- a/pytest_django/db_reuse.py +++ b/pytest_django/db_reuse.py @@ -119,5 +119,6 @@ def _sql_table_creation_suffix(self): from django.db import connections for connection in connections.all(): - _monkeypatch(connection.creation,'sql_table_creation_suffix',_sql_table_creation_suffix) + if "TEST_WITH_TEMPLATE" in connection.setting_dict: + _monkeypatch(connection.creation,'sql_table_creation_suffix',_sql_table_creation_suffix) \ No newline at end of file From f5a0a9b9c5fa0448e151a7ae61ba968ce05319e9 Mon Sep 17 00:00:00 2001 From: maho Date: Sun, 23 Feb 2014 19:02:02 +0100 Subject: [PATCH 3/4] fix typo --- pytest_django/db_reuse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytest_django/db_reuse.py b/pytest_django/db_reuse.py index ab50a61f2..4ebde6b8d 100644 --- a/pytest_django/db_reuse.py +++ b/pytest_django/db_reuse.py @@ -119,6 +119,6 @@ def _sql_table_creation_suffix(self): from django.db import connections for connection in connections.all(): - if "TEST_WITH_TEMPLATE" in connection.setting_dict: + if "TEST_WITH_TEMPLATE" in connection.settings_dict: _monkeypatch(connection.creation,'sql_table_creation_suffix',_sql_table_creation_suffix) \ No newline at end of file From 3acd38e2a2a7817701700d1a0d36716458e46627 Mon Sep 17 00:00:00 2001 From: maho Date: Sun, 23 Feb 2014 23:16:01 +0100 Subject: [PATCH 4/4] - multidb: let one connection be reused, and other not, setting in settings.py --- pytest_django/db_reuse.py | 9 ++++++--- pytest_django/fixtures.py | 15 ++++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pytest_django/db_reuse.py b/pytest_django/db_reuse.py index da1017003..1ffd05726 100644 --- a/pytest_django/db_reuse.py +++ b/pytest_django/db_reuse.py @@ -93,13 +93,16 @@ 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) diff --git a/pytest_django/fixtures.py b/pytest_django/fixtures.py index e434d9a46..6ddd09130 100644 --- a/pytest_django/fixtures.py +++ b/pytest_django/fixtures.py @@ -42,13 +42,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: