Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/pytest_postgresql/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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
Expand All @@ -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
Expand Down
13 changes: 7 additions & 6 deletions src/pytest_postgresql/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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='',
):
"""
Expand All @@ -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
Expand All @@ -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'))
Expand All @@ -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,
Expand All @@ -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.

Expand All @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions src/pytest_postgresql/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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',
Expand All @@ -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',
Expand Down