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 6ddcc85 commit 7e1d7db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
47 changes: 16 additions & 31 deletions tests/core/test_optimization_equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ def test_list_equation(self, test_mp, request):
equation_2 = run.optimization.equations.create(
"Equation 2", constrained_to_indexsets=[indexset_2.name]
)
# Create new run to test listing equations of 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.equations.create(
"Equation", constrained_to_indexsets=[indexset.name]
)
expected_ids = [equation.id, equation_2.id]
list_ids = [equation.id for equation in run.optimization.equations.list()]
assert not (set(expected_ids) ^ set(list_ids))
Expand All @@ -264,21 +272,6 @@ def test_list_equation(self, test_mp, request):
]
assert not (set(expected_id) ^ set(list_id))

# Test listing Equations of 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
)
equation_3 = run_2.optimization.equations.create(
"Equation", constrained_to_indexsets=[indexset.name]
)
equation_4 = run_2.optimization.equations.create(
"Equation 2", constrained_to_indexsets=[indexset.name]
)
expected_ids = [equation_3.id, equation_4.id]
list_ids = [equation.id for equation in run_2.optimization.equations.list()]
assert not (set(expected_ids) ^ set(list_ids))

def test_tabulate_equation(self, test_mp, request):
test_mp: Platform = request.getfixturevalue(test_mp) # type: ignore
run = test_mp.runs.create("Model", "Scenario")
Expand All @@ -294,6 +287,14 @@ def test_tabulate_equation(self, test_mp, request):
name="Equation 2",
constrained_to_indexsets=[indexset.name, indexset_2.name],
)
# Create new run to test tabulating equations of 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.equations.create(
"Equation", constrained_to_indexsets=[indexset_3.name]
)
pd.testing.assert_frame_equal(
df_from_list([equation_2]),
run.optimization.equations.tabulate(name="Equation 2"),
Expand Down Expand Up @@ -321,22 +322,6 @@ def test_tabulate_equation(self, test_mp, request):
run.optimization.equations.tabulate(),
)

# Test tabulating Equations of 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
)
equation_3 = run_2.optimization.equations.create(
"Equation", constrained_to_indexsets=[indexset.name]
)
equation_4 = run_2.optimization.equations.create(
"Equation 2", constrained_to_indexsets=[indexset.name]
)
pd.testing.assert_frame_equal(
df_from_list([equation_3, equation_4]),
run_2.optimization.equations.tabulate(),
)

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

Please sign in to comment.