Skip to content

Commit

Permalink
Merge pull request #4764 from jenshnielsen/reset_config_fixture
Browse files Browse the repository at this point in the history
Replace config context managers with pytest fixtures
  • Loading branch information
jenshnielsen authored Oct 25, 2022
2 parents ad5821c + 2101279 commit fe84e52
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 266 deletions.
6 changes: 4 additions & 2 deletions qcodes/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import qcodes
from qcodes.configuration import Config, DotDict
from qcodes.metadatable import Metadatable
from qcodes.utils import deprecate

if TYPE_CHECKING:
from pytest import ExceptionInfo
Expand Down Expand Up @@ -160,6 +161,7 @@ def set(self, value):
return value


@deprecate(reason="Unused internally", alternative="default_config fixture")
@contextmanager
def default_config(user_config: Optional[str] = None):
"""
Expand Down Expand Up @@ -212,13 +214,13 @@ def default_config(user_config: Optional[str] = None):
qcodes.config.current_config = default_config_obj


@deprecate(reason="Unused internally", alternative="reset_config_on_exit fixture")
@contextmanager
def reset_config_on_exit():
"""
Context manager to clean any modefication of the in memory config on exit
Context manager to clean any modification of the in memory config on exit
"""

default_config_obj: Optional[DotDict] = copy.deepcopy(
qcodes.config.current_config
)
Expand Down
66 changes: 66 additions & 0 deletions qcodes/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from __future__ import annotations

import copy
import gc
import os
import sys
import tempfile
from typing import TYPE_CHECKING

import pytest
from hypothesis import settings

import qcodes as qc
from qcodes.configuration import Config
from qcodes.dataset import initialise_database, new_data_set
from qcodes.dataset.descriptions.dependencies import InterDependencies_
from qcodes.dataset.descriptions.param_spec import ParamSpecBase
Expand All @@ -15,6 +21,8 @@

n_experiments = 0

if TYPE_CHECKING:
from qcodes.configuration import DotDict

def pytest_configure(config):
config.addinivalue_line("markers", "win32: tests that only run under windows")
Expand Down Expand Up @@ -43,6 +51,64 @@ def disable_telemetry():
qc.config.telemetry.enabled = original_state


@pytest.fixture(scope="function")
def default_config(tmp_path):
"""
Fixture to temporarily establish default config settings.
This is achieved by overwriting the config paths of the user-,
environment-, and current directory-config files with the path of the
config file in the qcodes repository,
additionally the current config object `qcodes.config` gets copied and
reestablished.
"""
home_file_name = Config.home_file_name
schema_home_file_name = Config.schema_home_file_name
env_file_name = Config.env_file_name
schema_env_file_name = Config.schema_env_file_name
cwd_file_name = Config.cwd_file_name
schema_cwd_file_name = Config.schema_cwd_file_name

file_name = str(tmp_path / "user_config.json")
file_name_schema = str(tmp_path / "user_config_schema.json")

Config.home_file_name = file_name
Config.schema_home_file_name = file_name_schema
Config.env_file_name = ""
Config.schema_env_file_name = ""
Config.cwd_file_name = ""
Config.schema_cwd_file_name = ""

default_config_obj: DotDict | None = copy.deepcopy(qc.config.current_config)
qc.config = Config()

try:
yield
finally:
Config.home_file_name = home_file_name
Config.schema_home_file_name = schema_home_file_name
Config.env_file_name = env_file_name
Config.schema_env_file_name = schema_env_file_name
Config.cwd_file_name = cwd_file_name
Config.schema_cwd_file_name = schema_cwd_file_name

qc.config.current_config = default_config_obj


@pytest.fixture(scope="function")
def reset_config_on_exit():

"""
Fixture to clean any modification of the in memory config on exit
"""
default_config_obj: DotDict | None = copy.deepcopy(qc.config.current_config)

try:
yield
finally:
qc.config.current_config = default_config_obj


@pytest.fixture(scope="session", autouse=True)
def disable_config_subscriber():
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,36 +318,36 @@ def test_setting_write_period(wp):
@given(wp=hst.one_of(hst.integers(), hst.floats(allow_nan=False),
hst.text()))
@pytest.mark.usefixtures("experiment")
@pytest.mark.usefixtures("reset_config_on_exit")
def test_setting_write_period_from_config(wp):
with reset_config_on_exit():
qc.config.dataset.write_period = wp
qc.config.dataset.write_period = wp

if isinstance(wp, str):
with pytest.raises(ValueError):
Measurement()
elif wp < 1e-3:
with pytest.raises(ValueError):
Measurement()
else:
meas = Measurement()
assert meas.write_period == float(wp)
meas.register_custom_parameter(name='dummy')
with meas.run() as datasaver:
assert datasaver.write_period == float(wp)
if isinstance(wp, str):
with pytest.raises(ValueError):
Measurement()
elif wp < 1e-3:
with pytest.raises(ValueError):
Measurement()
else:
meas = Measurement()
assert meas.write_period == float(wp)
meas.register_custom_parameter(name="dummy")
with meas.run() as datasaver:
assert datasaver.write_period == float(wp)


@pytest.mark.parametrize("write_in_background", [True, False])
@pytest.mark.usefixtures("experiment")
@pytest.mark.usefixtures("reset_config_on_exit")
def test_setting_write_in_background_from_config(write_in_background):
with reset_config_on_exit():
qc.config.dataset.write_in_background = write_in_background
qc.config.dataset.write_in_background = write_in_background

meas = Measurement()
meas.register_custom_parameter(name='dummy')
with meas.run() as datasaver:
ds = datasaver.dataset
assert isinstance(ds, DataSet)
assert ds._writer_status.write_in_background is write_in_background
meas = Measurement()
meas.register_custom_parameter(name="dummy")
with meas.run() as datasaver:
ds = datasaver.dataset
assert isinstance(ds, DataSet)
assert ds._writer_status.write_in_background is write_in_background


@pytest.mark.usefixtures("experiment")
Expand Down
Loading

0 comments on commit fe84e52

Please sign in to comment.