Skip to content

Commit

Permalink
Test from_url()
Browse files Browse the repository at this point in the history
  • Loading branch information
khaeru committed Oct 30, 2023
1 parent 90f3c37 commit db0c8d0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
29 changes: 12 additions & 17 deletions message_ix_models/report/computations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 23 additions & 4 deletions message_ix_models/tests/report/test_computations.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit db0c8d0

Please sign in to comment.