Skip to content
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

Introduce run.get_by_id() #140

Open
wants to merge 1 commit into
base: enh/run-remove-solution
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_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
Loading