8000 ignore coverage files and add fields/index exclude function · testerman77/python-jsonpath-rw@71f9ae5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 71f9ae5

Browse files
committed
ignore coverage files and add fields/index exclude function
1 parent 7ec0556 commit 71f9ae5

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ parser.out
1616
/jsonpath_rw/VERSION
1717

1818
.idea
19+
20+
venv
21+
.eggs
22+
coverage.xml
23+
htmlcov

jsonpath_rw/jsonpath.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def child(self, child):
4646
else:
4747
return Child(self, child)
4848

49+
def delete(self, data):
50+
raise NotImplementedError()
51+
4952
def make_datum(self, value):
5053
if isinstance(value, DatumInContext):
5154
return value
@@ -462,6 +465,12 @@ def update(self, data, val):
462465
data[field] = val
463466
return data
464467

468+
def delete(self, data):
469+
for field in self.reified_fields(DatumInContext.wrap(data)):
470+
if field in data:
471+
del data[field]
472+
return data
473+
465474
def __str__(self):
466475
return ','.join(map(str, self.fields))
467476

@@ -497,6 +506,11 @@ def update(self, data, val):
497506
data[self.index] = val
498507
return data
499508

509+
def delete(self, data):
510+
if data is not None and len(data) > self.index:
511+
del data[self.index]
512+
return data
513+
500514
def __eq__(self, other):
501515
return isinstance(other, Index) and self.index == other.index
502516

tests/test_jsonpath.py

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ class TestJsonPath(unittest.TestCase):
8181

8282
@classmethod
8383
def setup_class(cls):
84-
logging.basicConfig()
84+
logging.basicConfig(format = '%(levelname)s:%(funcName)s:%(message)s',
85+
level = logging.DEBUG)
8586

8687
#
8788
# Check that the data value returned is good
@@ -91,7 +92,7 @@ def check_cases(self, test_cases):
9192
# Also, we coerce iterables, etc, into the desired target type
9293

9394
for string, data, target in test_cases:
94-
print('parse("%s").find(%s) =?= %s' % (string, data, target))
95+
logging.debug('parse("%s").find(%s) =?= %s' % (string, data, target))
9596
result = parse(string).find(data)
9697
if isinstance(target, list):
9798
assert [r.value for r in result] == target
@@ -102,10 +103,12 @@ def check_cases(self, test_cases):
102103

103104
def test_fields_value(self):
104105
jsonpath.auto_id_field = None
105-
self.check_cases([ ('foo', {'foo': 'baz'}, ['baz']),
106-
('foo,baz', {'foo': 1, 'baz': 2}, [1, 2]),
107-
('@foo', {'@foo': 1}, [1]),
108-
('*', {'foo': 1, 'baz': 2}, set([1, 2])) ])
106+
self.check_cases([
107+
('foo', {'foo': 'baz'}, ['baz']),
108+
('foo,baz', {'foo': 1, 'baz': 2}, [1, 2]),
109+
('@foo', {'@foo': 1}, [1]),
110+
('*', {'foo': 1, 'baz': 2}, set([1, 2]))
111+
])
109112

110113
jsonpath.auto_id_field = 'id'
111114
self.check_cases([ ('*', {'foo': 1, 'baz': 2}, set([1, 2, '`this`'])) ])
@@ -182,7 +185,7 @@ def check_paths(self, test_cases):
182185
# Also, we coerce iterables, etc, into the desired target type
183186

184187
for string, data, target in test_cases:
185-
print('parse("%s").find(%s).paths =?= %s' % (string, data, target))
188+
logging.debug('parse("%s").find(%s).paths =?= %s' % (string, data, target))
186189
result = parse(string).find(data)
187190
if isinstance(target, list):
188191
assert [str(r.full_path) for r in result] == target
@@ -294,7 +297,7 @@ def test_descendants_auto_id(self):
294297

295298
def check_update_cases(self, test_cases):
296299
for original, expr_str, value, expected in test_cases:
297-
print('parse(%r).update(%r, %r) =?= %r'
300+
logge 57AE r.debug('parse(%r).update(%r, %r) =?= %r'
298301
% (expr_str, original, value, expected))
299302
expr = parse(expr_str)
300303
actual = expr.update(original, value)
@@ -353,3 +356,30 @@ def test_update_slice(self):
353356
self.check_update_cases([
354357
(['foo', 'bar', 'baz'], '[0:2]', 'test', ['test', 'test', 'baz'])
355358
])
359+
360+
def check_delete_cases(self, test_cases):
361+
for string, original, expected in test_cases:
362+
logging.debug('parse("%s").delete(%s) =?= %s' % (string, original, expected))
363+
actual = parse(string).delete(original)
364+
assert actual == expected
365+
366+
def test_delete_fields(self):
367+
jsonpath.auto_id_field = None
368+
self.check_delete_cases([
369+
('foo', {'foo': 'baz'}, {}),
370+
('foo', {'foo': 1, 'baz': 2}, {'baz': 2}),
371+
('foo,baz', {'foo': 1, 'baz': 2}, {}),
372+
('@foo', {'@foo': 1}, {}),
373+
('@foo', {'@foo': 1, 'baz': 2}, {'baz': 2}),
374+
('*', {'foo': 1, 'baz': 2}, {})
375+
])
376+
377+
def test_delete_index(self):
378+
self.check_delete_cases([
379+
('[0]', [42], []),
380+
('[5]', [42], [42]),
381+
('[2]', [34, 65, 29, 59], [34, 65, 59]),
382+
('[0]', None, None),
383+
('[0]', [], []),
384+
('[0]', ['foo', 'bar', 'baz'], ['bar', 'baz']),
385+
])

0 commit comments

Comments
 (0)
0