import unittest import pathlib import io import icartt # working directory, example files wd = pathlib.Path(__file__).parent fns_pass = (wd / "example_data").glob("*.ict") fns_fail = (wd / "example_data" / "will_fail").glob("*.ict") def compareFiles(fn, strIn, strOut, skiplines=0, nlines=-1): # pragma: no cover strOut.seek(0) strIn.seek(0) content_in = strIn.readlines() content_out = strOut.readlines() strIn.close() strOut.close() if nlines > 0: content_in = content_in[skiplines : (skiplines + nlines)] content_out = content_out[skiplines : (skiplines + nlines)] else: content_in = content_in[skiplines:] content_out = content_out[skiplines:] if not len(content_in) == len(content_out): return False for inline, outline in zip(content_in, content_out): inline = inline.strip().replace(" ", "") outline = outline.strip().replace(" ", "") if not inline == outline: valid_data_line = False # maybe this is a data line in which we only have different number formatting? # compare as floats # try: insteps = [float(x) for x in inline.split(",")] outsteps = [float(x) for x in outline.split(",")] if len(insteps) == len(outsteps): valid_data_line = True for i in range(len(insteps)): valid_data_line = valid_data_line and insteps[i] == outsteps[i] # except: # pass valid_var_line = False # try: insteps = [x.strip() for x in inline.split(",")] outsteps = [x.strip() for x in outline.split(",")] if len(insteps) == 2 and len(outsteps) == 3: valid_var_line = ( insteps[0] == outsteps[0] and insteps[1] == outsteps[1] and insteps[1] == outsteps[2] ) # except: # pass if not valid_data_line and not valid_var_line: print(f"{str(fn)}: line {i:d} differs:") print(f" input: {inline}") print(f" output: {outline}") return False return True class BulkIOTestCase(unittest.TestCase): # TODO: need to test invalid input # def testInvalid(self): # for fn in fns_fail: # with self.subTest(msg=f"Opening invalid file {str(fn)}"): # try: # _ = icartt.Dataset(fn, loadData=True) # except: # TODO: failure tests could be more specific # pass # else: # self.fail('expected to fail') def testOpen(self): for fn in fns_pass: with self.subTest(msg=f"Opening test file {str(fn)}"): ict = icartt.Dataset(fn, loadData=False) self.assertEqual(type(ict), icartt.Dataset) def testReadData(self): for fn in fns_pass: with self.subTest(msg=f"Reading data from test file {str(fn)}"): ict = icartt.Dataset(fn, loadData=True) self.assertEqual(type(ict), icartt.Dataset) def testWriteHeader(self): for fn in fns_pass: with self.subTest(msg=f"Writing header for test file {str(fn)}"): ict = icartt.Dataset(fn, loadData=False) strIn = open(fn) strOut = io.StringIO() ict.writeHeader(f=strOut) self.assertTrue(compareFiles(fn, strIn, strOut, nlines=ict.nHeader)) def testWrite(self): for fn in fns_pass: with self.subTest(msg=f"Writing data for test file {str(fn)}"): ict = icartt.Dataset(fn, loadData=True) strIn = open(fn) strOut = io.StringIO() ict.write(f=strOut) self.assertTrue(compareFiles(fn, strIn, strOut)) if __name__ == "__main__": # pragma: no cover import warnings with warnings.catch_warnings(): warnings.simplefilter("ignore", category=UserWarning) unittest.main()