From 65ca81441ab9d9ed20ee965e84d46c2f17577f9b Mon Sep 17 00:00:00 2001 From: Network to Code Date: Mon, 21 Nov 2022 14:48:12 +0100 Subject: [PATCH 1/2] add unittest placeholder --- tests/test_get_value.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_get_value.py b/tests/test_get_value.py index 737a12a..73fe83f 100644 --- a/tests/test_get_value.py +++ b/tests/test_get_value.py @@ -14,3 +14,33 @@ def test_jmspath_return_none(data): extract_data_from_json(data=data, path=my_jmspath)() # pylint: disable=E0110 assert "JMSPath returned 'None'. Please, verify your JMSPath regex." in error.value.__str__() + + +my_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 + } +} + +@pytest.mark.parametrize("data", my_data) +def test_jmspath_dict_of_dicts(data): + """Handle dict of dicts.""" + my_jmspath = "$*$.[is_up, is_enabled]" + result = extract_data_from_json(data=data, path=my_jmspath) # pylint: disable=E0110 + + assert result == {} \ No newline at end of file From a8ead8a5b30232cf8f4abaf86588053516bdc232 Mon Sep 17 00:00:00 2001 From: Network to Code Date: Tue, 22 Nov 2022 12:05:32 +0100 Subject: [PATCH 2/2] draft unittest --- tests/mock/napalm_get_interfaces/post.json | 38 ++++++++++++++++++++++ tests/mock/napalm_get_interfaces/pre.json | 38 ++++++++++++++++++++++ tests/test_get_value.py | 29 ----------------- tests/test_issues.py | 34 +++++++++++++++++++ 4 files changed, 110 insertions(+), 29 deletions(-) create mode 100644 tests/mock/napalm_get_interfaces/post.json create mode 100644 tests/mock/napalm_get_interfaces/pre.json create mode 100644 tests/test_issues.py diff --git a/tests/mock/napalm_get_interfaces/post.json b/tests/mock/napalm_get_interfaces/post.json new file mode 100644 index 0000000..231ca1d --- /dev/null +++ b/tests/mock/napalm_get_interfaces/post.json @@ -0,0 +1,38 @@ +{ + ".local.": { + "description": "", + "is_enabled": false, + "is_up": false, + "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 + }, + ".local..1": { + "description": "", + "is_enabled": true, + "is_up": true, + "last_flapped": -1, + "mac_address": "Unspecified", + "mtu": 0, + "speed": -1 + }, + ".local..2": { + "description": "", + "is_enabled": false, + "is_up": false, + "last_flapped": -1, + "mac_address": "Unspecified", + "mtu": 0, + "speed": -1 + } +} \ No newline at end of file diff --git a/tests/mock/napalm_get_interfaces/pre.json b/tests/mock/napalm_get_interfaces/pre.json new file mode 100644 index 0000000..8f9b273 --- /dev/null +++ b/tests/mock/napalm_get_interfaces/pre.json @@ -0,0 +1,38 @@ +{ + ".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 + }, + ".local..1": { + "description": "", + "is_enabled": true, + "is_up": true, + "last_flapped": -1, + "mac_address": "Unspecified", + "mtu": 0, + "speed": -1 + }, + ".local..2": { + "description": "", + "is_enabled": true, + "is_up": true, + "last_flapped": -1, + "mac_address": "Unspecified", + "mtu": 0, + "speed": -1 + } +} \ No newline at end of file diff --git a/tests/test_get_value.py b/tests/test_get_value.py index 73fe83f..e84db8d 100644 --- a/tests/test_get_value.py +++ b/tests/test_get_value.py @@ -15,32 +15,3 @@ def test_jmspath_return_none(data): assert "JMSPath returned 'None'. Please, verify your JMSPath regex." in error.value.__str__() - -my_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 - } -} - -@pytest.mark.parametrize("data", my_data) -def test_jmspath_dict_of_dicts(data): - """Handle dict of dicts.""" - my_jmspath = "$*$.[is_up, is_enabled]" - result = extract_data_from_json(data=data, path=my_jmspath) # pylint: disable=E0110 - - assert result == {} \ No newline at end of file diff --git a/tests/test_issues.py b/tests/test_issues.py new file mode 100644 index 0000000..01c2ca4 --- /dev/null +++ b/tests/test_issues.py @@ -0,0 +1,34 @@ +"""jdiff github issues unit tests.""" +import pytest +import jdiff +from jdiff import extract_data_from_json, CheckType +from .utility import load_mocks, ASSERT_FAIL_MESSAGE + +issue_91 = ( + "napalm_get_interfaces", + "exact_match", + {}, + "$*$.[is_up,is_enabled]", + ( + {}, + False, + ), +) + +check_tests = [ + issue_91, +] + + +@pytest.mark.parametrize("folder_name, check_type_str, evaluate_args, path, expected_result", check_tests) +def test_checks(folder_name, check_type_str, evaluate_args, path, expected_result): + """Validate multiple checks on the same data to catch corner cases.""" + check = CheckType.create(check_type_str) + pre_data, post_data = load_mocks(folder_name) + pre_value = extract_data_from_json(pre_data, path) + post_value = extract_data_from_json(post_data, path) + actual_results = check.evaluate(pre_value, post_value, **evaluate_args) + + assert actual_results == expected_result, ASSERT_FAIL_MESSAGE.format( + output=actual_results, expected_output=expected_result + )