-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_diff_element.py
191 lines (145 loc) · 7.63 KB
/
test_diff_element.py
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""Unit tests for the DiffElement class.
Copyright (c) 2020-2021 Network To Code, LLC <info@networktocode.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from diffsync.diff import DiffElement
def test_diff_element_empty():
"""Test the basic functionality of the DiffElement class when initialized and empty."""
element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
assert element.type == "interface"
assert element.name == "eth0"
assert element.keys == {"device_name": "device1", "name": "eth0"}
assert element.source_name == "source"
assert element.dest_name == "dest"
assert element.source_attrs is None
assert element.dest_attrs is None
assert not list(element.get_children())
assert not element.has_diffs()
assert not element.has_diffs(include_children=True)
assert not element.has_diffs(include_children=False)
assert element.get_attrs_keys() == []
element2 = DiffElement(
"interface", "eth0", {"device_name": "device1", "name": "eth0"}, source_name="S1", dest_name="D1"
)
assert element2.source_name == "S1"
assert element2.dest_name == "D1"
def test_diff_element_summary_with_no_diffs():
element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
assert element.summary() == {"create": 0, "update": 0, "delete": 0, "no-change": 1}
def test_diff_element_str_with_no_diffs():
element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
assert element.str() == "interface: eth0 (no diffs)"
def test_diff_element_dict_with_no_diffs():
element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
assert not element.dict()
def test_diff_element_len_with_no_diffs():
element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
assert len(element) == 1
def test_diff_element_attrs():
"""Test the basic functionality of the DiffElement class when setting and retrieving attrs."""
element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
source_attrs = {"interface_type": "ethernet", "description": "my interface"}
element.add_attrs(source=source_attrs)
assert element.source_attrs == source_attrs
assert element.has_diffs()
assert element.has_diffs(include_children=True)
assert element.has_diffs(include_children=False)
assert element.get_attrs_keys() == source_attrs.keys()
dest_attrs = {"description": "your interface"}
element.add_attrs(dest=dest_attrs)
assert element.source_attrs == source_attrs
assert element.dest_attrs == dest_attrs
assert element.has_diffs()
assert element.has_diffs(include_children=True)
assert element.has_diffs(include_children=False)
assert element.get_attrs_keys() == ["description"] # intersection of source_attrs.keys() and dest_attrs.keys()
def test_diff_element_summary_with_diffs():
element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
element.add_attrs(source={"interface_type": "ethernet", "description": "my interface"})
assert element.summary() == {"create": 1, "update": 0, "delete": 0, "no-change": 0}
element.add_attrs(dest={"description": "your interface"})
assert element.summary() == {"create": 0, "update": 1, "delete": 0, "no-change": 0}
def test_diff_element_str_with_diffs():
element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
element.add_attrs(source={"interface_type": "ethernet", "description": "my interface"})
assert element.str() == "interface: eth0 MISSING in dest"
element.add_attrs(dest={"description": "your interface"})
assert (
element.str()
== """\
interface: eth0
description source(my interface) dest(your interface)\
"""
)
def test_diff_element_dict_with_diffs():
element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
element.add_attrs(source={"interface_type": "ethernet", "description": "my interface"})
assert element.dict() == {"+": {"description": "my interface", "interface_type": "ethernet"}}
element.add_attrs(dest={"description": "your interface"})
assert element.dict() == {"-": {"description": "your interface"}, "+": {"description": "my interface"}}
def test_diff_element_len_with_diffs():
element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
element.add_attrs(source={"interface_type": "ethernet", "description": "my interface"})
element.add_attrs(dest={"description": "your interface"})
assert len(element) == 1
def test_diff_element_dict_with_diffs_no_attrs():
element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
element.add_attrs(source={})
assert element.dict() == {"+": {}}
element.add_attrs(dest={})
assert element.dict() == {"+": {}, "-": {}}
def test_diff_element_children():
"""Test the basic functionality of the DiffElement class when storing and retrieving child elements."""
child_element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"})
parent_element = DiffElement("device", "device1", {"name": "device1"})
parent_element.add_child(child_element)
assert list(parent_element.get_children()) == [child_element]
assert not parent_element.has_diffs()
assert not parent_element.has_diffs(include_children=True)
assert not parent_element.has_diffs(include_children=False)
source_attrs = {"interface_type": "ethernet", "description": "my interface"}
dest_attrs = {"description": "your interface"}
child_element.add_attrs(source=source_attrs, dest=dest_attrs)
assert parent_element.has_diffs()
assert parent_element.has_diffs(include_children=True)
assert not parent_element.has_diffs(include_children=False)
def test_diff_element_summary_with_child_diffs(diff_element_with_children):
# create interface "lo0"
# delete interface "lo1"
# update device "device1" and interface "eth0"
# no change to interface "lo100"
assert diff_element_with_children.summary() == {"create": 1, "update": 2, "delete": 1, "no-change": 1}
def test_diff_element_str_with_child_diffs(diff_element_with_children):
assert (
diff_element_with_children.str()
== """\
device: device1
role source(switch) dest(router)
interface
interface: eth0
description source(my interface) dest(your interface)
interface: lo0 MISSING in dest
interface: lo1 MISSING in source\
"""
)
def test_diff_element_dict_with_child_diffs(diff_element_with_children):
assert diff_element_with_children.dict() == {
"-": {"role": "router"},
"+": {"role": "switch"},
"interface": {
"eth0": {"-": {"description": "your interface"}, "+": {"description": "my interface"}},
"lo0": {"+": {}},
"lo1": {"-": {"description": "Loopback 1"}},
},
}
def test_diff_element_len_with_child_diffs(diff_element_with_children):
assert len(diff_element_with_children) == 5
assert len(diff_element_with_children) == sum(count for count in diff_element_with_children.summary().values())