From 29cc2f6e40648c1b2e4168195734d71f9a53d85c Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Mon, 16 Oct 2023 08:46:47 -0500 Subject: [PATCH 1/5] Demonstrate that there are no doctests --- tests/test_create.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_create.py b/tests/test_create.py index 64179ed..03e877c 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -182,3 +182,4 @@ def test_build_doc(string, initial_data, insert_val, target): def test_doctests(): results = doctest.testmod(jsonpath_ng) assert results.failed == 0 + assert results.attempted > 0 From 8ed406ba6d518d704488fa9245f336b51336fc4c Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Mon, 16 Oct 2023 08:47:41 -0500 Subject: [PATCH 2/5] Remove `test_doctests`, which is a no-op --- tests/test_create.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/test_create.py b/tests/test_create.py index 03e877c..fd17635 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -1,9 +1,7 @@ -import doctest from collections import namedtuple import pytest -import jsonpath_ng from jsonpath_ng.ext import parse Params = namedtuple('Params', 'string initial_data insert_val target') @@ -177,9 +175,3 @@ def test_build_doc(string, initial_data, insert_val, target): jsonpath = parse(string) result = jsonpath.update_or_create(initial_data, insert_val) assert result == target - - -def test_doctests(): - results = doctest.testmod(jsonpath_ng) - assert results.failed == 0 - assert results.attempted > 0 From d7cb124352e5305255746221b65a5bd54cc7e0a6 Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Mon, 16 Oct 2023 09:17:29 -0500 Subject: [PATCH 3/5] Reformat `tests/test_create.py` --- tests/test_create.py | 190 ++++++++++++++----------------------------- 1 file changed, 61 insertions(+), 129 deletions(-) diff --git a/tests/test_create.py b/tests/test_create.py index fd17635..200c561 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -1,4 +1,6 @@ +import copy from collections import namedtuple +from contextlib import nullcontext as does_not_raise import pytest @@ -7,137 +9,67 @@ Params = namedtuple('Params', 'string initial_data insert_val target') -@pytest.mark.parametrize('string, initial_data, insert_val, target', [ - - Params(string='$.foo', - initial_data={}, - insert_val=42, - target={'foo': 42}), - - Params(string='$.foo.bar', - initial_data={}, - insert_val=42, - target={'foo': {'bar': 42}}), - - Params(string='$.foo[0]', - initial_data={}, - insert_val=42, - target={'foo': [42]}), - - Params(string='$.foo[1]', - initial_data={}, - insert_val=42, - target={'foo': [{}, 42]}), - - Params(string='$.foo[0].bar', - initial_data={}, - insert_val=42, - target={'foo': [{'bar': 42}]}), - - Params(string='$.foo[1].bar', - initial_data={}, - insert_val=42, - target={'foo': [{}, {'bar': 42}]}), - - Params(string='$.foo[0][0]', - initial_data={}, - insert_val=42, - target={'foo': [[42]]}), - - Params(string='$.foo[1][1]', - initial_data={}, - insert_val=42, - target={'foo': [{}, [{}, 42]]}), - - Params(string='foo[0]', - initial_data={}, - insert_val=42, - target={'foo': [42]}), - - Params(string='foo[1]', - initial_data={}, - insert_val=42, - target={'foo': [{}, 42]}), - - Params(string='foo', - initial_data={}, - insert_val=42, - target={'foo': 42}), - - # Initial data can be a list if we expect a list back - Params(string='[0]', - initial_data=[], - insert_val=42, - target=[42]), - - Params(string='[1]', - initial_data=[], - insert_val=42, - target=[{}, 42]), - - # Converts initial data to a list if necessary - Params(string='[0]', - initial_data={}, - insert_val=42, - target=[42]), - - Params(string='[1]', - initial_data={}, - insert_val=42, - target=[{}, 42]), - - Params(string='foo[?bar="baz"].qux', - initial_data={'foo': [ - {'bar': 'baz'}, - {'bar': 'bizzle'}, - ]}, - insert_val=42, - target={'foo': [ - {'bar': 'baz', 'qux': 42}, - {'bar': 'bizzle'} - ]}), - Params(string='[1].foo', - initial_data=[{'foo': 1}, - {'bar': 2}], - insert_val=42, - target=[{'foo': 1}, - {'foo': 42, - 'bar': 2}],) -]) -def test_update_or_create(string, initial_data, insert_val, target): +@pytest.mark.parametrize( + "string, initial_data, expected_result", + ( + ("$.foo", {}, {"foo": 42}), + ("$.foo.bar", {}, {"foo": {"bar": 42}}), + ("$.foo[0]", {}, {"foo": [42]}), + ("$.foo[1]", {}, {"foo": [{}, 42]}), + ("$.foo[0].bar", {}, {"foo": [{"bar": 42}]}), + ("$.foo[1].bar", {}, {"foo": [{}, {"bar": 42}]}), + ("$.foo[0][0]", {}, {"foo": [[42]]}), + ("$.foo[1][1]", {}, {"foo": [{}, [{}, 42]]}), + ("foo[0]", {}, {"foo": [42]}), + ("foo[1]", {}, {"foo": [{}, 42]}), + ("foo", {}, {"foo": 42}), + # + # Initial data can be a list if we expect a list back. + ("[0]", [], [42]), + ("[1]", [], [{}, 42]), + # + # Convert initial data to a list, if necessary. + ("[0]", {}, [42]), + ("[1]", {}, [{}, 42]), + # + ( + 'foo[?bar="baz"].qux', + { + "foo": [ + {"bar": "baz"}, + {"bar": "bizzle"}, + ] + }, + {"foo": [{"bar": "baz", "qux": 42}, {"bar": "bizzle"}]}, + ), + ("[1].foo", [{"foo": 1}, {"bar": 2}], [{"foo": 1}, {"foo": 42, "bar": 2}]), + ), +) +def test_update_or_create(string, initial_data, expected_result): jsonpath = parse(string) - result = jsonpath.update_or_create(initial_data, insert_val) - assert result == target - - -@pytest.mark.parametrize('string, initial_data, insert_val, target', [ - # Slice not supported - Params(string='foo[0:1]', - initial_data={}, - insert_val=42, - target={'foo': [42, 42]}), - # result is {'foo': {}} - - # Filter does not create items to meet criteria - Params(string='foo[?bar="baz"].qux', - initial_data={}, - insert_val=42, - target={'foo': [{'bar': 'baz', 'qux': 42}]}), - # result is {'foo': {}} - - # Does not convert initial data to a dictionary - Params(string='foo', - initial_data=[], - insert_val=42, - target={'foo': 42}), - # raises TypeError - -]) -@pytest.mark.xfail -def test_unsupported_classes(string, initial_data, insert_val, target): + result = jsonpath.update_or_create(initial_data, 42) + assert result == expected_result + + +@pytest.mark.parametrize( + "string, initial_data, expectation", + ( + # Slice not supported + ("foo[0:1]", {}, does_not_raise()), + # + # Filter does not create items to meet criteria + ('foo[?bar="baz"].qux', {}, does_not_raise()), + # + # Does not convert initial data to a dictionary + ("foo", [], pytest.raises(TypeError)), + ), +) +def test_unsupported_classes(string, initial_data, expectation): + copied_initial_data = copy.copy(initial_data) jsonpath = parse(string) - result = jsonpath.update_or_create(initial_data, insert_val) - assert result == target + with expectation: + result = jsonpath.update_or_create(initial_data, 42) + assert result != copied_initial_data @pytest.mark.parametrize('string, initial_data, insert_val, target', [ From 38e607469f84be125bac7fa3014fd2e3c907a3e2 Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Mon, 16 Oct 2023 09:19:06 -0500 Subject: [PATCH 4/5] Remove tests that don't affect coverage and contribute nothing --- tests/test_create.py | 40 ---------------------------------------- 1 file changed, 40 deletions(-) diff --git a/tests/test_create.py b/tests/test_create.py index 200c561..6dce9e6 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -1,13 +1,10 @@ import copy -from collections import namedtuple from contextlib import nullcontext as does_not_raise import pytest from jsonpath_ng.ext import parse -Params = namedtuple('Params', 'string initial_data insert_val target') - @pytest.mark.parametrize( "string, initial_data, expected_result", @@ -70,40 +67,3 @@ def test_unsupported_classes(string, initial_data, expectation): with expectation: result = jsonpath.update_or_create(initial_data, 42) assert result != copied_initial_data - - -@pytest.mark.parametrize('string, initial_data, insert_val, target', [ - - Params(string='$.name[0].text', - initial_data={}, - insert_val='Sir Michael', - target={'name': [{'text': 'Sir Michael'}]}), - - Params(string='$.name[0].given[0]', - initial_data={'name': [{'text': 'Sir Michael'}]}, - insert_val='Michael', - target={'name': [{'text': 'Sir Michael', - 'given': ['Michael']}]}), - - Params(string='$.name[0].prefix[0]', - initial_data={'name': [{'text': 'Sir Michael', - 'given': ['Michael']}]}, - insert_val='Sir', - target={'name': [{'text': 'Sir Michael', - 'given': ['Michael'], - 'prefix': ['Sir']}]}), - - Params(string='$.birthDate', - initial_data={'name': [{'text': 'Sir Michael', - 'given': ['Michael'], - 'prefix': ['Sir']}]}, - insert_val='1943-05-05', - target={'name': [{'text': 'Sir Michael', - 'given': ['Michael'], - 'prefix': ['Sir']}], - 'birthDate': '1943-05-05'}), -]) -def test_build_doc(string, initial_data, insert_val, target): - jsonpath = parse(string) - result = jsonpath.update_or_create(initial_data, insert_val) - assert result == target From 4eb11602e00434873b892632b55b53c647bf0cd7 Mon Sep 17 00:00:00 2001 From: Kurt McKee Date: Mon, 16 Oct 2023 09:22:03 -0500 Subject: [PATCH 5/5] Include the test suite in coverage reports Several test files have code that isn't getting executed. --- .coveragerc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.coveragerc b/.coveragerc index 1347855..98d6664 100644 --- a/.coveragerc +++ b/.coveragerc @@ -1,3 +1,3 @@ [run] branch = True -source = jsonpath_ng +source = jsonpath_ng, tests