diff options
Diffstat (limited to 'ansible_collections/community/postgresql/tests/unit/plugins')
3 files changed, 43 insertions, 38 deletions
diff --git a/ansible_collections/community/postgresql/tests/unit/plugins/module_utils/test_postgres.py b/ansible_collections/community/postgresql/tests/unit/plugins/module_utils/test_postgres.py index 975542446..566c78851 100644 --- a/ansible_collections/community/postgresql/tests/unit/plugins/module_utils/test_postgres.py +++ b/ansible_collections/community/postgresql/tests/unit/plugins/module_utils/test_postgres.py @@ -1,14 +1,15 @@ # Copyright: (c) 2019, Andrew Klychkov (@Andersson007) <aaklychkov@mail.ru> # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -from __future__ import (absolute_import, division, print_function) +from __future__ import absolute_import, division, print_function + __metaclass__ = type from os import environ -import pytest - import ansible_collections.community.postgresql.plugins.module_utils.postgres as pg - +import pytest +from ansible_collections.community.postgresql.plugins.module_utils.version import \ + LooseVersion INPUT_DICT = dict( session_role=dict(default=''), @@ -124,7 +125,7 @@ def m_psycopg2(): class DummyPsycopg2(): def __init__(self): - self.__version__ = '2.4.3' + self.__version__ = "2.9.6" self.extras = Extras() self.extensions = Extensions() @@ -155,8 +156,12 @@ class TestEnsureReqLibs(): class Dummym_ansible_module(): def __init__(self): self.params = {'ca_cert': False} + self.warn_msg = '' self.err_msg = '' + def warn(self, msg): + self.warn_msg = msg + def fail_json(self, msg): self.err_msg = msg @@ -164,13 +169,14 @@ class TestEnsureReqLibs(): def test_ensure_req_libs_has_not_psycopg2(self, m_ansible_module): """Test ensure_required_libs() with psycopg2 is None.""" - # HAS_PSYCOPG2 is False by default + # HAS_PSYCOPG is False by default pg.ensure_required_libs(m_ansible_module) assert 'Failed to import the required Python library (psycopg2)' in m_ansible_module.err_msg def test_ensure_req_libs_has_psycopg2(self, m_ansible_module, monkeypatch): """Test ensure_required_libs() with psycopg2 is not None.""" - monkeypatch.setattr(pg, 'HAS_PSYCOPG2', True) + monkeypatch.setattr(pg, 'HAS_PSYCOPG', True) + monkeypatch.setattr(pg, 'PSYCOPG_VERSION', "2.9") pg.ensure_required_libs(m_ansible_module) assert m_ansible_module.err_msg == '' @@ -180,8 +186,9 @@ class TestEnsureReqLibs(): Test with module.params['ca_cert'], psycopg2 version is suitable. """ m_ansible_module.params['ca_cert'] = True - monkeypatch.setattr(pg, 'HAS_PSYCOPG2', True) - monkeypatch.setattr(pg, 'psycopg2', m_psycopg2) + monkeypatch.setattr(pg, 'HAS_PSYCOPG', True) + monkeypatch.setattr(pg, 'PSYCOPG_VERSION', LooseVersion("2.9.6")) + monkeypatch.setattr(pg, 'psycopg', m_psycopg2) pg.ensure_required_libs(m_ansible_module) assert m_ansible_module.err_msg == '' @@ -191,11 +198,10 @@ class TestEnsureReqLibs(): Test with module.params['ca_cert'], psycopg2 version is wrong. """ m_ansible_module.params['ca_cert'] = True - monkeypatch.setattr(pg, 'HAS_PSYCOPG2', True) + psycopg = m_psycopg2 + monkeypatch.setattr(pg, 'psycopg', psycopg) # Set wrong psycopg2 version number: - psycopg2 = m_psycopg2 - psycopg2.__version__ = '2.4.2' - monkeypatch.setattr(pg, 'psycopg2', psycopg2) + monkeypatch.setattr(pg, 'PSYCOPG_VERSION', LooseVersion("2.4.2")) pg.ensure_required_libs(m_ansible_module) assert 'psycopg2 must be at least 2.4.3' in m_ansible_module.err_msg @@ -231,10 +237,10 @@ class TestConnectToDb(): """ Namespace for testing connect_to_db() function. - When some connection errors occure connect_to_db() caught any of them + When some connection errors occur connect_to_db() caught any of them and invoke fail_json() or warn() methods of AnsibleModule object depending on the passed parameters. - connect_to_db may return db_connection object or None if errors occured. + connect_to_db may return db_connection object or None if errors occurred. Therefore we must check: 1. Values of err_msg and warn_msg attributes of m_ansible_module mock object. 2. Types of return objects (db_connection and cursor). @@ -242,22 +248,22 @@ class TestConnectToDb(): def test_connect_to_db(self, m_ansible_module, monkeypatch, m_psycopg2): """Test connect_to_db(), common test.""" - monkeypatch.setattr(pg, 'HAS_PSYCOPG2', True) + monkeypatch.setattr(pg, 'psycopg', m_psycopg2) monkeypatch.setattr(pg, 'psycopg2', m_psycopg2) conn_params = pg.get_conn_params(m_ansible_module, m_ansible_module.params) db_connection, dummy = pg.connect_to_db(m_ansible_module, conn_params) cursor = db_connection.cursor() # if errors, db_connection returned as None: - assert type(db_connection) == DbConnection - assert type(cursor) == Cursor + assert type(db_connection) is DbConnection + assert type(cursor) is Cursor assert m_ansible_module.err_msg == '' # The default behaviour, normal in this case: assert 'Database name has not been passed' in m_ansible_module.warn_msg def test_session_role(self, m_ansible_module, monkeypatch, m_psycopg2): """Test connect_to_db(), switch on session_role.""" - monkeypatch.setattr(pg, 'HAS_PSYCOPG2', True) + monkeypatch.setattr(pg, 'psycopg', m_psycopg2) monkeypatch.setattr(pg, 'psycopg2', m_psycopg2) m_ansible_module.params['session_role'] = 'test_role' @@ -265,8 +271,8 @@ class TestConnectToDb(): db_connection, dummy = pg.connect_to_db(m_ansible_module, conn_params) cursor = db_connection.cursor() # if errors, db_connection returned as None: - assert type(db_connection) == DbConnection - assert type(cursor) == Cursor + assert type(db_connection) is DbConnection + assert type(cursor) is Cursor assert m_ansible_module.err_msg == '' # The default behaviour, normal in this case: assert 'Database name has not been passed' in m_ansible_module.warn_msg @@ -275,8 +281,7 @@ class TestConnectToDb(): """ Test connect_to_db(), fail_on_conn arg passed as True (the default behavior). """ - monkeypatch.setattr(pg, 'HAS_PSYCOPG2', True) - monkeypatch.setattr(pg, 'psycopg2', m_psycopg2) + monkeypatch.setattr(pg, 'psycopg', m_psycopg2) m_ansible_module.params['login_user'] = 'Exception' # causes Exception @@ -290,8 +295,7 @@ class TestConnectToDb(): """ Test connect_to_db(), fail_on_conn arg passed as False. """ - monkeypatch.setattr(pg, 'HAS_PSYCOPG2', True) - monkeypatch.setattr(pg, 'psycopg2', m_psycopg2) + monkeypatch.setattr(pg, 'psycopg', m_psycopg2) m_ansible_module.params['login_user'] = 'Exception' # causes Exception @@ -306,9 +310,9 @@ class TestConnectToDb(): """ Test connect_to_db(), autocommit arg passed as True (the default is False). """ - monkeypatch.setattr(pg, 'HAS_PSYCOPG2', True) # case 1: psycopg2.__version >= 2.4.2 (the default in m_psycopg2) + monkeypatch.setattr(pg, 'psycopg', m_psycopg2) monkeypatch.setattr(pg, 'psycopg2', m_psycopg2) conn_params = pg.get_conn_params(m_ansible_module, m_ansible_module.params) @@ -316,8 +320,8 @@ class TestConnectToDb(): cursor = db_connection.cursor() # if errors, db_connection returned as None: - assert type(db_connection) == DbConnection - assert type(cursor) == Cursor + assert type(db_connection) is DbConnection + assert type(cursor) is Cursor assert m_ansible_module.err_msg == '' @@ -327,12 +331,12 @@ class TestGetConnParams(): def test_get_conn_params_def(self, m_ansible_module, m_psycopg2, monkeypatch): """Test get_conn_params(), warn_db_default kwarg is default.""" - monkeypatch.setattr(pg, 'psycopg2', m_psycopg2) + monkeypatch.setattr(pg, 'psycopg', m_psycopg2) assert pg.get_conn_params(m_ansible_module, INPUT_DICT) == EXPECTED_DICT assert m_ansible_module.warn_msg == 'Database name has not been passed, used default database to connect to.' def test_get_conn_params_warn_db_def_false(self, m_ansible_module, m_psycopg2, monkeypatch): """Test get_conn_params(), warn_db_default kwarg is False.""" - monkeypatch.setattr(pg, 'psycopg2', m_psycopg2) + monkeypatch.setattr(pg, 'psycopg', m_psycopg2) assert pg.get_conn_params(m_ansible_module, INPUT_DICT, warn_db_default=False) == EXPECTED_DICT assert m_ansible_module.warn_msg == '' diff --git a/ansible_collections/community/postgresql/tests/unit/plugins/module_utils/test_saslprep.py b/ansible_collections/community/postgresql/tests/unit/plugins/module_utils/test_saslprep.py index 62a1704ad..a93bf3f79 100644 --- a/ansible_collections/community/postgresql/tests/unit/plugins/module_utils/test_saslprep.py +++ b/ansible_collections/community/postgresql/tests/unit/plugins/module_utils/test_saslprep.py @@ -2,13 +2,13 @@ # Copyright: (c) 2019, Andrey Tuzhilin <andrei.tuzhilin@gmail.com> # Copyright: (c) 2020, Andrew Klychkov (@Andersson007) <aaklychkov@mail.ru> -from __future__ import (absolute_import, division, print_function) +from __future__ import absolute_import, division, print_function + __metaclass__ = type import pytest - -from ansible_collections.community.postgresql.plugins.module_utils.saslprep import saslprep - +from ansible_collections.community.postgresql.plugins.module_utils.saslprep import \ + saslprep VALID = [ (u'', u''), @@ -51,5 +51,5 @@ def test_saslprep_conversions(source, target): @pytest.mark.parametrize('source,exception', INVALID) def test_saslprep_exceptions(source, exception): - with pytest.raises(exception) as ex: + with pytest.raises(exception): saslprep(source) diff --git a/ansible_collections/community/postgresql/tests/unit/plugins/modules/test_postgresql_set.py b/ansible_collections/community/postgresql/tests/unit/plugins/modules/test_postgresql_set.py index a10678202..8850df251 100644 --- a/ansible_collections/community/postgresql/tests/unit/plugins/modules/test_postgresql_set.py +++ b/ansible_collections/community/postgresql/tests/unit/plugins/modules/test_postgresql_set.py @@ -1,12 +1,13 @@ # -*- coding: utf-8 -*- # Copyright: (c) 2021, Andrew Klychkov (@Andersson007) <aaklychkov@mail.ru> -from __future__ import (absolute_import, division, print_function) +from __future__ import absolute_import, division, print_function + __metaclass__ = type import pytest - -from ansible_collections.community.postgresql.plugins.modules.postgresql_set import pretty_to_bytes +from ansible_collections.community.postgresql.plugins.modules.postgresql_set import \ + pretty_to_bytes @pytest.mark.parametrize('input_,expected', [ |