Skip to content

Commit

Permalink
Adjust unit tests for python 3.12
Browse files Browse the repository at this point in the history
This commit introduces a few changes relating to unittest changes and
differences post-removal of old behavior.
  • Loading branch information
JacobCallahan committed Feb 2, 2024
1 parent f9a0c45 commit 23e2069
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 31 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
betelgeuse==1.11.0
# broker[docker]==0.4.1 - Temporarily disabled, see below
cryptography==42.0.2
cryptography==41.0.7
deepdiff==6.7.1
dynaconf[vault]==3.2.4
fauxfactory==3.1.0
Expand Down
8 changes: 4 additions & 4 deletions robottelo/cli/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions robottelo/cli/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
104 changes: 82 additions & 22 deletions tests/robottelo/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'})

Expand All @@ -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')
Expand Down Expand Up @@ -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},
)
Expand All @@ -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')
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions tests/robottelo/test_cli_method_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit 23e2069

Please sign in to comment.