def test_round_trip(): """ tests that a large Oil object loaded from JSON, then saved as JSON, then reloaded again, results in an equal object Not really much a test at this point, but why not? """ filein = TEST_DATA_DIR / "EC" / "EC00506.json" fileout = OUTPUT_DIR / "temp_oil.json" # read it in: oilin = Oil.from_file(filein) # check that they are not exactly the same # this used to be a test for when we had equal data written in # different order it's not longer valid oilin.to_file(fileout) # file1 = open(filein, encoding="utf-8").read().strip() # file2 = open(fileout, encoding="utf-8").read().strip() # assert not file1 == file2, "output is the same as input -- test not fully valid" # read the new one to an Oil object oilout = Oil.from_file(fileout) assert oilin == oilout
def test_full_round_trip(): """ This test makes sure that it can read a full record, save it as JSON, and then read it back again """ record = ExxonDataReader.read_excel_file( example_dir / "Crude_Oil_HOOPS_Blend_assay_xls.xlsx") assert record[0][0] == "ExxonMobil" oil = ExxonMapper(('HOOPS Blend Example', record)) assert oil.metadata.name == 'HOOPS Blend Example' print(oil.oil_id) filename = example_dir / "ExampleOutput.json" oil.to_file(filename) oil2 = Oil.from_file(filename) for bc2, bc in zip(oil2.sub_samples[0].bulk_composition, oil.sub_samples[0].bulk_composition): assert bc2 == bc assert oil2.sub_samples[0].bulk_composition == oil.sub_samples[ 0].bulk_composition assert oil2.sub_samples[0].industry_properties == oil.sub_samples[ 0].industry_properties assert oil2 == oil
def test_from_file_name(): """ test loading a Oil object directly from a file name """ oil = Oil.from_file(TEST_DATA_DIR / "EC" / "EC02234.json") # maybe it would be better to do more of a test, # but the full loading should be tested elsewhere assert oil.oil_id == "EC02234"
def test_metadata_from_full_record(): oil = Oil.from_file(full_oil_filename) log = oil.metadata.change_log print(log) assert len(log) == 1 assert log[0].name == "Bozo the Clown" assert log[0].date == "2021-04-01" assert log[0].comment == "Any random old thing"
def test_from_file(): """ test loading a Oil object directly from an open file """ oil = Oil.from_file( open(TEST_DATA_DIR / "EC" / "EC02234.json", encoding="utf-8")) # maybe it would be better to do more of a test, # but the full loading should be tested elsewhere assert oil.oil_id == "EC02234"
def get_sparse_oil(): return Oil.from_file(sparse_oil_filename)
def get_full_oil(): return Oil.from_file(full_oil_filename)
def split_biodiesels(base_path): ''' Here is where we split the biodiesel records. ''' logger.info('>> split_biodiesels()...') if base_path.is_dir(): for p in file_list: read_path = Path(base_path).joinpath(*p) if read_path.is_file(): logger.info('') logger.info(f'file: {Path(*p)}') oil_orig = Oil.from_file(read_path) names = split_names(oil_orig.metadata.name) comments = split_comments(oil_orig.metadata.comments) if len(comments) <= 1: # we only found 1 unique comment in the field, replicate it comments = comments * len(oil_orig.sub_samples) # Don't know how this could have happened, but the concatenated # oil name parts can be out of order from the samples # in some cases. ordered_names = [ ([n for n in names if n.endswith(s.metadata.name)] + [None])[0] for s in oil_orig.sub_samples ] # # split the file # for sample, name, comment in zip_longest( oil_orig.sub_samples, ordered_names, comments): sample_id = sample.metadata.sample_id oil_new = copy.deepcopy(oil_orig) oil_new.metadata.name = name oil_new.metadata.comments = comment oil_new.oil_id = make_new_oil_id('EC', sample_id) oil_new.metadata.source_id = sample_id oil_new.sub_samples = SampleList([ s for s in oil_new.sub_samples if s.metadata.sample_id == sample_id ]) product_type = get_product_type(sample) if product_type is not None: oil_new.metadata.product_type = product_type write_path = Path(base_path).joinpath( *p[:-1]) / f'{oil_new.oil_id}.json' logger.info(f'\twrite path: {write_path}') oil_new.to_file(write_path) read_path.unlink() else: sys.stdout.write(f'Error: "{read_path}" is not a file.\n') else: sys.stdout.write(f'Error: "{base_path}" is not a directory.\n')