Skip to content

Commit

Permalink
Add mock testing for XpressDirect/XpressPersistent.available()
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiirola committed Nov 5, 2024
1 parent 168389d commit 63c674e
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions pyomo/solvers/tests/checks/test_xpress_persistent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________
import logging

import pyomo.common.unittest as unittest
import pyomo.environ as pe
import pyomo.solvers.plugins.solvers.xpress_direct as xpd

from pyomo.common.log import LoggingIntercept
from pyomo.core.expr.taylor_series import taylor_series_expansion
from pyomo.opt.results.solver import TerminationCondition, SolverStatus
from pyomo.solvers.plugins.solvers.xpress_persistent import XpressPersistent

xpress_available = pe.SolverFactory('xpress_persistent').available(False)

Expand Down Expand Up @@ -330,3 +335,52 @@ def test_nonconvexqp_infeasible(self):
self.assertEqual(
results.solver.termination_condition, TerminationCondition.infeasible
)

def test_available(self):
class mock_xpress(object):
def __init__(self, importable, initable):
self._initable = initable
xpd.xpress_available = importable

def log_import_warning(self, logger):
logging.getLogger(logger).warning("import warning")

def init(self):
if not self._initable:
raise RuntimeError("init failed")

def free(self):
pass

orig = xpd.xpress, xpd.xpress_available
try:
_xpress_persistent = XpressPersistent
xpd.xpress = mock_xpress(True, True)
with LoggingIntercept() as LOG:
self.assertTrue(XpressPersistent().available(True))
self.assertTrue(XpressPersistent().available(False))
self.assertEqual(LOG.getvalue(), "")

xpd.xpress = mock_xpress(False, False)
with LoggingIntercept() as LOG:
self.assertFalse(XpressPersistent().available(False))
self.assertEqual(LOG.getvalue(), "")
with LoggingIntercept() as LOG:
with self.assertRaisesRegex(
xpd.ApplicationError,
"No Python bindings available for .*XpressPersistent.* "
"solver plugin",
):
XpressPersistent().available(True)
self.assertEqual(LOG.getvalue(), "import warning\n")

xpd.xpress = mock_xpress(True, False)
with LoggingIntercept() as LOG:
self.assertFalse(XpressPersistent().available(False))
self.assertEqual(LOG.getvalue(), "")
with LoggingIntercept() as LOG:
with self.assertRaisesRegex(RuntimeError, "init failed"):
XpressPersistent().available(True)
self.assertEqual(LOG.getvalue(), "")
finally:
xpd.xpress, xpd.xpress_available = orig

0 comments on commit 63c674e

Please sign in to comment.