From 79908fff2b1f4f581a12252fe2180bccb82b1fc2 Mon Sep 17 00:00:00 2001 From: Alexander Karev Date: Mon, 24 Feb 2014 23:59:26 +0400 Subject: [PATCH] tests for _get_test_db_name --- pytest_django/db_reuse.py | 22 +++++++++--------- tests/test_db_name.py | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 tests/test_db_name.py diff --git a/pytest_django/db_reuse.py b/pytest_django/db_reuse.py index f10646412..002d932a7 100644 --- a/pytest_django/db_reuse.py +++ b/pytest_django/db_reuse.py @@ -47,6 +47,16 @@ def _monkeypatch(obj, method_name, new_method): setattr(obj, method_name, wrapped_method) +def _get_db_name(db_settings, suffix): + if db_settings['TEST_NAME']: + name = db_settings['TEST_NAME'] + else: + name = 'test_' + db_settings['NAME'] + if suffix: + name = '%s_%s' % (name, suffix) + return name + + def monkey_patch_creation_for_db_suffix(suffix=None): from django.db import connections @@ -58,16 +68,8 @@ def _get_test_db_name(self): _create_test_db() and when no external munging is done with the 'NAME' or 'TEST_NAME' settings. """ - - if self.connection.settings_dict['TEST_NAME']: - original = self.connection.settings_dict['TEST_NAME'] - else: - original = 'test_' + self.connection.settings_dict['NAME'] - - if suffix: - return '%s_%s' % (original, suffix) - - return original + db_name = _get_db_name(self.connection.settings_dict, suffix) + return db_name for connection in connections.all(): diff --git a/tests/test_db_name.py b/tests/test_db_name.py new file mode 100644 index 000000000..f8069597f --- /dev/null +++ b/tests/test_db_name.py @@ -0,0 +1,47 @@ +# coding: utf-8 + +from pytest_django.db_reuse import _get_db_name + + +def test_testname(): + db_settings = { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'pytest_django', + 'TEST_NAME': 'test123', + 'HOST': 'localhost', + 'USER': '', + } + assert _get_db_name(db_settings, None) == 'test123' + + +def test_name(): + db_settings = { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'pytest_django', + 'TEST_NAME': '', + 'HOST': 'localhost', + 'USER': '', + } + assert _get_db_name(db_settings, None) == 'test_pytest_django' + + +def test_testname_with_suffix(): + db_settings = { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'pytest_django', + 'TEST_NAME': 'test123', + 'HOST': 'localhost', + 'USER': '', + } + assert _get_db_name(db_settings, 'abc') == 'test123_abc' + + +def test_name_with_suffix(): + db_settings = { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'pytest_django', + 'TEST_NAME': '', + 'HOST': 'localhost', + 'USER': '', + } + assert _get_db_name(db_settings, 'abc') == 'test_pytest_django_abc'