Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

setup(
name='testing.postgresql',
version='1.3.0',
version='1.3.1',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a minor release, not a patch release, but the version bump should be left out of the PR anyway.

description='automatically setups a postgresql instance in a temporary '
'directory, and destroys it after testing',
long_description=open('README.rst').read(),
Expand Down
8 changes: 5 additions & 3 deletions src/testing/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be clearer to call this parameter db_name like #18

copy_data_from=None)
subdirectories = ['data', 'tmp']

Expand All @@ -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

Expand Down Expand Up @@ -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())
Expand All @@ -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'])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The database name should be passed as a parameter to execute, instead of string interpolation.

if cursor.fetchone()[0] <= 0:
cursor.execute('CREATE DATABASE test')
cursor.execute('CREATE DATABASE %s' % self.settings['database'])

def is_server_available(self):
try:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def test_dsn_and_url(self):
pgsql.dsn())
self.assertEqual("postgresql://[email protected]: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://[email protected]:12345/foo", pgsql.url())

def test_with_statement(self):
with testing.postgresql.Postgresql() as pgsql:
self.assertIsNotNone(pgsql)
Expand Down