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 60dfca4 commit 1409044
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_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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")
Expand All @@ -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"),
Expand Down Expand Up @@ -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")
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 1409044

Please sign in to comment.