|
2 | 2 |
|
3 | 3 | from datetime import date |
4 | 4 | from pathlib import Path |
| 5 | +from contextlib import contextmanager |
5 | 6 | import math |
6 | 7 | import os |
7 | 8 | import random |
|
30 | 31 | REPRESENTATIVE_PROBLEMS = ["ALLINITU", "ALLINIT", "ALSOTAME", "ALLINITA"] |
31 | 32 |
|
32 | 33 |
|
| 34 | +@contextmanager |
| 35 | +def _temporary_env(**updates): |
| 36 | + previous = {key: os.environ.get(key) for key in updates} |
| 37 | + try: |
| 38 | + for key, value in updates.items(): |
| 39 | + if value is None: |
| 40 | + os.environ.pop(key, None) |
| 41 | + else: |
| 42 | + os.environ[key] = str(value) |
| 43 | + yield |
| 44 | + finally: |
| 45 | + for key, value in previous.items(): |
| 46 | + if value is None: |
| 47 | + os.environ.pop(key, None) |
| 48 | + else: |
| 49 | + os.environ[key] = value |
| 50 | + |
| 51 | + |
33 | 52 | def _as_array(value): |
34 | 53 | if value is None: |
35 | 54 | return np.empty(0) |
@@ -96,6 +115,64 @@ def test_daily_random_small_problem_sample(self): |
96 | 115 | with self.subTest(problem=problem_name): |
97 | 116 | _assert_problem_contract(self, problem_name) |
98 | 117 |
|
| 118 | + def test_config_environment_overrides_variable_size(self): |
| 119 | + options = { |
| 120 | + "ptype": "ubln", |
| 121 | + "maxdim": 5, |
| 122 | + "maxb": 20, |
| 123 | + "maxlcon": 20, |
| 124 | + "maxnlcon": 20, |
| 125 | + "maxcon": 20, |
| 126 | + } |
| 127 | + with _temporary_env( |
| 128 | + S2MPJ_VARIABLE_SIZE=None, |
| 129 | + S2MPJ_TEST_FEASIBILITY_PROBLEMS=None, |
| 130 | + ): |
| 131 | + default_names = s2mpj_select(dict(options)) |
| 132 | + with _temporary_env( |
| 133 | + S2MPJ_VARIABLE_SIZE="all", |
| 134 | + S2MPJ_TEST_FEASIBILITY_PROBLEMS="0", |
| 135 | + ): |
| 136 | + all_names = s2mpj_select(dict(options)) |
| 137 | + |
| 138 | + self.assertGreater(len(all_names), len(default_names)) |
| 139 | + self.assertIn("CHEBYQAD_2", all_names) |
| 140 | + self.assertNotIn("CHEBYQAD_2", default_names) |
| 141 | + |
| 142 | + def test_config_environment_overrides_feasibility_selection(self): |
| 143 | + options = { |
| 144 | + "ptype": "ubln", |
| 145 | + "maxdim": 5, |
| 146 | + "maxb": 20, |
| 147 | + "maxlcon": 20, |
| 148 | + "maxnlcon": 20, |
| 149 | + "maxcon": 20, |
| 150 | + } |
| 151 | + with _temporary_env( |
| 152 | + S2MPJ_VARIABLE_SIZE="default", |
| 153 | + S2MPJ_TEST_FEASIBILITY_PROBLEMS="1", |
| 154 | + ): |
| 155 | + feasibility_names = s2mpj_select(dict(options)) |
| 156 | + with _temporary_env( |
| 157 | + S2MPJ_VARIABLE_SIZE="default", |
| 158 | + S2MPJ_TEST_FEASIBILITY_PROBLEMS="2", |
| 159 | + ): |
| 160 | + all_names = s2mpj_select(dict(options)) |
| 161 | + |
| 162 | + self.assertIn("ARGAUSS", feasibility_names) |
| 163 | + self.assertNotIn("ALLINITU", feasibility_names) |
| 164 | + self.assertIn("ARGAUSS", all_names) |
| 165 | + self.assertIn("ALLINITU", all_names) |
| 166 | + |
| 167 | + def test_config_environment_rejects_invalid_values(self): |
| 168 | + options = {"ptype": "u", "maxdim": 5} |
| 169 | + with _temporary_env(S2MPJ_VARIABLE_SIZE="not-a-mode"): |
| 170 | + with self.assertRaises(ValueError): |
| 171 | + s2mpj_select(dict(options)) |
| 172 | + with _temporary_env(S2MPJ_TEST_FEASIBILITY_PROBLEMS="3"): |
| 173 | + with self.assertRaises(ValueError): |
| 174 | + s2mpj_select(dict(options)) |
| 175 | + |
99 | 176 |
|
100 | 177 | if __name__ == "__main__": |
101 | 178 | unittest.main() |
0 commit comments