From 80560f4a606c7a4fa1a133f8940d43a1d9ba2a0c Mon Sep 17 00:00:00 2001 From: Jacob Callahan Date: Fri, 2 Feb 2024 15:50:15 -0500 Subject: [PATCH] Adjust unit tests for python 3.12 This commit introduces a few changes relating to unittest changes and differences post-removal of old behavior. --- robottelo/cli/base.py | 8 +- robottelo/cli/subscription.py | 4 +- tests/robottelo/test_cli.py | 104 ++++++++++++++++++----- tests/robottelo/test_cli_method_calls.py | 4 +- 4 files changed, 90 insertions(+), 30 deletions(-) diff --git a/robottelo/cli/base.py b/robottelo/cli/base.py index 2d3e6dbf850..247d17985e1 100644 --- a/robottelo/cli/base.py +++ b/robottelo/cli/base.py @@ -132,26 +132,26 @@ def delete(cls, options=None, timeout=None): return cls.execute(cls._construct_command(options), ignore_stderr=True, timeout=timeout) @classmethod - def delete_parameter(cls, options=None): + def delete_parameter(cls, options=None, timeout=None): """ Deletes parameter from record. """ cls.command_sub = 'delete-parameter' - result = cls.execute(cls._construct_command(options)) + result = cls.execute(cls._construct_command(options), ignore_stderr=False, timeout=timeout) return result @classmethod - def dump(cls, options=None): + def dump(cls, options=None, timeout=None): """ Displays the content for existing partition table. """ cls.command_sub = 'dump' - result = cls.execute(cls._construct_command(options)) + result = cls.execute(cls._construct_command(options), ignore_stderr=False, timeout=timeout) return result diff --git a/robottelo/cli/subscription.py b/robottelo/cli/subscription.py index 86b57d51d41..0e22eaeaccd 100644 --- a/robottelo/cli/subscription.py +++ b/robottelo/cli/subscription.py @@ -47,7 +47,7 @@ def refresh_manifest(cls, options=None, timeout=None): return cls.execute(cls._construct_command(options), ignore_stderr=True, timeout=timeout) @classmethod - def manifest_history(cls, options=None): + def manifest_history(cls, options=None, timeout=None): """Provided history for subscription manifest""" cls.command_sub = 'manifest-history' - return cls.execute(cls._construct_command(options)) + return cls.execute(cls._construct_command(options), ignore_stderr=True, timeout=timeout) diff --git a/tests/robottelo/test_cli.py b/tests/robottelo/test_cli.py index 5c543e1745c..c6e8bffd846 100644 --- a/tests/robottelo/test_cli.py +++ b/tests/robottelo/test_cli.py @@ -200,7 +200,7 @@ def test_add_create_with_result_dct_with_id_required_org(self, construct, execut Base.command_requires_org = True assert execute.return_value == Base.create({'organization-id': 'org-id'}) assert 'create' == Base.command_sub - construct.assert_called_once_with({}) + construct.assert_called_once_with({'organization-id': 'org-id'}) execute.assert_called_once_with(construct.return_value, output_format='csv', timeout=None) info.assert_called_once_with({'id': 'foo', 'organization-id': 'org-id'}) @@ -224,8 +224,10 @@ def assert_cmd_execution( """Asssert Base class method successfully executed""" assert execute.return_value == base_method(**base_method_kwargs) assert cmd_sub == Base.command_sub - construct.assert_called_once_with(None) - execute.assert_called_once_with(construct.return_value, ignore_stderr=ignore_stderr, timeout=None) + construct.assert_called_once_with(base_method_kwargs.get('options')) + execute.assert_called_once_with( + construct.return_value, ignore_stderr=ignore_stderr, timeout=None + ) @mock.patch('robottelo.cli.base.Base.execute') @mock.patch('robottelo.cli.base.Base._construct_command') @@ -308,16 +310,36 @@ def test_info_requires_organization_id(self, _): # noqa: PT019 - not a fixture with pytest.raises(CLIError): Base.info() + def assert_alt_cmd_execution( + self, + construct, + execute, + base_method, + cmd_sub, + call_kwargs, + command_kwarg=True, + **base_method_kwargs, + ): + """Asssert Base class method successfully executed""" + assert execute.return_value == base_method(**base_method_kwargs) + assert cmd_sub == Base.command_sub + construct.assert_called_once_with(base_method_kwargs.get('options')) + if command_kwarg: + execute.assert_called_once_with(command=construct.return_value, **call_kwargs) + else: + execute.assert_called_once_with(construct.return_value, **call_kwargs) + @mock.patch('robottelo.cli.base.hammer.parse_info') @mock.patch('robottelo.cli.base.Base.execute') @mock.patch('robottelo.cli.base.Base._construct_command') def test_info_without_parsing_response(self, construct, execute, parse): """Check info method execution without parsing response""" - self.assert_cmd_execution( + self.assert_alt_cmd_execution( construct, execute, Base.info, 'info', + call_kwargs={'output_format': 'json', 'return_raw_response': None}, output_format='json', options={'organization-id': 1}, ) @@ -329,26 +351,23 @@ def test_info_without_parsing_response(self, construct, execute, parse): def test_info_parsing_response(self, construct, execute, parse): """Check info method execution parsing response""" parse.return_value = execute.return_value = 'some_response' - self.assert_cmd_execution( - construct, execute, Base.info, 'info', options={'organization-id': 1} + self.assert_alt_cmd_execution( + construct, + execute, + Base.info, + 'info', + call_kwargs={'output_format': None, 'return_raw_response': None}, + options={'organization-id': 1}, ) parse.assert_called_once_with('some_response') - # @mock.patch('robottelo.cli.base.Base.command_requires_org') - # def test_list_requires_organization_id(self, _): - # """Check list raises CLIError with organization-id is not present in - # options - # """ - # with pytest.raises(CLIError): - # Base.list() - @mock.patch('robottelo.cli.base.Base.execute') @mock.patch('robottelo.cli.base.Base._construct_command') def test_list_with_default_per_page(self, construct, execute): """Check list method set per_page as 1000 by default""" assert execute.return_value == Base.list(options={'organization-id': 1}) assert 'list' == Base.command_sub - construct.assert_called_once_with({'per-page': 1000}) + construct.assert_called_once_with({'organization-id': 1, 'per-page': 10000}) execute.assert_called_once_with(construct.return_value, output_format='csv') @mock.patch('robottelo.cli.base.Base.execute') @@ -358,39 +377,80 @@ def test_list_without_per_page(self, construct, execute): list_with_per_page_false = partial( Base.list, per_page=False, options={'organization-id': 1} ) - self.assert_cmd_execution(construct, execute, list_with_per_page_false, 'list') + self.assert_alt_cmd_execution( + construct, + execute, + list_with_per_page_false, + 'list', + call_kwargs={'output_format': 'csv'}, + command_kwarg=False, + options={'organization-id': 1}, + ) @mock.patch('robottelo.cli.base.Base.execute') @mock.patch('robottelo.cli.base.Base._construct_command') def test_puppet_classes(self, construct, execute): """Check puppet_classes method execution""" - self.assert_cmd_execution(construct, execute, Base.puppetclasses, 'puppet-classes') + self.assert_alt_cmd_execution( + construct, + execute, + Base.puppetclasses, + 'puppet-classes', + call_kwargs={'output_format': 'csv'}, + command_kwarg=False, + ) @mock.patch('robottelo.cli.base.Base.execute') @mock.patch('robottelo.cli.base.Base._construct_command') def test_remove_operating_system(self, construct, execute): """Check remove_operating_system method execution""" - self.assert_cmd_execution( - construct, execute, Base.remove_operating_system, 'remove-operatingsystem' + self.assert_alt_cmd_execution( + construct, + execute, + Base.remove_operating_system, + 'remove-operatingsystem', + call_kwargs={}, + command_kwarg=False, ) @mock.patch('robottelo.cli.base.Base.execute') @mock.patch('robottelo.cli.base.Base._construct_command') def test_sc_params(self, construct, execute): """Check sc_params method execution""" - self.assert_cmd_execution(construct, execute, Base.sc_params, 'sc-params') + self.assert_alt_cmd_execution( + construct, + execute, + Base.sc_params, + 'sc-params', + call_kwargs={'output_format': 'csv'}, + command_kwarg=False, + ) @mock.patch('robottelo.cli.base.Base.execute') @mock.patch('robottelo.cli.base.Base._construct_command') def test_set_parameter(self, construct, execute): """Check set_parameter method execution""" - self.assert_cmd_execution(construct, execute, Base.set_parameter, 'set-parameter') + self.assert_alt_cmd_execution( + construct, + execute, + Base.set_parameter, + 'set-parameter', + call_kwargs={}, + command_kwarg=False, + ) @mock.patch('robottelo.cli.base.Base.execute') @mock.patch('robottelo.cli.base.Base._construct_command') def test_update(self, construct, execute): """Check update method execution""" - self.assert_cmd_execution(construct, execute, Base.update, 'update') + self.assert_alt_cmd_execution( + construct, + execute, + Base.update, + 'update', + call_kwargs={'output_format': 'csv', 'return_raw_response': None}, + command_kwarg=False, + ) class CLIErrorTests(unittest.TestCase): diff --git a/tests/robottelo/test_cli_method_calls.py b/tests/robottelo/test_cli_method_calls.py index 0a02659bf71..765a24d2044 100644 --- a/tests/robottelo/test_cli_method_calls.py +++ b/tests/robottelo/test_cli_method_calls.py @@ -58,7 +58,7 @@ def test_cli_proxy_method_called(mocker, command_sub): execute.assert_called_once_with(construct.return_value) -@pytest.mark.parametrize('command_sub', ['synchronize', 'remove-content', 'upload-content']) +@pytest.mark.parametrize('command_sub', ['remove-content', 'upload-content']) def test_cli_repository_method_called(mocker, command_sub): """Check Repository methods are called and command_sub edited This is a parametrized test called by Pytest for each of Repository methods @@ -95,4 +95,4 @@ def test_cli_subscription_method_called(mocker, command_sub): assert execute.return_value == getattr(Subscription, command_sub.replace('-', '_'))(options) assert command_sub == Subscription.command_sub construct.assert_called_once_with(options) - execute.assert_called_once_with(construct.return_value) + execute.assert_called_once_with(construct.return_value, ignore_stderr=True, timeout=None)