Skip to content

Commit

Permalink
Introduce run.get_by_id()
Browse files Browse the repository at this point in the history
  • Loading branch information
glatterf42 committed Nov 28, 2024
1 parent aece6a0 commit 11183d2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ixmp4/data/abstract/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@ def get_default_version(self, model_name: str, scenario_name: str) -> Run:
"""
...

def get_by_id(self, id: int) -> Run:
"""Retrieves a Run by its id.
Parameters
----------
id : int
Unique integer id.
Raises
------
:class:`ixmp4.data.abstract.Run.NotFound`.
If the Run with `id` does not exist.
Returns
-------
:class:`ixmp4.data.abstract.Run`:
The retrieved Run.
"""
...

Check warning on line 158 in ixmp4/data/abstract/run.py

View check run for this annotation

Codecov / codecov/patch

ixmp4/data/abstract/run.py#L158

Added line #L158 was not covered by tests

def list(self, **kwargs: Unpack[EnumerateKwargs]) -> list[Run]:
r"""Lists runs by specified criteria.
Expand Down
4 changes: 4 additions & 0 deletions ixmp4/data/api/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def get(self, model_name: str, scenario_name: str, version: int) -> Run:
is_default=None,
)

def get_by_id(self, id: int) -> Run:
res = self._get_by_id(id)
return Run(**res)

def enumerate(
self, **kwargs: Unpack[abstract.run.EnumerateKwargs]
) -> list[Run] | pd.DataFrame:
Expand Down
8 changes: 8 additions & 0 deletions ixmp4/server/rest/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ def unset_as_default_version(
backend: Backend = Depends(deps.get_backend),
) -> None:
backend.runs.unset_as_default_version(id)


@router.get("/{id}/", response_model=api.Run)
def get_by_id(
id: int,
backend: Backend = Depends(deps.get_backend),
) -> Run:
return backend.runs.get_by_id(id)
10 changes: 10 additions & 0 deletions tests/data/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import pytest

import ixmp4
import ixmp4.data
import ixmp4.data.abstract
from ixmp4.core.exceptions import NoDefaultRunVersion

from ..utils import assert_unordered_equality
Expand Down Expand Up @@ -55,6 +57,14 @@ def test_get_or_create_run(self, platform: ixmp4.Platform) -> None:
run3 = platform.backend.runs.get_or_create("Model", "Scenario")
assert run1.id == run3.id

def test_get_run_by_id(self, platform: ixmp4.Platform) -> None:
expected = platform.backend.runs.create("Model", "Scenario")
result = platform.backend.runs.get_by_id(id=expected.id)
assert expected.id == result.id

with pytest.raises(ixmp4.data.abstract.Run.NotFound):
_ = platform.backend.runs.get_by_id(id=expected.id + 1)

def test_list_run(self, platform: ixmp4.Platform) -> None:
run1 = platform.backend.runs.create("Model", "Scenario")
platform.backend.runs.create("Model", "Scenario")
Expand Down

0 comments on commit 11183d2

Please sign in to comment.