Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace config context managers with pytest fixtures #4764

Merged
merged 7 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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