def test_reporter_file(tmp_path): r = Reporter() # Path to a temporary file p = tmp_path / 'foo.txt' # File can be added to the Reporter before it is created, because the file # is not read until/unless required k1 = r.add_file(p) # File has the expected key assert k1 == 'file:foo.txt' # Add some contents to the file p.write_text('Hello, world!') # The file's contents can be read through the Reporter assert r.get('file:foo.txt') == 'Hello, world!' # Write the report to file p2 = tmp_path / 'bar.txt' r.write('file:foo.txt', p2) # The Reporter produces the expected output file assert p2.read_text() == 'Hello, world!'
def test_file_formats(test_data_path, tmp_path): r = Reporter() expected = as_quantity(pd.read_csv(test_data_path / 'report-input0.csv', index_col=['i', 'j'])['value'], units='km') # CSV file is automatically parsed to xr.DataArray p1 = test_data_path / 'report-input0.csv' k = r.add_file(p1, units=pint.Unit('km')) assert_qty_equal(r.get(k), expected) # Dimensions can be specified p2 = test_data_path / 'report-input1.csv' k2 = r.add_file(p2, dims=dict(i='i', j_dim='j')) assert_qty_equal(r.get(k), r.get(k2)) # Units are loaded from a column assert r.get(k2).attrs['_unit'] == pint.Unit('km') # Specifying units that do not match file contents → ComputationError r.add_file(p2, key='bad', dims=dict(i='i', j_dim='j'), units='kg') with pytest.raises(ComputationError): r.get('bad') # Write to CSV p3 = tmp_path / 'report-output.csv' r.write(k, p3) # Output is identical to input file, except for order assert (sorted(p1.read_text().split('\n')) == sorted( p3.read_text().split('\n'))) # Write to Excel p4 = tmp_path / 'report-output.xlsx' r.write(k, p4)
def test_reporting_file_formats(test_data_path, tmp_path): r = Reporter() expected = xr.DataArray.from_series( pd.read_csv(test_data_path / 'report-input.csv', index_col=['i', 'j'])['value']) # CSV file is automatically parsed to xr.DataArray p1 = test_data_path / 'report-input.csv' k = r.add_file(p1) assert_xr_equal(r.get(k), expected) # Write to CSV p2 = tmp_path / 'report-output.csv' r.write(k, p2) # Output is identical to input file, except for order assert (sorted(p1.read_text().split('\n')) == sorted( p2.read_text().split('\n'))) # Write to Excel p3 = tmp_path / 'report-output.xlsx' r.write(k, p3)