summaryrefslogtreecommitdiff
path: root/testenv/tests/yamlconf/test_merger.py
blob: 661a187ed9a6eb1659d9e0e9c7d8daa2320ecf95 (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
154
155
156
157
158
159
160
161
162
163
# ========================================================================== #
#                                                                            #
#    KVMD - The main PiKVM daemon.                                           #
#                                                                            #
#    Copyright (C) 2018-2023  Maxim Devaev <[email protected]>               #
#                                                                            #
#    This program is free software: you can redistribute it and/or modify    #
#    it under the terms of the GNU General Public License as published by    #
#    the Free Software Foundation, either version 3 of the License, or       #
#    (at your option) any later version.                                     #
#                                                                            #
#    This program is distributed in the hope that it will be useful,         #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of          #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
#    GNU General Public License for more details.                            #
#                                                                            #
#    You should have received a copy of the GNU General Public License       #
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.  #
#                                                                            #
# ========================================================================== #


import pytest
from kvmd.yamlconf import merger


# =====
def test_simple_override() -> None:
    base = {"key1": "value1", "key2": "value2"}
    incoming = {"key1": "new_value1"}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": "new_value1", "key2": "value2"}


def test_nested_override() -> None:
    base = {"key1": {"nested_key1": "value1"}, "key2": "value2"}
    incoming = {"key1": {"nested_key1": "new_value1"}}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": {"nested_key1": "new_value1"}, "key2": "value2"}


def test_dest_none() -> None:
    base = None
    incoming = {"key1": "value1"}
    with pytest.raises(ValueError, match="destination cannot be None"):
        merger.yaml_merge(base, incoming)  # type: ignore[arg-type]


def test_src_none_or_empty() -> None:
    base = {"key1": "value1"}
    incoming = None
    merger.yaml_merge(base, incoming)  # type: ignore[arg-type]
    assert base == {"key1": "value1"}

    base = {"key1": "value1"}
    incoming2: dict = {}
    merger.yaml_merge(base, incoming2)
    assert base == {"key1": "value1"}


def test_merged_new_keys() -> None:
    base = {"key1": "value1"}
    incoming = {"key2": "value2"}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": "value1", "key2": "value2"}


def test_dest_not_dict() -> None:
    base = "I'm not a dict"
    incoming = {"key1": "value1"}
    with pytest.raises(TypeError, match="object does not support item assignment"):
        merger.yaml_merge(base, incoming)  # type: ignore[arg-type]


def test_src_not_dict() -> None:
    base = {"key1": "value1"}
    incoming = "I'm not a dict"
    with pytest.raises(TypeError, match="string indices must be integers, not 'str'"):
        merger.yaml_merge(base, incoming)  # type: ignore[arg-type]


def test_nested_lists_overwrite() -> None:
    base = {"key1": [1, 2, 3]}
    incoming = {"key1": ["a", "b", "c"]}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": ["a", "b", "c"]}


def test_same_information_rewrite() -> None:
    base = {"key1": "value1", "key2": "value2"}
    incoming = {"key1": "value1", "key2": "value2"}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": "value1", "key2": "value2"}


def test_deeply_nested_dictionaries() -> None:
    base = {"key1": {"nested_key1": {"deep_nested_key1": "value1"}}, "key2": "value2"}
    incoming = {"key1": {"nested_key1": {"deep_nested_key1": "new_value1"}}}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": {"nested_key1": {"deep_nested_key1": "new_value1"}}, "key2": "value2"}


def test_non_dict_values_in_source() -> None:
    base = {"key1": "value1", "key2": "value2"}
    incoming = {"key1": 123, "key2": ["value3", "value4"]}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": 123, "key2": ["value3", "value4"]}


def test_empty_base() -> None:
    base = {}
    incoming = {"key1": "value1"}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": "value1"}


def test_none_values_in_source() -> None:
    base = {"key1": "value1", "key2": "value2"}
    incoming = {"key1": None, "key2": "new_value2"}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": None, "key2": "new_value2"}


def test_key_not_present_in_incoming() -> None:
    base = {"key1": "value1", "key2": "value2"}
    incoming = {"key3": "value3"}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": "value1", "key2": "value2", "key3": "value3"}


def test_mixed_nested_non_nested_keys() -> None:
    base = {"key1": "value1", "key2": {"nested_key1": "value2"}}
    incoming = {"key1": "new_value1", "key2": {"nested_key1": "new_value2"}}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": "new_value1", "key2": {"nested_key1": "new_value2"}}


def test_additional_nested_keys_in_incoming() -> None:
    base = {"key1": "value1", "key2": {"nested_key1": "value2"}}
    incoming = {"key1": "new_value1", "key2": {"nested_key1": "new_value2", "nested_key2": "value3"}}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": "new_value1", "key2": {"nested_key1": "new_value2", "nested_key2": "value3"}}


def test_override_nested_dict_with_non_dict() -> None:
    base = {"key1": "value1", "key2": {"nested_key1": "value2"}}
    incoming = {"key1": "new_value1", "key2": "new_value2"}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": "new_value1", "key2": "new_value2"}


def test_multiple_value_types() -> None:
    base = {"key1": 1, "key2": True, "key3": [1, 2, 3], "key4": {"nested_key1": "value1"}}
    incoming = {"key1": 2, "key2": False, "key3": [4, 5, 6], "key4": {"nested_key1": "value2"}}
    merger.yaml_merge(base, incoming)
    assert base == {"key1": 2, "key2": False, "key3": [4, 5, 6], "key4": {"nested_key1": "value2"}}


def test_non_string_keys() -> None:
    base: dict = {1: "value1", 2: "value2"}
    incoming: dict = {1: "new_value1", 3: "value3"}
    merger.yaml_merge(base, incoming)
    assert base == {1: "new_value1", 2: "value2", 3: "value3"}