]> git.proxmox.com Git - mirror_frr.git/blame - tests/topotests/lib/test/test_json.py
topotest: implement json_cmp function
[mirror_frr.git] / tests / topotests / lib / test / test_json.py
CommitLineData
09e21b44
RZ
1#!/usr/bin/env python
2
3#
4# test_json.py
5# Tests for library function: json_cmp().
6#
7# Copyright (c) 2017 by
8# Network Device Education Foundation, Inc. ("NetDEF")
9#
10# Permission to use, copy, modify, and/or distribute this software
11# for any purpose with or without fee is hereby granted, provided
12# that the above copyright notice and this permission notice appear
13# in all copies.
14#
15# THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
16# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
18# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
19# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
21# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22# OF THIS SOFTWARE.
23#
24
25"""
26Tests for the json_cmp() function.
27"""
28
29import os
30import sys
31import pytest
32
33# Save the Current Working Directory to find lib files.
34CWD = os.path.dirname(os.path.realpath(__file__))
35sys.path.append(os.path.join(CWD, '../../'))
36
37# pylint: disable=C0413
38from lib.topotest import json_cmp
39
40def test_json_intersect_true():
41 "Test simple correct JSON intersections"
42
43 dcomplete = {
44 'i1': 'item1',
45 'i2': 'item2',
46 'i3': 'item3',
47 'i100': 'item4',
48 }
49
50 dsub1 = {
51 'i1': 'item1',
52 'i3': 'item3',
53 }
54 dsub2 = {
55 'i1': 'item1',
56 'i2': 'item2',
57 }
58 dsub3 = {
59 'i100': 'item4',
60 'i2': 'item2',
61 }
62 dsub4 = {
63 'i50': None,
64 'i100': 'item4',
65 }
66
67 assert json_cmp(dcomplete, dsub1) is None
68 assert json_cmp(dcomplete, dsub2) is None
69 assert json_cmp(dcomplete, dsub3) is None
70 assert json_cmp(dcomplete, dsub4) is None
71
72def test_json_intersect_false():
73 "Test simple incorrect JSON intersections"
74
75 dcomplete = {
76 'i1': 'item1',
77 'i2': 'item2',
78 'i3': 'item3',
79 'i100': 'item4',
80 }
81
82 # Incorrect value for 'i1'
83 dsub1 = {
84 'i1': 'item3',
85 'i3': 'item3',
86 }
87 # Non-existing key 'i5'
88 dsub2 = {
89 'i1': 'item1',
90 'i5': 'item2',
91 }
92 # Key should not exist
93 dsub3 = {
94 'i100': None,
95 }
96
97 assert json_cmp(dcomplete, dsub1) is not None
98 assert json_cmp(dcomplete, dsub2) is not None
99 assert json_cmp(dcomplete, dsub3) is not None
100
101def test_json_intersect_multilevel_true():
102 "Test multi level correct JSON intersections"
103
104 dcomplete = {
105 'i1': 'item1',
106 'i2': 'item2',
107 'i3': {
108 'i100': 'item100',
109 },
110 'i4': {
111 'i41': {
112 'i411': 'item411',
113 },
114 'i42': {
115 'i421': 'item421',
116 'i422': 'item422',
117 }
118 }
119 }
120
121 dsub1 = {
122 'i1': 'item1',
123 'i3': {
124 'i100': 'item100',
125 },
126 'i10': None,
127 }
128 dsub2 = {
129 'i1': 'item1',
130 'i2': 'item2',
131 'i3': {},
132 }
133 dsub3 = {
134 'i2': 'item2',
135 'i4': {
136 'i41': {
137 'i411': 'item411',
138 },
139 'i42': {
140 'i422': 'item422',
141 'i450': None,
142 }
143 }
144 }
145 dsub4 = {
146 'i2': 'item2',
147 'i4': {
148 'i41': {},
149 'i42': {
150 'i450': None,
151 }
152 }
153 }
154 dsub5 = {
155 'i2': 'item2',
156 'i3': {
157 'i100': 'item100',
158 },
159 'i4': {
160 'i42': {
161 'i450': None,
162 }
163 }
164 }
165
166 assert json_cmp(dcomplete, dsub1) is None
167 assert json_cmp(dcomplete, dsub2) is None
168 assert json_cmp(dcomplete, dsub3) is None
169 assert json_cmp(dcomplete, dsub4) is None
170 assert json_cmp(dcomplete, dsub5) is None
171
172def test_json_intersect_multilevel_false():
173 "Test multi level incorrect JSON intersections"
174
175 dcomplete = {
176 'i1': 'item1',
177 'i2': 'item2',
178 'i3': {
179 'i100': 'item100',
180 },
181 'i4': {
182 'i41': {
183 'i411': 'item411',
184 },
185 'i42': {
186 'i421': 'item421',
187 'i422': 'item422',
188 }
189 }
190 }
191
192 # Incorrect sub-level value
193 dsub1 = {
194 'i1': 'item1',
195 'i3': {
196 'i100': 'item00',
197 },
198 'i10': None,
199 }
200 # Inexistent sub-level
201 dsub2 = {
202 'i1': 'item1',
203 'i2': 'item2',
204 'i3': None,
205 }
206 # Inexistent sub-level value
207 dsub3 = {
208 'i1': 'item1',
209 'i3': {
210 'i100': None,
211 },
212 }
213 # Inexistent sub-sub-level value
214 dsub4 = {
215 'i4': {
216 'i41': {
217 'i412': 'item412',
218 },
219 'i42': {
220 'i421': 'item421',
221 }
222 }
223 }
224 # Invalid sub-sub-level value
225 dsub5 = {
226 'i4': {
227 'i41': {
228 'i411': 'item411',
229 },
230 'i42': {
231 'i421': 'item420000',
232 }
233 }
234 }
235 # sub-sub-level should be value
236 dsub6 = {
237 'i4': {
238 'i41': {
239 'i411': 'item411',
240 },
241 'i42': 'foobar',
242 }
243 }
244
245 assert json_cmp(dcomplete, dsub1) is not None
246 assert json_cmp(dcomplete, dsub2) is not None
247 assert json_cmp(dcomplete, dsub3) is not None
248 assert json_cmp(dcomplete, dsub4) is not None
249 assert json_cmp(dcomplete, dsub5) is not None
250 assert json_cmp(dcomplete, dsub6) is not None
251
252if __name__ == '__main__':
253 sys.exit(pytest.main())