Files
deb-python-sqlalchemy-utils/tests/functions/test_database.py
Jacob Magnusson 815f07d6c1 Use pytest fixtures to reduce complexity and repetition
Also:

Allow override of database name and user in tests (important for me as I would have to mess with my PSQL and MySQL database users otherwise)
Use dict.items instead of six.iteritems as it sporadically caused RuntimeError: dictionary changed size during iteration in Python 2.6 tests.
Fix typo DNS to DSN
Adds Python 3.5 to tox.ini
Added an .editorconfig
Import babel.dates in sqlalchemy_utils.i18n as an exception would be raised when using the latest versions of babel.
2016-01-19 10:52:30 +01:00

96 lines
2.3 KiB
Python

import pytest
import sqlalchemy as sa
from flexmock import flexmock
from sqlalchemy_utils import create_database, database_exists, drop_database
pymysql = None
try:
import pymysql # noqa
except ImportError:
pass
class DatabaseTest(object):
def test_create_and_drop(self, dsn):
assert not database_exists(dsn)
create_database(dsn)
assert database_exists(dsn)
drop_database(dsn)
assert not database_exists(dsn)
@pytest.mark.usefixtures('sqlite_memory_dsn')
class TestDatabaseSQLiteMemory(object):
def test_exists_memory(self, dsn):
assert database_exists(dsn)
@pytest.mark.usefixtures('sqlite_file_dsn')
class TestDatabaseSQLiteFile(DatabaseTest):
pass
@pytest.mark.skipif('pymysql is None')
@pytest.mark.usefixtures('mysql_dsn')
class TestDatabaseMySQL(DatabaseTest):
@pytest.fixture
def db_name(self):
return 'db_test_sqlalchemy_util'
@pytest.mark.skipif('pymysql is None')
@pytest.mark.usefixtures('mysql_dsn')
class TestDatabaseMySQLWithQuotedName(DatabaseTest):
@pytest.fixture
def db_name(self):
return 'db_test_sqlalchemy-util'
@pytest.mark.usefixtures('postgresql_dsn')
class TestDatabasePostgres(DatabaseTest):
@pytest.fixture
def db_name(self):
return 'db_test_sqlalchemy_util'
def test_template(self):
(
flexmock(sa.engine.Engine)
.should_receive('execute')
.with_args(
"CREATE DATABASE db_test_sqlalchemy_util ENCODING 'utf8' "
"TEMPLATE my_template"
)
)
create_database(
'postgres://postgres@localhost/db_test_sqlalchemy_util',
template='my_template'
)
@pytest.mark.usefixtures('postgresql_dsn')
class TestDatabasePostgresWithQuotedName(DatabaseTest):
@pytest.fixture
def db_name(self):
return 'db_test_sqlalchemy-util'
def test_template(self):
(
flexmock(sa.engine.Engine)
.should_receive('execute')
.with_args(
'''CREATE DATABASE "db_test_sqlalchemy-util"'''
" ENCODING 'utf8' "
'TEMPLATE "my-template"'
)
)
create_database(
'postgres://postgres@localhost/db_test_sqlalchemy-util',
template='my-template'
)