From 3ee1eaddab966fe8acda57498369b27b25ba5dcb Mon Sep 17 00:00:00 2001 From: Eivind Jahren Date: Mon, 14 Oct 2024 12:59:21 +0200 Subject: [PATCH] Push out api and storage to fixture --- .../gui/tools/plot/test_plot_api.py | 153 +++++++++--------- 1 file changed, 76 insertions(+), 77 deletions(-) diff --git a/tests/unit_tests/gui/tools/plot/test_plot_api.py b/tests/unit_tests/gui/tools/plot/test_plot_api.py index 077e88cde1f..3c001ea2b73 100644 --- a/tests/unit_tests/gui/tools/plot/test_plot_api.py +++ b/tests/unit_tests/gui/tools/plot/test_plot_api.py @@ -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 = "" - 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 = "" + 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