]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/lib/test/test_json.py
7b3c8593ccb9a9177f0e80178b6dc84f6616f699
5 # Tests for library function: json_cmp().
7 # Copyright (c) 2017 by
8 # Network Device Education Foundation, Inc. ("NetDEF")
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
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
26 Tests for the json_cmp() function.
33 # Save the Current Working Directory to find lib files.
34 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
35 sys
.path
.append(os
.path
.join(CWD
, "../../"))
37 # pylint: disable=C0413
38 from lib
.topotest
import json_cmp
41 def test_json_intersect_true():
42 "Test simple correct JSON intersections"
68 assert json_cmp(dcomplete
, dsub1
) is None
69 assert json_cmp(dcomplete
, dsub2
) is None
70 assert json_cmp(dcomplete
, dsub3
) is None
71 assert json_cmp(dcomplete
, dsub4
) is None
74 def test_json_intersect_false():
75 "Test simple incorrect JSON intersections"
84 # Incorrect value for 'i1'
89 # Non-existing key 'i5'
94 # Key should not exist
99 assert json_cmp(dcomplete
, dsub1
) is not None
100 assert json_cmp(dcomplete
, dsub2
) is not None
101 assert json_cmp(dcomplete
, dsub3
) is not None
104 def test_json_intersect_multilevel_true():
105 "Test multi level correct JSON intersections"
169 assert json_cmp(dcomplete
, dsub1
) is None
170 assert json_cmp(dcomplete
, dsub2
) is None
171 assert json_cmp(dcomplete
, dsub3
) is None
172 assert json_cmp(dcomplete
, dsub4
) is None
173 assert json_cmp(dcomplete
, dsub5
) is None
176 def test_json_intersect_multilevel_false():
177 "Test multi level incorrect JSON intersections"
196 # Incorrect sub-level value
204 # Inexistent sub-level
210 # Inexistent sub-level value
217 # Inexistent sub-sub-level value
228 # Invalid sub-sub-level value
235 "i421": "item420000",
239 # sub-sub-level should be value
249 assert json_cmp(dcomplete
, dsub1
) is not None
250 assert json_cmp(dcomplete
, dsub2
) is not None
251 assert json_cmp(dcomplete
, dsub3
) is not None
252 assert json_cmp(dcomplete
, dsub4
) is not None
253 assert json_cmp(dcomplete
, dsub5
) is not None
254 assert json_cmp(dcomplete
, dsub6
) is not None
257 def test_json_with_list_sucess():
258 "Test successful json comparisons that have lists."
277 # Test list correct list items
286 # Test list correct list size
294 assert json_cmp(dcomplete
, dsub1
) is None
295 assert json_cmp(dcomplete
, dsub2
) is None
296 assert json_cmp(dcomplete
, dsub3
) is None
299 def test_json_with_list_failure():
300 "Test failed json comparisons that have lists."
319 # Test list incorrect list items
328 # Test list correct list size
337 assert json_cmp(dcomplete
, dsub1
) is not None
338 assert json_cmp(dcomplete
, dsub2
) is not None
339 assert json_cmp(dcomplete
, dsub3
) is not None
342 def test_json_list_start_success():
343 "Test JSON encoded data that starts with a list that should succeed."
393 assert json_cmp(dcomplete
, dsub1
) is None
394 assert json_cmp(dcomplete
, dsub2
) is None
395 assert json_cmp(dcomplete
, dsub3
) is None
396 assert json_cmp(dcomplete
, dsub4
) is None
397 assert json_cmp(dcomplete
, dsub5
) is None
400 def test_json_list_start_failure():
401 "Test JSON encoded data that starts with a list that should fail."
404 {"id": 100, "value": "abc"},
405 {"id": 200, "value": "abcd"},
406 {"id": 300, "value": "abcde"},
448 assert json_cmp(dcomplete
, dsub1
) is not None
449 assert json_cmp(dcomplete
, dsub2
) is not None
450 assert json_cmp(dcomplete
, dsub3
) is not None
451 assert json_cmp(dcomplete
, dsub4
) is not None
454 def test_json_list_ordered():
455 "Test JSON encoded data that should be ordered using the '__ordered__' tag."
458 {"id": 1, "value": "abc"},
466 {"id": 1, "value": "abc"},
470 assert json_cmp(dcomplete
, dsub1
) is not None
473 def test_json_list_exact_matching():
474 "Test JSON array on exact matching using the 'exact' parameter."
477 {"id": 1, "value": "abc"},
485 {"id": 1, "value": "abc"},
498 {"id": 1, "value": "abc"},
504 assert json_cmp(dcomplete
, dsub1
, exact
=True) is not None
505 assert json_cmp(dcomplete
, dsub2
, exact
=True) is not None
508 def test_json_object_exact_matching():
509 "Test JSON object on exact matching using the 'exact' parameter."
512 "a": {"id": 1, "value": "abc"},
519 "a": {"id": 1, "value": "abc"},
532 "a": {"id": 1, "value": "abc"},
538 assert json_cmp(dcomplete
, dsub1
, exact
=True) is not None
539 assert json_cmp(dcomplete
, dsub2
, exact
=True) is not None
540 assert json_cmp(dcomplete
, dsub3
, exact
=True) is not None
543 def test_json_list_asterisk_matching():
544 "Test JSON array elements on matching '*' as a placeholder for arbitrary data."
547 {"id": 1, "value": "abc"},
561 {"id": "*", "value": "abc"},
568 {"id": 1, "value": "abc"},
581 assert json_cmp(dcomplete
, dsub1
) is None
582 assert json_cmp(dcomplete
, dsub2
) is None
583 assert json_cmp(dcomplete
, dsub3
) is None
584 assert json_cmp(dcomplete
, dsub4
) is None
587 def test_json_object_asterisk_matching():
588 "Test JSON object value elements on matching '*' as a placeholder for arbitrary data."
591 "a": {"id": 1, "value": "abc"},
605 "a": {"id": 1, "value": "abc"},
612 "a": {"id": "*", "value": "abc"},
625 assert json_cmp(dcomplete
, dsub1
) is None
626 assert json_cmp(dcomplete
, dsub2
) is None
627 assert json_cmp(dcomplete
, dsub3
) is None
628 assert json_cmp(dcomplete
, dsub4
) is None
631 def test_json_list_nested_with_objects():
633 dcomplete
= [{"key": 1, "list": [123]}, {"key": 2, "list": [123]}]
635 dsub1
= [{"key": 2, "list": [123]}, {"key": 1, "list": [123]}]
637 assert json_cmp(dcomplete
, dsub1
) is None
640 if __name__
== "__main__":
641 sys
.exit(pytest
.main())