Better test to protect against mutation.
modified: .gitignore modified: README.rst modified: nested_lookup/lookup_api.py modified: setup.py modified: test_lookup_api.py
This commit is contained in:
parent
a924829821
commit
ec281f6b3f
5 changed files with 44 additions and 26 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,4 +1,5 @@
|
|||
__pycache__*
|
||||
.pytest_cache*
|
||||
*.pyc
|
||||
.coverage
|
||||
build/*
|
||||
|
|
|
|||
10
README.rst
10
README.rst
|
|
@ -1,7 +1,7 @@
|
|||
nested_lookup
|
||||
#############
|
||||
|
||||
.. image:: https://img.shields.io/badge/pypi-0.2.11-green.svg
|
||||
.. image:: https://img.shields.io/badge/pypi-0.2.12-green.svg
|
||||
:target: https://pypi.python.org/pypi/nested-lookup
|
||||
|
||||
Make working with JSON, YAML, and XML document responses fun again!
|
||||
|
|
@ -14,12 +14,14 @@ A document in this case is a a mixture of Python dictionary and list objects typ
|
|||
Returns a `list` of matching values.
|
||||
|
||||
*nested_update:*
|
||||
Given a document, find all occurences of the given key and update the value
|
||||
By default, returns a copy of the document. To mutate the original one instead - please specify the `in_place=True` argument.
|
||||
Given a document, find all occurences of the given key and update the value.
|
||||
By default, returns a copy of the document.
|
||||
To mutate the original specify the `in_place=True` argument.
|
||||
|
||||
*nested_delete:*
|
||||
Given a document, find all occurrences of the given key and delete it.
|
||||
Returns a copy of the document.
|
||||
By default, returns a copy of the document.
|
||||
To mutate the original specify the `in_place=True` argument.
|
||||
|
||||
*get_all_keys:*
|
||||
Fetch all keys from a deeply nested dictionary.
|
||||
|
|
|
|||
|
|
@ -2,9 +2,10 @@ import copy
|
|||
from six import iteritems
|
||||
|
||||
|
||||
def nested_delete(document, key):
|
||||
duplicate = copy.deepcopy(document)
|
||||
return _nested_delete(document=duplicate, key=key)
|
||||
def nested_delete(document, key, in_place=False):
|
||||
if not in_place:
|
||||
document = copy.deepcopy(document)
|
||||
return _nested_delete(document=document, key=key)
|
||||
|
||||
|
||||
def _nested_delete(document, key):
|
||||
|
|
|
|||
9
setup.py
9
setup.py
|
|
@ -20,8 +20,8 @@ with open('requirements.txt', 'r') as f:
|
|||
|
||||
setup(
|
||||
name='nested-lookup',
|
||||
version='0.2.11',
|
||||
description='lookup a key in a deeply nested document of dicts and lists',
|
||||
version='0.2.12',
|
||||
description='Python functions for working with deeply nested documents (lists and dicts) ',
|
||||
keywords='nested document dictionary dict list lookup schema json xml yaml',
|
||||
long_description=open('README.rst').read(),
|
||||
|
||||
|
|
@ -40,13 +40,8 @@ setup(
|
|||
classifiers=[
|
||||
# Specify the Python versions you support here. In particular, ensure
|
||||
# that you indicate whether you support Python 2, Python 3 or both.
|
||||
'Programming Language :: Python :: 2',
|
||||
'Programming Language :: Python :: 2.6',
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Programming Language :: Python :: 3.2',
|
||||
'Programming Language :: Python :: 3.3',
|
||||
'Programming Language :: Python :: 3.4',
|
||||
'Programming Language :: Python :: 3.5',
|
||||
'Programming Language :: Python :: 3.6',
|
||||
'Programming Language :: Python :: 3.7',
|
||||
|
|
|
|||
|
|
@ -103,18 +103,37 @@ class TestNestedUpdate(BaseLookUpApi):
|
|||
result, nested_update(self.sample_data1, 'build_version', 'Test1')
|
||||
)
|
||||
|
||||
def test_in_place(self):
|
||||
result = {
|
||||
"build_version": "Test1",
|
||||
"os_details": {
|
||||
"product_version": '10.13.6',
|
||||
"build_version": 'Test1'
|
||||
},
|
||||
"name": 'Test',
|
||||
"date": 'YYYY-MM-DD HH:MM:SS'
|
||||
}
|
||||
nested_update(self.sample_data1, 'build_version', 'Test1', in_place=True)
|
||||
self.assertEqual(result, self.sample_data1)
|
||||
def test_nested_update_in_place_false(self):
|
||||
"""nested_update should mutate and return a copy of the original document"""
|
||||
before_id = id(self.sample_data1)
|
||||
result = nested_update(self.sample_data1, 'build_version', 'Test2', in_place=False)
|
||||
after_id = id(result)
|
||||
# the object ids should _not_ match.
|
||||
self.assertNotEqual(before_id, after_id)
|
||||
|
||||
def test_nested_update_in_place_true(self):
|
||||
"""nested_update should mutate and return the original document"""
|
||||
before_id = id(self.sample_data1)
|
||||
result = nested_update(self.sample_data1, 'build_version', 'Test2', in_place=True)
|
||||
after_id = id(result)
|
||||
# the object ids should match.
|
||||
self.assertEqual(before_id, after_id)
|
||||
|
||||
def test_nested_delete_in_place_false(self):
|
||||
"""nested_delete should mutate and return a copy of the original document"""
|
||||
before_id = id(self.sample_data1)
|
||||
result = nested_delete(self.sample_data1, 'build_version', in_place=False)
|
||||
after_id = id(result)
|
||||
# the object ids should _not_ match.
|
||||
self.assertNotEqual(before_id, after_id)
|
||||
|
||||
def test_nested_delete_in_place_true(self):
|
||||
"""nested_delete should mutate and return the original document"""
|
||||
before_id = id(self.sample_data1)
|
||||
result = nested_delete(self.sample_data1, 'build_version', in_place=True)
|
||||
after_id = id(result)
|
||||
# the object ids should match.
|
||||
self.assertEqual(before_id, after_id)
|
||||
|
||||
def test_sample_data2(self):
|
||||
result = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue