From c3f93fa840ad05e40268d6451c7354a553c1bc39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=9Aliwi=C5=84ski?= Date: Fri, 31 Jul 2020 11:57:01 +0200 Subject: [PATCH] Extract NoopExecutor into it's own module --- CHANGES.rst | 1 + docs/source/conf.py | 3 +- setup.py | 2 +- src/pytest_postgresql/executor.py | 4 +- src/pytest_postgresql/executor_noop.py | 69 ++++++++++++++++++++++++++ src/pytest_postgresql/factories.py | 48 ++---------------- src/pytest_postgresql/port.py | 2 +- 7 files changed, 79 insertions(+), 50 deletions(-) create mode 100644 src/pytest_postgresql/executor_noop.py diff --git a/CHANGES.rst b/CHANGES.rst index 0e934dda..f183c6c1 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/docs/source/conf.py b/docs/source/conf.py index fcd61b24..bf53efd2 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2016 by Clearcode +# Copyright (C) 2016-2020 by Clearcode + # and associates (see AUTHORS). # This file is part of pytest-postgresql. diff --git a/setup.py b/setup.py index 2f64e6c6..de85508b 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (C) 2016 by Clearcode +# Copyright (C) 2016-2020 by Clearcode # and associates (see AUTHORS). # This file is part of pytest-postgresql. diff --git a/src/pytest_postgresql/executor.py b/src/pytest_postgresql/executor.py index e4beec4c..72dfaaee 100644 --- a/src/pytest_postgresql/executor.py +++ b/src/pytest_postgresql/executor.py @@ -1,7 +1,7 @@ -# Copyright (C) 2013 by Clearcode +# Copyright (C) 2016-2020 by Clearcode # 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 diff --git a/src/pytest_postgresql/executor_noop.py b/src/pytest_postgresql/executor_noop.py new file mode 100644 index 00000000..8195a900 --- /dev/null +++ b/src/pytest_postgresql/executor_noop.py @@ -0,0 +1,69 @@ +# Copyright (C) 2020 by Clearcode +# 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 . +"""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 diff --git a/src/pytest_postgresql/factories.py b/src/pytest_postgresql/factories.py index f3d30ca1..2184136f 100644 --- a/src/pytest_postgresql/factories.py +++ b/src/pytest_postgresql/factories.py @@ -1,7 +1,7 @@ -# Copyright (C) 2013-2016 by Clearcode +# Copyright (C) 2013-2020 by Clearcode # 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 @@ -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 = {} diff --git a/src/pytest_postgresql/port.py b/src/pytest_postgresql/port.py index b838886b..31209115 100644 --- a/src/pytest_postgresql/port.py +++ b/src/pytest_postgresql/port.py @@ -1,4 +1,4 @@ -# Copyright (C) 2016 by Clearcode +# Copyright (C) 2016-2020 by Clearcode # and associates (see AUTHORS). # This file is part of pytest-postgresql.