diff --git a/README.rst b/README.rst index ff280e9..7a70100 100644 --- a/README.rst +++ b/README.rst @@ -134,6 +134,10 @@ Apache License 2.0 History ======= +1.3.1 (2016-02-03) +------------------- +* Add support to custom database name instead of only 'test' + 1.3.0 (2016-02-03) ------------------- * Add testing.postgresql.PostgresqlFactory diff --git a/setup.py b/setup.py index 9e95298..08ec2f0 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ setup( name='testing.postgresql', - version='1.3.0', + version='1.3.1', description='automatically setups a postgresql instance in a temporary ' 'directory, and destroys it after testing', long_description=open('README.rst').read(), diff --git a/src/testing/postgresql.py b/src/testing/postgresql.py index 592f707..6d9be17 100644 --- a/src/testing/postgresql.py +++ b/src/testing/postgresql.py @@ -42,6 +42,7 @@ class Postgresql(Database): postgres_args='-h 127.0.0.1 -F -c logging_collector=off', pid=None, port=None, + database='test', copy_data_from=None) subdirectories = ['data', 'tmp'] @@ -60,7 +61,7 @@ def dsn(self, **kwargs): params.setdefault('port', self.settings['port']) params.setdefault('host', '127.0.0.1') params.setdefault('user', 'postgres') - params.setdefault('database', 'test') + params.setdefault('database', self.settings['database']) return params @@ -91,6 +92,7 @@ def initialize_database(self): def get_server_commandline(self): return ([self.postgres, '-p', str(self.settings['port']), + '-d', str(self.settings['database']), '-D', os.path.join(self.base_dir, 'data'), '-k', os.path.join(self.base_dir, 'tmp')] + self.settings['postgres_args'].split()) @@ -99,9 +101,9 @@ def poststart(self): with closing(pg8000.connect(**self.dsn(database='postgres'))) as conn: conn.autocommit = True with closing(conn.cursor()) as cursor: - cursor.execute("SELECT COUNT(*) FROM pg_database WHERE datname='test'") + cursor.execute("SELECT COUNT(*) FROM pg_database WHERE datname='%s'" % self.settings['database']) if cursor.fetchone()[0] <= 0: - cursor.execute('CREATE DATABASE test') + cursor.execute('CREATE DATABASE %s' % self.settings['database']) def is_server_available(self): try: diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 1fa1116..da43404 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -77,6 +77,12 @@ def test_dsn_and_url(self): pgsql.dsn()) self.assertEqual("postgresql://postgres@127.0.0.1:12345/test", pgsql.url()) + def test_dsn_and_url_with_custom_database_name(self): + pgsql = testing.postgresql.Postgresql(port=12345, auto_start=0, database='foo') + self.assertEqual({'database': 'foo', 'host': '127.0.0.1', 'port': 12345, 'user': 'postgres'}, + pgsql.dsn()) + self.assertEqual("postgresql://postgres@127.0.0.1:12345/foo", pgsql.url()) + def test_with_statement(self): with testing.postgresql.Postgresql() as pgsql: self.assertIsNotNone(pgsql)