| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2017, Will Thames <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from ansible.module_utils.common.dict_transformations import ( |
| 10 |
_camel_to_snake, |
| 11 |
_snake_to_camel, |
| 12 |
camel_dict_to_snake_dict, |
| 13 |
dict_merge, |
| 14 |
recursive_diff, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
EXPECTED_SNAKIFICATION = { |
| 19 |
'alllower': 'alllower', |
| 20 |
'TwoWords': 'two_words', |
| 21 |
'AllUpperAtEND': 'all_upper_at_end', |
| 22 |
'AllUpperButPLURALs': 'all_upper_but_plurals', |
| 23 |
'TargetGroupARNs': 'target_group_arns', |
| 24 |
'HTTPEndpoints': 'http_endpoints', |
| 25 |
'PLURALs': 'plurals' |
| 26 |
} |
| 27 |
|
| 28 |
EXPECTED_REVERSIBLE = { |
| 29 |
'TwoWords': 'two_words', |
| 30 |
'AllUpperAtEND': 'all_upper_at_e_n_d', |
| 31 |
'AllUpperButPLURALs': 'all_upper_but_p_l_u_r_a_ls', |
| 32 |
'TargetGroupARNs': 'target_group_a_r_ns', |
| 33 |
'HTTPEndpoints': 'h_t_t_p_endpoints', |
| 34 |
'PLURALs': 'p_l_u_r_a_ls' |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
class TestCaseCamelToSnake: |
| 39 |
|
| 40 |
def test_camel_to_snake(self): |
| 41 |
for (k, v) in EXPECTED_SNAKIFICATION.items(): |
| 42 |
assert _camel_to_snake(k) == v |
| 43 |
|
| 44 |
def test_reversible_camel_to_snake(self): |
| 45 |
for (k, v) in EXPECTED_REVERSIBLE.items(): |
| 46 |
assert _camel_to_snake(k, reversible=True) == v |
| 47 |
|
| 48 |
|
| 49 |
class TestCaseSnakeToCamel: |
| 50 |
|
| 51 |
def test_snake_to_camel_reversed(self): |
| 52 |
for (k, v) in EXPECTED_REVERSIBLE.items(): |
| 53 |
assert _snake_to_camel(v, capitalize_first=True) == k |
| 54 |
|
| 55 |
|
| 56 |
class TestCaseCamelToSnakeAndBack: |
| 57 |
def test_camel_to_snake_and_back(self): |
| 58 |
for (k, v) in EXPECTED_REVERSIBLE.items(): |
| 59 |
assert _snake_to_camel(_camel_to_snake(k, reversible=True), capitalize_first=True) == k |
| 60 |
|
| 61 |
|
| 62 |
class TestCaseCamelDictToSnakeDict: |
| 63 |
def test_ignore_list(self): |
| 64 |
camel_dict = dict(Hello=dict(One='one', Two='two'), World=dict(Three='three', Four='four')) |
| 65 |
snake_dict = camel_dict_to_snake_dict(camel_dict, ignore_list='World') |
| 66 |
assert snake_dict['hello'] == dict(one='one', two='two') |
| 67 |
assert snake_dict['world'] == dict(Three='three', Four='four') |
| 68 |
|
| 69 |
|
| 70 |
class TestCaseDictMerge: |
| 71 |
def test_dict_merge(self): |
| 72 |
base = dict(obj2=dict(), b1=True, b2=False, b3=False, |
| 73 |
one=1, two=2, three=3, obj1=dict(key1=1, key2=2), |
| 74 |
l1=[1, 3], l2=[1, 2, 3], l4=[4], |
| 75 |
nested=dict(n1=dict(n2=2))) |
| 76 |
|
| 77 |
other = dict(b1=True, b2=False, b3=True, b4=True, |
| 78 |
one=1, three=4, four=4, obj1=dict(key1=2), |
| 79 |
l1=[2, 1], l2=[3, 2, 1], l3=[1], |
| 80 |
nested=dict(n1=dict(n2=2, n3=3))) |
| 81 |
|
| 82 |
result = dict_merge(base, other) |
| 83 |
|
| 84 |
# string assertions |
| 85 |
assert 'one' in result |
| 86 |
assert 'two' in result |
| 87 |
assert result['three'] == 4 |
| 88 |
assert result['four'] == 4 |
| 89 |
|
| 90 |
# dict assertions |
| 91 |
assert 'obj1' in result |
| 92 |
assert 'key1' in result['obj1'] |
| 93 |
assert 'key2' in result['obj1'] |
| 94 |
|
| 95 |
# list assertions |
| 96 |
# this line differs from the network_utils/common test of the function of the |
| 97 |
# same name as this method does not merge lists |
| 98 |
assert result['l1'], [2, 1] |
| 99 |
assert 'l2' in result |
| 100 |
assert result['l3'], [1] |
| 101 |
assert 'l4' in result |
| 102 |
|
| 103 |
# nested assertions |
| 104 |
assert 'obj1' in result |
| 105 |
assert result['obj1']['key1'], 2 |
| 106 |
assert 'key2' in result['obj1'] |
| 107 |
|
| 108 |
# bool assertions |
| 109 |
assert 'b1' in result |
| 110 |
assert 'b2' in result |
| 111 |
assert result['b3'] |
| 112 |
assert result['b4'] |
| 113 |
|
| 114 |
|
| 115 |
class TestCaseAzureIncidental: |
| 116 |
|
| 117 |
def test_dict_merge_invalid_dict(self): |
| 118 |
""" if b is not a dict, return b """ |
| 119 |
res = dict_merge({}, None) |
| 120 |
assert res is None |
| 121 |
|
| 122 |
def test_merge_sub_dicts(self): |
| 123 |
"""merge sub dicts """ |
| 124 |
a = {'a': {'a1': 1}} |
| 125 |
b = {'a': {'b1': 2}} |
| 126 |
c = {'a': {'a1': 1, 'b1': 2}} |
| 127 |
res = dict_merge(a, b) |
| 128 |
assert res == c |
| 129 |
|
| 130 |
|
| 131 |
class TestCaseRecursiveDiff: |
| 132 |
def test_recursive_diff(self): |
| 133 |
a = {'foo': {'bar': [{'baz': {'qux': 'ham_sandwich'}}]}} |
| 134 |
c = {'foo': {'bar': [{'baz': {'qux': 'ham_sandwich'}}]}} |
| 135 |
b = {'foo': {'bar': [{'baz': {'qux': 'turkey_sandwich'}}]}} |
| 136 |
|
| 137 |
assert recursive_diff(a, b) is not None |
| 138 |
assert len(recursive_diff(a, b)) == 2 |
| 139 |
assert recursive_diff(a, c) is None |
| 140 |
|
| 141 |
@pytest.mark.parametrize( |
| 142 |
'p1, p2', ( |
| 143 |
([1, 2], [2, 3]), |
| 144 |
({1: 2}, [2, 3]), |
| 145 |
([1, 2], {2: 3}), |
| 146 |
({2: 3}, 'notadict'), |
| 147 |
('notadict', {2: 3}), |
| 148 |
) |
| 149 |
) |
| 150 |
def test_recursive_diff_negative(self, p1, p2): |
| 151 |
with pytest.raises(TypeError, match="Unable to diff"): |
| 152 |
recursive_diff(p1, p2) |