summaryrefslogtreecommitdiffstats
path: root/test/units/module_utils/common/test_dict_transformations.py
blob: ba55299c996ada1e2567cc34325c2c450c860381 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
# Copyright: (c) 2017, Will Thames <will.thames@xvt.com.au>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import pytest

from ansible.module_utils.common.dict_transformations import (
    _camel_to_snake,
    _snake_to_camel,
    camel_dict_to_snake_dict,
    dict_merge,
    recursive_diff,
)


EXPECTED_SNAKIFICATION = {
    'alllower': 'alllower',
    'TwoWords': 'two_words',
    'AllUpperAtEND': 'all_upper_at_end',
    'AllUpperButPLURALs': 'all_upper_but_plurals',
    'TargetGroupARNs': 'target_group_arns',
    'HTTPEndpoints': 'http_endpoints',
    'PLURALs': 'plurals'
}

EXPECTED_REVERSIBLE = {
    'TwoWords': 'two_words',
    'AllUpperAtEND': 'all_upper_at_e_n_d',
    'AllUpperButPLURALs': 'all_upper_but_p_l_u_r_a_ls',
    'TargetGroupARNs': 'target_group_a_r_ns',
    'HTTPEndpoints': 'h_t_t_p_endpoints',
    'PLURALs': 'p_l_u_r_a_ls'
}


class TestCaseCamelToSnake:

    def test_camel_to_snake(self):
        for (k, v) in EXPECTED_SNAKIFICATION.items():
            assert _camel_to_snake(k) == v

    def test_reversible_camel_to_snake(self):
        for (k, v) in EXPECTED_REVERSIBLE.items():
            assert _camel_to_snake(k, reversible=True) == v


class TestCaseSnakeToCamel:

    def test_snake_to_camel_reversed(self):
        for (k, v) in EXPECTED_REVERSIBLE.items():
            assert _snake_to_camel(v, capitalize_first=True) == k


class TestCaseCamelToSnakeAndBack:
    def test_camel_to_snake_and_back(self):
        for (k, v) in EXPECTED_REVERSIBLE.items():
            assert _snake_to_camel(_camel_to_snake(k, reversible=True), capitalize_first=True) == k


class TestCaseCamelDictToSnakeDict:
    def test_ignore_list(self):
        camel_dict = dict(Hello=dict(One='one', Two='two'), World=dict(Three='three', Four='four'))
        snake_dict = camel_dict_to_snake_dict(camel_dict, ignore_list='World')
        assert snake_dict['hello'] == dict(one='one', two='two')
        assert snake_dict['world'] == dict(Three='three', Four='four')


class TestCaseDictMerge:
    def test_dict_merge(self):
        base = dict(obj2=dict(), b1=True, b2=False, b3=False,
                    one=1, two=2, three=3, obj1=dict(key1=1, key2=2),
                    l1=[1, 3], l2=[1, 2, 3], l4=[4],
                    nested=dict(n1=dict(n2=2)))

        other = dict(b1=True, b2=False, b3=True, b4=True,
                     one=1, three=4, four=4, obj1=dict(key1=2),
                     l1=[2, 1], l2=[3, 2, 1], l3=[1],
                     nested=dict(n1=dict(n2=2, n3=3)))

        result = dict_merge(base, other)

        # string assertions
        assert 'one' in result
        assert 'two' in result
        assert result['three'] == 4
        assert result['four'] == 4

        # dict assertions
        assert 'obj1' in result
        assert 'key1' in result['obj1']
        assert 'key2' in result['obj1']

        # list assertions
        # this line differs from the network_utils/common test of the function of the
        # same name as this method does not merge lists
        assert result['l1'], [2, 1]
        assert 'l2' in result
        assert result['l3'], [1]
        assert 'l4' in result

        # nested assertions
        assert 'obj1' in result
        assert result['obj1']['key1'], 2
        assert 'key2' in result['obj1']

        # bool assertions
        assert 'b1' in result
        assert 'b2' in result
        assert result['b3']
        assert result['b4']


class TestCaseAzureIncidental:

    def test_dict_merge_invalid_dict(self):
        ''' if b is not a dict, return b '''
        res = dict_merge({}, None)
        assert res is None

    def test_merge_sub_dicts(self):
        '''merge sub dicts '''
        a = {'a': {'a1': 1}}
        b = {'a': {'b1': 2}}
        c = {'a': {'a1': 1, 'b1': 2}}
        res = dict_merge(a, b)
        assert res == c


class TestCaseRecursiveDiff:
    def test_recursive_diff(self):
        a = {'foo': {'bar': [{'baz': {'qux': 'ham_sandwich'}}]}}
        c = {'foo': {'bar': [{'baz': {'qux': 'ham_sandwich'}}]}}
        b = {'foo': {'bar': [{'baz': {'qux': 'turkey_sandwich'}}]}}

        assert recursive_diff(a, b) is not None
        assert len(recursive_diff(a, b)) == 2
        assert recursive_diff(a, c) is None

    @pytest.mark.parametrize(
        'p1, p2', (
            ([1, 2], [2, 3]),
            ({1: 2}, [2, 3]),
            ([1, 2], {2: 3}),
            ({2: 3}, 'notadict'),
            ('notadict', {2: 3}),
        )
    )
    def test_recursive_diff_negative(self, p1, p2):
        with pytest.raises(TypeError, match="Unable to diff"):
            recursive_diff(p1, p2)