Skip to content

Commit

Permalink
TST: Switch to fixtures from private method
Browse files Browse the repository at this point in the history
Fixtures explicitly map onto lifetime and makes it clear how the object
is used within a test.

By switching to it, we've brought this in-line with other unit testing
patterns across projects
  • Loading branch information
DavidFair committed Mar 26, 2024
1 parent bf7adcb commit 2f1389c
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions scripts/test/test_upload_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,28 @@ def test_upload_images_to_openstack_dry_run():
mocked_upload_single_image.assert_not_called()


def _expected_args_helper(file: Path, visibility: str) -> Dict:
"""
Helper function to generate the expected arguments for upload_single_image
:param file: The Path of a file to upload
:param visibility: A string of either "public" or "private"
:return: A dictionary of expected arguments for an assert call list
"""
# pylint: disable=duplicate-code
return {
"name": get_upload_name(file),
"filename": file.as_posix(),
"disk_format": "qcow2",
"container_format": "bare",
"wait": True,
"visibility": visibility,
}


def test_upload_images_to_openstack_real_run():
@pytest.fixture(name="fake_upload_args")
def fake_upload_args_fixture():
def _expected_args_helper(file: Path, visibility: str) -> Dict:
"""
Helper function to generate the expected arguments for upload_single_image
:param file: The Path of a file to upload
:param visibility: A string of either "public" or "private"
:return: A dictionary of expected arguments for an assert call list
"""
# pylint: disable=duplicate-code
return {
"name": get_upload_name(file),
"filename": file.as_posix(),
"disk_format": "qcow2",
"container_format": "bare",
"wait": True,
"visibility": visibility,
}
return _expected_args_helper


def test_upload_images_to_openstack_real_run(fake_upload_args):
"""
Tests that the upload_images_to_openstack function calls
upload_single_image with the expected arguments
Expand All @@ -121,13 +124,13 @@ def test_upload_images_to_openstack_real_run():
upload_images_to_openstack(expected_files, args)

expected = [
call(args.os_cloud, _expected_args_helper(file, "public"))
call(args.os_cloud, fake_upload_args(file, "public"))
for file in expected_files
]
mocked_upload_single_image.assert_has_calls(expected, any_order=True)


def test_upload_images_with_private_annotation():
def test_upload_images_with_private_annotation(fake_upload_args):
"""
Tests that the upload_images_to_openstack function calls
upload_single_image with the expected arguments if the public flag is not set
Expand All @@ -137,7 +140,7 @@ def test_upload_images_with_private_annotation():
upload_images_to_openstack([Path("file1")], args)

assert mocked_upload_single_image.call_count == 1
expected = [call(args.os_cloud, _expected_args_helper(Path("file1"), "private"))]
expected = [call(args.os_cloud, fake_upload_args(Path("file1"), "private"))]
assert mocked_upload_single_image.call_args_list == expected


Expand Down

0 comments on commit 2f1389c

Please sign in to comment.