Skip to content

Commit

Permalink
tests for _get_test_db_name
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Karev committed Feb 24, 2014
1 parent 050932c commit 79908ff
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
22 changes: 12 additions & 10 deletions pytest_django/db_reuse.py
Expand Up @@ -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

Expand All @@ -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():

Expand Down
47 changes: 47 additions & 0 deletions 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'

0 comments on commit 79908ff

Please sign in to comment.