-
Notifications
You must be signed in to change notification settings - Fork 347
Add tests for previously uncovered utility, dataset, and metadatable … #7961
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
Open
jenshnielsen
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
jenshnielsen:better_coverage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from qcodes.dataset.export_config import ( | ||
| DataExportType, | ||
| get_data_export_name_elements, | ||
| get_data_export_prefix, | ||
| get_data_export_type, | ||
| set_data_export_prefix, | ||
| set_data_export_type, | ||
| ) | ||
|
|
||
|
|
||
| def test_data_export_type_enum_members() -> None: | ||
| assert DataExportType.NETCDF.value == "nc" | ||
| assert DataExportType.CSV.value == "csv" | ||
| assert len(DataExportType) == 2 | ||
|
|
||
|
|
||
| def test_get_data_export_type_with_string_netcdf() -> None: | ||
| result = get_data_export_type("NETCDF") | ||
| assert result is DataExportType.NETCDF | ||
|
|
||
|
|
||
| def test_get_data_export_type_with_string_csv() -> None: | ||
| result = get_data_export_type("CSV") | ||
| assert result is DataExportType.CSV | ||
|
|
||
|
|
||
| def test_get_data_export_type_case_insensitive() -> None: | ||
| assert get_data_export_type("netcdf") is DataExportType.NETCDF | ||
| assert get_data_export_type("csv") is DataExportType.CSV | ||
| assert get_data_export_type("Csv") is DataExportType.CSV | ||
|
|
||
|
|
||
| def test_get_data_export_type_with_enum_input() -> None: | ||
| result = get_data_export_type(DataExportType.NETCDF) | ||
| assert result is DataExportType.NETCDF | ||
|
|
||
| result = get_data_export_type(DataExportType.CSV) | ||
| assert result is DataExportType.CSV | ||
|
|
||
|
|
||
| def test_get_data_export_type_with_none_returns_none() -> None: | ||
| # When config export_type is also None/empty, should return None | ||
| set_data_export_type(None) # type: ignore[arg-type] | ||
| result = get_data_export_type(None) | ||
| assert result is None | ||
|
|
||
|
|
||
| def test_get_data_export_type_with_invalid_string_returns_none() -> None: | ||
| result = get_data_export_type("nonexistent_format") | ||
| assert result is None | ||
|
|
||
|
|
||
| def test_set_and_get_data_export_prefix_roundtrip() -> None: | ||
| set_data_export_prefix("my_prefix_") | ||
| assert get_data_export_prefix() == "my_prefix_" | ||
|
|
||
| set_data_export_prefix("") | ||
| assert get_data_export_prefix() == "" | ||
|
|
||
|
|
||
| def test_get_data_export_name_elements_returns_list() -> None: | ||
| result = get_data_export_name_elements() | ||
| assert isinstance(result, list) | ||
|
|
||
|
|
||
| def test_set_data_export_type_valid() -> None: | ||
| set_data_export_type("netcdf") | ||
| result = get_data_export_type() | ||
| assert result is DataExportType.NETCDF | ||
|
|
||
| set_data_export_type("csv") | ||
| result = get_data_export_type() | ||
| assert result is DataExportType.CSV | ||
|
|
||
|
|
||
| def test_set_data_export_type_invalid_does_not_change_config() -> None: | ||
| set_data_export_type("netcdf") | ||
| set_data_export_type("invalid_type") | ||
| # Config should still have the previous valid value | ||
| result = get_data_export_type() | ||
| assert result is DataExportType.NETCDF | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import copy | ||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
|
|
||
| from qcodes.dataset.json_exporter import ( | ||
| export_data_as_json_heatmap, | ||
| export_data_as_json_linear, | ||
| json_template_heatmap, | ||
| json_template_linear, | ||
| ) | ||
|
|
||
|
|
||
| def test_json_template_linear_structure() -> None: | ||
| assert json_template_linear["type"] == "linear" | ||
| assert "x" in json_template_linear | ||
| assert "y" in json_template_linear | ||
| assert isinstance(json_template_linear["x"], dict) | ||
| assert isinstance(json_template_linear["y"], dict) | ||
| assert "data" in json_template_linear["x"] | ||
| assert "data" in json_template_linear["y"] | ||
| assert json_template_linear["x"]["is_setpoint"] is True | ||
| assert json_template_linear["y"]["is_setpoint"] is False | ||
|
|
||
|
|
||
| def test_json_template_heatmap_structure() -> None: | ||
| assert json_template_heatmap["type"] == "heatmap" | ||
| assert "x" in json_template_heatmap | ||
| assert "y" in json_template_heatmap | ||
| assert "z" in json_template_heatmap | ||
| assert isinstance(json_template_heatmap["x"], dict) | ||
| assert isinstance(json_template_heatmap["y"], dict) | ||
| assert isinstance(json_template_heatmap["z"], dict) | ||
| assert json_template_heatmap["x"]["is_setpoint"] is True | ||
| assert json_template_heatmap["y"]["is_setpoint"] is True | ||
| assert json_template_heatmap["z"]["is_setpoint"] is False | ||
|
|
||
|
|
||
| def test_export_linear_writes_correct_json(tmp_path: Path) -> None: | ||
| location = str(tmp_path / "linear.json") | ||
| state: dict = {"json": copy.deepcopy(json_template_linear)} | ||
| data = [[1.0, 10.0], [2.0, 20.0], [3.0, 30.0]] | ||
|
|
||
| export_data_as_json_linear(data, len(data), state, location) | ||
|
|
||
| with open(location) as f: | ||
| result = json.load(f) | ||
|
|
||
| assert result["type"] == "linear" | ||
| assert result["x"]["data"] == [1.0, 2.0, 3.0] | ||
| assert result["y"]["data"] == [10.0, 20.0, 30.0] | ||
|
|
||
|
|
||
| def test_export_linear_accumulates_data(tmp_path: Path) -> None: | ||
| location = str(tmp_path / "linear.json") | ||
| state: dict = {"json": copy.deepcopy(json_template_linear)} | ||
|
|
||
| export_data_as_json_linear([[1.0, 10.0]], 1, state, location) | ||
| export_data_as_json_linear([[2.0, 20.0]], 2, state, location) | ||
|
|
||
| with open(location) as f: | ||
| result = json.load(f) | ||
|
|
||
| assert result["x"]["data"] == [1.0, 2.0] | ||
| assert result["y"]["data"] == [10.0, 20.0] | ||
|
|
||
|
|
||
| def test_export_linear_does_nothing_for_empty_data(tmp_path: Path) -> None: | ||
| location = str(tmp_path / "linear.json") | ||
| state: dict = {"json": copy.deepcopy(json_template_linear)} | ||
|
|
||
| export_data_as_json_linear([], 0, state, location) | ||
|
|
||
| assert not Path(location).exists() | ||
|
|
||
|
|
||
| def test_export_heatmap_writes_correct_json(tmp_path: Path) -> None: | ||
| location = str(tmp_path / "heatmap.json") | ||
| xlen = 2 | ||
| ylen = 3 | ||
| total = xlen * ylen | ||
|
|
||
| state: dict = { | ||
| "json": copy.deepcopy(json_template_heatmap), | ||
| "data": { | ||
| "x": np.zeros(total), | ||
| "y": np.zeros(total), | ||
| "z": np.zeros(total), | ||
| "location": 0, | ||
| "xlen": xlen, | ||
| "ylen": ylen, | ||
| }, | ||
| } | ||
|
|
||
| # 2x3 grid: x varies slowly, y varies fast | ||
| data = [ | ||
| [0.0, 0.0, 1.0], | ||
| [0.0, 1.0, 2.0], | ||
| [0.0, 2.0, 3.0], | ||
| [1.0, 0.0, 4.0], | ||
| [1.0, 1.0, 5.0], | ||
| [1.0, 2.0, 6.0], | ||
| ] | ||
|
|
||
| export_data_as_json_heatmap(data, total, state, location) | ||
|
|
||
| with open(location) as f: | ||
| result = json.load(f) | ||
|
|
||
| assert result["type"] == "heatmap" | ||
| assert result["x"]["data"] == [0.0, 1.0] | ||
| assert result["y"]["data"] == [0.0, 1.0, 2.0] | ||
| assert result["z"]["data"] == [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] | ||
|
|
||
|
|
||
| def test_export_heatmap_does_nothing_for_empty_data(tmp_path: Path) -> None: | ||
| location = str(tmp_path / "heatmap.json") | ||
| state: dict = { | ||
| "json": copy.deepcopy(json_template_heatmap), | ||
| "data": { | ||
| "x": np.zeros(4), | ||
| "y": np.zeros(4), | ||
| "z": np.zeros(4), | ||
| "location": 0, | ||
| "xlen": 2, | ||
| "ylen": 2, | ||
| }, | ||
| } | ||
|
|
||
| export_data_as_json_heatmap([], 0, state, location) | ||
|
|
||
| assert not Path(location).exists() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| """ | ||
| Tests for qcodes.dataset.descriptions.versioning.rundescribertypes. | ||
|
|
||
| Verifies the TypedDict classes, inheritance relationships, type aliases, | ||
| and the RunDescriberDicts union. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import typing | ||
|
|
||
| from typing_extensions import get_annotations, get_original_bases | ||
|
|
||
| from qcodes.dataset.descriptions.versioning.rundescribertypes import ( | ||
| InterDependencies_Dict, | ||
| InterDependenciesDict, | ||
| RunDescriberDicts, | ||
| RunDescriberV0Dict, | ||
| RunDescriberV1Dict, | ||
| RunDescriberV2Dict, | ||
| RunDescriberV3Dict, | ||
| Shapes, | ||
| ) | ||
|
|
||
| # --------------- Shapes type alias --------------- | ||
|
|
||
|
|
||
| def test_shapes_type_alias() -> None: | ||
| sample: Shapes = {"param": (1, 2, 3)} | ||
| assert sample["param"] == (1, 2, 3) | ||
|
|
||
|
|
||
| # --------------- InterDependenciesDict --------------- | ||
|
|
||
|
|
||
| def test_interdependencies_dict_instantiation() -> None: | ||
| d: InterDependenciesDict = {"paramspecs": ()} | ||
| assert d["paramspecs"] == () | ||
|
|
||
|
|
||
| # --------------- InterDependencies_Dict --------------- | ||
|
|
||
|
|
||
| def test_interdependencies_underscore_dict_instantiation() -> None: | ||
| d: InterDependencies_Dict = { | ||
| "parameters": {}, | ||
| "dependencies": {}, | ||
| "inferences": {}, | ||
| "standalones": [], | ||
| } | ||
| assert d["parameters"] == {} | ||
| assert d["standalones"] == [] | ||
|
|
||
|
|
||
| # --------------- RunDescriberV0Dict --------------- | ||
|
|
||
|
|
||
| def test_v0_dict_instantiation() -> None: | ||
| d: RunDescriberV0Dict = { | ||
| "version": 0, | ||
| "interdependencies": {"paramspecs": ()}, | ||
| } | ||
| assert d["version"] == 0 | ||
|
|
||
|
|
||
| # --------------- RunDescriberV1Dict --------------- | ||
|
|
||
|
|
||
| def test_v1_dict_instantiation() -> None: | ||
| d: RunDescriberV1Dict = { | ||
| "version": 1, | ||
| "interdependencies": { | ||
| "parameters": {}, | ||
| "dependencies": {}, | ||
| "inferences": {}, | ||
| "standalones": [], | ||
| }, | ||
| } | ||
| assert d["version"] == 1 | ||
|
|
||
|
|
||
| # --------------- RunDescriberV2Dict inherits from V0 --------------- | ||
|
|
||
|
|
||
| def test_v2_dict_inherits_from_v0() -> None: | ||
| # typing_extensions TypedDict flattens __bases__ to (dict,) at runtime; | ||
| # verify structural inheritance via __orig_bases__ and annotations. | ||
| assert RunDescriberV0Dict in get_original_bases(RunDescriberV2Dict) | ||
| # V2 should contain all V0 keys plus its own | ||
| v0_keys = set(get_annotations(RunDescriberV0Dict)) | ||
| v2_keys = set(get_annotations(RunDescriberV2Dict)) | ||
| assert v0_keys.issubset(v2_keys) | ||
jenshnielsen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_v2_dict_instantiation() -> None: | ||
| d: RunDescriberV2Dict = { | ||
| "version": 2, | ||
| "interdependencies": {"paramspecs": ()}, | ||
| "interdependencies_": { | ||
| "parameters": {}, | ||
| "dependencies": {}, | ||
| "inferences": {}, | ||
| "standalones": [], | ||
| }, | ||
| } | ||
| assert d["version"] == 2 | ||
| assert "interdependencies_" in d | ||
|
|
||
|
|
||
| # --------------- RunDescriberV3Dict inherits from V2 --------------- | ||
|
|
||
|
|
||
| def test_v3_dict_inherits_from_v2() -> None: | ||
| assert RunDescriberV2Dict in get_original_bases(RunDescriberV3Dict) | ||
| v2_keys = set(get_annotations(RunDescriberV2Dict)) | ||
| v3_keys = set(get_annotations(RunDescriberV3Dict)) | ||
| assert v2_keys.issubset(v3_keys) | ||
|
|
||
|
|
||
| def test_v3_dict_inherits_from_v0_transitively() -> None: | ||
| # V3 inherits from V2 which inherits from V0 — all V0 keys present | ||
| v0_keys = set(get_annotations(RunDescriberV0Dict)) | ||
| v3_keys = set(get_annotations(RunDescriberV3Dict)) | ||
| assert v0_keys.issubset(v3_keys) | ||
|
|
||
|
|
||
| def test_v3_dict_instantiation() -> None: | ||
| d: RunDescriberV3Dict = { | ||
| "version": 3, | ||
| "interdependencies": {"paramspecs": ()}, | ||
| "interdependencies_": { | ||
| "parameters": {}, | ||
| "dependencies": {}, | ||
| "inferences": {}, | ||
| "standalones": [], | ||
| }, | ||
| "shapes": {"x": (10,)}, | ||
| } | ||
| assert d["version"] == 3 | ||
| assert d["shapes"] == {"x": (10,)} | ||
|
|
||
|
|
||
| def test_v3_dict_shapes_none() -> None: | ||
| d: RunDescriberV3Dict = { | ||
| "version": 3, | ||
| "interdependencies": {"paramspecs": ()}, | ||
| "interdependencies_": { | ||
| "parameters": {}, | ||
| "dependencies": {}, | ||
| "inferences": {}, | ||
| "standalones": [], | ||
| }, | ||
| "shapes": None, | ||
| } | ||
| assert d["shapes"] is None | ||
|
|
||
|
|
||
| # --------------- RunDescriberDicts union --------------- | ||
|
|
||
|
|
||
| def test_rundescriber_dicts_includes_all_versions() -> None: | ||
| args = typing.get_args(RunDescriberDicts) | ||
| expected = { | ||
| RunDescriberV0Dict, | ||
| RunDescriberV1Dict, | ||
| RunDescriberV2Dict, | ||
| RunDescriberV3Dict, | ||
| } | ||
| assert set(args) == expected | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.