def test_create_logwriter(mode, tmp_path, calibration_data): d = tmp_path / f"{ mode }.h5" h = LogWriter(store_path=d, calibration_data=calibration_data, mode=mode) assert not d.exists() h.__enter__() assert d.exists() h.__exit__()
def data_h5(tmp_path): store_path = tmp_path / "record_example.h5" with LogWriter(store_path, CalibrationData.from_default()) as store: for i in range(100): len_ = 10_000 fake_data = DataBuffer(random_data(len_), random_data(len_), i) store.write_buffer(fake_data) return store_path
def log_writer(tmp_path): calib = CalibrationData.from_default() with LogWriter( force_overwrite=True, store_path=tmp_path / "test.h5", mode="load", calibration_data=calib, ) as lw: yield lw
def log_writer(tmp_path, mode): calib = CalibrationData.from_default() with LogWriter( mode=mode, calibration_data=calib, force=True, store_path=tmp_path / "test.h5", ) as lw: yield lw
def test_create_logwriter_with_force(tmp_path, calibration_data): d = tmp_path / "harvest.h5" d.touch() stat = d.stat() time.sleep(0.1) h = LogWriter(store_path=d, calibration_data=calibration_data, force_overwrite=False) h.__enter__() h.__exit__() # This should have created the following alternative file: d_altered = tmp_path / "harvest.0.h5" assert h.store_path == d_altered assert d_altered.exists() h = LogWriter(store_path=d, calibration_data=calibration_data, force_overwrite=True) h.__enter__() h.__exit__() new_stat = d.stat() assert new_stat.st_mtime > stat.st_mtime
def test_key_value_store(tmp_path, calibration_data): d = tmp_path / "harvest.h5" with LogWriter(store_path=d, calibration_data=calibration_data) as writer: writer["some string"] = "this is a string" writer["some value"] = 5 with h5py.File(d, "r+") as hf: assert hf.attrs["some value"] == 5 assert hf.attrs["some string"] == "this is a string"
def test_calibration_logging(mode, tmp_path, calibration_data): d = tmp_path / "recording.h5" with LogWriter( store_path=d, mode=mode, calibration_data=calibration_data ) as _: pass h5store = h5py.File(d, "r") # hint: logReader would be more direct, but less untouched for channel_entry, parameter in product(cal_channel_harvest_dict.items(), cal_parameter_list): assert ( h5store["data"][channel_entry[0]].attrs[parameter] == calibration_data[mode][channel_entry[1]][parameter] )
def test_logwriter_data(mode, tmp_path, data_buffer, calibration_data): d = tmp_path / "harvest.h5" with LogWriter( store_path=d, calibration_data=calibration_data, mode=mode ) as log: log.write_buffer(data_buffer) with h5py.File(d, "r") as written: assert "data" in written.keys() assert "time" in written["data"].keys() for variable in ["voltage", "current"]: assert variable in written["data"].keys() ref_var = getattr(data_buffer, variable) assert all(written["data"][variable][:] == ref_var)
def test_calibration_logging(mode, tmp_path, calibration_data): d = tmp_path / "recording.h5" with LogWriter( store_path=d, mode=mode, calibration_data=calibration_data ) as _: pass h5store = h5py.File(d, "r") for channel, parameter in product( ["voltage", "current"], ["gain", "offset"] ): assert ( h5store["data"][channel].attrs[parameter] == calibration_data["load"][channel][parameter] )
def test_exception_logging(tmp_path, data_buffer, calibration_data): d = tmp_path / "harvest.h5" with LogWriter(store_path=d, calibration_data=calibration_data) as writer: writer.write_buffer(data_buffer) ts = int(time.time() * 1000) writer.write_exception(ExceptionRecord(ts, "there was an exception", 0)) writer.write_exception( ExceptionRecord(ts + 1, "there was another exception", 1) ) assert writer.log_grp["message"][0] == "there was an exception" assert writer.log_grp["message"][1] == "there was another exception" assert writer.log_grp["value"][0] == 0 assert writer.log_grp["value"][1] == 1 assert writer.log_grp["time"][0] == ts assert writer.log_grp["time"][1] == ts + 1
def test_logwriter_performance(tmp_path, data_buffer, calibration_data): d = tmp_path / "harvest.h5" with LogWriter(store_path=d, calibration_data=calibration_data) as log: log.write_buffer(data_buffer)