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_find_and_replace(self): test_pelican = PelicanJson(self.pelecanus_occidentalis) test_pelican.find_and_replace('Pelecanus occidentalis', 'Brown Pelican') replace_paths = [['query', 'normalized', 0, 'to'], ['query', 'pages', '1266004', 'title']] for path in test_pelican.search_value('Brown Pelican'): self.assertIn(path, replace_paths)
def test_searchvalue(self): test_monty = PelicanJson(self.monterrey) values = list(test_monty.values()) self.assertEqual(len(values), 63) answers = [('gov.noaa.ncdc:C00822', ['results', 7, 'uid']), ('gov.noaa.ncdc:C00040', ['results', 0, 'uid'])] for item, answer in answers: self.assertEqual(next(test_monty.search_value(item)), answer) self.assertEqual(list(test_monty.search_value('2014-08-25')), [['results', 1, 'maxdate'], ['results', 3, 'maxdate']]) npr_api_tag = [['attributes', 'tags', 0], ['items', 0, 'attributes', 'tags', 0]] pelican_item = PelicanJson(self.item) for path in pelican_item.search_value('npr_api'): self.assertIn(path, npr_api_tag)
def test_generate_paths_inside_list(self): test_pelican = PelicanJson(self.item) expected_paths = [] for path in test_pelican.search_value(''): expected_paths.append(path) for path in generate_paths(self.item): value = get_nested_value(self.item, path) if value == '': self.assertIn(path, expected_paths)
def empty_values(data, profile_type="story"): """Function that lists all of the fields missing values. """ 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()) optional = profile['OPTIONAL'] temp_data = PelicanJson(data) return list(temp_data.search_value(optional))
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)