Skip to content

Conversation

@Cristi1324
Copy link
Contributor

@Cristi1324 Cristi1324 commented May 15, 2024

Add unit tests for the following modules:

  • coriolisclient.cli.formatter
  • coriolisclient.cli.shell
  • coriolisclient.cli.utils

@Cristi1324 Cristi1324 force-pushed the add_cli_unit_tests_3 branch 2 times, most recently from 6cc2d4a to d09571f Compare May 16, 2024 12:23
@Cristi1324 Cristi1324 changed the title Add unit tests for coriolisclient.cli.shell and coriolisclient.cli.utils modules Add unit tests for coriolisclient.cli shell, utils and formatter modules May 16, 2024
@Cristi1324 Cristi1324 changed the title Add unit tests for coriolisclient.cli shell, utils and formatter modules Add unit tests for coriolisclient.cli: shell, utils and formatter modules May 16, 2024
@Cristi1324 Cristi1324 force-pushed the add_cli_unit_tests_3 branch 2 times, most recently from 77c0323 to bb62a9e Compare May 16, 2024 15:26
def setUp(self):
def _get_formatted_data(self, obj):
return mock.sentinel.data
setattr(formatter.EntityFormatter, '_get_formatted_data',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing this, let's use the EntityFormatter as it was supposed to be used, and that is by creating a mock class that inherits from this tested mixin class. You just need to "implement" the _get_formatted_data method and then you can test the rest of the methods as below.

):
obj_list = ["obj2", "obj1"]
mock_get_sorted_list.return_value = ["obj1", "obj2"]
mock_get_generic_data.side_effect = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you implement _get_formatted_data for your test class, you won't need to mock this, and also you wouldn't have to use next() in results (especially if you just return the passed obj). Let's give it a simpler look please

result
)

def testget_formatted_entity(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix this typo

setattr(args, key, value)
endpoint_filter_kwargs = {"filter_arg": "mock_filter_arg"}
mock_get_endpoint_filter_kwargs.return_value = endpoint_filter_kwargs
call_args = data.get("call_args", None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pro tip: unless set, get() method's value argument always defaults to None, you don't need to explicitly set it

result
)

if create_keystone_session_args is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if you'd change this condition to if args.no_auth:
Makes more sense, because if auth is otherwise set, a session needs to be created, hence create_keystone_session is called

mock_register_session_argparse_arguments.assert_called_once_with(
mock_build_option_parser.return_value)

@mock.patch.object(os, 'environ')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please actually use os.environ instead of mocking it: https://docs.python.org/3/library/os.html#os.environ

)
def test_compose_user_scripts(self, data):
global_scripts = data["global_scripts"]
global_scripts = data["global_scripts"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove duplicate line

},
{
"global_scripts": ["linux="],
"instance_scripts": ["linux="],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't input os_type as instance. It should be something like instance_1

@Cristi1324 Cristi1324 force-pushed the add_cli_unit_tests_3 branch from bc98c1f to cffaca8 Compare May 24, 2024 19:57
utils.add_storage_mappings_arguments_to_parser(parser)
args = parser.parse_args(
['--storage-backend-mapping', 'mock_source=mock_destination',
'--disk-storage-mapping', 'mock_disk_id=mock_destination'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a default_storage_backend also. Great test btw, I love it!

Comment on lines 96 to 108

result = utils.parse_storage_mappings(None)

self.assertEqual(
(None, {}, {}),
result
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this into a separate test method, call it test_parse_storageg_mappings_none or something like that.

)

def test_add_args_for_json_option_to_parser(self):
parser = mock.Mock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really loved the approach of using an empty ArgumentParser to test these kinds of methods. Can you please also do that here?

Comment on lines 284 to 285
script_path = os.path.join(script_path, 'data')
script_path = os.path.join(script_path, 'user_scripts.yml')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify this by:

Suggested change
script_path = os.path.join(script_path, 'data')
script_path = os.path.join(script_path, 'user_scripts.yml')
script_path = os.path.join(script_path, 'data/user_scripts.yml')

@Cristi1324 Cristi1324 force-pushed the add_cli_unit_tests_3 branch 3 times, most recently from 24512ac to 917d782 Compare November 22, 2024 13:12
Comment on lines 98 to 106
"args": {
"no_auth": "mock_no_auth",
"os_auth_url": "mock_os_auth_url"
}
},
{
"args": {
"no_auth": "mock_no_auth",
"os_auth_url": "mock_os_auth_url"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two cases seem to be the same.

@Cristi1324 Cristi1324 force-pushed the add_cli_unit_tests_3 branch 3 times, most recently from 50fbbb0 to 27b499c Compare November 27, 2024 15:59
@Dany9966 Dany9966 merged commit 2f17b44 into cloudbase:master Nov 28, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants