From 65b957a82f873199d36a80b0925b9baab1e0c4a7 Mon Sep 17 00:00:00 2001 From: Paul Natsuo Kishimoto Date: Mon, 30 Oct 2023 12:15:10 +0100 Subject: [PATCH] Test from_url() --- message_ix_models/report/computations.py | 29 ++++++++----------- .../tests/report/test_computations.py | 27 ++++++++++++++--- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/message_ix_models/report/computations.py b/message_ix_models/report/computations.py index 394c4109a7..20b5acfb2f 100644 --- a/message_ix_models/report/computations.py +++ b/message_ix_models/report/computations.py @@ -210,23 +210,18 @@ def remove_ts( # Non-weak references to objects to keep them alive _FROM_URL_REF: Set[Any] = set() -# def from_url(url: str) -> message_ix.Scenario: -# """Return a :class:`message_ix.Scenario` given its `url`. -# -# .. todo:: Move upstream to :mod:`message_ix.reporting`. -# .. todo:: Create a similar method in :mod:`ixmp.reporting` to load and return -# :class:`ixmp.TimeSeries` (or :class:`ixmp.Scenario`) given its `url`. -# """ -# s, mp = message_ix.Scenario.from_url(url) -# assert s is not None -# _FROM_URL_REF.add(s) -# _FROM_URL_REF.add(mp) -# return s - - -def from_url(url: str) -> ixmp.TimeSeries: - """Return a :class:`ixmp.TimeSeries` given its `url`.""" - ts, mp = ixmp.TimeSeries.from_url(url) + +def from_url(url: str, cls=ixmp.TimeSeries) -> ixmp.TimeSeries: + """Return a :class:`ixmp.TimeSeries` or subclass instance, given its `url`. + + .. todo:: Move upstream, to :mod:`ixmp.reporting`. + + Parameters + ---------- + cls : type, *optional* + Subclass to instantiate and return; for instance, |Scenario|. + """ + ts, mp = cls.from_url(url) assert ts is not None _FROM_URL_REF.add(ts) _FROM_URL_REF.add(mp) diff --git a/message_ix_models/tests/report/test_computations.py b/message_ix_models/tests/report/test_computations.py index ed71e2a3b3..2c7b4544e7 100644 --- a/message_ix_models/tests/report/test_computations.py +++ b/message_ix_models/tests/report/test_computations.py @@ -1,9 +1,12 @@ import re +import ixmp +import message_ix import pandas as pd import pytest import xarray as xr from genno import Computer, Quantity +from message_ix.testing import make_dantzig from message_ix_models import ScenarioInfo from message_ix_models.model.structure import get_codes @@ -25,6 +28,12 @@ def c() -> Computer: return Computer() +@pytest.fixture +def scenario(test_context): + mp = test_context.get_platform() + yield make_dantzig(mp) + + def test_compound_growth(): """:func:`.compound_growth` on a 2-D quantity.""" qty = Quantity( @@ -65,12 +74,22 @@ def test_filter_ts(): assert {"ar"} == set(result.variable.unique()) -@pytest.mark.xfail(reason="Incomplete") -def test_from_url(): - from_url() +def test_from_url(scenario): + full_url = f"ixmp://{scenario.platform.name}/{scenario.url}" + + # Operator runs + result = from_url(full_url) + # Result is of the default class + assert result.__class__ is ixmp.TimeSeries + # Same object was retrieved + assert scenario.url == result.url + + # Same, but specifying message_ix.Scenario + result = from_url(full_url, message_ix.Scenario) + assert result.__class__ is message_ix.Scenario + assert scenario.url == result.url -@pytest.mark.xfail(reason="Incomplete") def test_get_ts(): get_ts()