diff --git a/ixmp4/core/optimization/table.py b/ixmp4/core/optimization/table.py index 065d6793..dad74e6d 100644 --- a/ixmp4/core/optimization/table.py +++ b/ixmp4/core/optimization/table.py @@ -116,4 +116,4 @@ def list(self, name: str | None = None) -> Iterable[Table]: ] def tabulate(self, name: str | None = None) -> pd.DataFrame: - return self.backend.optimization.tables.tabulate(name=name) + return self.backend.optimization.tables.tabulate(run_id=self._run.id, name=name) diff --git a/tests/core/test_table.py b/tests/core/test_table.py index ac923f4d..16661857 100644 --- a/tests/core/test_table.py +++ b/tests/core/test_table.py @@ -1,7 +1,7 @@ import pandas as pd import pytest -from ixmp4 import Platform, Table +from ixmp4.core import Platform, Table from ..utils import all_platforms @@ -216,8 +216,6 @@ def test_table_add_data(self, test_mp, request): def test_list_tables(self, test_mp, request): test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore run = test_mp.runs.create("Model", "Scenario") - # Per default, list() lists scalars for `default` version runs: - run.set_as_default() _ = run.optimization.indexsets.create("Indexset") _ = run.optimization.indexsets.create("Indexset 2") table = run.optimization.tables.create( @@ -235,11 +233,23 @@ def test_list_tables(self, test_mp, request): list_id = [table.id for table in run.optimization.tables.list(name="Table")] assert not (set(expected_id) ^ set(list_id)) + # Test that only Tables belonging to this Run are listed + run_2 = test_mp.runs.create("Model", "Scenario") + indexset_3 = run_2.optimization.indexsets.create("Indexset 3") + indexset_4 = run_2.optimization.indexsets.create("Indexset 4") + table_3 = run_2.optimization.tables.create( + "Table", constrained_to_indexsets=[indexset_3.name, indexset_4.name] + ) + table_4 = run_2.optimization.tables.create( + "Table 2", constrained_to_indexsets=[indexset_3.name, indexset_4.name] + ) + expected_ids = [table_3.id, table_4.id] + list_ids = [table.id for table in run_2.optimization.tables.list()] + assert not (set(expected_ids) ^ set(list_ids)) + def test_tabulate_table(self, test_mp, request): test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore run = test_mp.runs.create("Model", "Scenario") - # Per default, tabulate() lists scalars for `default` version runs: - run.set_as_default() indexset = run.optimization.indexsets.create("Indexset") indexset_2 = run.optimization.indexsets.create("Indexset 2") table = run.optimization.tables.create( @@ -266,6 +276,21 @@ def test_tabulate_table(self, test_mp, request): run.optimization.tables.tabulate(), ) + # Test that only Tables belonging to this Run are listed + run_2 = test_mp.runs.create("Model", "Scenario") + indexset_3 = run_2.optimization.indexsets.create("Indexset 3") + indexset_4 = run_2.optimization.indexsets.create("Indexset 4") + table_3 = run_2.optimization.tables.create( + "Table", constrained_to_indexsets=[indexset_3.name, indexset_4.name] + ) + table_4 = run_2.optimization.tables.create( + "Table 2", constrained_to_indexsets=[indexset_3.name, indexset_4.name] + ) + pd.testing.assert_frame_equal( + df_from_list([table_3, table_4]), + run_2.optimization.tables.tabulate(), + ) + def test_table_docs(self, test_mp, request): test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore run = test_mp.runs.create("Model", "Scenario") diff --git a/tests/data/test_optimization_table.py b/tests/data/test_optimization_table.py index d2b3c49c..c25f31d8 100644 --- a/tests/data/test_optimization_table.py +++ b/tests/data/test_optimization_table.py @@ -1,7 +1,7 @@ import pandas as pd import pytest -from ixmp4 import Platform, Table +from ixmp4.core import Platform, Table from ..utils import all_platforms @@ -101,7 +101,7 @@ def test_create_table(self, test_mp, request): assert table_3.columns[1].dtype == "int64" def test_get_table(self, test_mp, request): - test_mp = request.getfixturevalue(test_mp) + test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore run = test_mp.backend.runs.create("Model", "Scenario") _ = test_mp.backend.optimization.indexsets.create( run_id=run.id, name="Indexset" @@ -267,10 +267,8 @@ def test_table_add_data(self, test_mp, request): assert table_5.data == test_data_5 def test_list_table(self, test_mp, request): - test_mp = request.getfixturevalue(test_mp) + test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore run = test_mp.backend.runs.create("Model", "Scenario") - # Per default, list() lists scalars for `default` version runs: - test_mp.backend.runs.set_as_default_version(run.id) _ = test_mp.backend.optimization.indexsets.create( run_id=run.id, name="Indexset" ) @@ -287,11 +285,27 @@ def test_list_table(self, test_mp, request): assert [table] == test_mp.backend.optimization.tables.list(name="Table") + # Test listing of Tables when specifying a Run + run_2 = test_mp.backend.runs.create("Model", "Scenario") + indexset_3 = test_mp.backend.optimization.indexsets.create( + run_id=run_2.id, name="Indexset 3" + ) + indexset_4 = test_mp.backend.optimization.indexsets.create( + run_id=run_2.id, name="Indexset 4" + ) + table_3 = test_mp.backend.optimization.tables.create( + run_id=run_2.id, name="Table", constrained_to_indexsets=[indexset_3.name] + ) + table_4 = test_mp.backend.optimization.tables.create( + run_id=run_2.id, name="Table 2", constrained_to_indexsets=[indexset_4.name] + ) + assert [table_3, table_4] == test_mp.backend.optimization.tables.list( + run_id=run_2.id + ) + def test_tabulate_table(self, test_mp, request): test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore run = test_mp.backend.runs.create("Model", "Scenario") - # Per default, tabulate() lists scalars for `default` version runs: - test_mp.backend.runs.set_as_default_version(run.id) indexset = test_mp.backend.optimization.indexsets.create( run_id=run.id, name="Indexset" ) @@ -334,3 +348,22 @@ def test_tabulate_table(self, test_mp, request): df_from_list([table, table_2]), test_mp.backend.optimization.tables.tabulate(), ) + + # Test tabulation of Tables when specifying a Run + run_2 = test_mp.backend.runs.create("Model", "Scenario") + indexset_3 = test_mp.backend.optimization.indexsets.create( + run_id=run_2.id, name="Indexset 3" + ) + indexset_4 = test_mp.backend.optimization.indexsets.create( + run_id=run_2.id, name="Indexset 4" + ) + table_3 = test_mp.backend.optimization.tables.create( + run_id=run_2.id, name="Table", constrained_to_indexsets=[indexset_3.name] + ) + table_4 = test_mp.backend.optimization.tables.create( + run_id=run_2.id, name="Table 2", constrained_to_indexsets=[indexset_4.name] + ) + pd.testing.assert_frame_equal( + df_from_list([table_3, table_4]), + test_mp.backend.optimization.tables.tabulate(run_id=run_2.id), + )