Skip to content

Commit

Permalink
Push out api and storage to fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Oct 14, 2024
1 parent bf4d6f6 commit 3ee1ead
Showing 1 changed file with 76 additions and 77 deletions.
153 changes: 76 additions & 77 deletions tests/unit_tests/gui/tools/plot/test_plot_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,86 +164,85 @@ def test_plot_api_request_errors(api):
api.data_for_key(ensemble.id, "should_not_be_there")


def test_plot_api_handles_empty_gen_kw(tmp_path, monkeypatch):
@pytest.fixture
def api_and_storage(monkeypatch, tmp_path):
with open_storage(tmp_path / "storage", mode="w") as storage:
monkeypatch.setenv("ERT_STORAGE_NO_TOKEN", "yup")
monkeypatch.setenv("ERT_STORAGE_ENS_PATH", storage.path)
api = PlotApi()
key = "gen_kw"
name = "<poro>"
experiment = storage.create_experiment(
parameters=[
GenKwConfig(
name=key,
forward_init=False,
update=False,
template_file=None,
output_file=None,
transform_function_definitions=[],
),
],
responses=[],
observations={},
)
ensemble = storage.create_ensemble(experiment.id, ensemble_size=10)
assert api.data_for_key(str(ensemble.id), key).empty
ensemble.save_parameters(
key,
1,
xr.Dataset(
{
"values": ("names", [1.0]),
"transformed_values": ("names", [1.0]),
"names": [name],
}
yield api, storage
if enkf._storage is not None:
enkf._storage.close()
enkf._storage = None
gc.collect()


def test_plot_api_handles_empty_gen_kw(api_and_storage):
api, storage = api_and_storage
key = "gen_kw"
name = "<poro>"
experiment = storage.create_experiment(
parameters=[
GenKwConfig(
name=key,
forward_init=False,
update=False,
template_file=None,
output_file=None,
transform_function_definitions=[],
),
)
assert api.data_for_key(str(ensemble.id), key + ":" + name).to_csv() == dedent(
"""\
Realization,0
1,1.0
"""
)
if enkf._storage is not None:
enkf._storage.close()
enkf._storage = None
gc.collect()


def test_plot_api_handles_non_existant_gen_kw(tmp_path, monkeypatch):
with open_storage(tmp_path / "storage", mode="w") as storage:
monkeypatch.setenv("ERT_STORAGE_NO_TOKEN", "yup")
monkeypatch.setenv("ERT_STORAGE_ENS_PATH", storage.path)
api = PlotApi()
experiment = storage.create_experiment(
parameters=[
GenKwConfig(
name="gen_kw",
forward_init=False,
update=False,
template_file=None,
output_file=None,
transform_function_definitions=[],
),
],
responses=[],
observations={},
)
ensemble = storage.create_ensemble(experiment.id, ensemble_size=10)
ensemble.save_parameters(
"gen_kw",
1,
xr.Dataset(
{
"values": ("names", [1.0]),
"transformed_values": ("names", [1.0]),
"names": ["key"],
}
],
responses=[],
observations={},
)
ensemble = storage.create_ensemble(experiment.id, ensemble_size=10)
assert api.data_for_key(str(ensemble.id), key).empty
ensemble.save_parameters(
key,
1,
xr.Dataset(
{
"values": ("names", [1.0]),
"transformed_values": ("names", [1.0]),
"names": [name],
}
),
)
assert api.data_for_key(str(ensemble.id), key + ":" + name).to_csv() == dedent(
"""\
Realization,0
1,1.0
"""
)


def test_plot_api_handles_non_existant_gen_kw(api_and_storage):
api, storage = api_and_storage
experiment = storage.create_experiment(
parameters=[
GenKwConfig(
name="gen_kw",
forward_init=False,
update=False,
template_file=None,
output_file=None,
transform_function_definitions=[],
),
)
assert api.data_for_key(str(ensemble.id), "gen_kw").empty
assert api.data_for_key(str(ensemble.id), "gen_kw:does_not_exist").empty
if enkf._storage is not None:
enkf._storage.close()
enkf._storage = None
gc.collect()
],
responses=[],
observations={},
)
ensemble = storage.create_ensemble(experiment.id, ensemble_size=10)
ensemble.save_parameters(
"gen_kw",
1,
xr.Dataset(
{
"values": ("names", [1.0]),
"transformed_values": ("names", [1.0]),
"names": ["key"],
}
),
)
assert api.data_for_key(str(ensemble.id), "gen_kw").empty
assert api.data_for_key(str(ensemble.id), "gen_kw:does_not_exist").empty

0 comments on commit 3ee1ead

Please sign in to comment.