Esempio n. 1
0
def test_load_by_id(dataset):
    ds = load_by_id(dataset.run_id)
    assert dataset.run_id == ds.run_id
    assert dataset.path_to_db == ds.path_to_db
Esempio n. 2
0
def test_load_by_id_for_nonexisting_run_id(non_existing_run_id):
    with pytest.raises(ValueError,
                       match=f'Run with run_id '
                       f'{non_existing_run_id} does not '
                       f'exist in the database'):
        _ = load_by_id(non_existing_run_id)
Esempio n. 3
0
def test_load_by_id_for_none():
    with pytest.raises(ValueError,
                       match='run_id has to be a positive integer, '
                       'not None.'):
        _ = load_by_id(None)
def test_load_from_db_dataset_moved(meas_with_registered_param, DMM, DAC,
                                    tmp_path):
    Station(DAC, DMM)
    with meas_with_registered_param.run(
            dataset_class=DataSetType.DataSetInMem) as datasaver:
        for set_v in np.linspace(0, 25, 10):
            DAC.ch1.set(set_v)
            get_v = DMM.v1()
            datasaver.add_result((DAC.ch1, set_v), (DMM.v1, get_v))

    ds = datasaver.dataset
    ds.add_metadata("foo", "bar")
    ds.export(export_type="netcdf", path=tmp_path)

    ds.add_metadata("metadata_added_after_export", 69)

    export_path = ds.export_info.export_paths["nc"]

    with contextlib.closing(xarray.open_dataset(export_path)) as xr_ds:
        assert xr_ds.attrs["metadata_added_after_export"] == 69

    new_path = str(Path(export_path).parent / "someotherfilename.nc")
    shutil.move(export_path, new_path)

    with pytest.warns(UserWarning,
                      match="Could not load raw data for dataset with guid"):
        loaded_ds = load_by_id(ds.run_id)

    assert isinstance(loaded_ds, DataSetInMem)
    assert loaded_ds.snapshot == ds.snapshot
    assert loaded_ds.export_info == ds.export_info
    assert loaded_ds.metadata == ds.metadata

    assert "foo" in loaded_ds.metadata.keys()
    assert "export_info" in loaded_ds.metadata.keys()
    assert "metadata_added_after_export" in loaded_ds.metadata.keys()

    assert loaded_ds.cache.data() == {}

    with pytest.warns(
            UserWarning,
            match="Could not add metadata to the exported NetCDF file"):
        ds.add_metadata("metadata_added_after_move", 696)

    with contextlib.closing(xarray.open_dataset(new_path)) as new_xr_ds:
        assert new_xr_ds.attrs["metadata_added_after_export"] == 69
        assert "metadata_added_after_move" not in new_xr_ds.attrs

    loaded_ds.set_netcdf_location(new_path)

    assert loaded_ds.cache.data().keys() == ds.cache.data().keys()

    with contextlib.closing(xarray.open_dataset(new_path)) as new_xr_ds:
        assert new_xr_ds.attrs["metadata_added_after_export"] == 69
        assert "metadata_added_after_move" not in new_xr_ds.attrs

    # This should have effect neither on the loaded_ds nor on the netcdf file
    with pytest.warns(
            UserWarning,
            match="Could not add metadata to the exported NetCDF file"):
        ds.add_metadata(
            "metadata_added_to_old_dataset_after_set_new_netcdf_location",
            696977)

    loaded_ds.add_metadata("metadata_added_after_set_new_netcdf_location",
                           6969)

    with contextlib.closing(xarray.open_dataset(new_path)) as new_xr_ds:
        assert new_xr_ds.attrs["metadata_added_after_export"] == 69
        assert "metadata_added_after_move" not in new_xr_ds.attrs
        assert ("metadata_added_to_old_dataset_after_set_new_netcdf_location"
                not in new_xr_ds.attrs)
        assert new_xr_ds.attrs[
            "metadata_added_after_set_new_netcdf_location"] == 6969