Skip to content

Commit

Permalink
chore: auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Aug 13, 2024
1 parent 5c5e81d commit 2a69e0e
Showing 1 changed file with 50 additions and 32 deletions.
82 changes: 50 additions & 32 deletions tests/unit/plugins/module_utils/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)


from unittest.mock import MagicMock, Mock

import pytest
from unittest.mock import Mock, MagicMock
from ansible_collections.ansible.eda.plugins.module_utils.errors import EDAError
from ansible_collections.ansible.eda.plugins.module_utils.controller import Controller

from ansible_collections.ansible.eda.plugins.module_utils.errors import EDAError

ENDPOINT = "test_endpoint"

Expand All @@ -21,7 +21,7 @@ def mock_client():
@pytest.fixture
def mock_module():
module = Mock()
module.params = {'update_secrets': True}
module.params = {"update_secrets": True}
module.check_mode = False
return module

Expand All @@ -37,23 +37,29 @@ def controller(mock_client, mock_module):
# create_if_needed without existing item
(
None,
{'name': 'Test'},
{"name": "Test"},
Mock(status=201, json={"id": 1}),
{'changed': True, 'id': 1},
{"changed": True, "id": 1},
1, # Expected number of post calls
),
# create_if_needed with existing item
(
{'id': 1, 'url': 'http://test.com/api/item/1'},
{'name': 'Test'},
{"id": 1, "url": "http://test.com/api/item/1"},
{"name": "Test"},
None,
None,
0, # Expected number of post calls
),
],
)
def test_create_if_needed(
mock_client, controller, existing_item, new_item, mock_response, expected_result, expected_calls
mock_client,
controller,
existing_item,
new_item,
mock_response,
expected_result,
expected_calls,
):
if mock_response:
mock_client.post.return_value = mock_response
Expand All @@ -69,22 +75,27 @@ def test_create_if_needed(
[
# delete_if_needed with an existing item
(
{'id': 1, 'name': 'test_item'},
{"id": 1, "name": "test_item"},
Mock(status=204, json={}),
{'changed': True, 'id': 1},
{"changed": True, "id": 1},
1, # Expected number of delete calls
),
# delete_if_needed without an existing item
(
None,
None,
{'changed': False},
{"changed": False},
0, # Expected number of delete calls
),
],
)
def test_delete_if_needed(
mock_client, controller, existing_item, mock_response, expected_result, expected_calls
mock_client,
controller,
existing_item,
mock_response,
expected_result,
expected_calls,
):
if mock_response:
mock_client.delete.return_value = mock_response
Expand All @@ -93,18 +104,20 @@ def test_delete_if_needed(
assert result == expected_result
assert mock_client.delete.call_count == expected_calls
if expected_calls > 0:
mock_client.delete.assert_called_with(ENDPOINT, **{"id": existing_item['id']})
mock_client.delete.assert_called_with(ENDPOINT, **{"id": existing_item["id"]})


def test_update_if_needed_with_existing_item(mock_client, controller):
existing_item = {'id': 1, 'name': 'Test1'}
new_item = {'name': 'Test2'}
existing_item = {"id": 1, "name": "Test1"}
new_item = {"name": "Test2"}
response = Mock(status=200, json={"id": 1, "name": "Test2"})
mock_client.patch.return_value = response
result = controller.update_if_needed(existing_item, new_item, ENDPOINT, 'resource type')
result = controller.update_if_needed(
existing_item, new_item, ENDPOINT, "resource type"
)
mock_client.patch.assert_called_with(ENDPOINT, **{"data": new_item, "id": 1})
assert result['changed'] is True
assert result['id'] == 1
assert result["changed"] is True
assert result["id"] == 1


def test_get_endpoint(mock_client, controller):
Expand All @@ -126,12 +139,12 @@ def test_post_endpoint(mock_client, controller):
def test_patch_endpoint_check_mode(controller):
controller.module.check_mode = True
result = controller.patch_endpoint(ENDPOINT)
assert result['changed'] is True
assert result["changed"] is True


def test_get_name_field_from_endpoint():
assert Controller.get_name_field_from_endpoint('users') == 'username'
assert Controller.get_name_field_from_endpoint('unknown') == 'name'
assert Controller.get_name_field_from_endpoint("users") == "username"
assert Controller.get_name_field_from_endpoint("unknown") == "name"


@pytest.mark.parametrize(
Expand All @@ -151,30 +164,35 @@ def test_get_item_name(controller, item, expected_name, should_raise):


def test_has_encrypted_values():
assert Controller.has_encrypted_values({'key': '$encrypted$'}) is True
assert Controller.has_encrypted_values({'key': 'value'}) is False
assert Controller.has_encrypted_values({"key": "$encrypted$"}) is True
assert Controller.has_encrypted_values({"key": "value"}) is False


def test_fail_wanted_one(mock_client, controller):
response = MagicMock()
response.json.return_value = {'count': 2, 'results': [{'id': 1}, {'id': 2}]}
mock_client.build_url.return_value.geturl.return_value = 'http://example.com/api'
mock_client.host = 'http://example.com'
with pytest.raises(EDAError, match='expected 1'):
controller.fail_wanted_one(response, 'endpoint', {})
response.json.return_value = {"count": 2, "results": [{"id": 1}, {"id": 2}]}
mock_client.build_url.return_value.geturl.return_value = "http://example.com/api"
mock_client.host = "http://example.com"
with pytest.raises(EDAError, match="expected 1"):
controller.fail_wanted_one(response, "endpoint", {})


def test_fields_could_be_same():
assert Controller.fields_could_be_same({'key': '$encrypted$'}, {'key': 'value'}) is True
assert Controller.fields_could_be_same({'key1': 'value1'}, {'key2': 'value2'}) is False
assert (
Controller.fields_could_be_same({"key": "$encrypted$"}, {"key": "value"})
is True
)
assert (
Controller.fields_could_be_same({"key1": "value1"}, {"key2": "value2"}) is False
)


@pytest.mark.parametrize(
"old, new, expected",
[
({"key": "$encrypted$"}, {"key": "value"}, True),
({"key": "value"}, {"key": "value"}, False),
]
],
)
def test_objects_could_be_different(controller, old, new, expected):
assert controller.objects_could_be_different(old, new) is expected

0 comments on commit 2a69e0e

Please sign in to comment.