From 1409044c0000ec14ffa76f66fbcd6a5d0beecae0 Mon Sep 17 00:00:00 2001 From: Fridolin Glatter Date: Fri, 9 Aug 2024 08:57:44 +0200 Subject: [PATCH] Make new tests more efficient --- tests/core/test_optimization_variable.py | 47 ++++++++---------------- tests/utils.py | 7 ++-- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/tests/core/test_optimization_variable.py b/tests/core/test_optimization_variable.py index d22380a9..ccf5ba05 100644 --- a/tests/core/test_optimization_variable.py +++ b/tests/core/test_optimization_variable.py @@ -263,6 +263,14 @@ def test_list_variable(self, test_mp, request): variable_2 = run.optimization.variables.create( "Variable 2", constrained_to_indexsets=[indexset_2.name] ) + # Create new run to test listing variables for specific run + run_2 = test_mp.runs.create("Model", "Scenario") + (indexset,) = create_indexsets_for_run( + platform=test_mp, run_id=run_2.id, amount=1 + ) + run_2.optimization.variables.create( + "Variable", constrained_to_indexsets=[indexset.name] + ) expected_ids = [variable.id, variable_2.id] list_ids = [variable.id for variable in run.optimization.variables.list()] assert not (set(expected_ids) ^ set(list_ids)) @@ -274,21 +282,6 @@ def test_list_variable(self, test_mp, request): ] assert not (set(expected_id) ^ set(list_id)) - # Test listing Variables for specific Run - run_2 = test_mp.runs.create("Model", "Scenario") - (indexset,) = create_indexsets_for_run( - platform=test_mp, run_id=run_2.id, amount=1 - ) - variable_3 = run_2.optimization.variables.create( - "Variable", constrained_to_indexsets=[indexset.name] - ) - variable_4 = run_2.optimization.variables.create( - "Variable 2", constrained_to_indexsets=[indexset.name] - ) - expected_ids = [variable_3.id, variable_4.id] - list_ids = [variable.id for variable in run_2.optimization.variables.list()] - assert not (set(expected_ids) ^ set(list_ids)) - def test_tabulate_variable(self, test_mp, request): test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore run = test_mp.runs.create("Model", "Scenario") @@ -304,6 +297,14 @@ def test_tabulate_variable(self, test_mp, request): name="Variable 2", constrained_to_indexsets=[indexset.name, indexset_2.name], ) + # Create new run to test tabulating variables for specific run + run_2 = test_mp.runs.create("Model", "Scenario") + (indexset_3,) = create_indexsets_for_run( + platform=test_mp, run_id=run_2.id, amount=1 + ) + run_2.optimization.variables.create( + "Variable", constrained_to_indexsets=[indexset_3.name] + ) pd.testing.assert_frame_equal( df_from_list([variable_2]), run.optimization.variables.tabulate(name="Variable 2"), @@ -331,22 +332,6 @@ def test_tabulate_variable(self, test_mp, request): run.optimization.variables.tabulate(), ) - # Test tabulation of Variables for specific Run - run_2 = test_mp.runs.create("Model", "Scenario") - (indexset,) = create_indexsets_for_run( - platform=test_mp, run_id=run_2.id, amount=1 - ) - variable_3 = run_2.optimization.variables.create( - "Variable", constrained_to_indexsets=[indexset.name] - ) - variable_4 = run_2.optimization.variables.create( - "Variable 2", constrained_to_indexsets=[indexset.name] - ) - pd.testing.assert_frame_equal( - df_from_list([variable_3, variable_4]), - run_2.optimization.variables.tabulate(), - ) - def test_variable_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/utils.py b/tests/utils.py index e6da592f..79c8d826 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -203,13 +203,12 @@ def create_iamc_query_test_data(test_mp): def create_indexsets_for_run( - platform: Platform, run_id: int, amount: int = 2, offset: int = 0 + platform: Platform, run_id: int, amount: int = 2, offset: int = 1 ) -> tuple[IndexSet, ...]: - """Create `amount` indexsets called `Indexset n` for `run` (n in (offset, - offset+amount]).""" + """Create `amount` indexsets called `Indexset n` for `run`.""" return tuple( platform.backend.optimization.indexsets.create( - run_id=run_id, name=f"Indexset {i+1}" + run_id=run_id, name=f"Indexset {i}" ) for i in range(offset, offset + amount) )