def test_create_path_totally_new_path(self): test_rickettsi = PelicanJson(self.ricketts) path = ['new', 'path', 'in', 1, 'object'] with self.assertRaises(KeyError): test_rickettsi.get_nested_value(path) test_rickettsi.create_path(path, "TEST VALUE") self.assertEqual(test_rickettsi.get_nested_value(path), "TEST VALUE")
def test_set_nested_value_force_add_to_list(self): path = ['attributes', 'tags', 4] test_pelican = PelicanJson(self.item) test_pelican.set_nested_value(path, 'New Tag', force=True) new_tag = test_pelican.get_nested_value(path) self.assertEqual(new_tag, 'New Tag') none_placeholder = ['attributes', 'tags', 3] self.assertEqual(test_pelican.get_nested_value(none_placeholder), None)
def test_get_nested_value(self): pelican_item = PelicanJson(self.item) answer = pelican_item.get_nested_value(['attributes', 'tags', 0]) self.assertEqual(answer, 'npr_api') test_monty = PelicanJson(self.monterrey) answers = [('gov.noaa.ncdc:C00822', ['results', 7, 'uid']), ('gov.noaa.ncdc:C00040', ['results', 0, 'uid'])] for answer, path in answers: self.assertEqual(test_monty.get_nested_value(path), answer) # Testing with a tuple, also should be allowed path = ('results', 9, 'uid') self.assertEqual(test_monty.get_nested_value(path), 'gov.noaa.ncdc:C00313')
def test_create_path_add_item_to_list(self): test_item = PelicanJson(self.item) paths = [['attributes', 'tags', 2], ['attributes', 'tags', 5]] check_for_none = paths[1][:-1] check_for_none.append(4) test_item.create_path(paths[0], "New value inside list") self.assertEqual(test_item.get_nested_value(paths[0]), "New value inside list") test_item.create_path(paths[1], "New value inside list with None") self.assertEqual(test_item.get_nested_value(paths[1]), "New value inside list with None") self.assertEqual(test_item.get_nested_value(check_for_none), None)
def validate(data, profile_type="story"): """Validator for a profile: it will return False if profile contains required fields that have not been filled in. """ # Do we want to return a Boolean or raise an error name, profile_file = profiles[profile_type] current_dir = os.path.abspath(os.path.dirname(__file__)) profile_location = os.path.join(current_dir, 'stored_profiles', profile_file) with open(profile_location, 'r') as f: profile = json.loads(f.read()) required = profile['REQUIRED'] temp_data = PelicanJson(data) try: next(temp_data.search_value(required)) return False except StopIteration: pass # next step is to type-check all the values: pelican_profile = PelicanJson(profile) for path, value in temp_data.enumerate(): try: if type(value) != type(pelican_profile.get_nested_value(path)): return False, path except (TypeError, IndexError, KeyError): return False, path return True, None
def test_create_path_new_object_inside_list(self): test_rickettsi = PelicanJson(self.ricketts) paths = [['query', 'pages', '1422396', 'images', 7, 'title'], ['query', 'normalized', 10, 'NEW']] check_for_none = ['query', 'normalized', 5] test_rickettsi.create_path(paths[0], "VALUE APPENDED TO LIST") test_rickettsi.create_path(paths[1], "VALUE in LIST with BACKFILL") self.assertEqual(test_rickettsi.get_nested_value(paths[0]), "VALUE APPENDED TO LIST") self.assertEqual(test_rickettsi.get_nested_value(paths[1]), "VALUE in LIST with BACKFILL") self.assertEqual(test_rickettsi.get_nested_value(check_for_none), None) self.assertEqual(len(test_rickettsi.get_nested_value(['query', 'normalized'])), 11)
def test_set_nested_value_force_previous_index_error(self): test_rickettsi = PelicanJson(self.ricketts) success_msg = "Should now work" # IndexErrors overridden: paths created index_error_paths = [['query', 'normalized', 1, 'from'], ['query', 'normalized', 1, 'to']] for path in index_error_paths: test_rickettsi.set_nested_value(path, success_msg, force=True) self.assertEqual(test_rickettsi.get_nested_value(path), success_msg)
def wiki_summary_by_name(self): # building link link_asked_wiki = 'https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&redirects=1' \ '&exintro=&explaintext=&titles=' + str(self) wiki_response = PelicanJson(requests.get(link_asked_wiki).json()) # getting a json tree for item in wiki_response.enumerate(): tree_path = item # printing wiki content page return wiki_response.get_nested_value(tree_path[0])
def test_set_nested_value_force_type_error(self): test_rickettsi = PelicanJson(self.ricketts) success_msg = "Should now work" # TypeErrors overridden: path created type_error_path = ['query-continue', 'extlinks', 'eloffset', 'newdict-with-key'] test_rickettsi.set_nested_value(type_error_path, success_msg, force=True) self.assertEqual(test_rickettsi.get_nested_value(type_error_path), success_msg)
def test_create_path_in_dict(self): test_rickettsi = PelicanJson(self.ricketts) overwrite_path = ['query-continue', 'extlinks', 'eloffset', 'newdict-with-key'] msg = "Previous value overwritten with dictionary" test_rickettsi.create_path(overwrite_path, msg) self.assertEqual(test_rickettsi.get_nested_value(overwrite_path), msg) newdict = test_rickettsi.get_nested_value(overwrite_path[:-1]) self.assertTrue(isinstance(newdict, PelicanJson)) paths = [['query-continue', 'extlinks', 'newkey1'], ['query-continue', 'newkey1', 'newkey2'], ['newkey1', 'newkey2', 'newkey3']] for path in paths: with self.assertRaises(KeyError): test_rickettsi.get_nested_value(path) test_rickettsi.create_path(path, "NEWVALUE") self.assertEqual(test_rickettsi.get_nested_value(path), "NEWVALUE")
def test_set_nested_value_force_key_error(self): test_rickettsi = PelicanJson(self.ricketts) success_msg = "Should now work" # KeyErrors overidden key_error_paths = [['unknownKey', 'unknownKey2'], ['query-continue', 'unknownKey']] for path in key_error_paths: test_rickettsi.set_nested_value(path, success_msg, force=True) self.assertEqual(test_rickettsi.get_nested_value(path), success_msg)
def test_get_nested_value_raises_bad_path(self): test_pelican = PelicanJson(self.monterrey) # Try a string with self.assertRaises(BadPath): test_pelican.get_nested_value("STRING") # Howsbout a dict? somedict = {'results': 'value', '9': 'value2'} with self.assertRaises(BadPath): test_pelican.get_nested_value(somedict) # What happens with an integer? with self.assertRaises(BadPath): test_pelican.get_nested_value(8) # ...and a set? with self.assertRaises(BadPath): test_pelican.get_nested_value({'results', 8})
def test_set_nested_value(self): new_path = ['ISBN:9780804720687', 'book_title'] test_book = PelicanJson(self.book) test_book.set_nested_value(new_path, 'Between Pacific Tides') self.assertEqual(test_book.get_nested_value(new_path), 'Between Pacific Tides') test_pelican = PelicanJson(self.pelecanus_occidentalis) values = [] self.assertEqual(len(list(test_pelican.values())), 5) for path, value in test_pelican.enumerate(): values.append(value) test_pelican.set_nested_value(path, None) self.assertEqual(len(set(test_pelican.values())), 1) for path in test_pelican.search_value(None): test_pelican.set_nested_value(path, values.pop()) self.assertEqual(len(list(test_pelican.values())), 5) pelican_item = PelicanJson(self.item) pelican_item.set_nested_value(['href'], None) self.assertEqual(pelican_item['href'], None)
def general_test_enum_nestedval(self): test_pelican = PelicanJson(self.data) for path, value in test_pelican.enumerate(): retrieved_value = test_pelican.get_nested_value(path) self.assertEqual(retrieved_value, value)
def test_get_nested_value_raises_empty_path(self): test_pelican = PelicanJson(self.monterrey) with self.assertRaises(EmptyPath): test_pelican.get_nested_value([])