Skip to content

Commit

Permalink
Address many unexpected warnings in pytest (#1403)
Browse files Browse the repository at this point in the history
* Fix grid integration class names

* Fixing pytest marks and more TestClass names

* Fixing some regexes

* Fixing returns in tests

* Cleaning up surrogte plotting tests

* Fixing plotting modifications

* Fixing some re.escapes

* Fixing misused pytest fixtures

* Fixing more re.escapes

* Hopefully the last re.escapes

* Missed re.escape

* Fixing typo
  • Loading branch information
andrewlee94 committed May 6, 2024
1 parent bc2f6c0 commit 6fe724a
Show file tree
Hide file tree
Showing 58 changed files with 479 additions and 1,169 deletions.
34 changes: 18 additions & 16 deletions idaes/apps/grid_integration/tests/test_bidder.py
Expand Up @@ -11,18 +11,20 @@
# for full copyright and license information.
#################################################################################
import pytest

import pyomo.environ as pyo
from pyomo.common import unittest as pyo_unittest

from idaes.apps.grid_integration.bidder import Bidder
from idaes.apps.grid_integration.tests.util import (
TestingModel,
TestingForecaster,
ExampleModel,
ExampleForecaster,
testing_model_data,
)
from pyomo.common import unittest as pyo_unittest
from idaes.apps.grid_integration.coordinator import prescient_avail


class TestMissingModel:
class MissingModel:
"""
A class for testing missing methods and attributes.
"""
Expand Down Expand Up @@ -62,14 +64,14 @@ def __init__(self, missing_method=None, missing_attr=None):
def test_model_object_missing_methods():

solver = pyo.SolverFactory("cbc")
forecaster = TestingForecaster(prediction=30)
forecaster = ExampleForecaster(prediction=30)

# By definition, the model object should contain these methods
method_list = ["populate_model", "update_model"]

# test if the correct error message is raised if a model misses necessary methods
for m in method_list:
bidding_model_object = TestMissingModel(missing_method=m)
bidding_model_object = MissingModel(missing_method=m)
with pytest.raises(AttributeError, match=r".*{}().*".format(m)):
bidder_object = Bidder(
bidding_model_object=bidding_model_object,
Expand All @@ -85,14 +87,14 @@ def test_model_object_missing_methods():
def test_model_object_missing_attr():

solver = pyo.SolverFactory("cbc")
forecaster = TestingForecaster(prediction=30)
forecaster = ExampleForecaster(prediction=30)

# By definition, the model object should contain these attributes
attr_list = ["power_output", "total_cost", "model_data"]

# test if the correct error message is raised if a model misses necessary attributes
for attr in attr_list:
bidding_model_object = TestMissingModel(missing_attr=attr)
bidding_model_object = MissingModel(missing_attr=attr)
with pytest.raises(AttributeError, match=r".*{}().*".format(attr)):
bidder_object = Bidder(
bidding_model_object=bidding_model_object,
Expand All @@ -108,8 +110,8 @@ def test_model_object_missing_attr():
def test_n_scenario_checker():

solver = pyo.SolverFactory("cbc")
forecaster = TestingForecaster(prediction=30)
bidding_model_object = TestingModel(model_data=testing_model_data)
forecaster = ExampleForecaster(prediction=30)
bidding_model_object = ExampleModel(model_data=testing_model_data)

# test if bidder raise error when negative number of scenario is given
with pytest.raises(ValueError, match=r".*greater than zero.*"):
Expand Down Expand Up @@ -137,8 +139,8 @@ def test_n_scenario_checker():
@pytest.mark.unit
def test_solver_checker():

forecaster = TestingForecaster(prediction=30)
bidding_model_object = TestingModel(model_data=testing_model_data)
forecaster = ExampleForecaster(prediction=30)
bidding_model_object = ExampleModel(model_data=testing_model_data)

# test if bidder raise error when invalid solver is provided
invalid_solvers = [5, "cbc", "ipopt"]
Expand All @@ -158,10 +160,10 @@ def test_solver_checker():
def bidder_object():

solver = pyo.SolverFactory("cbc")
forecaster = TestingForecaster(prediction=30)
forecaster = ExampleForecaster(prediction=30)

# create a bidder model
bidding_model_object = TestingModel(model_data=testing_model_data)
bidding_model_object = ExampleModel(model_data=testing_model_data)
bidder_object = Bidder(
bidding_model_object=bidding_model_object,
day_ahead_horizon=day_ahead_horizon,
Expand Down Expand Up @@ -299,11 +301,11 @@ def test_compute_RT_bids(bidder_object):
def bidder_object_pcost():

solver = pyo.SolverFactory("cbc")
forecaster = TestingForecaster(prediction=30)
forecaster = ExampleForecaster(prediction=30)

# create a bidder model
testing_model_data.include_default_p_cost = False
bidding_model_object = TestingModel(model_data=testing_model_data)
bidding_model_object = ExampleModel(model_data=testing_model_data)
bidder_object = Bidder(
bidding_model_object=bidding_model_object,
day_ahead_horizon=day_ahead_horizon,
Expand Down
15 changes: 8 additions & 7 deletions idaes/apps/grid_integration/tests/test_coordinator.py
Expand Up @@ -13,18 +13,19 @@
import pytest

import pyomo.environ as pyo
from pyomo.common import unittest as pyo_unittest

from idaes.apps.grid_integration.bidder import Bidder
from idaes.apps.grid_integration.tracker import Tracker
from idaes.apps.grid_integration.coordinator import DoubleLoopCoordinator
from idaes.apps.grid_integration.tests.util import (
TestingModel,
TestingForecaster,
ExampleModel,
ExampleForecaster,
testing_model_data,
testing_renewable_data,
renewable_generator_params,
testing_generator_params,
)
from pyomo.common import unittest as pyo_unittest

tracking_horizon = 4
day_ahead_bidding_horizon = 48
Expand All @@ -46,7 +47,7 @@ def coordinator_object(request):
else:
model_data = testing_renewable_data

tracking_model_object = TestingModel(model_data=model_data)
tracking_model_object = ExampleModel(model_data=model_data)
thermal_tracker = Tracker(
tracking_model_object=tracking_model_object,
tracking_horizon=tracking_horizon,
Expand All @@ -55,7 +56,7 @@ def coordinator_object(request):
)

# make a projection tracker
projection_tracking_model_object = TestingModel(model_data=model_data)
projection_tracking_model_object = ExampleModel(model_data=model_data)
thermal_projection_tracker = Tracker(
tracking_model_object=projection_tracking_model_object,
tracking_horizon=tracking_horizon,
Expand All @@ -64,8 +65,8 @@ def coordinator_object(request):
)

## create a bidder
forecaster = TestingForecaster(prediction=30)
bidding_model_object = TestingModel(model_data=model_data)
forecaster = ExampleForecaster(prediction=30)
bidding_model_object = ExampleModel(model_data=model_data)
thermal_bidder = Bidder(
bidding_model_object=bidding_model_object,
day_ahead_horizon=day_ahead_bidding_horizon,
Expand Down
7 changes: 3 additions & 4 deletions idaes/apps/grid_integration/tests/test_integration.py
Expand Up @@ -19,10 +19,6 @@

import pytest


# define custom type for type hinting
PrescientOptions = Dict[str, Union[str, bool, Number, dict]]

from idaes.apps.grid_integration import DoubleLoopCoordinator
from idaes.apps.grid_integration.tests.util import (
make_testing_tracker,
Expand All @@ -41,6 +37,9 @@
projection_tracker=thermal_projection_tracker,
)

# define custom type for type hinting
PrescientOptions = Dict[str, Union[str, bool, Number, dict]]


class TestDoubleLoopIntegration:
"Integration test for the double loop using 5bus use case."
Expand Down
14 changes: 7 additions & 7 deletions idaes/apps/grid_integration/tests/test_tracker.py
Expand Up @@ -13,10 +13,10 @@
import pytest
import pyomo.environ as pyo
from idaes.apps.grid_integration.tracker import Tracker
from idaes.apps.grid_integration.tests.util import TestingModel, testing_model_data
from idaes.apps.grid_integration.tests.util import ExampleModel, testing_model_data


class TestMissingModel:
class MissingModel:
"""
A class for testing missing methods and attributes.
"""
Expand Down Expand Up @@ -68,7 +68,7 @@ def test_model_object_missing_methods():

# test if the correct error message is raised if a model misses necessary methods
for m in method_list:
tracking_model_object = TestMissingModel(missing_method=m)
tracking_model_object = MissingModel(missing_method=m)
with pytest.raises(AttributeError, match=r".*{}().*".format(m)):
tracker_object = Tracker(
tracking_model_object=tracking_model_object,
Expand All @@ -87,7 +87,7 @@ def test_model_object_missing_attr():

# test if the correct error message is raised if a model misses necessary attributes
for a in attr_list:
tracking_model_object = TestMissingModel(missing_attr=a)
tracking_model_object = MissingModel(missing_attr=a)
with pytest.raises(AttributeError, match=r".*{}.*".format(a)):
tracker_object = Tracker(
tracking_model_object=tracking_model_object,
Expand All @@ -100,7 +100,7 @@ def test_model_object_missing_attr():
@pytest.mark.unit
def test_n_tracking_hour_checker():
solver = pyo.SolverFactory("cbc")
tracking_model_object = TestingModel(model_data=testing_model_data)
tracking_model_object = ExampleModel(model_data=testing_model_data)

# test if tracker raise error when negative number of hours is given
n_tracking_hour = -1
Expand All @@ -126,7 +126,7 @@ def test_n_tracking_hour_checker():
@pytest.mark.unit
def test_solver_checker():
n_tracking_hour = 1
tracking_model_object = TestingModel(model_data=testing_model_data)
tracking_model_object = ExampleModel(model_data=testing_model_data)

# test if bidder raise error when invalid solver is provided
invalid_solvers = [5, "cbc", "ipopt"]
Expand All @@ -146,7 +146,7 @@ def tracker_object():
solver = pyo.SolverFactory("cbc")

# create a tracker model
tracking_model_object = TestingModel(model_data=testing_model_data)
tracking_model_object = ExampleModel(model_data=testing_model_data)
tracker_object = Tracker(
tracking_model_object=tracking_model_object,
tracking_horizon=horizon,
Expand Down
12 changes: 6 additions & 6 deletions idaes/apps/grid_integration/tests/util.py
Expand Up @@ -22,7 +22,7 @@
)


class TestingModel:
class ExampleModel:
"""
Simple model object for testing.
"""
Expand Down Expand Up @@ -256,7 +256,7 @@ def total_cost(self):
return ("tot_cost", 1)


class TestingForecaster(AbstractPrescientPriceForecaster):
class ExampleForecaster(AbstractPrescientPriceForecaster):
"""
A fake forecaster class for testing.
"""
Expand Down Expand Up @@ -345,7 +345,7 @@ def make_testing_forecaster():
forecaster: a forecaster object for testing.
"""

return TestingForecaster(prediction=30)
return ExampleForecaster(prediction=30)


def make_testing_tracker():
Expand All @@ -359,7 +359,7 @@ def make_testing_tracker():
thermal_tracker: a tracker object for testing.
"""

tracking_model_object = TestingModel(model_data=testing_model_data)
tracking_model_object = ExampleModel(model_data=testing_model_data)
thermal_tracker = Tracker(
tracking_model_object=tracking_model_object,
tracking_horizon=tracking_horizon,
Expand All @@ -383,7 +383,7 @@ def make_testing_bidder():

forecaster = make_testing_forecaster()

bidding_model_object = TestingModel(model_data=testing_model_data)
bidding_model_object = ExampleModel(model_data=testing_model_data)
thermal_bidder = Bidder(
bidding_model_object=bidding_model_object,
day_ahead_horizon=day_ahead_bidding_horizon,
Expand All @@ -409,7 +409,7 @@ def make_testing_selfscheduler():

forecaster = make_testing_forecaster()

bidding_model_object = TestingModel(model_data=testing_model_data)
bidding_model_object = ExampleModel(model_data=testing_model_data)
self_scheduler = SelfScheduler(
bidding_model_object=bidding_model_object,
day_ahead_horizon=day_ahead_bidding_horizon,
Expand Down
8 changes: 4 additions & 4 deletions idaes/apps/matopt/opt/pyomo_modeling.py
Expand Up @@ -1462,11 +1462,11 @@ def addConsZicFromYiLifted(m, blnConfsAreMutExc=True, blnConfsAreColExh=False):
for c in m.Zic[i, :].wildcard_keys()
if m.Confs[c][l] is None
)
if PosZic is not 0:
if PosZic != 0:
m.AssignZicFromYiLifted1.add(
index=(i, j), expr=(PosZic <= m.Yi[j])
)
if NegZic is not 0:
if NegZic != 0:
m.AssignZicFromYiLifted2.add(
index=(i, j), expr=(NegZic <= 1 - m.Yi[j])
)
Expand Down Expand Up @@ -1584,11 +1584,11 @@ def addConsZicFromYikLifted(m, blnConfsAreMutExc=True, blnConfsAreColExh=False):
for c in m.Zic[i, :].wildcard_keys()
if m.Confs[c][l] != k
)
if PosZic is not 0:
if PosZic != 0:
m.AssignZicFromYikLifted1.add(
index=(i, j, k), expr=(PosZic <= m.Yik[j, k])
)
if NegZic is not 0:
if NegZic != 0:
m.AssignZicFromYikLifted2.add(
index=(i, j, k), expr=(NegZic <= 1 - m.Yik[j, k])
)
Expand Down
17 changes: 11 additions & 6 deletions idaes/core/base/tests/test_components.py
Expand Up @@ -16,6 +16,7 @@
Author: Andrew Lee
"""
import pytest
import re
import types

from pyomo.environ import ConcreteModel, Set, Param, Var, units as pyunits
Expand Down Expand Up @@ -84,19 +85,23 @@ def test_populate_component_list(self, m):
def test_is_solute(self, m):
with pytest.raises(
TypeError,
match="comp Generic Component objects do not "
"support is_solute\(\) method. Use a Solvent or "
"Solute Component instead.",
match=re.escape(
"comp Generic Component objects do not "
"support is_solute() method. Use a Solvent or "
"Solute Component instead."
),
):
m.comp.is_solute()

@pytest.mark.unit
def test_is_solvent(self, m):
with pytest.raises(
TypeError,
match="comp Generic Component objects do not "
"support is_solvent\(\) method. Use a Solvent or "
"Solute Component instead.",
match=re.escape(
"comp Generic Component objects do not "
"support is_solvent() method. Use a Solvent or "
"Solute Component instead."
),
):
m.comp.is_solvent()

Expand Down
12 changes: 8 additions & 4 deletions idaes/core/base/tests/test_control_volume_1d.py
Expand Up @@ -16,6 +16,8 @@
Author: Andrew Lee
"""
import pytest
import re

from pyomo.environ import (
ConcreteModel,
Constraint,
Expand Down Expand Up @@ -418,7 +420,7 @@ def test_add_geometry_length_var_indexed():

with pytest.raises(
ConfigurationError,
match="fs.cv length_var must be a scalar \(unindexed\) component.",
match=re.escape("fs.cv length_var must be a scalar (unindexed) component."),
):
m.fs.cv.add_geometry(length_var=m.fs.length)

Expand Down Expand Up @@ -2310,9 +2312,11 @@ def test_add_total_component_balances_in_rxns_no_idx():

with pytest.raises(
PropertyNotSupportedError,
match="fs.cv Property package does not contain a "
"list of inherent reactions \(inherent_reaction_idx\), "
"but include_inherent_reactions is True.",
match=re.escape(
"fs.cv Property package does not contain a "
"list of inherent reactions (inherent_reaction_idx), "
"but include_inherent_reactions is True."
),
):
m.fs.cv.add_total_component_balances()

Expand Down

0 comments on commit 6fe724a

Please sign in to comment.