def test_set_all_paths(self): test_pelican = PelicanJson(self.item) for path in test_pelican.paths(): set_nested_value(self.item, path, "NEWVALUE") for path in test_pelican.paths(): value = get_nested_value(self.item, path) self.assertEqual(value, "NEWVALUE")
def test_set_nested_value(self): new_path = ['ISBN:9780804720687', 'book_title'] set_nested_value(self.book, new_path, 'Between Pacific Tides') self.assertEqual(get_nested_value(self.book, new_path), 'Between Pacific Tides') set_nested_value(self.pelecanus_occidentalis, ['href'], None) self.assertEqual(self.pelecanus_occidentalis['href'], None)
def edit(self, keys, update_val): """Convenience method to change a particular value inside the `collectiondoc` attribute without setting it directly. To use this method, provide a list of keys and/or indices to get to the value you want to be changed and include the value you would like to overwrite with. Args: `keys` -- list of keys/indices that return the val to be edited `update_val` -- new value Returns: Lowest level object that has been edited or None if not found. """ set_nested_value(self.collectiondoc, keys, update_val) return get_nested_value(self.collectiondoc, keys)
def test_validate_fail_incorrect_value_type(self): profile = new_profile() for path in find_value(profile, 'REQUIRED_VALUE'): set_nested_value(profile, path, 'Fixed!') # Clobber list with new dictionary test_copy = copy.deepcopy(profile) test_copy['links']['alternate'] = {'WRONG': 'Bad Value'} self.assertFalse(validate(test_copy)[0]) # Clobber list with string test_copy = copy.deepcopy(profile) set_nested_value(test_copy, ['links', 'item'], 'Bad Value') self.assertFalse(validate(test_copy)[0]) # Clobber string with new dictionary test_copy = copy.deepcopy(profile) test_copy['attributes']['guid'] = [{'new Key': 'Bad Value'}] self.assertFalse(validate(test_copy)[0])
def test_validate_pass(self): profile = new_profile() for path in find_value(profile, 'REQUIRED_VALUE'): set_nested_value(profile, path, 'Fixed!') self.assertTrue(validate(profile))