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__*
|
__pycache__*
|
||||||
|
.pytest_cache*
|
||||||
*.pyc
|
*.pyc
|
||||||
.coverage
|
.coverage
|
||||||
build/*
|
build/*
|
||||||
|
|
|
||||||
10
README.rst
10
README.rst
|
|
@ -1,7 +1,7 @@
|
||||||
nested_lookup
|
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
|
:target: https://pypi.python.org/pypi/nested-lookup
|
||||||
|
|
||||||
Make working with JSON, YAML, and XML document responses fun again!
|
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.
|
Returns a `list` of matching values.
|
||||||
|
|
||||||
*nested_update:*
|
*nested_update:*
|
||||||
Given a document, find all occurences of the given key and update the value
|
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.
|
By default, returns a copy of the document.
|
||||||
|
To mutate the original specify the `in_place=True` argument.
|
||||||
|
|
||||||
*nested_delete:*
|
*nested_delete:*
|
||||||
Given a document, find all occurrences of the given key and delete it.
|
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:*
|
*get_all_keys:*
|
||||||
Fetch all keys from a deeply nested dictionary.
|
Fetch all keys from a deeply nested dictionary.
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,10 @@ import copy
|
||||||
from six import iteritems
|
from six import iteritems
|
||||||
|
|
||||||
|
|
||||||
def nested_delete(document, key):
|
def nested_delete(document, key, in_place=False):
|
||||||
duplicate = copy.deepcopy(document)
|
if not in_place:
|
||||||
return _nested_delete(document=duplicate, key=key)
|
document = copy.deepcopy(document)
|
||||||
|
return _nested_delete(document=document, key=key)
|
||||||
|
|
||||||
|
|
||||||
def _nested_delete(document, key):
|
def _nested_delete(document, key):
|
||||||
|
|
|
||||||
9
setup.py
9
setup.py
|
|
@ -20,8 +20,8 @@ with open('requirements.txt', 'r') as f:
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='nested-lookup',
|
name='nested-lookup',
|
||||||
version='0.2.11',
|
version='0.2.12',
|
||||||
description='lookup a key in a deeply nested document of dicts and lists',
|
description='Python functions for working with deeply nested documents (lists and dicts) ',
|
||||||
keywords='nested document dictionary dict list lookup schema json xml yaml',
|
keywords='nested document dictionary dict list lookup schema json xml yaml',
|
||||||
long_description=open('README.rst').read(),
|
long_description=open('README.rst').read(),
|
||||||
|
|
||||||
|
|
@ -40,13 +40,8 @@ setup(
|
||||||
classifiers=[
|
classifiers=[
|
||||||
# Specify the Python versions you support here. In particular, ensure
|
# Specify the Python versions you support here. In particular, ensure
|
||||||
# that you indicate whether you support Python 2, Python 3 or both.
|
# 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.6',
|
||||||
'Programming Language :: Python :: 2.7',
|
'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.5',
|
||||||
'Programming Language :: Python :: 3.6',
|
'Programming Language :: Python :: 3.6',
|
||||||
'Programming Language :: Python :: 3.7',
|
'Programming Language :: Python :: 3.7',
|
||||||
|
|
|
||||||
|
|
@ -103,18 +103,37 @@ class TestNestedUpdate(BaseLookUpApi):
|
||||||
result, nested_update(self.sample_data1, 'build_version', 'Test1')
|
result, nested_update(self.sample_data1, 'build_version', 'Test1')
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_in_place(self):
|
def test_nested_update_in_place_false(self):
|
||||||
result = {
|
"""nested_update should mutate and return a copy of the original document"""
|
||||||
"build_version": "Test1",
|
before_id = id(self.sample_data1)
|
||||||
"os_details": {
|
result = nested_update(self.sample_data1, 'build_version', 'Test2', in_place=False)
|
||||||
"product_version": '10.13.6',
|
after_id = id(result)
|
||||||
"build_version": 'Test1'
|
# the object ids should _not_ match.
|
||||||
},
|
self.assertNotEqual(before_id, after_id)
|
||||||
"name": 'Test',
|
|
||||||
"date": 'YYYY-MM-DD HH:MM:SS'
|
def test_nested_update_in_place_true(self):
|
||||||
}
|
"""nested_update should mutate and return the original document"""
|
||||||
nested_update(self.sample_data1, 'build_version', 'Test1', in_place=True)
|
before_id = id(self.sample_data1)
|
||||||
self.assertEqual(result, 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):
|
def test_sample_data2(self):
|
||||||
result = {
|
result = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue