def test_perform_actual_upgrade_0_to_1(): # we cannot use the empty_temp_db, since that has already called connect # and is therefore latest version already v0fixpath = os.path.join(fixturepath, 'db_files', 'version0') dbname_old = os.path.join(v0fixpath, 'empty.db') if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the " "https://github.com/QCoDeS/qcodes_generate_test_db/ repo") with temporarily_copied_DB(dbname_old, debug=False, version=0) as conn: assert get_user_version(conn) == 0 guid_table_query = "SELECT guid FROM runs" with pytest.raises(RuntimeError) as excinfo: atomic_transaction(conn, guid_table_query) assert error_caused_by(excinfo, 'no such column: guid') perform_db_upgrade_0_to_1(conn) assert get_user_version(conn) == 1 c = atomic_transaction(conn, guid_table_query) assert len(c.fetchall()) == 0
def test_version_4a_bugfix(): v4fixpath = os.path.join(fixturepath, 'db_files', 'version4a') dbname_old = os.path.join(v4fixpath, 'some_runs.db') if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the legacy_DB_generation folder") with temporarily_copied_DB(dbname_old, debug=False, version=4) as conn: dd = fix_version_4a_run_description_bug(conn) assert dd['runs_inspected'] == 10 assert dd['runs_fixed'] == 10 # Ensure the structure of the run_description JSON after applying # the fix function for run_id in range(1, 10 + 1): rd_str = get_run_description(conn, run_id) rd_dict = json.loads(rd_str) assert list(rd_dict.keys()) == ['interdependencies'] assert list(rd_dict['interdependencies'].keys()) == ['paramspecs'] dd = fix_version_4a_run_description_bug(conn) assert dd['runs_inspected'] == 10 assert dd['runs_fixed'] == 0
def test_perform_actual_upgrade_2_to_3_empty(): v2fixpath = os.path.join(fixturepath, 'db_files', 'version2') dbname_old = os.path.join(v2fixpath, 'empty.db') if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the " "https://github.com/QCoDeS/qcodes_generate_test_db/ repo") with temporarily_copied_DB(dbname_old, debug=False, version=2) as conn: assert get_user_version(conn) == 2 desc_query = 'SELECT run_description FROM runs' with pytest.raises(RuntimeError) as excinfo: atomic_transaction(conn, desc_query) assert error_caused_by(excinfo, 'no such column: run_description') perform_db_upgrade_2_to_3(conn) assert get_user_version(conn) == 3 c = atomic_transaction(conn, desc_query) assert len(c.fetchall()) == 0
def test_perform_actual_upgrade_1_to_2(): v1fixpath = os.path.join(fixturepath, 'db_files', 'version1') dbname_old = os.path.join(v1fixpath, 'empty.db') if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the legacy_DB_generation folder") with temporarily_copied_DB(dbname_old, debug=False, version=1) as conn: assert get_user_version(conn) == 1 guid_table_query = "SELECT guid FROM runs" c = atomic_transaction(conn, guid_table_query) assert len(c.fetchall()) == 0 index_query = "PRAGMA index_list(runs)" c = atomic_transaction(conn, index_query) assert len(c.fetchall()) == 0 perform_db_upgrade_1_to_2(conn) c = atomic_transaction(conn, index_query) assert len(c.fetchall()) == 2
def test_version_4a_bugfix_raises(): v3fixpath = os.path.join(fixturepath, 'db_files', 'version3') dbname_old = os.path.join(v3fixpath, 'some_runs_without_run_description.db') if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the legacy_DB_generation folder") with temporarily_copied_DB(dbname_old, debug=False, version=3) as conn: with pytest.raises(RuntimeError): fix_version_4a_run_description_bug(conn)
def test_perform_actual_upgrade_6_to_7(): fixpath = os.path.join(fixturepath, 'db_files', 'version6') db_file = 'some_runs.db' dbname_old = os.path.join(fixpath, db_file) if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the " "https://github.com/QCoDeS/qcodes_generate_test_db/ repo") with temporarily_copied_DB(dbname_old, debug=False, version=6) as conn: assert isinstance(conn, ConnectionPlus) perform_db_upgrade_6_to_7(conn) assert get_user_version(conn) == 7 no_of_runs_query = "SELECT max(run_id) FROM runs" no_of_runs = one(atomic_transaction(conn, no_of_runs_query), 'max(run_id)') assert no_of_runs == 10 columns = atomic_transaction(conn, "PRAGMA table_info(runs)").fetchall() col_names = [col['name'] for col in columns] assert 'captured_run_id' in col_names assert 'captured_counter' in col_names for run_id in range(1, no_of_runs + 1): ds1 = load_by_id(run_id, conn) ds2 = load_by_run_spec(captured_run_id=run_id, conn=conn) assert ds1.the_same_dataset_as(ds2) assert ds1.run_id == run_id assert ds1.run_id == ds1.captured_run_id assert ds2.run_id == run_id assert ds2.run_id == ds2.captured_run_id exp_id = 1 for counter in range(1, no_of_runs + 1): ds1 = load_by_counter(counter, exp_id, conn) ds2 = load_by_run_spec(captured_counter=counter, conn=conn) assert ds1.the_same_dataset_as(ds2) assert ds1.counter == counter assert ds1.counter == ds1.captured_counter assert ds2.counter == counter assert ds2.counter == ds2.captured_counter
def test_perform_upgrade_6_7(): fixpath = os.path.join(fixturepath, 'db_files', 'version6') db_file = 'empty.db' dbname_old = os.path.join(fixpath, db_file) if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the " "https://github.com/QCoDeS/qcodes_generate_test_db/ repo") with temporarily_copied_DB(dbname_old, debug=False, version=6) as conn: perform_db_upgrade_6_to_7(conn) assert get_user_version(conn) == 7
def test_perform_actual_upgrade_2_to_3_empty_runs(): v2fixpath = os.path.join(fixturepath, 'db_files', 'version2') dbname_old = os.path.join(v2fixpath, 'empty_runs.db') if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the " "https://github.com/QCoDeS/qcodes_generate_test_db/ repo") with temporarily_copied_DB(dbname_old, debug=False, version=2) as conn: perform_db_upgrade_2_to_3(conn)
def test_perform_actual_upgrade_7_to_8(db_file): v7fixpath = os.path.join(fixturepath, 'db_files', 'version7') db_file += '.db' dbname_old = os.path.join(v7fixpath, db_file) if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the " "https://github.com/QCoDeS/qcodes_generate_test_db/ repo") with temporarily_copied_DB(dbname_old, debug=False, version=7) as conn: perform_db_upgrade_7_to_8(conn) assert is_column_in_table(conn, 'runs', 'parent_datasets')
def test_fix_wrong_run_descriptions(): v3fixpath = os.path.join(fixturepath, 'db_files', 'version3') dbname_old = os.path.join(v3fixpath, 'some_runs_without_run_description.db') if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the legacy_DB_generation folder") def make_ps(n): ps = ParamSpec(f'p{n}', label=f'Parameter {n}', unit=f'unit {n}', paramtype='numeric') return ps paramspecs = [make_ps(n) for n in range(6)] paramspecs[2]._inferred_from = ['p0'] paramspecs[3]._inferred_from = ['p1', 'p0'] paramspecs[4]._depends_on = ['p2', 'p3'] paramspecs[5]._inferred_from = ['p0'] with temporarily_copied_DB(dbname_old, debug=False, version=3) as conn: assert get_user_version(conn) == 3 expected_description = v0.RunDescriber( v0.InterDependencies(*paramspecs)) empty_description = v0.RunDescriber(v0.InterDependencies()) fix_wrong_run_descriptions(conn, [1, 2, 3, 4]) for run_id in [1, 2, 3]: desc_str = get_run_description(conn, run_id) desc = serial.from_json_to_native(desc_str) assert desc == expected_description desc_str = get_run_description(conn, run_id=4) desc = serial.from_json_to_native(desc_str) assert desc == empty_description
def test_perform_actual_upgrade_8_to_9(db_file): v8fixpath = os.path.join(fixturepath, 'db_files', 'version8') db_file += '.db' dbname_old = os.path.join(v8fixpath, db_file) if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the " "https://github.com/QCoDeS/qcodes_generate_test_db/ repo") with temporarily_copied_DB(dbname_old, debug=False, version=8) as conn: index_query = "PRAGMA index_list(runs)" c = atomic_transaction(conn, index_query) assert len(c.fetchall()) == 2 perform_db_upgrade_8_to_9(conn) c = atomic_transaction(conn, index_query) assert len(c.fetchall()) == 3
def test_perform_actual_upgrade_4_to_5(db_file): v4fixpath = os.path.join(fixturepath, 'db_files', 'version4') db_file += '.db' dbname_old = os.path.join(v4fixpath, db_file) if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the " "https://github.com/QCoDeS/qcodes_generate_test_db/ repo") with temporarily_copied_DB(dbname_old, debug=False, version=4) as conn: # firstly, assert the situation with 'snapshot' column of 'runs' table if 'with_runs_and_snapshots' in db_file: assert is_column_in_table(conn, 'runs', 'snapshot') else: assert not is_column_in_table(conn, 'runs', 'snapshot') # secondly, perform the upgrade perform_db_upgrade_4_to_5(conn) # finally, assert the 'snapshot' column exists in 'runs' table assert is_column_in_table(conn, 'runs', 'snapshot')
def test_perform_actual_upgrade_6_to_newest_add_new_data(): """ Insert new runs on top of existing runs upgraded and verify that they get the correct captured_run_id and captured_counter """ from qcodes.dataset.measurements import Measurement from qcodes.instrument.parameter import Parameter import numpy as np fixpath = os.path.join(fixturepath, 'db_files', 'version6') db_file = 'some_runs.db' dbname_old = os.path.join(fixpath, db_file) if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the " "https://github.com/QCoDeS/qcodes_generate_test_db/ repo") with temporarily_copied_DB(dbname_old, debug=False, version=6) as conn: assert isinstance(conn, ConnectionPlus) perform_db_upgrade(conn) assert get_user_version(conn) >= 7 no_of_runs_query = "SELECT max(run_id) FROM runs" no_of_runs = one(atomic_transaction(conn, no_of_runs_query), 'max(run_id)') # Now let's insert new runs and ensure that they also get # captured_run_id assigned. params = [] for n in range(5): params.append( Parameter(f'p{n}', label=f'Parameter {n}', unit=f'unit {n}', set_cmd=None, get_cmd=None)) # Set up an experiment exp = new_experiment('some-exp', 'some-sample', conn=conn) meas = Measurement(exp=exp) meas.register_parameter(params[0]) meas.register_parameter(params[1]) meas.register_parameter(params[2], basis=(params[0], )) meas.register_parameter(params[3], basis=(params[1], )) meas.register_parameter(params[4], setpoints=(params[2], params[3])) # Make a number of identical runs for _ in range(10): with meas.run() as datasaver: for x in np.random.rand(10): for y in np.random.rand(10): z = np.random.rand() datasaver.add_result((params[0], 0), (params[1], 1), (params[2], x), (params[3], y), (params[4], z)) no_of_runs_new = one(atomic_transaction(conn, no_of_runs_query), 'max(run_id)') assert no_of_runs_new == 20 # check that run_id is equivalent to captured_run_id for new # runs for run_id in range(no_of_runs, no_of_runs_new + 1): ds1 = load_by_id(run_id, conn) ds2 = load_by_run_spec(captured_run_id=run_id, conn=conn) assert ds1.the_same_dataset_as(ds2) assert ds1.run_id == run_id assert ds1.run_id == ds1.captured_run_id assert ds2.run_id == run_id assert ds2.run_id == ds2.captured_run_id # we are creating a new experiment into a db with one exp so: exp_id = 2 # check that counter is equivalent to captured_counter for new # runs for counter in range(1, no_of_runs_new - no_of_runs + 1): ds1 = load_by_counter(counter, exp_id, conn) # giving only the counter is not unique since we have 2 experiments with pytest.raises(NameError, match="More than one" " matching dataset"): load_by_run_spec(captured_counter=counter, conn=conn) # however we can supply counter and experiment ds2 = load_by_run_spec(captured_counter=counter, experiment_name='some-exp', conn=conn) assert ds1.the_same_dataset_as(ds2) assert ds1.counter == counter assert ds1.counter == ds1.captured_counter assert ds2.counter == counter assert ds2.counter == ds2.captured_counter
def test_perform_upgrade_v3_to_v4(): """ Test that a db upgrade from v2 to v4 works correctly. """ v3fixpath = os.path.join(fixturepath, 'db_files', 'version3') dbname_old = os.path.join(v3fixpath, 'some_runs_upgraded_2.db') if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the " "https://github.com/QCoDeS/qcodes_generate_test_db/ repo") with temporarily_copied_DB(dbname_old, debug=False, version=3) as conn: assert get_user_version(conn) == 3 sql = f""" SELECT run_description FROM runs WHERE run_id == 1 """ perform_db_upgrade_3_to_4(conn) c = atomic_transaction(conn, sql) json_str = one(c, 'run_description') unversioned_dict = json.loads(json_str) idp = InterDependencies._from_dict( unversioned_dict['interdependencies']) assert isinstance(idp, InterDependencies) p0 = [p for p in idp.paramspecs if p.name == 'p0'][0] assert p0.depends_on == '' assert p0.depends_on_ == [] assert p0.inferred_from == '' assert p0.inferred_from_ == [] assert p0.label == "Parameter 0" assert p0.unit == "unit 0" p1 = [p for p in idp.paramspecs if p.name == 'p1'][0] assert p1.depends_on == '' assert p1.depends_on_ == [] assert p1.inferred_from == '' assert p1.inferred_from_ == [] assert p1.label == "Parameter 1" assert p1.unit == "unit 1" p2 = [p for p in idp.paramspecs if p.name == 'p2'][0] assert p2.depends_on == '' assert p2.depends_on_ == [] assert p2.inferred_from == 'p0' assert p2.inferred_from_ == ['p0'] assert p2.label == "Parameter 2" assert p2.unit == "unit 2" p3 = [p for p in idp.paramspecs if p.name == 'p3'][0] assert p3.depends_on == '' assert p3.depends_on_ == [] assert p3.inferred_from == 'p1, p0' assert p3.inferred_from_ == ['p1', 'p0'] assert p3.label == "Parameter 3" assert p3.unit == "unit 3" p4 = [p for p in idp.paramspecs if p.name == 'p4'][0] assert p4.depends_on == 'p2, p3' assert p4.depends_on_ == ['p2', 'p3'] assert p4.inferred_from == '' assert p4.inferred_from_ == [] assert p4.label == "Parameter 4" assert p4.unit == "unit 4" p5 = [p for p in idp.paramspecs if p.name == 'p5'][0] assert p5.depends_on == '' assert p5.depends_on_ == [] assert p5.inferred_from == 'p0' assert p5.inferred_from_ == ['p0'] assert p5.label == "Parameter 5" assert p5.unit == "unit 5"
def test_perform_actual_upgrade_2_to_3_some_runs(): v2fixpath = os.path.join(fixturepath, 'db_files', 'version2') dbname_old = os.path.join(v2fixpath, 'some_runs.db') if not os.path.exists(dbname_old): pytest.skip("No db-file fixtures found. You can generate test db-files" " using the scripts in the" "https://github.com/QCoDeS/qcodes_generate_test_db/ repo") with temporarily_copied_DB(dbname_old, debug=False, version=2) as conn: assert get_user_version(conn) == 2 perform_db_upgrade_2_to_3(conn) desc_query = 'SELECT run_description FROM runs' c = atomic_transaction(conn, desc_query) assert len(c.fetchall()) == 10 # retrieve the json string and recreate the object sql = f""" SELECT run_description FROM runs WHERE run_id == 1 """ c = atomic_transaction(conn, sql) json_str = one(c, 'run_description') unversioned_dict = json.loads(json_str) idp = InterDependencies._from_dict( unversioned_dict['interdependencies']) assert isinstance(idp, InterDependencies) # here we verify that the dependencies encoded in # tests/dataset/legacy_DB_generation/generate_version_2.py # are recovered p0 = [p for p in idp.paramspecs if p.name == 'p0'][0] assert p0.depends_on == '' assert p0.depends_on_ == [] assert p0.inferred_from == '' assert p0.inferred_from_ == [] assert p0.label == "Parameter 0" assert p0.unit == "unit 0" p1 = [p for p in idp.paramspecs if p.name == 'p1'][0] assert p1.depends_on == '' assert p1.depends_on_ == [] assert p1.inferred_from == '' assert p1.inferred_from_ == [] assert p1.label == "Parameter 1" assert p1.unit == "unit 1" p2 = [p for p in idp.paramspecs if p.name == 'p2'][0] assert p2.depends_on == '' assert p2.depends_on_ == [] assert p2.inferred_from == 'p0' assert p2.inferred_from_ == ['p0'] assert p2.label == "Parameter 2" assert p2.unit == "unit 2" p3 = [p for p in idp.paramspecs if p.name == 'p3'][0] assert p3.depends_on == '' assert p3.depends_on_ == [] assert p3.inferred_from == 'p1, p0' assert p3.inferred_from_ == ['p1', 'p0'] assert p3.label == "Parameter 3" assert p3.unit == "unit 3" p4 = [p for p in idp.paramspecs if p.name == 'p4'][0] assert p4.depends_on == 'p2, p3' assert p4.depends_on_ == ['p2', 'p3'] assert p4.inferred_from == '' assert p4.inferred_from_ == [] assert p4.label == "Parameter 4" assert p4.unit == "unit 4" p5 = [p for p in idp.paramspecs if p.name == 'p5'][0] assert p5.depends_on == '' assert p5.depends_on_ == [] assert p5.inferred_from == 'p0' assert p5.inferred_from_ == ['p0'] assert p5.label == "Parameter 5" assert p5.unit == "unit 5"