def test_deleting_single_nested_key(self): base = {'key1': {'key1.1': {'key1.1.1': ['value1']}}} target = ['key1.1.1'] du.recursive_dict_removal(base, target) self.assertNotIn('key1.1.1', base['key1']['key1.1'])
def test_deleting_single_item_top_level_list(self): base = {'key1': ['value1']} target = ['value1'] du.recursive_dict_removal(base, target) self.assertEqual(0, len(base['key1']))
def test_deleting_single_item(self): base = {'key1': {'key1.1': 'value1'}} target = ['value1'] du.recursive_dict_removal(base, target) self.assertEqual('value1', base['key1']['key1.1'])
def test_deleting_single_item_from_list(self): base = {'key1': {'key1.1': ['value1']}} target = ['value1'] du.recursive_dict_removal(base, target) self.assertEqual(0, len(base['key1']['key1.1'])) self.assertNotIn('value1', base['key1']['key1.1'])
def test_deleting_single_item_in_single_level_noop(self): """The function only operates on nested dictionaries""" base = {'key1': 'value1'} target = ['value1'] du.recursive_dict_removal(base, target) self.assertEqual('value1', base['key1'])
def test_deleting_single_item_from_nested_list(self): """The function only operates on the 2nd level dictionary""" base = {'key1': {'key1.1': {'key1.1.1': ['value1']}}} target = ['value1'] du.recursive_dict_removal(base, target) self.assertEqual(1, len(base['key1']['key1.1']['key1.1.1'])) self.assertIn('value1', base['key1']['key1.1']['key1.1.1'])
def remove_inventory_item(remove_item, inventory, filepath=None): """Removes inventory item from the inventory dictionary Writes the changes into the inventory file in filepath if available All container_networks information for containers will be deleted. """ du.recursive_dict_removal(inventory, remove_item) if filepath is not None: inventory_json = json.dumps(inventory, indent=2, separators=(',', ': ')) filesys.save_inventory(inventory_json, filepath)