|
1 |
| -"""Test GitHub issues.""" |
| 1 | +"""Test extract_data_from_json.""" |
2 | 2 | import pytest
|
3 | 3 | from jdiff import extract_data_from_json
|
| 4 | +from .utility import load_json_file, ASSERT_FAIL_MESSAGE |
4 | 5 |
|
5 | 6 |
|
6 |
| -my_data = [{"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}}] |
| 7 | +test_cases_extract_data_none = [ |
| 8 | + "global[*]", |
| 9 | + 'global.peers."1.1.1.1"', |
| 10 | +] |
7 | 11 |
|
8 | 12 |
|
9 |
| -@pytest.mark.parametrize("data", my_data) |
10 |
| -def test_jmspath_return_none(data): |
11 |
| - """Handle exception when JMSPath retunr None.""" |
12 |
| - my_jmspath = "global[*]" |
| 13 | +@pytest.mark.parametrize("jmspath", test_cases_extract_data_none) |
| 14 | +def test_jmspath_return_none(jmspath): |
| 15 | + """Handle exception when JMSPath returns None.""" |
| 16 | + data = {"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}} |
13 | 17 | with pytest.raises(TypeError) as error:
|
14 |
| - extract_data_from_json(data=data, path=my_jmspath)() # pylint: disable=E0110 |
| 18 | + extract_data_from_json(data=data, path=jmspath) |
15 | 19 |
|
16 | 20 | assert "JMSPath returned 'None'. Please, verify your JMSPath regex." in error.value.__str__()
|
| 21 | + |
| 22 | + |
| 23 | +test_cases_extract_data_no_ref_key = [ |
| 24 | + ("global.peers.*.*.ipv6.[accepted_prefixes]", [[1000], [1000], [1000], [1000]]), |
| 25 | + ("vpn.peers.*.*.ipv6.[accepted_prefixes]", [[1000], [1000]]), |
| 26 | + ( |
| 27 | + "*.peers.*.*.*.[accepted_prefixes]", |
| 28 | + [[1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000], [1000]], |
| 29 | + ), |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +test_cases_extract_data_with_ref_key = [ |
| 34 | + ( |
| 35 | + "global.peers.$*$.*.ipv6.[accepted_prefixes]", |
| 36 | + [ |
| 37 | + {"10.1.0.0": {"accepted_prefixes": 1000}}, |
| 38 | + {"10.2.0.0": {"accepted_prefixes": 1000}}, |
| 39 | + {"10.64.207.255": {"accepted_prefixes": 1000}}, |
| 40 | + {"7.7.7.7": {"accepted_prefixes": 1000}}, |
| 41 | + ], |
| 42 | + ), |
| 43 | + ( |
| 44 | + "vpn.peers.$*$.*.ipv6.[accepted_prefixes]", |
| 45 | + [{"10.1.0.0": {"accepted_prefixes": 1000}}, {"10.2.0.0": {"accepted_prefixes": 1000}}], |
| 46 | + ), |
| 47 | + ( |
| 48 | + "$*$.peers.$*$.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]", |
| 49 | + [ |
| 50 | + {"global.10.1.0.0": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}}, |
| 51 | + {"global.10.2.0.0": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}}, |
| 52 | + {"global.10.64.207.255": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}}, |
| 53 | + {"global.7.7.7.7": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}}, |
| 54 | + {"vpn.10.1.0.0": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}}, |
| 55 | + {"vpn.10.2.0.0": {"accepted_prefixes": 1000, "received_prefixes": 1000, "sent_prefixes": 1000}}, |
| 56 | + ], |
| 57 | + ), |
| 58 | + ( |
| 59 | + "$*$.peers.$*$.*.ipv6.[received_prefixes,sent_prefixes]", |
| 60 | + [ |
| 61 | + {"global.10.1.0.0": {"received_prefixes": 1000, "sent_prefixes": 1000}}, |
| 62 | + {"global.10.2.0.0": {"received_prefixes": 1000, "sent_prefixes": 1000}}, |
| 63 | + {"global.10.64.207.255": {"received_prefixes": 1000, "sent_prefixes": 1000}}, |
| 64 | + {"global.7.7.7.7": {"received_prefixes": 1000, "sent_prefixes": 1000}}, |
| 65 | + {"vpn.10.1.0.0": {"received_prefixes": 1000, "sent_prefixes": 1000}}, |
| 66 | + {"vpn.10.2.0.0": {"received_prefixes": 1000, "sent_prefixes": 1000}}, |
| 67 | + ], |
| 68 | + ), |
| 69 | + pytest.param( |
| 70 | + "$*$.peers.$*$.address_family.$*$.[accepted_prefixes]", |
| 71 | + "", |
| 72 | + marks=pytest.mark.xfail(reason="Jmespath issue - path returns empty list."), |
| 73 | + ), |
| 74 | +] |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize( |
| 78 | + "jmspath, expected_value", test_cases_extract_data_no_ref_key + test_cases_extract_data_with_ref_key |
| 79 | +) |
| 80 | +def test_extract_data_from_json(jmspath, expected_value): |
| 81 | + """Test JMSPath return value.""" |
| 82 | + data = load_json_file("napalm_get_bgp_neighbors", "multi_vrf.json") |
| 83 | + value = extract_data_from_json(data=data, path=jmspath) |
| 84 | + |
| 85 | + assert value == expected_value, ASSERT_FAIL_MESSAGE.format(output=value, expected_output=expected_value) |
0 commit comments