def _create_exp_if_needed(target_conn: ConnectionPlus, exp_name: str, sample_name: str, fmt_str: str, start_time: float, end_time: Union[float, None]) -> int: """ Look up in the database whether an experiment already exists and create it if it doesn't. Note that experiments do not have GUIDs, so this method is not guaranteed to work. Matching names and times is the best we can do. """ matching_exp_ids = get_matching_exp_ids(target_conn, name=exp_name, sample_name=sample_name, format_string=fmt_str, start_time=start_time, end_time=end_time) if len(matching_exp_ids) > 1: exp_id = matching_exp_ids[0] warn(f'{len(matching_exp_ids)} experiments found in target DB that ' 'match name, sample_name, fmt_str, start_time, and end_time. ' f'Inserting into the experiment with exp_id={exp_id}.') return exp_id if len(matching_exp_ids) == 1: return matching_exp_ids[0] else: lastrowid = new_experiment(target_conn, name=exp_name, sample_name=sample_name, format_string=fmt_str, start_time=start_time, end_time=end_time) return lastrowid
def test_is_run_id_in_db(empty_temp_db): conn = mut.connect(get_DB_location()) mut.new_experiment(conn, 'test_exp', 'no_sample') for _ in range(5): ds = DataSet(conn=conn, run_id=None) # there should now be run_ids 1, 2, 3, 4, 5 in the database good_ids = [1, 2, 3, 4, 5] try_ids = [1, 3, 9999, 23, 0, 1, 1, 3, 34] sorted_try_ids = np.unique(try_ids) expected_dict = {tid: (tid in good_ids) for tid in sorted_try_ids} acquired_dict = mut.is_run_id_in_database(conn, *try_ids) assert expected_dict == acquired_dict