Skip to content

Commit

Permalink
adding unit-test for src/evaluation_system/api
Browse files Browse the repository at this point in the history
  • Loading branch information
Mo authored and Mo committed Apr 4, 2024
1 parent f2fff47 commit 8a99a88
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ docs/source/_static/*.gif
!docs/build/.empty
*.egg-info
venv/
tmp/
10 changes: 9 additions & 1 deletion src/evaluation_system/api/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def _set_environment(

@property
def conda_path(self) -> str:
"""Add the conda env path of the plugin to the environment.i
"""Add the conda env path of the plugin to the environment.
:meta private:
"""
Expand All @@ -421,6 +421,10 @@ def _run_tool(
out_file: Optional[Path] = None,
rowid: Optional[int] = None,
) -> Optional[Any]:
"""Run the plugin with the given configuration.
:meta private:
"""
config_dict = self._append_unique_id(config_dict, unique_output)
if out_file is None:
is_interactive_job = True
Expand Down Expand Up @@ -632,6 +636,10 @@ def prepare_output(
return result

def _extend_output_metadata(self, file_path, metadata):
"""Extend the metadata dictionary with file information.
:meta private:
"""
fstat = os.stat(file_path)
if "timestamp" not in metadata:
metadata["timestamp"] = fstat[stat.ST_CTIME]
Expand Down
15 changes: 13 additions & 2 deletions src/evaluation_system/api/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@ def reload_plugins(user_name: Optional[str] = None) -> None:
def get_plugins_user() -> dict[str, dict[str, PluginMetadata]]:
"""Get plugins per user
Example
-------
In order to get the plugins for the current user, you can use the:
.. code-block:: python
from evaluation_system.api.plugin import get_plugins_user
plugins = get_plugins_user()
print(plugins)
Returns
-------
dict[str, dict[str, PluginMetadata]]
Expand Down Expand Up @@ -484,7 +495,7 @@ def parse_arguments(
complete_conf.update(p.read_configuration(f))
# now if we still have a config file update what the configuration with it
if isinstance(config_file, str):
if config_file == "-":
if config_file == "-": # TODO: find out what this is for
# reading from stdin
complete_conf.update(p.read_configuration(sys.stdin))
elif config_file is not None:
Expand Down Expand Up @@ -1478,5 +1489,5 @@ def find_plugin_class(mod: ModuleType) -> type[PluginAbstract]:
# This only runs once after start. To load new plugins on the fly we have
# 2 possibilities:
# 1) Watch the tool directory
# 2) Use the plugin metaclass trigger (see `evaluation_system.api.plugin`
# 2) Use the plugin metaclass trigger (see `evaluation_system.api.plugin`)
reload_plugins()
7 changes: 6 additions & 1 deletion src/evaluation_system/tests/crawl_my_data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,12 @@ def test_link_my_data(dummy_crawl, dummy_plugin, valid_data_files, time_mock):

input_files = list(valid_data_files.rglob("*.nc"))
dummy_plugin.add_output_to_databrowser(
valid_data_files, "muh", "mah", experiment="foo"
valid_data_files,
"muh",
"mah",
experiment="foo",
time_frequency="1day",
variable="tas",
)
assert len(list(freva.databrowser(experiment="foo"))) == len(input_files)
assert len(list(freva.databrowser(product="muh.mah"))) == len(input_files)
Expand Down
1 change: 1 addition & 0 deletions src/evaluation_system/tests/history_models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pwd
import unittest
from datetime import datetime
from unittest.mock import MagicMock, patch


def test_history_model(test_user):
Expand Down
38 changes: 18 additions & 20 deletions src/evaluation_system/tests/plugin_command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,24 @@ def test_run_pyclientplugin(dummy_history):
freva.run_plugin("dummyplugin0")


# def test_plugin_status(dummy_env, caplog) -> None:
# """Test the plugin status quries."""
# import os

# import freva

# res = freva.run_plugin("dummyplugin", the_number=2, other=-5, batchmode=True)
# with pytest.raises(ValueError):
# res.wait(2)
# # The following is a hack to get the `batch_id`, but it is not recommended
# # and in the future need to be refactored
# assert res.status == "running" or res.status == "scheduled"
# res.kill()
# time.sleep(0.5)
# assert res.status == "broken"
# res.kill()
# res = freva.run_plugin("dummyplugin", the_number=2, other=-1, batchmode=True)
# res.wait()
# assert res.status == "finished"
# assert isinstance(res.batch_id, int)
def test_plugin_status(dummy_env, caplog) -> None:
"""Test the plugin status quries."""
import os

import freva

# res = freva.run_plugin("dummyplugin", the_number=2, other=-5, batchmode=True)
# # with pytest.raises(ValueError):
# # res.wait()
# # assert res.status == "running"
# res.kill()
# time.sleep(0.5)
# assert res.status == "broken"
# res.kill()
res = freva.run_plugin("dummyplugin", the_number=2, other=-1, batchmode=False)
res.wait()
assert res.status == "finished"
# assert isinstance(res.batch_id, int)


def test_plugin_output(dummy_history) -> None:
Expand Down
Loading

0 comments on commit 8a99a88

Please sign in to comment.