-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_get_value.py
More file actions
156 lines (131 loc) · 5.44 KB
/
test_get_value.py
File metadata and controls
156 lines (131 loc) · 5.44 KB
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
"""Test extract_data_from_json."""
import pytest
from jdiff import extract_data_from_json
from .utility import ASSERT_FAIL_MESSAGE, load_json_file
test_cases_extract_data_none = [
"global[*]",
'global.peers."1.1.1.1"',
]
@pytest.mark.parametrize("jmspath", test_cases_extract_data_none)
def test_jmspath_return_none(jmspath):
"""Handle exception when JMSPath returns None."""
data = {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}}
with pytest.raises(TypeError) as error:
extract_data_from_json(data=data, path=jmspath)
assert "JMSPath returned 'None'. Please, verify your JMSPath regex." in str(error.value)
test_cases_extract_data_no_ref_key = [
("global.peers.*.*.ipv6.[accepted_prefixes]", [[1000], [1000], [1000], [1000]]),
("vpn.peers.*.*.ipv6.[accepted_prefixes]", [[1000], [1000]]),
(
"*.peers.*.*.*.[accepted_prefixes]",
[[1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000]],
),
]
test_cases_extract_data_with_ref_key = [
(
"global.peers.$*$.*.ipv6.[accepted_prefixes]",
[
{"10.1.0.0": {"accepted_prefixes": 1000}},
{"10.2.0.0": {"accepted_prefixes": 1000}},
{"10.64.207.255": {"accepted_prefixes": 1000}},
{"7.7.7.7": {"accepted_prefixes": 1000}},
],
),
(
"vpn.peers.$*$.*.ipv6.[accepted_prefixes]",
[{"10.1.0.0": {"accepted_prefixes": 1000}}, {"10.2.0.0": {"accepted_prefixes": 1000}}],
),
(
"$*$.peers.$*$.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]",
[
{"global.10.1.0.0": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}},
{"global.10.2.0.0": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}},
{"global.10.64.207.255": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}},
{"global.7.7.7.7": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}},
{"vpn.10.1.0.0": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}},
{"vpn.10.2.0.0": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}},
],
),
(
"$*$.peers.$*$.*.ipv6.[received_prefixes,sent_prefixes]",
[
{"global.10.1.0.0": {"received_prefixes": 1000, "sent_prefixes": 1000}},
{"global.10.2.0.0": {"received_prefixes": 1000, "sent_prefixes": 1000}},
{"global.10.64.207.255": {"received_prefixes": 1000, "sent_prefixes": 1000}},
{"global.7.7.7.7": {"received_prefixes": 1000, "sent_prefixes": 1000}},
{"vpn.10.1.0.0": {"received_prefixes": 1000, "sent_prefixes": 1000}},
{"vpn.10.2.0.0": {"received_prefixes": 1000, "sent_prefixes": 1000}},
],
),
pytest.param(
"$*$.peers.$*$.address_family.$*$.[accepted_prefixes]",
"",
marks=pytest.mark.xfail(reason="Jmespath issue - path returns empty list."),
),
("global.peers.*.is_enabled", [True, True, False, True]),
(
"global.peers.$*$.is_enabled",
[
{"10.1.0.0": {"is_enabled": True}},
{"10.2.0.0": {"is_enabled": True}},
{"10.64.207.255": {"is_enabled": False}},
{"7.7.7.7": {"is_enabled": True}},
],
),
(
"global.peers.$*$.[is_enabled]",
[
{"10.1.0.0": {"is_enabled": True}},
{"10.2.0.0": {"is_enabled": True}},
{"10.64.207.255": {"is_enabled": False}},
{"7.7.7.7": {"is_enabled": True}},
],
),
]
@pytest.mark.parametrize(
"jmspath, expected_value",
test_cases_extract_data_no_ref_key + test_cases_extract_data_with_ref_key,
)
def test_extract_data_from_json(jmspath, expected_value):
"""Test JMSPath return value."""
data = load_json_file("napalm_get_bgp_neighbors", "multi_vrf.json")
value = extract_data_from_json(data=data, path=jmspath)
assert value == expected_value, ASSERT_FAIL_MESSAGE.format(output=value, expected_output=expected_value)
test_cases_top_key_anchor = [
("$*$.is_enabled", [{".local.": {"is_enabled": True}}, {".local..0": {"is_enabled": True}}]),
("$*$.is_up", [{".local.": {"is_up": True}}, {".local..0": {"is_up": True}}]),
]
@pytest.mark.parametrize("jmspath, expected_value", test_cases_top_key_anchor)
def test_top_key_anchor(jmspath, expected_value):
"""Test JMSPath return value for anchoring the top key."""
data = {
".local.": {
"description": "",
"is_enabled": True,
"is_up": True,
"last_flapped": -1,
"mac_address": "Unspecified",
"mtu": 0,
"speed": -1,
},
".local..0": {
"description": "",
"is_enabled": True,
"is_up": True,
"last_flapped": -1,
"mac_address": "Unspecified",
"mtu": 0,
"speed": -1,
},
}
value = extract_data_from_json(data=data, path=jmspath)
assert value == expected_value, ASSERT_FAIL_MESSAGE.format(output=value, expected_output=expected_value)
def test_not_iterable_value():
"""Test JMSPath return value for anchoring the top key."""
data = {
"isBool": True,
}
path = "isBool"
value = extract_data_from_json(data=data, path=path)
expected_output = True
assert value == expected_output