def test_write_file(): """Tests writing XML data as file.""" # check path and correct if necessary working_dir = Path.cwd() xml_read_path = working_dir.joinpath(xml_file_paths.get("OEMv2")) if not working_dir.joinpath(xml_read_path).exists(): working_dir = working_dir.joinpath(extra_path) xml_read_path = working_dir.joinpath(xml_file_paths.get("OEMv2")) xml_write_path = working_dir.joinpath(Path("data", "xml", "write_test.xml")) # read XML file as text xml_text = _text_to_list(xml_read_path.read_text()) # read XML file into object and write to file ndm = NdmIo().from_path(xml_read_path) NdmIo().to_file( ndm, NDMFileFormats.XML, xml_write_path, no_namespace_schema_location= "http://cwe.ccsds.org/moims/docs/MOIMS-NAV/Schemas/ndmxml-1.0-master.xsd", ) # read written XML file as text xml_text_out = _text_to_list(xml_write_path.read_text()) # compare strings assert xml_text[4:] == xml_text_out[2:]
def test_write_multi_ndm_kvn(): """Combi-NDM for KVN fail test.""" with pytest.raises(NotImplementedError): # this should throw an NotImplementedError exception # check path and correct if necessary kvn_path = Path.cwd().joinpath(file_paths.get("NDMv2")) if not Path.cwd().joinpath(kvn_path).exists(): kvn_path = Path.cwd().joinpath(extra_path).joinpath( file_paths.get("NDMv2")) ndm = NdmIo().from_path(kvn_path) NdmIo().to_file(ndm, NDMFileFormats.KVN, Path("new.kvn"))
def test_read_string_and_bytes(ndm_key): """Tests reading XML data as string and bytes.""" # check path and correct if necessary xml_path_ndm = Path.cwd().joinpath(xml_file_paths.get(ndm_key)) if not Path.cwd().joinpath(xml_path_ndm).exists(): xml_path_ndm = (Path.cwd().joinpath(extra_path).joinpath( xml_file_paths.get(ndm_key))) # read XML file as text NdmIo().from_string(xml_path_ndm.read_text()) # read XML file as bytes NdmXmlIo().from_bytes(xml_path_ndm.read_bytes()) # read XML file as bytes from top level interface NdmIo().from_bytes(xml_path_ndm.read_bytes())
def test_read_file_against_xml(ndm_key, path): # *** read KVN files *** working_dir = Path.cwd() if path is not None: kvn_path = process_paths(working_dir, path) # read KVN file ndm = NdmKvnIo().from_path(kvn_path) # read equivalent XML file ndm_truth = NdmIo().from_path(kvn_path.with_suffix(".xml")) # export both files to xml and compare xml_text = NdmIo().to_string(ndm, NDMFileFormats.XML) xml_text_truth = NdmIo().to_string(ndm_truth, NDMFileFormats.XML) # compare strings assert xml_text == xml_text_truth
def test_write_kvn_string(): """Tests writing KVN data as string.""" # check path and correct if necessary kvn_path = Path.cwd().joinpath(file_paths.get("OMMv2_1")) if not Path.cwd().joinpath(kvn_path).exists(): kvn_path = Path.cwd().joinpath(extra_path).joinpath( file_paths.get("OMMv2_1")) # read KVN file ndm = NdmIo().from_path(kvn_path) # read equivalent XML file ndm_truth = NdmIo().from_path(kvn_path.with_suffix(".xml")) # export both files to KVN and compare kvn_text = NdmIo().to_string(ndm, NDMFileFormats.KVN) kvn_text_truth = NdmIo().to_string(ndm_truth, NDMFileFormats.KVN) # compare strings assert kvn_text_truth == kvn_text
def test_read_json_file(ndm_key, path): with pytest.raises(NotImplementedError): # *** read files *** # *** should raise an error in case something goes wrong *** if path is not None: json_path = Path.cwd().joinpath(path) if not Path.cwd().joinpath(json_path).exists(): json_path = Path.cwd().joinpath(extra_path).joinpath(path) # this should throw an NotImplementedError exception NdmIo().from_path(json_path)
def test_read_files(ndm_key, path): """Tests reading NDM files.""" # *** read files *** # *** should raise an error in case something goes wrong *** if path is not None: xml_path = Path.cwd().joinpath(path) if not Path.cwd().joinpath(xml_path).exists(): xml_path = Path.cwd().joinpath(extra_path).joinpath(path) # try a string rather than a path NdmIo().from_path(xml_path)
def test_write_json_string(): """Tests writing JSON data as string.""" with pytest.raises(NotImplementedError): # check path and correct if necessary kvn_path = Path.cwd().joinpath(file_paths.get("OMMv2_1")) if not Path.cwd().joinpath(kvn_path).exists(): kvn_path = (Path.cwd().joinpath(extra_path).joinpath( file_paths.get("OMMv2_1"))) # read KVN file ndm = NdmIo().from_path(kvn_path) # read equivalent JSON file ndm_truth = NdmIo().from_path(kvn_path.with_suffix(".xml")) # export both files to JSON and compare kvn_text = NdmIo().to_string(ndm, NDMFileFormats.JSON) kvn_text_truth = NdmIo().to_string(ndm_truth, NDMFileFormats.JSON) # compare strings assert kvn_text_truth == kvn_text
def test_strip_ndm_combi(): """Tests stripping the single instantiation from the NDM Combined Instantiation file.""" # *** read XML file *** path = xml_file_paths.get("NDMv2_strip") xml_path = Path.cwd().joinpath(path) if not Path.cwd().joinpath(xml_path).exists(): xml_path = Path.cwd().joinpath(extra_path).joinpath(path) omm = NdmIo().from_path(xml_path) # End result should be an OMM file, not NDM assert isinstance(omm, Omm)
def test_write_string(): """Tests writing XML data as string.""" # check path and correct if necessary working_dir = Path.cwd() xml_path = working_dir.joinpath(xml_file_paths.get("OEMv2")) if not working_dir.joinpath(xml_path).exists(): working_dir = working_dir.joinpath(extra_path) xml_path = working_dir.joinpath(xml_file_paths.get("OEMv2")) # read XML file as text xml_text = xml_path.read_text() # read XML file into object and write to string ndm = NdmIo().from_path(xml_path) xml_text_out = NdmIo().to_string(ndm, NDMFileFormats.XML) # Prepare texts for comparison (convert lines to list, delete empty items) xml_text = _text_to_list(xml_text) xml_text_out = _text_to_list(xml_text_out) # compare strings assert xml_text[4:] == xml_text_out[2:]
def test_write_data(ndm_key): path = kvn_xml_file_paths[ndm_key] working_dir = Path.cwd() if path is not None: kvn_path = process_paths(working_dir, path) # read KVN file ndm = NdmIo().from_path(kvn_path) # export file to KVN and compare kvn_string = NdmKvnIo().to_string(ndm) kvn_text = _text_to_list(kvn_string) kvn_text_truth = _text_to_list(kvn_path.read_text()) # write to file # kvn_path.with_suffix(".txt").write_text(kvn_string) using_nominal_tests = True if using_nominal_tests: # compare strings - check whether each line matches (delete spaces) match_status = [ " ".join(kvn_text[i]).replace( " ", "") == " ".join(text_truth).replace(" ", "") for i, text_truth in enumerate(kvn_text_truth) ] # Find the non-matching ones no_match_list = [ i for i, match in enumerate(match_status) if not match ] match_decimals_list = _check_detailed_match( no_match_list, kvn_text, kvn_text_truth) # either no_match_list should be empty or try_to_match_list should be # all True. This means that all initial mismatches are checked OK assert not no_match_list or all(match_decimals_list) else: # This enables us to see the differences easily, but will fail for string # differences (e.g "0.000" is not equal to "0.0E-1") assert kvn_text == kvn_text_truth
def test_read_errs(source_data): with pytest.raises(ValueError): # this should throw an ValueError exception NdmIo().from_string(source_data)