-
Notifications
You must be signed in to change notification settings - Fork 47
Adding custom database name for testing.postgres #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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', | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be clearer to call this parameter |
||
| 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']) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
|
||
There was a problem hiding this comment.
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.