modified: README.rst

modified:   nested_lookup/__init__.py
	modified:   nested_lookup/lookup_api.py
	modified:   nested_lookup/nested_lookup.py
	modified:   setup.py
	modified:   test_lookup_api.py
	modified:   test_nested_lookup.py
This commit is contained in:
russellballestrini 2019-05-21 10:23:00 -04:00
parent 8cec46c76e
commit 9348af1790
7 changed files with 361 additions and 387 deletions

View file

@ -89,21 +89,21 @@ In this example we can be sure that only int will be passed, in production you s
.. code-block:: python .. code-block:: python
>>> def callback(data): >>> def callback(data):
>>> return data + 10 # add 10 to every taco prize >>> return data + 10 # add 10 to every taco prize
The alter-version only works for scalar input (one dict), if you need to adress a list of dicts, you have to The alter-version only works for scalar input (one dict), if you need to adress a list of dicts, you have to
manually iterate over those and pass them to nested_update one by one manually iterate over those and pass them to nested_update one by one
.. code-block:: python .. code-block:: python
>>> out =[] >>> out =[]
>>> for elem in document: >>> for elem in document:
>>> altered_document = nested_alter(elem,"taco", callback) >>> altered_document = nested_alter(elem,"taco", callback)
>>> out.append(altered_document) >>> out.append(altered_document)
>>> print(out) >>> print(out)
[ { 'taco' : 52 } , { 'salsa' : [ { 'burrito' : { 'taco' : 79 } } ] } ] [ { 'taco' : 52 } , { 'salsa' : [ { 'burrito' : { 'taco' : 79 } } ] } ]
>>> from nested_lookup import get_all_keys >>> from nested_lookup import get_all_keys

View file

@ -1,3 +1,7 @@
from .nested_lookup import nested_lookup, get_all_keys, get_occurrence_of_key,\ from .nested_lookup import (
get_occurrence_of_value nested_lookup,
get_all_keys,
get_occurrence_of_key,
get_occurrence_of_value,
)
from .lookup_api import nested_update, nested_delete, nested_alter from .lookup_api import nested_update, nested_delete, nested_alter

View file

@ -31,9 +31,7 @@ def _nested_delete(document, key):
return document return document
def nested_update( def nested_update(document, key, value, in_place=False, treat_as_element=True):
document, key, value, in_place=False, treat_as_element=True
):
""" """
Method to update a key->value pair in a nested document Method to update a key->value pair in a nested document
Args: Args:
@ -61,8 +59,9 @@ def nested_update(
# from the scalar value # from the scalar value
# check the length of the list and provide it to _nested_update # check the length of the list and provide it to _nested_update
if not treat_as_element and not isinstance(value, list): if not treat_as_element and not isinstance(value, list):
raise Exception('You need to pass value as list if you opt for' + raise Exception(
'this feature') "You need to pass value as list if you opt for" + "this feature"
)
elif treat_as_element: elif treat_as_element:
value = [value] value = [value]
@ -70,13 +69,10 @@ def nested_update(
if not in_place: if not in_place:
document = copy.deepcopy(document) document = copy.deepcopy(document)
return _nested_update(document=document, key=key, value=value, return _nested_update(document=document, key=key, value=value, val_len=val_len)
val_len=val_len)
def _nested_update( def _nested_update(document, key, value, val_len, run=0):
document, key, value, val_len, run=0
):
""" """
Method to update a key->value pair in a nested document Method to update a key->value pair in a nested document
Args: Args:
@ -95,8 +91,9 @@ def _nested_update(
""" """
if isinstance(document, list): if isinstance(document, list):
for list_items in document: for list_items in document:
_nested_update(document=list_items, key=key, value=value, _nested_update(
val_len=val_len, run=run) document=list_items, key=key, value=value, val_len=val_len, run=run
)
elif isinstance(document, dict): elif isinstance(document, dict):
if document.get(key): if document.get(key):
# check if a value with the coresponding index exists and # check if a value with the coresponding index exists and
@ -109,14 +106,20 @@ def _nested_update(
document[key] = val document[key] = val
run = run + 1 run = run + 1
for dict_key, dict_value in iteritems(document): for dict_key, dict_value in iteritems(document):
_nested_update(document=dict_value, key=key, value=value, _nested_update(
val_len=val_len, run=run) document=dict_value, key=key, value=value, val_len=val_len, run=run
)
return document return document
def nested_alter( def nested_alter(
document, key, callback_function=None, function_parameters=None, document,
conversion_function=None, wild_alter=False, in_place=True key,
callback_function=None,
function_parameters=None,
conversion_function=None,
wild_alter=False,
in_place=True,
): ):
""" """
Method to alter all values of the occurences of the key "key". Method to alter all values of the occurences of the key "key".
@ -155,17 +158,20 @@ def nested_alter(
if not in_place: if not in_place:
document = copy.deepcopy(document) document = copy.deepcopy(document)
return _nested_alter(document=document, keys=key, return _nested_alter(
callback_function=callback_function, document=document,
function_parameters=function_parameters, keys=key,
conversion_function=conversion_function, callback_function=callback_function,
wild_alter=wild_alter, function_parameters=function_parameters,
in_place=in_place, key_len=key_len) conversion_function=conversion_function,
wild_alter=wild_alter,
in_place=in_place,
key_len=key_len,
)
def _call_callback( def _call_callback(
value_list, callback_function, function_parameters, value_list, callback_function, function_parameters, conversion_function
conversion_function
): ):
""" """
internal helper to call the callback function internal helper to call the callback function
@ -188,8 +194,14 @@ def _call_callback(
def _nested_alter( def _nested_alter(
document, keys, callback_function, function_parameters, document,
conversion_function, wild_alter, in_place, key_len keys,
callback_function,
function_parameters,
conversion_function,
wild_alter,
in_place,
key_len,
): ):
""" """
""" """
@ -201,17 +213,15 @@ def _nested_alter(
# iterate over all given keys in the list # iterate over all given keys in the list
for key in keys: for key in keys:
# try to find the key: # try to find the key:
findings = nested_lookup( findings = nested_lookup(key, document, with_keys=True, wild=wild_alter)
key, document, with_keys=True, wild=wild_alter
)
for k, v in findings.items(): for k, v in findings.items():
trans_val = _call_callback(v, callback_function, trans_val = _call_callback(
function_parameters, v, callback_function, function_parameters, conversion_function
conversion_function) )
# use the transformed value and apply the update to the key # use the transformed value and apply the update to the key
# (dont treat the lists as elements here) # (dont treat the lists as elements here)
document = nested_update(document, k, trans_val, document = nested_update(
in_place=in_place, document, k, trans_val, in_place=in_place, treat_as_element=False
treat_as_element=False) )
return document return document

View file

@ -7,9 +7,7 @@ def nested_lookup(key, document, wild=False, with_keys=False):
"""Lookup a key in a nested document, return a list of values""" """Lookup a key in a nested document, return a list of values"""
if with_keys: if with_keys:
d = defaultdict(list) d = defaultdict(list)
for k, v in _nested_lookup( for k, v in _nested_lookup(key, document, wild=wild, with_keys=with_keys):
key, document, wild=wild, with_keys=with_keys
):
d[k].append(v) d[k].append(v)
return d return d
return list(_nested_lookup(key, document, wild=wild, with_keys=with_keys)) return list(_nested_lookup(key, document, wild=wild, with_keys=with_keys))
@ -19,9 +17,7 @@ def _nested_lookup(key, document, wild=False, with_keys=False):
"""Lookup a key in a nested document, yield a value""" """Lookup a key in a nested document, yield a value"""
if isinstance(document, list): if isinstance(document, list):
for d in document: for d in document:
for result in _nested_lookup( for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys):
key, d, wild=wild, with_keys=with_keys
):
yield result yield result
if isinstance(document, dict): if isinstance(document, dict):
@ -32,9 +28,7 @@ def _nested_lookup(key, document, wild=False, with_keys=False):
else: else:
yield v yield v
if isinstance(v, dict): if isinstance(v, dict):
for result in _nested_lookup( for result in _nested_lookup(key, v, wild=wild, with_keys=with_keys):
key, v, wild=wild, with_keys=with_keys
):
yield result yield result
elif isinstance(v, list): elif isinstance(v, list):
for d in v: for d in v:
@ -78,7 +72,7 @@ def get_occurrence_of_key(dictionary, key):
Return: Return:
Number of occurrence (Integer) Number of occurrence (Integer)
""" """
return _get_occurrence(dictionary=dictionary, item='key', keyword=key) return _get_occurrence(dictionary=dictionary, item="key", keyword=key)
def get_occurrence_of_value(dictionary, value): def get_occurrence_of_value(dictionary, value):
@ -91,7 +85,7 @@ def get_occurrence_of_value(dictionary, value):
Return: Return:
Number of occurrence (Integer) Number of occurrence (Integer)
""" """
return _get_occurrence(dictionary=dictionary, item='value', keyword=value) return _get_occurrence(dictionary=dictionary, item="value", keyword=value)
def _get_occurrence(dictionary, item, keyword): def _get_occurrence(dictionary, item, keyword):
@ -108,7 +102,7 @@ def _get_occurrence(dictionary, item, keyword):
occurrence = [0] occurrence = [0]
def recrusion(dictionary): def recrusion(dictionary):
if item == 'key': if item == "key":
if dictionary.get(keyword) is not None: if dictionary.get(keyword) is not None:
occurrence[0] += 1 occurrence[0] += 1
elif keyword in list(dictionary.values()): elif keyword in list(dictionary.values()):
@ -118,10 +112,10 @@ def _get_occurrence(dictionary, item, keyword):
recrusion(dictionary=value) recrusion(dictionary=value)
elif isinstance(value, list): elif isinstance(value, list):
for list_items in value: for list_items in value:
if hasattr(list_items, 'items'): if hasattr(list_items, "items"):
recrusion(dictionary=list_items) recrusion(dictionary=list_items)
elif list_items == keyword: elif list_items == keyword:
occurrence[0] += 1 if item == 'value' else 0 occurrence[0] += 1 if item == "value" else 0
recrusion(dictionary=dictionary) recrusion(dictionary=dictionary)
return occurrence[0] return occurrence[0]

View file

@ -1,54 +1,50 @@
# installation: pip install nested-lookup # installation: pip install nested-lookup
from setuptools import ( from setuptools import setup, find_packages
setup,
find_packages,
)
# get list of requirement strings from requirements.txt # get list of requirement strings from requirements.txt
def remove_whitespace(x): def remove_whitespace(x):
return ''.join(x.split()) return "".join(x.split())
def sanitize(x): def sanitize(x):
return not x.startswith('#') and x != '' return not x.startswith("#") and x != ""
with open('requirements.txt', 'r') as f: with open("requirements.txt", "r") as f:
requires = filter(sanitize, map(remove_whitespace, f.readlines())) requires = filter(sanitize, map(remove_whitespace, f.readlines()))
setup( setup(
name='nested-lookup', name="nested-lookup",
version='0.2.15', version="0.2.15",
description='Python functions for working with deeply nested documents (lists and dicts) ', 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(),
author="Russell Ballestrini",
author='Russell Ballestrini', author_email="russell@ballestrini.net",
author_email='russell@ballestrini.net', url="https://github.com/russellballestrini/nested-lookup",
url='https://github.com/russellballestrini/nested-lookup', platforms=["All"],
license="Public Domain",
platforms=['All'],
license='Public Domain',
packages=find_packages(), packages=find_packages(),
include_package_data=True, include_package_data=True,
install_requires=requires, install_requires=requires,
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.6', "Programming Language :: Python :: 2.6",
'Programming Language :: Python :: 2.7', "Programming Language :: Python :: 2.7",
'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",
], ],
) )
# setup keyword args: http://peak.telecommunity.com/DevCenter/setuptools # setup keyword args: http://peak.telecommunity.com/DevCenter/setuptools
# build package:
# pip install twine
# python setup.py sdist
# built and uploaded to pypi with this: # built and uploaded to pypi with this:
# python setup.py sdist bdist_egg register upload # python setup.py sdist bdist_egg register upload

View file

@ -8,52 +8,53 @@ class BaseLookUpApi(TestCase):
def setUp(self): def setUp(self):
self.sample_data1 = { self.sample_data1 = {
"build_version": { "build_version": {
"model_name": 'MacBook Pro', "model_name": "MacBook Pro",
"build_version": { "build_version": {
"processor_name": 'Intel Core i7', "processor_name": "Intel Core i7",
"processor_speed": '2.7 GHz', "processor_speed": "2.7 GHz",
"core_details": { "core_details": {
"build_version": '4', "build_version": "4",
"l2_cache(per_core)": '256 KB' "l2_cache(per_core)": "256 KB",
} },
}, },
"number_of_cores": '4', "number_of_cores": "4",
"memory": '256 KB' "memory": "256 KB",
}, },
"os_details": { "os_details": {"product_version": "10.13.6", "build_version": "17G65"},
"product_version": '10.13.6', "name": "Test",
"build_version": '17G65' "date": "YYYY-MM-DD HH:MM:SS",
},
"name": 'Test',
"date": 'YYYY-MM-DD HH:MM:SS'
} }
self.sample_data2 = { self.sample_data2 = {
"hardware_details": { "hardware_details": {
"model_name": 'MacBook Pro', "model_name": "MacBook Pro",
"processor_details": [ "processor_details": [
{ {"processor_name": "Intel Core i7", "processor_speed": "2.7 GHz"},
"processor_name": 'Intel Core i7', {"total_number_of_cores": "4", "l2_cache(per_core)": "256 KB"},
"processor_speed": '2.7 GHz'
},
{
"total_number_of_cores": '4',
"l2_cache(per_core)": '256 KB'
}
], ],
"total_number_of_cores": '5', "total_number_of_cores": "5",
"memory": '16 GB' "memory": "16 GB",
} }
} }
self.sample_data3 = { self.sample_data3 = {
"values": [{ "values": [
"checks": [{ {
"monitoring_zones": "checks": [
["mzdfw", "mzfra", "mzhkg", "mziad", {
"mzlon", "mzord", "mzsyd"] "monitoring_zones": [
}] "mzdfw",
}] "mzfra",
"mzhkg",
"mziad",
"mzlon",
"mzord",
"mzsyd",
]
}
]
}
]
} }
self.sample_data4 = { self.sample_data4 = {
@ -70,26 +71,23 @@ class BaseLookUpApi(TestCase):
"brutto": 58.76, "brutto": 58.76,
"netto": 58.76, "netto": 58.76,
"zahlungsrhythmus": "MONATLICH", "zahlungsrhythmus": "MONATLICH",
"plz": 86899 "plz": 86899,
}, },
"beginn": 1512082800000, "beginn": 1512082800000,
"lebenslang": "True", "lebenslang": "True",
"ueberschussverwendung": { "ueberschussverwendung": {
"ueberschussverwendung": "2", "ueberschussverwendung": "2",
"indexoption": "3" "indexoption": "3",
}, },
"deckung": [ "deckung": [
{ {
"typ": "2", "typ": "2",
"art": "1", "art": "1",
"leistung": { "leistung": {"value": 7500242424.0, "einheit": "2"},
"value": 7500242424.0, "leistungsRhythmus": "1",
"einheit": "2"
},
"leistungsRhythmus": "1"
} }
], ],
"zuschlagNachlass": [] "zuschlagNachlass": [],
}, },
{ {
"typ": "1", "typ": "1",
@ -97,79 +95,69 @@ class BaseLookUpApi(TestCase):
"endalter": 85, "endalter": 85,
"brutto": 0.6, "brutto": 0.6,
"netto": 0.6, "netto": 0.6,
"zahlungsrhythmus": "1" "zahlungsrhythmus": "1",
}, },
"zuschlagNachlass": [] "zuschlagNachlass": [],
} },
] ],
} }
class TestNestedDelete(BaseLookUpApi): class TestNestedDelete(BaseLookUpApi):
def test_sample_data1(self): def test_sample_data1(self):
result = { result = {
"os_details": { "os_details": {"product_version": "10.13.6"},
"product_version": '10.13.6' "name": "Test",
}, "date": "YYYY-MM-DD HH:MM:SS",
"name": 'Test',
"date": 'YYYY-MM-DD HH:MM:SS'
} }
self.assertEqual( self.assertEqual(result, nested_delete(self.sample_data1, "build_version"))
result, nested_delete(self.sample_data1, 'build_version')
)
def test_sample_data2(self): def test_sample_data2(self):
result = { result = {
"hardware_details": { "hardware_details": {
"model_name": 'MacBook Pro', "model_name": "MacBook Pro",
"total_number_of_cores": '5', "total_number_of_cores": "5",
"memory": '16 GB' "memory": "16 GB",
} }
} }
self.assertEqual( self.assertEqual(result, nested_delete(self.sample_data2, "processor_details"))
result, nested_delete(self.sample_data2, 'processor_details')
)
def test_sample_data3(self): def test_sample_data3(self):
result = {"values": [{"checks": [{}]}]} result = {"values": [{"checks": [{}]}]}
self.assertEqual( self.assertEqual(result, nested_delete(self.sample_data3, "monitoring_zones"))
result, nested_delete(self.sample_data3, 'monitoring_zones')
)
class TestNestedUpdate(BaseLookUpApi): class TestNestedUpdate(BaseLookUpApi):
def test_sample_data1(self): def test_sample_data1(self):
result = { result = {
"build_version": "Test1", "build_version": "Test1",
"os_details": { "os_details": {"product_version": "10.13.6", "build_version": "Test1"},
"product_version": '10.13.6', "name": "Test",
"build_version": 'Test1' "date": "YYYY-MM-DD HH:MM:SS",
},
"name": 'Test',
"date": 'YYYY-MM-DD HH:MM:SS'
} }
self.assertEqual( self.assertEqual(
result, nested_update(self.sample_data1, 'build_version', 'Test1') result, nested_update(self.sample_data1, "build_version", "Test1")
) )
def test_sample_data1_list_input_treat_list_as_element_true(self): def test_sample_data1_list_input_treat_list_as_element_true(self):
result = { result = {
"build_version": ["Test5", "Test6", "Test7"], "build_version": ["Test5", "Test6", "Test7"],
"os_details": { "os_details": {
"product_version": '10.13.6', "product_version": "10.13.6",
"build_version": ["Test5", "Test6", "Test7"] "build_version": ["Test5", "Test6", "Test7"],
}, },
"name": 'Test', "name": "Test",
"date": 'YYYY-MM-DD HH:MM:SS' "date": "YYYY-MM-DD HH:MM:SS",
} }
self.assertEqual( self.assertEqual(
result, nested_update( result,
nested_update(
self.sample_data1, self.sample_data1,
'build_version', "build_version",
["Test5", "Test6", "Test7"], ["Test5", "Test6", "Test7"],
treat_as_element=True) treat_as_element=True,
),
) )
def test_nested_update_in_place_false(self): def test_nested_update_in_place_false(self):
@ -177,8 +165,9 @@ class TestNestedUpdate(BaseLookUpApi):
ested_update should mutate and return a copy of the original document ested_update should mutate and return a copy of the original document
""" """
before_id = id(self.sample_data1) before_id = id(self.sample_data1)
result = nested_update(self.sample_data1, 'build_version', 'Test2', result = nested_update(
in_place=False) self.sample_data1, "build_version", "Test2", in_place=False
)
after_id = id(result) after_id = id(result)
# the object ids should _not_ match. # the object ids should _not_ match.
self.assertNotEqual(before_id, after_id) self.assertNotEqual(before_id, after_id)
@ -189,7 +178,8 @@ class TestNestedUpdate(BaseLookUpApi):
""" """
before_id = id(self.sample_data1) before_id = id(self.sample_data1)
result = nested_update( result = nested_update(
self.sample_data1, 'build_version', 'Test2', in_place=True) self.sample_data1, "build_version", "Test2", in_place=True
)
after_id = id(result) after_id = id(result)
# the object ids should match. # the object ids should match.
self.assertEqual(before_id, after_id) self.assertEqual(before_id, after_id)
@ -205,7 +195,8 @@ class TestNestedUpdate(BaseLookUpApi):
updated_findings.append(elem + 300) updated_findings.append(elem + 300)
# update those instances with the altered results # update those instances with the altered results
doc_updated = nested_update( doc_updated = nested_update(
doc, "plz", updated_findings, treat_as_element=False) doc, "plz", updated_findings, treat_as_element=False
)
elem1 = doc_updated["plz"] # 85269 elem1 = doc_updated["plz"] # 85269
# 87199 # 87199
elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
@ -223,8 +214,8 @@ class TestNestedUpdate(BaseLookUpApi):
updated_findings.append(elem + 300) updated_findings.append(elem + 300)
# update those instances with the altered results # update those instances with the altered results
doc_updated = nested_update( doc_updated = nested_update(
doc, "plz", updated_findings, in_place=False, doc, "plz", updated_findings, in_place=False, treat_as_element=False
treat_as_element=False) )
elem1 = doc_updated["plz"] elem1 = doc_updated["plz"]
elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
self.assertEqual(elem1, 82569) self.assertEqual(elem1, 82569)
@ -236,7 +227,8 @@ class TestNestedUpdate(BaseLookUpApi):
list_input = [1, 2, 3, 4, 5] list_input = [1, 2, 3, 4, 5]
# update those instances with the altered results # update those instances with the altered results
doc_updated = nested_update( doc_updated = nested_update(
doc, "plz", list_input, in_place=False, treat_as_element=False) doc, "plz", list_input, in_place=False, treat_as_element=False
)
elem1 = doc_updated["plz"] elem1 = doc_updated["plz"]
elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
# should not work without specifying "treat_list_as_element = True" # should not work without specifying "treat_list_as_element = True"
@ -250,7 +242,8 @@ class TestNestedUpdate(BaseLookUpApi):
list_input = [1, 2, 3, 4, 5] list_input = [1, 2, 3, 4, 5]
# update those instances with the altered results # update those instances with the altered results
doc_updated = nested_update( doc_updated = nested_update(
doc, "plz", list_input, in_place=False, treat_as_element=True) doc, "plz", list_input, in_place=False, treat_as_element=True
)
elem1 = doc_updated["plz"] elem1 = doc_updated["plz"]
elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
# should not work without specifying "treat_list_as_element = True" # should not work without specifying "treat_list_as_element = True"
@ -264,7 +257,8 @@ class TestNestedUpdate(BaseLookUpApi):
list_input = [1, 2, 3, 4, 5] list_input = [1, 2, 3, 4, 5]
# update those instances with the altered results # update those instances with the altered results
doc_updated = nested_update( doc_updated = nested_update(
doc, "plz", list_input, in_place=True, treat_as_element=False) doc, "plz", list_input, in_place=True, treat_as_element=False
)
elem1 = doc_updated["plz"] elem1 = doc_updated["plz"]
elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
# should not work without specifying "treat_list_as_element = True" # should not work without specifying "treat_list_as_element = True"
@ -278,7 +272,8 @@ class TestNestedUpdate(BaseLookUpApi):
list_input = [1, 2, 3, 4, 5] list_input = [1, 2, 3, 4, 5]
# update those instances with the altered results # update those instances with the altered results
doc_updated = nested_update( doc_updated = nested_update(
doc, "plz", list_input, in_place=True, treat_as_element=True) doc, "plz", list_input, in_place=True, treat_as_element=True
)
elem1 = doc_updated["plz"] elem1 = doc_updated["plz"]
elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] elem2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
# should not work without specifying "treat_list_as_element = True" # should not work without specifying "treat_list_as_element = True"
@ -291,8 +286,7 @@ class TestNestedUpdate(BaseLookUpApi):
nested_delete should mutate and return a copy of the original document nested_delete should mutate and return a copy of the original document
""" """
before_id = id(self.sample_data1) before_id = id(self.sample_data1)
result = nested_delete( result = nested_delete(self.sample_data1, "build_version", in_place=False)
self.sample_data1, 'build_version', in_place=False)
after_id = id(result) after_id = id(result)
# the object ids should _not_ match. # the object ids should _not_ match.
self.assertNotEqual(before_id, after_id) self.assertNotEqual(before_id, after_id)
@ -300,28 +294,24 @@ class TestNestedUpdate(BaseLookUpApi):
def test_nested_delete_in_place_true(self): def test_nested_delete_in_place_true(self):
"""nested_delete should mutate and return the original document""" """nested_delete should mutate and return the original document"""
before_id = id(self.sample_data1) before_id = id(self.sample_data1)
result = nested_delete( result = nested_delete(self.sample_data1, "build_version", in_place=True)
self.sample_data1, 'build_version', in_place=True)
after_id = id(result) after_id = id(result)
# the object ids should match. # the object ids should match.
self.assertEqual(before_id, after_id) self.assertEqual(before_id, after_id)
def test_nested_update_taco_for_example(self): def test_nested_update_taco_for_example(self):
document = [ document = [{"taco": 42}, {"salsa": [{"burrito": {"taco": 69}}]}]
{'taco': 42},
{'salsa': [{'burrito': {'taco': 69}}]}
]
updated_document = nested_update( updated_document = nested_update(
document, "taco", [100, 200], treat_as_element=False) document, "taco", [100, 200], treat_as_element=False
)
self.assertEqual(updated_document[0]["taco"], 100) self.assertEqual(updated_document[0]["taco"], 100)
# The multi-update version only works for scalar input, # The multi-update version only works for scalar input,
# if you need to adress a list of dicts, you have to # if you need to adress a list of dicts, you have to
# manually iterate over those and pass them to nested_update # manually iterate over those and pass them to nested_update
# one by one # one by one
self.assertNotEqual( self.assertNotEqual(updated_document[1]["salsa"][0]["burrito"]["taco"], 200)
updated_document[1]["salsa"][0]["burrito"]["taco"], 200)
def test_nested_update_raise_error(self): def test_nested_update_raise_error(self):
doc = self.sample_data4 doc = self.sample_data4
@ -330,53 +320,42 @@ class TestNestedUpdate(BaseLookUpApi):
# update those instances with the altered results # update those instances with the altered results
self.assertRaises( self.assertRaises(
Exception, Exception,
nested_update, doc, "plz", list_input, nested_update,
in_place=True, treat_as_element=False doc,
"plz",
list_input,
in_place=True,
treat_as_element=False,
) )
def test_sample_data2(self): def test_sample_data2(self):
result = { result = {
"hardware_details": { "hardware_details": {
"model_name": 'MacBook Pro', "model_name": "MacBook Pro",
"processor_details": { "processor_details": {"test_key1": "test_value1"},
'test_key1': 'test_value1' "total_number_of_cores": "5",
}, "memory": "16 GB",
"total_number_of_cores": '5',
"memory": '16 GB'
} }
} }
self.assertEqual( self.assertEqual(
result, nested_update( result,
self.sample_data2, 'processor_details', nested_update(
{'test_key1': 'test_value1'} self.sample_data2, "processor_details", {"test_key1": "test_value1"}
) ),
) )
def test_sample_data3(self): def test_sample_data3(self):
result = { result = {"values": [{"checks": {"key1": ["value1"], "key2": "value2"}}]}
"values": [{
"checks": {
'key1': ['value1'],
'key2': 'value2'
}
}]
}
self.assertEqual( self.assertEqual(
result, nested_update( result,
self.sample_data3, 'checks', nested_update(
{ self.sample_data3, "checks", {"key1": ["value1"], "key2": "value2"}
'key1': ['value1'], ),
'key2': 'value2'
}
)
) )
def test_sample_data4(self): def test_sample_data4(self):
result = { result = {
"modelversion": { "modelversion": {"key1": ["value1"], "key2": "value2"},
'key1': ['value1'],
'key2': 'value2'
},
"vorgangsID": "1", "vorgangsID": "1",
"versorgungsvorschlagDatum": 1510558834978, "versorgungsvorschlagDatum": 1510558834978,
"eingangsdatum": 1510558834978, "eingangsdatum": 1510558834978,
@ -389,26 +368,23 @@ class TestNestedUpdate(BaseLookUpApi):
"brutto": 58.76, "brutto": 58.76,
"netto": 58.76, "netto": 58.76,
"zahlungsrhythmus": "MONATLICH", "zahlungsrhythmus": "MONATLICH",
"plz": 86899 "plz": 86899,
}, },
"beginn": 1512082800000, "beginn": 1512082800000,
"lebenslang": "True", "lebenslang": "True",
"ueberschussverwendung": { "ueberschussverwendung": {
"ueberschussverwendung": "2", "ueberschussverwendung": "2",
"indexoption": "3" "indexoption": "3",
}, },
"deckung": [ "deckung": [
{ {
"typ": "2", "typ": "2",
"art": "1", "art": "1",
"leistung": { "leistung": {"value": 7500242424.0, "einheit": "2"},
"value": 7500242424.0, "leistungsRhythmus": "1",
"einheit": "2"
},
"leistungsRhythmus": "1"
} }
], ],
"zuschlagNachlass": [] "zuschlagNachlass": [],
}, },
{ {
"typ": "1", "typ": "1",
@ -416,25 +392,23 @@ class TestNestedUpdate(BaseLookUpApi):
"endalter": 85, "endalter": 85,
"brutto": 0.6, "brutto": 0.6,
"netto": 0.6, "netto": 0.6,
"zahlungsrhythmus": "1" "zahlungsrhythmus": "1",
}, },
"zuschlagNachlass": [] "zuschlagNachlass": [],
} },
] ],
} }
self.assertEqual( self.assertEqual(
result, nested_update( result,
self.sample_data4, 'modelversion', nested_update(
{ self.sample_data4,
'key1': ['value1'], "modelversion",
'key2': 'value2' {"key1": ["value1"], "key2": "value2"},
} ),
)
) )
class TestNestedAlter(BaseLookUpApi): class TestNestedAlter(BaseLookUpApi):
def test_nested_alter_in_place_true(self): def test_nested_alter_in_place_true(self):
# callback functions # callback functions
@ -442,7 +416,8 @@ class TestNestedAlter(BaseLookUpApi):
return str(data) + "###" return str(data) + "###"
doc_updated = nested_alter( doc_updated = nested_alter(
self.sample_data4, "vorgangsID", callback, in_place=True) self.sample_data4, "vorgangsID", callback, in_place=True
)
vorgangsid = doc_updated["vorgangsID"] vorgangsid = doc_updated["vorgangsID"]
@ -455,7 +430,8 @@ class TestNestedAlter(BaseLookUpApi):
return str(data) + "###" return str(data) + "###"
doc_updated = nested_alter( doc_updated = nested_alter(
self.sample_data4, "vorgangsID", callback, in_place=False) self.sample_data4, "vorgangsID", callback, in_place=False
)
vorgangsid = doc_updated["vorgangsID"] vorgangsid = doc_updated["vorgangsID"]
@ -470,7 +446,8 @@ class TestNestedAlter(BaseLookUpApi):
return str(data) + "###" return str(data) + "###"
doc_updated = nested_alter( doc_updated = nested_alter(
self.sample_data4, ["plz", "vorgangsID"], callback, in_place=True) self.sample_data4, ["plz", "vorgangsID"], callback, in_place=True
)
plz1 = doc_updated["plz"] plz1 = doc_updated["plz"]
plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
@ -488,12 +465,13 @@ class TestNestedAlter(BaseLookUpApi):
def callback(data, str1, str2): def callback(data, str1, str2):
return str(data) + str1 + str2 return str(data) + str1 + str2
doc_updated = nested_alter(self.sample_data4, [ doc_updated = nested_alter(
"plz", "vorgangsID"], self.sample_data4,
callback, ["plz", "vorgangsID"],
function_parameters=["abc", "def"], callback,
in_place=True function_parameters=["abc", "def"],
) in_place=True,
)
plz1 = doc_updated["plz"] plz1 = doc_updated["plz"]
plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
@ -511,12 +489,13 @@ class TestNestedAlter(BaseLookUpApi):
def callback(data, str1, str2): def callback(data, str1, str2):
return str(data) + str1 + str2 return str(data) + str1 + str2
doc_updated = nested_alter(self.sample_data4, [ doc_updated = nested_alter(
"plz", "vorgangsID"], self.sample_data4,
callback, ["plz", "vorgangsID"],
function_parameters=["abc", "def"], callback,
in_place=False function_parameters=["abc", "def"],
) in_place=False,
)
plz1 = doc_updated["plz"] plz1 = doc_updated["plz"]
plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
@ -535,7 +514,8 @@ class TestNestedAlter(BaseLookUpApi):
return str(data) + "###" return str(data) + "###"
doc_updated = nested_alter( doc_updated = nested_alter(
self.sample_data4, ["plz", "vorgangsID"], callback, in_place=False) self.sample_data4, ["plz", "vorgangsID"], callback, in_place=False
)
plz1 = doc_updated["plz"] plz1 = doc_updated["plz"]
plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"] plz2 = doc_updated["vertragsteile"][0]["beitragsDaten"]["plz"]
@ -548,10 +528,7 @@ class TestNestedAlter(BaseLookUpApi):
self.assertEqual(vorgangsid, "1###") self.assertEqual(vorgangsid, "1###")
def test_nested_alter_taco_for_example(self): def test_nested_alter_taco_for_example(self):
documents = [ documents = [{"taco": 42}, {"salsa": [{"burrito": {"taco": 69}}]}]
{'taco': 42},
{'salsa': [{'burrito': {'taco': 69}}]}
]
# write a callback function which processes a scalar value. # write a callback function which processes a scalar value.
# Be aware about the possible types which can be passed to # Be aware about the possible types which can be passed to
@ -591,26 +568,23 @@ class TestNestedAlter(BaseLookUpApi):
"brutto": 58.76, "brutto": 58.76,
"netto": 58.76, "netto": 58.76,
"zahlungsrhythmus": "MONATLICH", "zahlungsrhythmus": "MONATLICH",
"plz": 86900 "plz": 86900,
}, },
"beginn": 1512082800000, "beginn": 1512082800000,
"lebenslang": "True", "lebenslang": "True",
"ueberschussverwendung": { "ueberschussverwendung": {
"ueberschussverwendung": "2", "ueberschussverwendung": "2",
"indexoption": "3" "indexoption": "3",
}, },
"deckung": [ "deckung": [
{ {
"typ": "2", "typ": "2",
"art": "1", "art": "1",
"leistung": { "leistung": {"value": 7500242424.0, "einheit": "2"},
"value": 7500242424.0, "leistungsRhythmus": "1",
"einheit": "2"
},
"leistungsRhythmus": "1"
} }
], ],
"zuschlagNachlass": [] "zuschlagNachlass": [],
}, },
{ {
"typ": "1", "typ": "1",
@ -618,11 +592,11 @@ class TestNestedAlter(BaseLookUpApi):
"endalter": 85, "endalter": 85,
"brutto": 0.6, "brutto": 0.6,
"netto": 0.6, "netto": 0.6,
"zahlungsrhythmus": "1" "zahlungsrhythmus": "1",
}, },
"zuschlagNachlass": [] "zuschlagNachlass": [],
} },
] ],
} }
# add +1 to all plz # add +1 to all plz
@ -630,6 +604,4 @@ class TestNestedAlter(BaseLookUpApi):
return data + 1 return data + 1
self.maxDiff = None self.maxDiff = None
self.assertEqual( self.assertEqual(result, nested_alter(self.sample_data4, "plz", callback))
result, nested_alter(self.sample_data4, 'plz', callback)
)

View file

@ -1,11 +1,14 @@
from unittest import TestCase from unittest import TestCase
from nested_lookup import nested_lookup, get_all_keys, get_occurrence_of_key,\ from nested_lookup import (
get_occurrence_of_value nested_lookup,
get_all_keys,
get_occurrence_of_key,
get_occurrence_of_value,
)
class TestNestedLookup(TestCase): class TestNestedLookup(TestCase):
def setUp(self): def setUp(self):
self.subject_dict = {"a": 1, "b": {"d": 100}, "c": {"d": 200}} self.subject_dict = {"a": 1, "b": {"d": 100}, "c": {"d": 200}}
self.subject_dict2 = { self.subject_dict2 = {
@ -25,18 +28,15 @@ class TestNestedLookup(TestCase):
"processor_speed": "2.7 GHz", "processor_speed": "2.7 GHz",
"core_details": { "core_details": {
"build_version": "4", "build_version": "4",
"l2_cache(per_core)": "256 KB" "l2_cache(per_core)": "256 KB",
} },
}, },
"number_of_cores": "4", "number_of_cores": "4",
"memory": "256 KB", "memory": "256 KB",
}, },
"os_details": { "os_details": {"product_version": "10.13.6", "build_version": "17G65"},
"product_version": "10.13.6",
"build_version": "17G65"
},
"name": "Test", "name": "Test",
"date": "YYYY-MM-DD HH:MM:SS" "date": "YYYY-MM-DD HH:MM:SS",
} }
def test_nested_lookup(self): def test_nested_lookup(self):
@ -68,8 +68,7 @@ class TestNestedLookup(TestCase):
self.assertSetEqual({100, 200}, set(results)) self.assertSetEqual({100, 200}, set(results))
def test_wild_nested_lookup(self): def test_wild_nested_lookup(self):
results = nested_lookup( results = nested_lookup(key="mail", document=self.subject_dict2, wild=True)
key="mail", document=self.subject_dict2, wild=True)
self.assertEqual(4, len(results)) self.assertEqual(4, len(results))
self.assertIn("test1@example.com", results) self.assertIn("test1@example.com", results)
self.assertIn("test2@example.com", results) self.assertIn("test2@example.com", results)
@ -83,9 +82,8 @@ class TestNestedLookup(TestCase):
self.assertIn("email_address", matches) self.assertIn("email_address", matches)
self.assertIn("secondary_email", matches) self.assertIn("secondary_email", matches)
self.assertIn("EMAIL_RECOVERY", matches) self.assertIn("EMAIL_RECOVERY", matches)
self.assertSetEqual({ self.assertSetEqual(
"test1@example.com", "test4@example.com"}, {"test1@example.com", "test4@example.com"}, set(matches["email_address"])
set(matches["email_address"])
) )
self.assertIn("test2@example.com", matches["secondary_email"]) self.assertIn("test2@example.com", matches["secondary_email"])
@ -96,39 +94,30 @@ class TestNestedLookup(TestCase):
self.assertSetEqual({100, 200}, set(matches["d"])) self.assertSetEqual({100, 200}, set(matches["d"]))
def test_after_key_is_found(self): def test_after_key_is_found(self):
result = nested_lookup( result = nested_lookup(key="build_version", document=self.subject_dict3)
key='build_version', document=self.subject_dict3
)
self.assertEqual(4, len(result)) self.assertEqual(4, len(result))
self.assertIn('4', result) self.assertIn("4", result)
self.assertIn('17G65', result) self.assertIn("17G65", result)
match1 = { match1 = {
'processor_name': 'Intel Core i7', "processor_name": "Intel Core i7",
'processor_speed': '2.7 GHz', "processor_speed": "2.7 GHz",
'core_details': { "core_details": {"build_version": "4", "l2_cache(per_core)": "256 KB"},
'build_version': '4',
'l2_cache(per_core)': '256 KB'
}
} }
self.assertIn(match1, result) self.assertIn(match1, result)
match2 = { match2 = {
'build_version': { "build_version": {
'processor_name': 'Intel Core i7', "processor_name": "Intel Core i7",
'processor_speed': '2.7 GHz', "processor_speed": "2.7 GHz",
'core_details': { "core_details": {"build_version": "4", "l2_cache(per_core)": "256 KB"},
'build_version': '4',
'l2_cache(per_core)': '256 KB'
}
}, },
'memory': '256 KB', "memory": "256 KB",
'model_name': 'MacBook Pro', "model_name": "MacBook Pro",
'number_of_cores': '4' "number_of_cores": "4",
} }
self.assertIn(match2, result) self.assertIn(match2, result)
class TestGetAllKeys(TestCase): class TestGetAllKeys(TestCase):
def setUp(self): def setUp(self):
self.sample1 = { self.sample1 = {
"hardware_details": { "hardware_details": {
@ -144,9 +133,7 @@ class TestGetAllKeys(TestCase):
"total_number_of_cores": "4", "total_number_of_cores": "4",
"memory": "16 GB", "memory": "16 GB",
}, },
"os_details": { "os_details": {"product_version": "10.13.6", "build_version": "17G65"},
"product_version": "10.13.6", "build_version": "17G65"
},
"name": "Test", "name": "Test",
"date": "YYYY-MM-DD HH:MM:SS", "date": "YYYY-MM-DD HH:MM:SS",
} }
@ -171,38 +158,46 @@ class TestGetAllKeys(TestCase):
"hardware_details": { "hardware_details": {
"model_name": "MacBook Pro", "model_name": "MacBook Pro",
"processor_details": [ "processor_details": [
{ {"processor_name": "Intel Core i7", "processor_speed": "2.7 GHz"},
"processor_name": "Intel Core i7", {"total_numberof_cores": "4", "l2_cache(per_core)": "256 KB"},
"processor_speed": "2.7 GHz"
},
{
"total_numberof_cores": "4",
"l2_cache(per_core)": "256 KB"
},
], ],
"total_number_of_cores": "4", "total_number_of_cores": "4",
"memory": "16 GB", "memory": "16 GB",
} }
} }
self.sample4 = { self.sample4 = {
"values": [{ "values": [
"checks": [{ {
"monitoring_zones": "checks": [
["mzdfw", "mzfra", "mzhkg", "mziad", {
"mzlon", "mzord", "mzsyd"] "monitoring_zones": [
}] "mzdfw",
}] "mzfra",
"mzhkg",
"mziad",
"mzlon",
"mzord",
"mzsyd",
]
}
]
}
]
} }
self.sample5 = [{ self.sample5 = [
"listings": [{ {
"name": "title", "listings": [
"postcode": "postcode", {
"full_address": "fulladdress", "name": "title",
"city": "city", "postcode": "postcode",
"lat": "latitude", "full_address": "fulladdress",
"lng": "longitude" "city": "city",
}] "lat": "latitude",
}] "lng": "longitude",
}
]
}
]
def test_sample_data1(self): def test_sample_data1(self):
result = get_all_keys(self.sample1) result = get_all_keys(self.sample1)
@ -244,11 +239,7 @@ class TestGetAllKeys(TestCase):
def test_sample_data4(self): def test_sample_data4(self):
result = get_all_keys(self.sample4) result = get_all_keys(self.sample4)
self.assertEqual(3, len(result)) self.assertEqual(3, len(result))
keys_to_verify = [ keys_to_verify = ["values", "checks", "monitoring_zones"]
"values",
"checks",
"monitoring_zones"
]
for key in keys_to_verify: for key in keys_to_verify:
self.assertIn(key, result) self.assertIn(key, result)
@ -256,8 +247,13 @@ class TestGetAllKeys(TestCase):
result = get_all_keys(self.sample5) result = get_all_keys(self.sample5)
self.assertEqual(7, len(result)) self.assertEqual(7, len(result))
keys_to_verify = [ keys_to_verify = [
'listings', 'name', 'postcode', 'full_address', 'city', "listings",
'lat', 'lng' "name",
"postcode",
"full_address",
"city",
"lat",
"lng",
] ]
for key in keys_to_verify: for key in keys_to_verify:
self.assertIn(key, result) self.assertIn(key, result)
@ -273,30 +269,29 @@ class TestGetOccurrence(TestCase):
"processor_speed": "2.7 GHz", "processor_speed": "2.7 GHz",
"core_details": { "core_details": {
"build_version": "4", "build_version": "4",
"l2_cache(per_core)": "256 KB" "l2_cache(per_core)": "256 KB",
} },
}, },
"number_of_cores": "4", "number_of_cores": "4",
"memory": "256 KB", "memory": "256 KB",
}, },
"os_details": { "os_details": {"product_version": "10.13.6", "build_version": "17G65"},
"product_version": "10.13.6",
"build_version": "17G65"
},
"name": "Test", "name": "Test",
"date": "YYYY-MM-DD HH:MM:SS" "date": "YYYY-MM-DD HH:MM:SS",
} }
self.sample2 = { self.sample2 = {
"hardware_details": { "hardware_details": {
"model_name": "MacBook Pro", "model_name": "MacBook Pro",
"processor_details": [{ "processor_details": [
"processor_name": "4", {
"processor_speed": "2.7 GHz", "processor_name": "4",
"core_details": { "processor_speed": "2.7 GHz",
"total_numberof_cores": "4", "core_details": {
"l2_cache(per_core)": "256 KB" "total_numberof_cores": "4",
"l2_cache(per_core)": "256 KB",
},
} }
}], ],
"total_number_of_cores": "4", "total_number_of_cores": "4",
"memory": "16 GB", "memory": "16 GB",
} }
@ -305,73 +300,76 @@ class TestGetOccurrence(TestCase):
"hardware_details": { "hardware_details": {
"model_name": "MacBook Pro", "model_name": "MacBook Pro",
"processor_details": [ "processor_details": [
{ {"total_number_of_cores": "4", "processor_speed": "2.7 GHz"},
"total_number_of_cores": "4", {"total_number_of_cores": "4", "l2_cache(per_core)": "256 KB"},
"processor_speed": "2.7 GHz",
},
{
"total_number_of_cores": "4",
"l2_cache(per_core)": "256 KB"
}
], ],
"total_number_of_cores": "4", "total_number_of_cores": "4",
"memory": "16 GB", "memory": "16 GB",
} }
} }
self.sample4 = { self.sample4 = {
"values": [{ "values": [
"checks": [{ {
"monitoring_zones": "checks": [
["mzdfw", "mzfra", "mzhkg", "mziad", {
"mzlon", "mzord", "mzsyd"] "monitoring_zones": [
}] "mzdfw",
}] "mzfra",
"mzhkg",
"mziad",
"mzlon",
"mzord",
"mzsyd",
]
}
]
}
]
} }
self.sample5 = { self.sample5 = {
"hardware_details": { "hardware_details": {
"model_name": 'MacBook Pro', "model_name": "MacBook Pro",
"total_number_of_cores": 0, "total_number_of_cores": 0,
"memory": False "memory": False,
} }
} }
def test_sample_data1(self): def test_sample_data1(self):
result = get_occurrence_of_key(self.sample1, 'build_version') result = get_occurrence_of_key(self.sample1, "build_version")
self.assertEqual(4, result) self.assertEqual(4, result)
result = get_occurrence_of_value(self.sample1, '256 KB') result = get_occurrence_of_value(self.sample1, "256 KB")
self.assertEqual(2, result) self.assertEqual(2, result)
def test_sample_data2(self): def test_sample_data2(self):
result = get_occurrence_of_key(self.sample2, 'core_details') result = get_occurrence_of_key(self.sample2, "core_details")
self.assertEqual(1, result) self.assertEqual(1, result)
result = get_occurrence_of_value(self.sample2, '4') result = get_occurrence_of_value(self.sample2, "4")
self.assertEqual(3, result) self.assertEqual(3, result)
def test_sample_data3(self): def test_sample_data3(self):
result = get_occurrence_of_key(self.sample3, 'total_number_of_cores') result = get_occurrence_of_key(self.sample3, "total_number_of_cores")
self.assertEqual(3, result) self.assertEqual(3, result)
result = get_occurrence_of_value(self.sample3, '4') result = get_occurrence_of_value(self.sample3, "4")
self.assertEqual(3, result) self.assertEqual(3, result)
def test_sample_data4(self): def test_sample_data4(self):
result = get_occurrence_of_key(self.sample4, 'checks') result = get_occurrence_of_key(self.sample4, "checks")
self.assertEqual(1, result) self.assertEqual(1, result)
result = get_occurrence_of_value(self.sample4, 'mziad') result = get_occurrence_of_value(self.sample4, "mziad")
self.assertEqual(1, result) self.assertEqual(1, result)
# Add one more value in key "monitoring_zones" and verify # Add one more value in key "monitoring_zones" and verify
self.sample4['values'][0]['checks'][0]['monitoring_zones'].append( self.sample4["values"][0]["checks"][0]["monitoring_zones"].append("mziad")
'mziad') self.assertEqual(2, get_occurrence_of_value(self.sample4, "mziad"))
self.assertEqual(2, get_occurrence_of_value(self.sample4, 'mziad'))
def test_sample_data5(self): def test_sample_data5(self):
self.assertEqual( self.assertEqual(
1, get_occurrence_of_key(self.sample5, 'total_number_of_cores') 1, get_occurrence_of_key(self.sample5, "total_number_of_cores")
) )
self.assertEqual(1, get_occurrence_of_key(self.sample5, 'memory')) self.assertEqual(1, get_occurrence_of_key(self.sample5, "memory"))
# Add key 'memory' and verify # Add key 'memory' and verify
self.sample5['memory'] = 0 self.sample5["memory"] = 0
self.assertEqual(2, get_occurrence_of_key(self.sample5, 'memory')) self.assertEqual(2, get_occurrence_of_key(self.sample5, "memory"))
if __name__ == "__main__": if __name__ == "__main__":