Skip to content

Commit

Permalink
Update tests to not directly instantiate the LegacySolverWrapper mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
jsiirola committed Aug 6, 2024
1 parent 017258d commit 61bb290
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions pyomo/contrib/solver/tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
from pyomo.contrib.solver import base


class _LegacyWrappedSolverBase(base.LegacySolverWrapper, base.SolverBase):
pass


class TestSolverBase(unittest.TestCase):
def test_abstract_member_list(self):
expected_list = ['solve', 'available', 'version']
Expand Down Expand Up @@ -192,11 +196,13 @@ def test_class_method_list(self):
]
self.assertEqual(sorted(expected_list), sorted(method_list))

@unittest.mock.patch.multiple(_LegacyWrappedSolverBase, __abstractmethods__=set())
def test_context_manager(self):
with base.LegacySolverWrapper() as instance:
with self.assertRaises(AttributeError):
instance.available()
with _LegacyWrappedSolverBase() as instance:
self.assertIsInstance(instance, _LegacyWrappedSolverBase)
self.assertFalse(instance.available(False))

@unittest.mock.patch.multiple(_LegacyWrappedSolverBase, __abstractmethods__=set())
def test_map_config(self):
# Create a fake/empty config structure that can be added to an empty
# instance of LegacySolverWrapper
Expand All @@ -205,7 +211,7 @@ def test_map_config(self):
'solver_options',
ConfigDict(implicit=True, description="Options to pass to the solver."),
)
instance = base.LegacySolverWrapper()
instance = _LegacyWrappedSolverBase()
instance.config = self.config
instance._map_config(
True, False, False, 20, True, False, None, None, None, False, None, None
Expand Down Expand Up @@ -272,20 +278,21 @@ def test_map_config(self):
with self.assertRaises(AttributeError):
print(instance.config.keepfiles)

@unittest.mock.patch.multiple(_LegacyWrappedSolverBase, __abstractmethods__=set())
def test_solver_options_behavior(self):
# options can work in multiple ways (set from instantiation, set
# after instantiation, set during solve).
# Test case 1: Set at instantiation
solver = base.LegacySolverWrapper(options={'max_iter': 6})
solver = _LegacyWrappedSolverBase(options={'max_iter': 6})
self.assertEqual(solver.options, {'max_iter': 6})

# Test case 2: Set later
solver = base.LegacySolverWrapper()
solver = _LegacyWrappedSolverBase()
solver.options = {'max_iter': 4, 'foo': 'bar'}
self.assertEqual(solver.options, {'max_iter': 4, 'foo': 'bar'})

# Test case 3: pass some options to the mapping (aka, 'solve' command)
solver = base.LegacySolverWrapper()
solver = _LegacyWrappedSolverBase()
config = ConfigDict(implicit=True)
config.declare(
'solver_options',
Expand All @@ -296,7 +303,7 @@ def test_solver_options_behavior(self):
self.assertEqual(solver.config.solver_options, {'max_iter': 4})

# Test case 4: Set at instantiation and override during 'solve' call
solver = base.LegacySolverWrapper(options={'max_iter': 6})
solver = _LegacyWrappedSolverBase(options={'max_iter': 6})
config = ConfigDict(implicit=True)
config.declare(
'solver_options',
Expand All @@ -309,11 +316,11 @@ def test_solver_options_behavior(self):

# solver_options are also supported
# Test case 1: set at instantiation
solver = base.LegacySolverWrapper(solver_options={'max_iter': 6})
solver = _LegacyWrappedSolverBase(solver_options={'max_iter': 6})
self.assertEqual(solver.options, {'max_iter': 6})

# Test case 2: pass some solver_options to the mapping (aka, 'solve' command)
solver = base.LegacySolverWrapper()
solver = _LegacyWrappedSolverBase()
config = ConfigDict(implicit=True)
config.declare(
'solver_options',
Expand All @@ -324,7 +331,7 @@ def test_solver_options_behavior(self):
self.assertEqual(solver.config.solver_options, {'max_iter': 4})

# Test case 3: Set at instantiation and override during 'solve' call
solver = base.LegacySolverWrapper(solver_options={'max_iter': 6})
solver = _LegacyWrappedSolverBase(solver_options={'max_iter': 6})
config = ConfigDict(implicit=True)
config.declare(
'solver_options',
Expand All @@ -337,7 +344,7 @@ def test_solver_options_behavior(self):

# users can mix... sort of
# Test case 1: Initialize with options, solve with solver_options
solver = base.LegacySolverWrapper(options={'max_iter': 6})
solver = _LegacyWrappedSolverBase(options={'max_iter': 6})
config = ConfigDict(implicit=True)
config.declare(
'solver_options',
Expand All @@ -351,11 +358,11 @@ def test_solver_options_behavior(self):
# do we know what to do with it then?
# Test case 1: Class instance
with self.assertRaises(ValueError):
solver = base.LegacySolverWrapper(
solver = _LegacyWrappedSolverBase(
options={'max_iter': 6}, solver_options={'max_iter': 4}
)
# Test case 2: Passing to `solve`
solver = base.LegacySolverWrapper()
solver = _LegacyWrappedSolverBase()
config = ConfigDict(implicit=True)
config.declare(
'solver_options',
Expand Down

0 comments on commit 61bb290

Please sign in to comment.