Skip to content

Commit

Permalink
Merge pull request #320 from fizyk/extract_noop
Browse files Browse the repository at this point in the history
Extract noop
  • Loading branch information
fizyk committed Aug 18, 2020
2 parents fb4ede8 + c3f93fa commit e0c6979
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 50 deletions.
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
----------

- [enhancement] extract NoopExecutor into it's own submodule
- [bugfix] Fix version detection, to allow for two-digit minor version part

2.4.0
Expand Down
3 changes: 2 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2016 by Clearcode <http://clearcode.cc>
# Copyright (C) 2016-2020 by Clearcode <http://clearcode.cc>

# and associates (see AUTHORS).

# This file is part of pytest-postgresql.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2016 by Clearcode <http://clearcode.cc>
# Copyright (C) 2016-2020 by Clearcode <http://clearcode.cc>
# and associates (see AUTHORS).

# This file is part of pytest-postgresql.
Expand Down
4 changes: 2 additions & 2 deletions src/pytest_postgresql/executor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (C) 2013 by Clearcode <http://clearcode.cc>
# Copyright (C) 2016-2020 by Clearcode <http://clearcode.cc>
# and associates (see AUTHORS).

# This file is part of pytest-dbfixtures.
# This file is part of pytest-postgresql.

# pytest-dbfixtures is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
Expand Down
69 changes: 69 additions & 0 deletions src/pytest_postgresql/executor_noop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright (C) 2020 by Clearcode <http://clearcode.cc>
# and associates (see AUTHORS).

# This file is part of pytest-postgresql.

# pytest-dbfixtures is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# pytest-dbfixtures is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public License
# along with pytest-dbfixtures. If not, see <http://www.gnu.org/licenses/>.
"""PostgreSQL Noop executor providing connection details for postgres client."""
from pkg_resources import parse_version

from pytest_postgresql.janitor import psycopg2


class NoopExecutor: # pylint: disable=too-few-public-methods
"""
Nooperator executor.
This executor actually does nothing more than provide connection details
for existing PostgreSQL server. I.E. one already started either on machine
or with the use of containerisation like kubernetes or docker compose.
"""

def __init__(self, host, port, user, options, password=None):
"""
Initialize nooperator executor mock.
:param str host: Postgresql hostname
:param str|int port: Postrgesql port
:param str user: Postgresql username
:param str user: Postgresql password
:param str options: Additional connection options
"""
self.host = host
self.port = int(port)
self.user = user
self.options = options
self.password = password
self._version = None

@property
def version(self):
"""Get postgresql's version."""
if not self._version:
with psycopg2.connect(
dbname='postgres',
user=self.user,
host=self.host,
port=self.port,
password=self.password,
options=self.options
) as connection:
version = str(connection.server_version)
self._version = parse_version(
'.'.join([
version[i: i+2] for i in range(0, len(version), 2)
if int(version[i: i+2])
])
)
return self._version
48 changes: 3 additions & 45 deletions src/pytest_postgresql/factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (C) 2013-2016 by Clearcode <http://clearcode.cc>
# Copyright (C) 2013-2020 by Clearcode <http://clearcode.cc>
# and associates (see AUTHORS).

# This file is part of pytest-dbfixtures.
# This file is part of pytest-postgresql.

# pytest-dbfixtures is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -24,55 +24,13 @@
from warnings import warn

import pytest
from pkg_resources import parse_version

from pytest_postgresql.executor_noop import NoopExecutor
from pytest_postgresql.janitor import DatabaseJanitor, psycopg2
from pytest_postgresql.executor import PostgreSQLExecutor
from pytest_postgresql.port import get_port


class NoopExecutor: # pylint: disable=too-few-public-methods
"""Nooperator executor."""

def __init__(self, host, port, user, options, password=None):
"""
Initialize nooperator executor mock.
:param str host: Postgresql hostname
:param str|int port: Postrgesql port
:param str user: Postgresql username
:param str user: Postgresql password
:param str options: Additional connection options
"""
self.host = host
self.port = int(port)
self.user = user
self.options = options
self.password = password
self._version = None

@property
def version(self):
"""Get postgresql's version."""
if not self._version:
with psycopg2.connect(
dbname='postgres',
user=self.user,
host=self.host,
port=self.port,
password=self.password,
options=self.options
) as connection:
version = str(connection.server_version)
self._version = parse_version(
'.'.join([
version[i: i+2] for i in range(0, len(version), 2)
if int(version[i: i+2])
])
)
return self._version


def get_config(request):
"""Return a dictionary with config options."""
config = {}
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_postgresql/port.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2016 by Clearcode <http://clearcode.cc>
# Copyright (C) 2016-2020 by Clearcode <http://clearcode.cc>
# and associates (see AUTHORS).

# This file is part of pytest-postgresql.
Expand Down

0 comments on commit e0c6979

Please sign in to comment.