Skip to content

Commit

Permalink
Adding new test_relax_growth_constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
adrivinca committed Oct 22, 2024
1 parent 447afeb commit d7ea29a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
2 changes: 2 additions & 0 deletions message_ix_models/model/water/data/water_for_ppl.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ def relax_growth_constraint(ref_hist, scen, cooling_df, g_lo, constraint_type):
map_parent.rename(columns={"technology_name": "technology"}, inplace=True)
# expand bound_up to all cooling technologies in map_parent
combined_bound = pd.merge(combined_bound, map_parent, how="left")
# rename tear_type to year_act, because g_lo use it
combined_bound.rename(columns={year_type: "year_act"}, inplace=True)

# Merge to g_lo to be able to remove the technologies
g_lo = pd.merge(g_lo, combined_bound, how="left")
Expand Down
106 changes: 105 additions & 1 deletion message_ix_models/tests/model/water/data/test_water_for_ppl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
from message_ix_models import ScenarioInfo

# from message_ix_models.model.structure import get_codes
from message_ix_models.model.water.data.water_for_ppl import cool_tech, non_cooling_tec
from message_ix_models.model.water.data.water_for_ppl import (
cool_tech,
non_cooling_tec,
relax_growth_constraint,
)


@cool_tech.minimum_version
Expand Down Expand Up @@ -201,3 +205,103 @@ def test_non_cooling_tec(request, test_context):
"year_act",
]
)


# Mock function for scen.par
class MockScenario:
def par(self, param, filters):
if param == "bound_activity_up":
return pd.DataFrame(
{
"node_loc": ["R12_AFR", "R12_AFR", "R12_AFR"],
"technology": ["coal_ppl", "coal_ppl", "coal_ppl"],
"year_act": [2030, 2040, 2050],
"value": [30, 15, 0],
}
)
elif param == "bound_new_capacity_up":
return pd.DataFrame(
{
"node_loc": ["R12_AFR", "R12_AFR", "R12_AFR"],
"technology": ["coal_ppl", "coal_ppl", "coal_ppl"],
"year_vtg": [2030, 2040, 2050],
"value": [30, 15, 0],
}
)
return pd.DataFrame()

Check warning on line 231 in message_ix_models/tests/model/water/data/test_water_for_ppl.py

View check run for this annotation

Codecov / codecov/patch

message_ix_models/tests/model/water/data/test_water_for_ppl.py#L231

Added line #L231 was not covered by tests


@pytest.mark.parametrize("constraint_type", ["activity", "new_capacity"])
def test_relax_growth_constraint(constraint_type):
# Sample data for g_lo
if constraint_type == "activity":
year_type = "year_act"
elif constraint_type == "new_capacity":
year_type = "year_vtg"
else:
raise ValueError(

Check warning on line 242 in message_ix_models/tests/model/water/data/test_water_for_ppl.py

View check run for this annotation

Codecov / codecov/patch

message_ix_models/tests/model/water/data/test_water_for_ppl.py#L242

Added line #L242 was not covered by tests
"Invalid constraint_type. Must be 'activity' or 'new_capacity'."
)

g_lo = pd.DataFrame(
{
"node_loc": ["R12_AFR", "R12_AFR", "R12_AFR", "R12_AFR"],
"technology": [
"coal_ppl__ot_fresh",
"coal_ppl__ot_fresh",
"coal_ppl__ot_fresh",
"gas_ppl__ot_fresh",
],
"year_act": [2030, 2040, 2050, 2030],
"time": ["year", "year", "year", "year"],
"value": [-0.05, -0.05, -0.05, -0.05],
"unit": ["%", "%", "%", "%"],
}
)

# Sample data for ref_hist
ref_hist = pd.DataFrame(
{
"node_loc": ["R12_AFR", "R12_AFR", "R12_AFR"],
"technology": ["coal_ppl", "coal_ppl", "coal_ppl"],
year_type: [2015, 2020, 2025],
"time": ["year", "year", "year"],
"value": [30, 50, 80],
"unit": ["GWa", "GWa", "GWa"],
}
)

# Sample data for cooling_df
cooling_df = pd.DataFrame(
{
"technology_name": [
"coal_ppl__ot_fresh",
"coal_ppl__ot_fresh",
"coal_ppl__ot_fresh",
],
"parent_tech": ["coal_ppl", "coal_ppl", "coal_ppl"],
}
)

# Instantiate mock scenario
scen = MockScenario()

# Call the function with mock data
result = relax_growth_constraint(ref_hist, scen, cooling_df, g_lo, constraint_type)
# reset_index to make the comparison easier
result = result.reset_index(drop=True)

# Expected result (this is an example, you should adjust this based on expected behavior)
expected_result = pd.DataFrame(
{
"node_loc": ["R12_AFR", "R12_AFR"],
"technology": ["coal_ppl__ot_fresh", "gas_ppl__ot_fresh"],
"year_act": [2050, 2030],
"time": ["year", "year"],
"value": [-0.05, -0.05],
"unit": ["%", "%"],
}
)

# Assert that the result matches the expected DataFrame
pd.testing.assert_frame_equal(result, expected_result)

0 comments on commit d7ea29a

Please sign in to comment.