mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-20 07:59:10 -07:00
Rekey on member (#33836)
* Change cast_list_to_dict to more generic rekey_on_member cast_list_to_dict was taking an arbitrary data format in and returning an arbitrary data format out. Rework this to be a more generic function which creates a dict of dicts based on a member of the dict. Remove cast_dict_to_list since rekey_on_member handles the use cases we know about and cast_dict_to_list suffers from the same problems as cast_list_to_dict. If this is still needed we could think about filters we could add to do this in a short jinja2 pipeline. * Fix bare excepts (bare excepts even catch sys.exit())
This commit is contained in:
parent
5b6ba8cbfd
commit
155f36bbd8
4 changed files with 99 additions and 129 deletions
|
@ -1,63 +0,0 @@
|
|||
# Make coding more python3-ish
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.plugins.filter.cast_type import (cast_list_to_dict, cast_dict_to_list)
|
||||
from ansible.errors import AnsibleError, AnsibleFilterError
|
||||
|
||||
|
||||
class TestTypeFilter(unittest.TestCase):
|
||||
def test_cast_list_to_dict(self):
|
||||
# Good test
|
||||
list_original = [{"proto": "eigrp", "state": "enabled"}, {"proto": "ospf", "state": "enabled"}]
|
||||
key = 'proto'
|
||||
dict_return = {'eigrp': {'state': 'enabled', 'proto': 'eigrp'}, 'ospf': {'state': 'enabled', 'proto': 'ospf'}}
|
||||
self.assertEqual(cast_list_to_dict(list_original, key), dict_return)
|
||||
|
||||
# Fail when key is not found
|
||||
key = 'key_not_to_be_found'
|
||||
self.assertRaisesRegexp(AnsibleFilterError, 'was not found', cast_list_to_dict, list_original, key)
|
||||
|
||||
# Fail when key is duplicated
|
||||
list_original = [{"proto": "eigrp", "state": "enabled"}, {"proto": "ospf", "state": "enabled"}, {"proto": "ospf", "state": "enabled"}]
|
||||
key = 'proto'
|
||||
self.assertRaisesRegexp(AnsibleFilterError, 'is not unique', cast_list_to_dict, list_original, key)
|
||||
|
||||
# Fail when list item is not a dict
|
||||
list_original = [{"proto": "eigrp", "state": "enabled"}, "ospf"]
|
||||
key = 'proto'
|
||||
self.assertRaisesRegexp(AnsibleFilterError, 'List item is not a valid dict', cast_list_to_dict, list_original, key)
|
||||
|
||||
# Fail when a non list is sent
|
||||
list_original = {"proto": "eigrp", "state": "enabled"}
|
||||
key = 'proto'
|
||||
self.assertRaisesRegexp(AnsibleFilterError, 'not a valid list', cast_list_to_dict, list_original, key)
|
||||
|
||||
def test_cast_dict_to_list(self):
|
||||
# Good test
|
||||
dict_original = {'eigrp': {'state': 'enabled', 'as': '1'}, 'ospf': {'state': 'enabled', 'as': '2'}}
|
||||
key_name = 'proto'
|
||||
list_return = [{'state': 'enabled', 'proto': 'ospf', 'as': '2'}, {'state': 'enabled', 'proto': 'eigrp', 'as': '1'}]
|
||||
actual_return = cast_dict_to_list(dict_original, key_name)
|
||||
|
||||
try:
|
||||
_assertItemsEqual = self.assertCountEqual
|
||||
_assertItemsEqual(actual_return, list_return)
|
||||
except AttributeError:
|
||||
self.assertEqual(sorted(actual_return), sorted(list_return))
|
||||
|
||||
# Fail when dict key is already used
|
||||
dict_original = {'eigrp': {'state': 'enabled', 'as': '1', 'proto': 'bgp'}, 'ospf': {'state': 'enabled', 'as': '2'}}
|
||||
key_name = 'proto'
|
||||
self.assertRaisesRegexp(AnsibleFilterError, ' already in use, cannot correctly turn into dict', cast_dict_to_list, dict_original, key_name)
|
||||
|
||||
# Fail when sending a non-dict
|
||||
dict_original = [{'eigrp': {'state': 'enabled', 'as': '1'}, 'ospf': {'state': 'enabled', 'as': '2'}}]
|
||||
key_name = 'proto'
|
||||
self.assertRaisesRegexp(AnsibleFilterError, 'Type is not a valid dict', cast_dict_to_list, dict_original, key_name)
|
||||
|
||||
# Fail when dict value is not a dict
|
||||
dict_original = {'eigrp': [{'state': 'enabled', 'as': '1'}], 'ospf': {'state': 'enabled', 'as': '2'}}
|
||||
key_name = 'proto'
|
||||
self.assertRaisesRegexp(AnsibleFilterError, 'Type of key', cast_dict_to_list, dict_original, key_name)
|
|
@ -120,3 +120,48 @@ class TestInversePower:
|
|||
|
||||
def test_cube_root(self):
|
||||
assert ms.inversepower(27, 3) == 3
|
||||
|
||||
|
||||
class TestRekeyOnMember():
|
||||
# (Input data structure, member to rekey on, expected return)
|
||||
VALID_ENTRIES = (
|
||||
([{"proto": "eigrp", "state": "enabled"}, {"proto": "ospf", "state": "enabled"}],
|
||||
'proto',
|
||||
{'eigrp': {'state': 'enabled', 'proto': 'eigrp'}, 'ospf': {'state': 'enabled', 'proto': 'ospf'}}),
|
||||
({'eigrp': {"proto": "eigrp", "state": "enabled"}, 'ospf': {"proto": "ospf", "state": "enabled"}},
|
||||
'proto',
|
||||
{'eigrp': {'state': 'enabled', 'proto': 'eigrp'}, 'ospf': {'state': 'enabled', 'proto': 'ospf'}}),
|
||||
)
|
||||
|
||||
# (Input data structure, member to rekey on, expected error message)
|
||||
INVALID_ENTRIES = (
|
||||
# Fail when key is not found
|
||||
([{"proto": "eigrp", "state": "enabled"}], 'invalid_key', "Key invalid_key was not found"),
|
||||
({"eigrp": {"proto": "eigrp", "state": "enabled"}}, 'invalid_key', "Key invalid_key was not found"),
|
||||
# Fail when key is duplicated
|
||||
([{"proto": "eigrp"}, {"proto": "ospf"}, {"proto": "ospf"}],
|
||||
'proto', 'Key ospf is not unique, cannot correctly turn into dict'),
|
||||
# Fail when value is not a dict
|
||||
(["string"], 'proto', "List item is not a valid dict"),
|
||||
([123], 'proto', "List item is not a valid dict"),
|
||||
([[{'proto': 1}]], 'proto', "List item is not a valid dict"),
|
||||
# Fail when we do not send a dict or list
|
||||
("string", 'proto', "Type is not a valid list, set, or dict"),
|
||||
(123, 'proto', "Type is not a valid list, set, or dict"),
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize("list_original, key, expected", VALID_ENTRIES)
|
||||
def test_rekey_on_member_success(self, list_original, key, expected):
|
||||
assert ms.rekey_on_member(list_original, key) == expected
|
||||
|
||||
@pytest.mark.parametrize("list_original, key, expected", INVALID_ENTRIES)
|
||||
def test_fail_rekey_on_member(self, list_original, key, expected):
|
||||
with pytest.raises(AnsibleFilterError) as err:
|
||||
ms.rekey_on_member(list_original, key)
|
||||
|
||||
assert err.value.message == expected
|
||||
|
||||
def test_duplicate_strategy_overwrite(self):
|
||||
list_original = ({'proto': 'eigrp', 'id': 1}, {'proto': 'ospf', 'id': 2}, {'proto': 'eigrp', 'id': 3})
|
||||
expected = {'eigrp': {'proto': 'eigrp', 'id': 3}, 'ospf': {'proto': 'ospf', 'id': 2}}
|
||||
assert ms.rekey_on_member(list_original, 'proto', duplicates='overwrite') == expected
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue