Skip to content

Commit

Permalink
Merge pull request microsoft#2016 from GateBuilder/prevent_save_confi…
Browse files Browse the repository at this point in the history
…g_to_home

Prevent save config to home in tests
  • Loading branch information
jenshnielsen authored Jun 8, 2020
2 parents bdb8c13 + 1a6a0cb commit aa79ae8
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 66 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,3 @@ after_success:
- cd $TRAVIS_BUILD_DIR
- python-codacy-coverage -r qcodes/coverage.xml
- codecov

10 changes: 5 additions & 5 deletions qcodes/configuration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ class Config:
schema_file_name)
"""Filename of cwd schema"""

current_schema: Optional[dict] = None
current_schema: Optional['DotDict'] = None
"""Validators and descriptions of config values"""
current_config: Optional[dict] = None
current_config: Optional['DotDict'] = None
"""Valid config values"""

defaults: dict
defaults: 'DotDict'
"""The default configuration"""
defaults_schema: dict
defaults_schema: 'DotDict'
"""The default schema"""

_diff_config: Dict[str, Any] = {}
Expand All @@ -93,7 +93,7 @@ def __init__(self, path: Optional[str] = None) -> None:
self.defaults, self.defaults_schema = self.load_default()
self.update_config()

def load_default(self) -> Tuple[dict, dict]:
def load_default(self) -> Tuple['DotDict', 'DotDict']:
defaults = self.load_config(self.default_file_name)
defaults_schema = self.load_config(self.schema_default_file_name)
self.validate(defaults, defaults_schema)
Expand Down
8 changes: 4 additions & 4 deletions qcodes/dataset/guids.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np

from qcodes.configuration import Config
import qcodes as qc

_guid_pattern = re.compile(r'^[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}$')

Expand All @@ -25,7 +25,7 @@ def generate_guid(timeint: Union[int, None]=None,
timeint: An integer of miliseconds since unix epoch time
sampleint: A code for the sample
"""
cfg = Config()
cfg = qc.config

try:
guid_comp = cfg['GUID_components']
Expand Down Expand Up @@ -81,7 +81,7 @@ def set_guid_location_code() -> None:
"""
Interactive function to set the location code.
"""
cfg = Config()
cfg = qc.config
old_loc = cfg['GUID_components']['location']
print(f'Updating GUID location code. Current location code is: {old_loc}')
if old_loc != 0:
Expand All @@ -106,7 +106,7 @@ def set_guid_work_station_code() -> None:
"""
Interactive function to set the work station code
"""
cfg = Config()
cfg = qc.config
old_ws = cfg['GUID_components']['work_station']
print('Updating GUID work station code. '
f'Current work station code is: {old_ws}')
Expand Down
2 changes: 1 addition & 1 deletion qcodes/dataset/sqlite/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,7 @@ def update_GUIDs(conn: ConnectionPlus) -> None:

log.info('Commencing update of all GUIDs in database')

cfg = Config()
cfg = qc.config

location = cfg['GUID_components']['location']
work_station = cfg['GUID_components']['work_station']
Expand Down
54 changes: 23 additions & 31 deletions qcodes/tests/dataset/test_database_creation_and_upgrading.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import qcodes as qc
import qcodes.dataset.descriptions.versioning.serialization as serial
import qcodes.tests.dataset
from qcodes.configuration import Config
from qcodes import new_data_set, new_experiment
from qcodes.dataset.descriptions.dependencies import InterDependencies_
from qcodes.dataset.descriptions.param_spec import ParamSpecBase
Expand Down Expand Up @@ -48,19 +47,18 @@

@contextmanager
def location_and_station_set_to(location: int, work_station: int):
cfg = Config()
old_cfg = deepcopy(cfg.current_config)
cfg = qc.config.current_config
if cfg is None:
raise RuntimeError("Expected config to be not None.")
old_cfg = deepcopy(cfg)
cfg['GUID_components']['location'] = location
cfg['GUID_components']['work_station'] = work_station
cfg.save_to_home()

try:
yield

finally:
cfg.current_config = old_cfg
cfg.save_to_home()

qc.config.current_config = old_cfg

LATEST_VERSION = _latest_available_version()
VERSIONS = tuple(range(LATEST_VERSION + 1))
Expand Down Expand Up @@ -574,32 +572,33 @@ def test_update_existing_guids(caplog):
ds2.mark_started()
ds2.add_results([{'x': 2}])

guid_comps_1 = parse_guid(ds1.guid)
assert guid_comps_1['location'] == 0
assert guid_comps_1['work_station'] == 0

guid_comps_2 = parse_guid(ds2.guid)
assert guid_comps_2['location'] == 0
assert guid_comps_2['work_station'] == 0
_assert_loc_station(ds1, 0, 0)
_assert_loc_station(ds2, 0, 0)

with location_and_station_set_to(0, old_ws):
ds3 = new_data_set('ds_three')
ds3.set_interdependencies(idps)
ds3.mark_started()
ds3.add_results([{'x': 3}])

_assert_loc_station(ds3, 0, old_ws)

with location_and_station_set_to(old_loc, 0):
ds4 = new_data_set('ds_four')
ds4.set_interdependencies(idps)
ds4.mark_started()
ds4.add_results([{'x': 4}])

_assert_loc_station(ds4, old_loc, 0)

with location_and_station_set_to(old_loc, old_ws):
ds5 = new_data_set('ds_five')
ds5.set_interdependencies(idps)
ds5.mark_started()
ds5.add_results([{'x': 5}])

_assert_loc_station(ds5, old_loc, old_ws)

with location_and_station_set_to(new_loc, new_ws):

caplog.clear()
Expand All @@ -614,27 +613,20 @@ def test_update_existing_guids(caplog):
update_GUIDs(ds1.conn)

for record, lvl in zip(caplog.records, expected_levels):
print(record)
assert record.levelname == lvl

guid_comps_1 = parse_guid(ds1.guid)
assert guid_comps_1['location'] == new_loc
assert guid_comps_1['work_station'] == new_ws

guid_comps_2 = parse_guid(ds2.guid)
assert guid_comps_2['location'] == new_loc
assert guid_comps_2['work_station'] == new_ws

guid_comps_3 = parse_guid(ds3.guid)
assert guid_comps_3['location'] == 0
assert guid_comps_3['work_station'] == old_ws
_assert_loc_station(ds1, new_loc, new_ws)
_assert_loc_station(ds2, new_loc, new_ws)
_assert_loc_station(ds3, 0, old_ws)
_assert_loc_station(ds4, old_loc, 0)
_assert_loc_station(ds5, old_loc, old_ws)

guid_comps_4 = parse_guid(ds4.guid)
assert guid_comps_4['location'] == old_loc
assert guid_comps_4['work_station'] == 0

guid_comps_5 = parse_guid(ds5.guid)
assert guid_comps_5['location'] == old_loc
assert guid_comps_5['work_station'] == old_ws
def _assert_loc_station(ds, expected_loc, expected_station):
guid_dict = parse_guid(ds.guid)
assert guid_dict["location"] == expected_loc
assert guid_dict["work_station"] == expected_station


@pytest.mark.parametrize('db_file',
Expand Down
4 changes: 2 additions & 2 deletions qcodes/tests/dataset/test_database_extract_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest
import numpy as np

from qcodes.configuration import Config
import qcodes as qc
import qcodes.tests.dataset
from qcodes.dataset.experiment_container import Experiment,\
load_experiment_by_name
Expand Down Expand Up @@ -511,7 +511,7 @@ def test_combine_runs(two_empty_temp_db_connections,
lines = table.split('\n')
headers = re.split(r'\s+', lines[0].strip())

cfg = Config()
cfg = qc.config
guid_comp = cfg['GUID_components']

# borrowed fallback logic from generate_guid
Expand Down
32 changes: 16 additions & 16 deletions qcodes/tests/dataset/test_guids.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,27 @@
import hypothesis.strategies as hst
import numpy as np

import qcodes as qc
from qcodes.dataset.guids import (generate_guid, parse_guid,
set_guid_location_code,
set_guid_work_station_code,
validate_guid_format,
filter_guids_by_parts)
from qcodes.configuration import Config, DotDict
from qcodes.configuration import DotDict

@contextmanager
def protected_config():
"""
Context manager to be used in all tests that modify the config to ensure
that the config is left untouched even if the tests fail
"""
ocfg: DotDict = Config().current_config
ocfg: DotDict = qc.config.current_config
original_config = deepcopy(ocfg)

try:
yield
finally:
cfg = Config()
cfg.current_config = original_config
cfg.save_to_home()
qc.config.current_config = original_config


@settings(max_examples=50, deadline=1000)
Expand All @@ -38,11 +37,10 @@ def protected_config():
def test_generate_guid(loc, stat, smpl):
# update config to generate a particular guid. Read it back to verify
with protected_config():
cfg = Config()
cfg = qc.config
cfg['GUID_components']['location'] = loc
cfg['GUID_components']['work_station'] = stat
cfg['GUID_components']['sample'] = smpl
cfg.save_to_home()

guid = generate_guid()
gen_time = int(np.round(time.time()*1000))
Expand All @@ -63,14 +61,14 @@ def test_generate_guid(loc, stat, smpl):
def test_set_guid_location_code(loc, monkeypatch):
monkeypatch.setattr('builtins.input', lambda x: str(loc))

orig_cfg = Config().current_config
orig_cfg = qc.config

original_loc = orig_cfg['GUID_components']['location']

with protected_config():
set_guid_location_code()

cfg = Config().current_config
cfg = qc.config

if 257 > loc > 0:
assert cfg['GUID_components']['location'] == loc
Expand All @@ -83,14 +81,14 @@ def test_set_guid_location_code(loc, monkeypatch):
def test_set_guid_workstatio_code(ws, monkeypatch):
monkeypatch.setattr('builtins.input', lambda x: str(ws))

orig_cfg = Config().current_config
orig_cfg = qc.config

original_ws = orig_cfg['GUID_components']['work_station']

with protected_config():
set_guid_work_station_code()

cfg = Config().current_config
cfg = qc.config

if 16777216 > ws > 0:
assert cfg['GUID_components']['work_station'] == ws
Expand All @@ -99,17 +97,19 @@ def test_set_guid_workstatio_code(ws, monkeypatch):


@settings(max_examples=50, deadline=1000)
@given(locs=hst.lists(hst.integers(0, 255), min_size=2, max_size=2, unique=True),
stats=hst.lists(hst.integers(0, 65535), min_size=2, max_size=2, unique=True),
smpls=hst.lists(hst.integers(0, 4294967295), min_size=2, max_size=2, unique=True),
@given(locs=hst.lists(hst.integers(0, 255), min_size=2, max_size=2,
unique=True),
stats=hst.lists(hst.integers(0, 65535), min_size=2, max_size=2,
unique=True),
smpls=hst.lists(hst.integers(0, 4294967295), min_size=2, max_size=2,
unique=True),
)
def test_filter_guid(locs, stats, smpls):

def make_test_guid(cfg, loc: int, smpl: int, stat: int):
cfg['GUID_components']['location'] = loc
cfg['GUID_components']['work_station'] = stat
cfg['GUID_components']['sample'] = smpl
cfg.save_to_home()

guid = generate_guid()
gen_time = int(np.round(time.time() * 1000))
Expand All @@ -126,7 +126,7 @@ def make_test_guid(cfg, loc: int, smpl: int, stat: int):
with protected_config():

guids = []
cfg = Config()
cfg = qc.config

corrected_smpls = [smpl if smpl != 0 else int('a' * 8, base=16)
for smpl in smpls]
Expand Down
13 changes: 7 additions & 6 deletions qcodes/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import tempfile
import qcodes

from qcodes.configuration import Config
from qcodes.configuration import Config, DotDict

VALID_JSON = "{}"
ENV_KEY = "/dev/random"
Expand Down Expand Up @@ -173,7 +173,8 @@ def default_config(user_config: Optional[str] = None):
Config.cwd_file_name = ''
Config.schema_cwd_file_name = ''

default_config_obj = copy.deepcopy(qcodes.config)
default_config_obj: Optional[DotDict] = copy.\
deepcopy(qcodes.config.current_config)
qcodes.config = Config()

try:
Expand All @@ -186,7 +187,7 @@ def default_config(user_config: Optional[str] = None):
Config.cwd_file_name = cwd_file_name
Config.schema_cwd_file_name = schema_cwd_file_name

qcodes.config = default_config_obj
qcodes.config.current_config = default_config_obj


def side_effect(map, name):
Expand Down Expand Up @@ -319,7 +320,7 @@ def test_update_and_validate_user_config(self, config, schema):

def test_update_from_path(path_to_config_file_on_disk):
with default_config():
cfg = Config()
cfg = qcodes.config

# check that the default is still the default
assert cfg["core"]["db_debug"] is False
Expand All @@ -338,7 +339,7 @@ def test_update_from_path(path_to_config_file_on_disk):


def test_repr():
cfg = Config()
cfg = qcodes.config
rep = cfg.__repr__()

expected_rep = (f"Current values: \n {cfg.current_config} \n"
Expand All @@ -360,7 +361,7 @@ def test_add_and_describe():
description ='A test'
default = 'testdefault'

cfg = Config()
cfg = qcodes.config
cfg.add(key=key, value=value, value_type=value_type,
description=description, default=default)

Expand Down

0 comments on commit aa79ae8

Please sign in to comment.