From 121c17183f0a58acfc521f611e5933a7d7bb393a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=9Aliwi=C5=84ski?= Date: Mon, 26 Sep 2016 23:55:44 +0200 Subject: [PATCH] create command line and pytest.ini configuration options for postgresql username --- CHANGES.rst | 1 + src/pytest_postgresql/executor.py | 5 ++++- src/pytest_postgresql/factories.py | 13 +++++++------ src/pytest_postgresql/plugin.py | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index ed8acde8..fb2f53c4 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,7 @@ CHANGELOG unreleased ------- +- create command line and pytest.ini configuration options for postgresql username - make the port random by default - create command line and pytest.ini configuration options for executable - create command line and pytest.ini configuration options for host diff --git a/src/pytest_postgresql/executor.py b/src/pytest_postgresql/executor.py index f171e2e3..b23c926e 100644 --- a/src/pytest_postgresql/executor.py +++ b/src/pytest_postgresql/executor.py @@ -37,7 +37,7 @@ class PostgreSQLExecutor(TCPExecutor): -o "-F -p {port} -c log_destination='stderr' -c %s='{unixsocketdir}'" -l {logfile} {startparams}""" - def __init__(self, pg_ctl, host, port, + def __init__(self, pg_ctl, host, port, user, datadir, unixsocketdir, logfile, startparams, shell=False, timeout=60, sleep=0.1): """ @@ -46,6 +46,8 @@ def __init__(self, pg_ctl, host, port, :param str pg_ctl: pg_ctl location :param str host: host under which process is accessible :param int port: port under which process is accessible + :param str user: postgresql's username used to manage + and access PostgreSQL :param str datadir: path to postgresql datadir :param str unixsocketdir: path to socket directory :param str logfile: path to logfile for postgresql @@ -56,6 +58,7 @@ def __init__(self, pg_ctl, host, port, :param float sleep: how often to check for start/stop condition """ self.pg_ctl = pg_ctl + self.user = user self.version = self.version() self.datadir = path(datadir) self.unixsocketdir = unixsocketdir diff --git a/src/pytest_postgresql/factories.py b/src/pytest_postgresql/factories.py index 097d3eab..c92b99d3 100644 --- a/src/pytest_postgresql/factories.py +++ b/src/pytest_postgresql/factories.py @@ -35,7 +35,7 @@ def get_config(request): """Return a dictionary with config options.""" config = {} - options = ['exec', 'host', 'port'] + options = ['exec', 'host', 'port', 'user'] for option in options: option_name = 'postgresql_' + option conf = request.config.getoption(option_name) or \ @@ -146,8 +146,7 @@ def drop_postgresql_database(user, host, port, db, version): def postgresql_proc( - executable=None, - host=None, port=-1, user='postgres', + executable=None, host=None, port=-1, user=None, startparams='-w', unixsocketdir='/tmp', logs_prefix='', ): """ @@ -162,6 +161,7 @@ def postgresql_proc( [(2000,3000)] or (2000,3000) - random available port from a given range [{4002,4003}] or {4002,4003} - random of 4002 or 4003 ports [(2000,3000), {4002,4003}] - random of given range and set + :param str user: postgresql username :param str logs_prefix: prefix for log filename :rtype: func :returns: function which makes a postgresql process @@ -188,7 +188,7 @@ def postgresql_proc_fixture(request): pg_host = host or config['host'] pg_port = get_port(port) or get_port(config['port']) datadir = path(gettempdir()) / 'postgresqldata.{0}'.format(pg_port) - pg_user = user + pg_user = user or config['user'] pg_unixsocketdir = unixsocketdir pg_startparams = startparams logsdir = path(request.config.getvalue('pgsql_logsdir')) @@ -209,6 +209,7 @@ def postgresql_proc_fixture(request): pg_ctl=postgresql_ctl, host=pg_host, port=pg_port, + user=pg_user, datadir=datadir, unixsocketdir=pg_unixsocketdir, logfile=logfile_path, @@ -231,7 +232,7 @@ def stop_server_and_remove_directory(): return postgresql_proc_fixture -def postgresql(process_fixture_name, db='tests', user='postgres'): +def postgresql(process_fixture_name, db='tests'): """ Connection fixture factory for PostgreSQL. @@ -254,7 +255,7 @@ def postgresql_factory(request): # _, config = try_import('psycopg2', request) pg_host = proc_fixture.host pg_port = proc_fixture.port - pg_user = user + pg_user = proc_fixture.user pg_db = db init_postgresql_database( diff --git a/src/pytest_postgresql/plugin.py b/src/pytest_postgresql/plugin.py index 11339c19..3aec16c1 100644 --- a/src/pytest_postgresql/plugin.py +++ b/src/pytest_postgresql/plugin.py @@ -22,6 +22,7 @@ _help_executable = 'Path to PostgreSQL executable' _help_host = 'Host at which PostgreSQL will accept connections' _help_port = 'Port at which PostgreSQL will accept connections' +_help_user = "PostgreSQL username" def pytest_addoption(parser): @@ -44,6 +45,12 @@ def pytest_addoption(parser): default=None, ) + parser.addini( + name='postgresql_user', + help=_help_user, + default='postgres' + ) + parser.addoption( '--postgresql-exec', action='store', @@ -66,6 +73,13 @@ def pytest_addoption(parser): help=_help_port ) + parser.addoption( + '--postgresql-user', + action='store', + dest='postgresql_user', + help=_help_user + ) + parser.addoption( '--pgsql-logsdir', action='store',