-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathtest_other.py
More file actions
48 lines (39 loc) · 1.72 KB
/
test_other.py
File metadata and controls
48 lines (39 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Standard Python modules
import os
import sys
import unittest
# First party modules
from pyoptsparse import Optimizers, list_optimizers
from pyoptsparse.pyOpt_solution import SolutionInform
from pyoptsparse.pyOpt_utils import try_import_compiled_module_from_path
# we have to unset this environment variable because otherwise
# the snopt module gets automatically imported, thus failing the import test below
os.environ.pop("PYOPTSPARSE_IMPORT_SNOPT_FROM", None)
class TestImportSnoptFromPath(unittest.TestCase):
def test_nonexistent_path(self):
# first unload `snopt` from namespace
for key in list(sys.modules.keys()):
if "snopt" in key:
sys.modules.pop(key)
with self.assertWarns(UserWarning):
module = try_import_compiled_module_from_path("snopt", "/a/nonexistent/path", raise_warning=True)
self.assertTrue(isinstance(module, str))
def test_sys_path_unchanged(self):
path = tuple(sys.path)
try_import_compiled_module_from_path("snopt", "/some/path")
self.assertEqual(tuple(sys.path), path)
class TestListOpt(unittest.TestCase):
def test_list_optimizers(self):
all_opt = list_optimizers()
self.assertIn(Optimizers.SLSQP, all_opt)
self.assertIn(Optimizers.CONMIN, all_opt)
self.assertIn(Optimizers.PSQP, all_opt)
self.assertIn(Optimizers.ALPSO, all_opt)
self.assertIn(Optimizers.NSGA2, all_opt)
class TestSolInform(unittest.TestCase):
def test_sol_inform_key_access(self):
sol = SolutionInform(value=1, message="test message")
assert sol.value == 1
assert sol["value"] == 1
assert sol.message == "test message"
assert sol["text"] == "test message"