From e3e8c5a0da9e7f35d8268aefc5511e94701aec98 Mon Sep 17 00:00:00 2001 From: Kate Case Date: Mon, 27 Feb 2023 13:36:22 -0500 Subject: [PATCH] Low-hanging unit fixes (#515) --- .../sub_plugins/cli_parser/pyats_parser.py | 3 -- .../network/common/test_parsing.py | 4 --- tests/unit/plugins/connection/test_netconf.py | 19 +++--------- .../unit/plugins/plugin_utils/test_version.py | 31 +++++++++++++++++++ 4 files changed, 36 insertions(+), 21 deletions(-) create mode 100644 tests/unit/plugins/plugin_utils/test_version.py diff --git a/plugins/sub_plugins/cli_parser/pyats_parser.py b/plugins/sub_plugins/cli_parser/pyats_parser.py index d702e4851..1a0005af4 100644 --- a/plugins/sub_plugins/cli_parser/pyats_parser.py +++ b/plugins/sub_plugins/cli_parser/pyats_parser.py @@ -42,7 +42,6 @@ from ansible.module_utils._text import to_native from ansible.module_utils.basic import missing_required_lib -from ansible.module_utils.six import PY3 from ansible_collections.ansible.utils.plugins.plugin_utils.base.cli_parser import ( CliParserBase, ) @@ -77,8 +76,6 @@ def _check_reqs(): :return dict: A dict with a list of errors """ errors = [] - if not PY3: - errors.append("Pyats and Genie require Python 3") if not HAS_GENIE: errors.append(missing_required_lib("genie")) if not HAS_PYATS: diff --git a/tests/unit/module_utils/network/common/test_parsing.py b/tests/unit/module_utils/network/common/test_parsing.py index 015b20f2c..7a3b95d69 100644 --- a/tests/unit/module_utils/network/common/test_parsing.py +++ b/tests/unit/module_utils/network/common/test_parsing.py @@ -31,7 +31,3 @@ def test_key_value_instance_variable_assignment(self): def test_conditionals_w_not_keyword(self): assert c1(test_results) and c2(test_results) and c3(test_results) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/unit/plugins/connection/test_netconf.py b/tests/unit/plugins/connection/test_netconf.py index c87155b80..439751fc6 100644 --- a/tests/unit/plugins/connection/test_netconf.py +++ b/tests/unit/plugins/connection/test_netconf.py @@ -8,7 +8,6 @@ __metaclass__ = type -import sys from unittest.mock import MagicMock, PropertyMock, patch import pytest @@ -28,19 +27,11 @@ def import_mock(name, *args): return builtin_import(name, *args) -PY3 = sys.version_info[0] == 3 -if PY3: - with patch("builtins.__import__", side_effect=import_mock): - from ansible.plugins.loader import connection_loader - from ansible_collections.ansible.netcommon.plugins.connection import ( - netconf, - ) -else: - with patch("__builtin__.__import__", side_effect=import_mock): - from ansible.plugins.loader import connection_loader - from ansible_collections.ansible.netcommon.plugins.connection import ( - netconf, - ) +with patch("builtins.__import__", side_effect=import_mock): + from ansible.plugins.loader import connection_loader + from ansible_collections.ansible.netcommon.plugins.connection import ( + netconf, + ) def test_netconf_init(): diff --git a/tests/unit/plugins/plugin_utils/test_version.py b/tests/unit/plugins/plugin_utils/test_version.py new file mode 100644 index 000000000..b7a8d43f1 --- /dev/null +++ b/tests/unit/plugins/plugin_utils/test_version.py @@ -0,0 +1,31 @@ +# (c) 2023 Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import pytest +from ansible_collections.ansible.netcommon.plugins.plugin_utils.version import ( + Version, +) + + +@pytest.mark.parametrize("left", ["6.0.0", 6, 6.0]) +@pytest.mark.parametrize("right", ["4.0.0", 4, 4.0]) +def test_versions_different(left, right): + assert Version(str(left)) > right + + +@pytest.mark.parametrize("value", ["6.0.0", 6, 6.0]) +def test_versions_same(value): + assert Version(str(value)) == value + + +def test_version_error(): + with pytest.raises(TypeError): + Version("1.2.3") < [1, 2, 3] + + # with pytest.raises(TypeError): + Version("1.2.3") == [1, 2, 3]