def test_json_desc_version_number(self): """ Tests ability to parse a file with heading levels mapped directly to outline level. So Outline Level 1 is Heading Level 1 etc. Also uses JSON syntax which doesn't specify all matching criteria - these should be assigned None in the JSON translation. :return: """ json_specifier_generic_levels = os.path.join( self.test_root, "custom_json_test_descriptors_generic_levels.json") opml_file_name = os.path.join(self.test_root, "custom_json_test_descriptors.opml") root_node_index = 1 expected_number_of_rows = 32 # Read json file into a string (later this will be done within the outline engine). with open(json_specifier_generic_levels, 'r') as f: json_specifier_string = f.read() # Read opml file into an outline outline = Outline.from_opml(opml_file_name) unleashed_outline = UnleashedOutline(outline) node_list = list(unleashed_outline.iter_unleashed_nodes()) root_node = node_list[root_node_index].node() specifier = DataNodeSpecifier.from_json_string(json_specifier_string) extracted_data_nodes = specifier.extract_data_node_dispatch(root_node) self.assertEqual(expected_number_of_rows, len(extracted_data_nodes))
def test_opml_from_json(self, index, key1, key2, non_key1, non_key2, non_key3): """ Data driven test to check that a data node specifier record imported from JSON can be used correctly to parse a data node and get correct results. The intention isn't to do a full test of data node extract functionality but to use sufficiently complex data to provide confidence that the from_json functionality is working. :param index: Index of the node under the data node where the data is to be checked. :param key1: First key field expected to be in the extracted data :param key2: Second key field expected to be in the extracted data :param non_key1: Expected data :param non_key2: Expected data :param non_key3: Expected data :return: """ descriptor = DataNodeSpecifier.from_json_string(serialized_json_specifier_03x) # tag_text_delimiter = tuple(descriptor.dns_structure['header']['tag_delimiters']['text_delimiters']) # tag_note_delimiter = tuple(descriptor.dns_structure['header']['tag_delimiters']['note_delimiters']) # Use descriptor to process a node and check that output results are correct. data_node_index = 31 outline = Outline.from_opml( os.path.join(test_root, 'opml_data_extraction_test_02.opml'), ) unleashed_outline = UnleashedOutline(outline) outline_node_list = unleashed_outline.list_unleashed_nodes() data_node = outline_node_list[data_node_index].node() extracted_data_records = descriptor.extract_data_node_dispatch(data_node) test_record = extracted_data_records[index] self.assertEqual(key1, test_record['key_field_1']) self.assertEqual(key2, test_record['key_field_2']) self.assertEqual(non_key1, test_record['data_field_1']) self.assertEqual(non_key2, test_record['data_field_2']) self.assertEqual(non_key3, test_record['data_field_3'])