Skip to content

Commit

Permalink
Make new tests more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
glatterf42 committed Aug 9, 2024
1 parent bd6fd59 commit 77d9fc8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 35 deletions.
47 changes: 16 additions & 31 deletions tests/core/test_optimization_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ def test_list_parameter(self, test_mp, request):
parameter_2 = run.optimization.parameters.create(
"Parameter 2", constrained_to_indexsets=["Indexset 2"]
)
# Create new run to test listing parameters 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.parameters.create(
"Parameter", constrained_to_indexsets=[indexset.name]
)
expected_ids = [parameter.id, parameter_2.id]
list_ids = [parameter.id for parameter in run.optimization.parameters.list()]
assert not (set(expected_ids) ^ set(list_ids))
Expand All @@ -267,21 +275,6 @@ def test_list_parameter(self, test_mp, request):
]
assert not (set(expected_id) ^ set(list_id))

# Test that only Parameters belonging to a Run are listed
run_2 = test_mp.runs.create("Model", "Scenario")
(indexset,) = create_indexsets_for_run(
platform=test_mp, run_id=run_2.id, amount=1
)
parameter_3 = run_2.optimization.parameters.create(
"Parameter", constrained_to_indexsets=[indexset.name]
)
parameter_4 = run_2.optimization.parameters.create(
"Parameter 2", constrained_to_indexsets=[indexset.name]
)
expected_ids = [parameter_3.id, parameter_4.id]
list_ids = [parameter.id for parameter in run_2.optimization.parameters.list()]
assert not (set(expected_ids) ^ set(list_ids))

def test_tabulate_parameter(self, test_mp, request):
test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore
run = test_mp.runs.create("Model", "Scenario")
Expand All @@ -297,6 +290,14 @@ def test_tabulate_parameter(self, test_mp, request):
name="Parameter 2",
constrained_to_indexsets=[indexset.name, indexset_2.name],
)
# Create new run to test listing parameters 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.parameters.create(
"Parameter", constrained_to_indexsets=[indexset_3.name]
)
pd.testing.assert_frame_equal(
df_from_list([parameter_2]),
run.optimization.parameters.tabulate(name="Parameter 2"),
Expand Down Expand Up @@ -326,22 +327,6 @@ def test_tabulate_parameter(self, test_mp, request):
run.optimization.parameters.tabulate(),
)

# Test that only Parameters belonging to a Run are listed
run_2 = test_mp.runs.create("Model", "Scenario")
(indexset,) = create_indexsets_for_run(
platform=test_mp, run_id=run_2.id, amount=1
)
parameter_3 = run_2.optimization.parameters.create(
"Parameter", constrained_to_indexsets=[indexset.name]
)
parameter_4 = run_2.optimization.parameters.create(
"Parameter 2", constrained_to_indexsets=[indexset.name]
)
pd.testing.assert_frame_equal(
df_from_list([parameter_3, parameter_4]),
run_2.optimization.parameters.tabulate(),
)

def test_parameter_docs(self, test_mp, request):
test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore
run = test_mp.runs.create("Model", "Scenario")
Expand Down
7 changes: 3 additions & 4 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

0 comments on commit 77d9fc8

Please sign in to comment.