From 7b01498d99eb0d3a772366b642e5fab3d6fc6aa2 Mon Sep 17 00:00:00 2001 From: Anna Lushnikova Date: Tue, 10 Sep 2024 16:48:10 -0400 Subject: [PATCH] Add Mocked Tests: Image Actions (#331) * Add Mocked Tests: Image Actions * Format with black, remove extra values in mocks to reduce number of lines * remove todos --- tests/mocked/test_image_actions.py | 121 +++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 tests/mocked/test_image_actions.py diff --git a/tests/mocked/test_image_actions.py b/tests/mocked/test_image_actions.py new file mode 100644 index 0000000..3e94d3e --- /dev/null +++ b/tests/mocked/test_image_actions.py @@ -0,0 +1,121 @@ +# pylint: disable=line-too-long +# pylint: disable=duplicate-code +"""Mock tests for the Image Actions API resource""" + +import responses + +from pydo import Client + + +@responses.activate +def test_image_actions_get(mock_client: Client, mock_client_url): + """Tests Retrieving an Existing Image Action""" + image_id = 7938269 + action_id = 36805527 + expected = { + "action": { + "id": action_id, + "status": "in-progress", + "type": "transfer", + "started_at": "2014-11-14T16:42:45Z", + "completed_at": None, + "resource_id": image_id, + "resource_type": "image", + "region": { + "name": "New York 3", + "slug": "nyc3", + "sizes": ["s-1vcpu-3gb", "m-1vcpu-8gb"], + "features": ["private_networking", "image_transfer"], + "available": True, + }, + "region_slug": "nyc3", + } + } + + responses.add( + responses.GET, + f"{mock_client_url}/v2/images/{image_id}/actions/{action_id}", + json=expected, + ) + + get_resp = mock_client.image_actions.get(image_id=image_id, action_id=action_id) + + assert get_resp == expected + + +@responses.activate +def test_image_actions_list(mock_client: Client, mock_client_url): + """Mocks the image actions list operation""" + image_id = 7555620 + expected = { + "actions": [ + { + "id": 29410565, + "status": "completed", + "type": "transfer", + "started_at": "2014-07-25T15:04:21Z", + "completed_at": "2014-07-25T15:10:20Z", + "resource_id": image_id, + "resource_type": "image", + "region": { + "name": "New York 2", + "slug": "nyc2", + "sizes": ["s-1vcpu-3gb", "s-24vcpu-128gb"], + "features": ["private_networking", "image_transfer"], + "available": True, + }, + "region_slug": "nyc2", + } + ], + "links": { + "pages": { + "last": "https://api.digitalocean.com/v2/images/{image_id}/actions?page=5&per_page=1", + "next": "https://api.digitalocean.com/v2/images/{image_id}/actions?page=2&per_page=1", + } + }, + "meta": {"total": 5}, + } + + responses.add( + responses.GET, f"{mock_client_url}/v2/images/{image_id}/actions", json=expected + ) + list_resp = mock_client.image_actions.list(image_id=image_id) + + assert list_resp == expected + + +@responses.activate +def test_image_actions_post(mock_client: Client, mock_client_url): + """Mocks the image actions post operation.""" + image_id = 7938269 + expected = { + "action": { + "id": 36805527, + "status": "in-progress", + "type": "transfer", + "started_at": "2014-11-14T16:42:45Z", + "completed_at": None, + "resource_id": image_id, + "resource_type": "image", + "region": { + "name": "New York 3", + "slug": "nyc3", + "sizes": ["s-1vcpu-3gb", "s-24vcpu-128gb"], + "features": ["private_networking", "image_transfer"], + "available": True, + }, + "region_slug": "nyc3", + } + } + + responses.add( + responses.POST, + f"{mock_client_url}/v2/images/{image_id}/actions", + json=expected, + status=201, + ) + + create_req = {"type": "convert"} + post_resp = mock_client.image_actions.post(image_id=image_id, body=create_req) + + assert post_resp == expected