-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_settings.py
266 lines (203 loc) · 7.48 KB
/
test_settings.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import random
import string
import pathlib
from unittest.mock import patch
import pytest
from hydep.settings import HydepSettings, SubSetting, asBool
def test_settings():
ARCHIVE = "archive on success"
DEP_SOLVER = "depletion solver"
BC = "boundary conditions"
FIT_ORDER = "fitting order"
N_FIT_POINTS = "fitting points"
UNBOUND_FIT = "unbounded fitting"
h = HydepSettings()
h.update(
{
ARCHIVE: "true",
DEP_SOLVER: "cram48",
BC: "reflective reflective vacuum",
FIT_ORDER: 2,
N_FIT_POINTS: 4,
UNBOUND_FIT: False,
}
)
assert h.archiveOnSuccess
assert h.depletionSolver == "cram48"
assert h.boundaryConditions == ("reflective", "reflective", "vacuum")
assert h.fittingOrder == 2
assert h.numFittingPoints == 4
assert not h.unboundedFitting
h.validate()
h.update(
{
ARCHIVE: "0",
BC: "vacuum",
FIT_ORDER: "1",
N_FIT_POINTS: "none",
UNBOUND_FIT: "y",
}
)
assert not h.archiveOnSuccess
assert h.boundaryConditions == ("vacuum", ) * 3
assert h.fittingOrder == 1
assert h.numFittingPoints is None
assert h.unboundedFitting
with pytest.raises(TypeError, match=".*archive.*bool"):
h.update({ARCHIVE: "positive"})
assert not h.archiveOnSuccess
with pytest.raises(ValueError, match=".*[B|b]oundary"):
HydepSettings().update({BC: ["reflective", "very strange", "vacuum"]})
assert h.boundaryConditions == ("vacuum", ) * 3
with pytest.raises(TypeError):
h.archiveOnSuccess = 1
with pytest.raises(TypeError):
h.unboundedFitting = 1
fresh = HydepSettings(
archiveOnSuccess=True,
depletionSolver="testSolver",
boundaryConditions="reflective",
)
assert fresh.boundaryConditions == ("reflective", ) * 3
assert fresh.archiveOnSuccess
assert fresh.depletionSolver == "testSolver"
def test_validate():
with pytest.raises(ValueError):
HydepSettings(fittingOrder=2, numFittingPoints=1).validate()
with pytest.raises(ValueError):
HydepSettings(numFittingPoints=1, unboundedFitting=True).validate()
# Only enforce if number of previous points is definitely given
HydepSettings(numFittingPoints=None, unboundedFitting=True).validate()
def test_subsettings():
randomSection = "".join(random.sample(string.ascii_letters, 10))
settings = HydepSettings()
assert not hasattr(settings, "test")
assert not hasattr(settings, randomSection)
class IncompleteSetting(SubSetting, sectionName="incomplete"):
pass
with pytest.raises(TypeError, match=".*abstract methods"):
IncompleteSetting()
class MySubSettings(SubSetting, sectionName="test"):
def __init__(self):
self.truth = True
def update(self, options):
v = options.get("truth", None)
if v is not None:
self.truth = asBool("truth", v)
t = settings.test
assert isinstance(t, MySubSettings)
assert not hasattr(settings, randomSection)
assert t.truth
settings.updateAll({"hydep.test": {"truth": "0"}})
assert not t.truth
fresh = HydepSettings()
fresh.updateAll(
{"hydep": {"depletion solver": "cram48"}, "hydep.test": {"truth": "n"}}
)
assert fresh.depletionSolver == "cram48"
assert not fresh.test.truth
with pytest.raises(ValueError, match=f".*{randomSection}"):
fresh.updateAll({f"hydep.{randomSection}": {"key": "value"}})
with pytest.raises(ValueError, match=".*test"):
class DuplicateSetting(SubSetting, sectionName="test"):
pass
@pytest.mark.parametrize(
"name", ("0hello", "hello world", "hydep.serpent", "mock-test", "w!ld3xample")
)
def test_badSubsectionNames(name):
with pytest.raises(ValueError, match=f".*{name}"):
class Failure(SubSetting, sectionName=name):
pass
def test_directories(tmpdir):
PWD = pathlib.Path.cwd()
FAKE_DIR = pathlib.Path(tmpdir)
settings = HydepSettings(basedir=FAKE_DIR, rundir=None)
assert settings.basedir.is_absolute()
assert settings.basedir == FAKE_DIR
assert settings.rundir is None
assert not settings.useTempDir
# Resolution relative to current working directory
settings.rundir = FAKE_DIR.name
assert settings.rundir.is_absolute()
assert settings.rundir == PWD / FAKE_DIR.name
# String contains an absolute path
settings.basedir = str(FAKE_DIR)
assert settings.basedir.is_absolute()
assert settings.basedir == FAKE_DIR
# Check defaulting to CWD
fresh = HydepSettings(basedir=None)
assert fresh.basedir.is_absolute()
assert fresh.basedir == pathlib.Path.cwd()
with pytest.raises(TypeError):
fresh.basedir = None
assert fresh.basedir == pathlib.Path.cwd()
fresh.update(
{
"basedir": FAKE_DIR.name,
"rundir": "nONe",
"use temp dir": "true",
}
)
assert fresh.basedir == PWD / FAKE_DIR.name
assert fresh.basedir.is_absolute()
assert fresh.rundir is None
assert fresh.useTempDir
fresh.update({"rundir": FAKE_DIR.name, "use temp dir": "falSe"})
assert fresh.rundir == PWD / FAKE_DIR.name
assert fresh.rundir.is_absolute()
assert not fresh.useTempDir
with pytest.raises(TypeError):
fresh.update({"basedir": "none"})
@pytest.fixture
def serpentdata(tmpdir):
datadir = pathlib.Path(tmpdir / "serpentdata")
datadir.mkdir()
# Must match up with example config file
files = {"sss_endfb7u.xsdata", "sss_endfb71.dec", "sss_endfb71.nfy"}
for name in files:
(datadir / name).touch()
with patch.dict(
"os.environ", {"SERPENT_DATA": str(datadir), "OMP_NUM_THREADS": ""}
):
yield datadir
for name in files:
(datadir / name).unlink()
def test_exampleConfig(serpentdata):
cfg = pathlib.Path(__file__).parents[1] / "hydep.cfg.example"
assert cfg.is_file(), cfg
settings = HydepSettings.fromFile(cfg)
assert settings.boundaryConditions == ("reflective", "vacuum", "reflective")
assert settings.basedir == pathlib.Path("example/base").resolve()
assert settings.depletionSolver == "48"
assert settings.rundir == pathlib.Path("example/run").resolve()
assert not settings.useTempDir
assert settings.fittingOrder == 0
assert settings.numFittingPoints == 2
assert not settings.unboundedFitting # default
serpent = settings.serpent
assert serpent.acelib.parent == serpentdata
assert serpent.acelib.name == "sss_endfb7u.xsdata"
assert serpent.declib.parent == serpentdata
assert serpent.declib.name == "sss_endfb71.dec"
assert serpent.nfylib.parent == serpentdata
assert serpent.nfylib.name == "sss_endfb71.nfy"
assert serpent.particles == int(5e6)
assert serpent.generationsPerBatch == 10
assert serpent.active == 20
assert serpent.inactive == 15
assert serpent.seed == 123456
assert serpent.k0 == 1.2
assert serpent.executable == "sss2"
assert serpent.omp == 16
assert serpent.mpi == 2
sfv = settings.sfv
assert sfv.modes == 10
assert sfv.modeFraction == 0.75
assert sfv.densityCutoff == 1e-20
def test_emptyconfig(tmpdir):
cfg = tmpdir / "bad.cfg"
cfg.write("[DEFAULT]\nkey = value\n")
with pytest.raises(KeyError):
HydepSettings.fromFile(cfg, strict=True)
with pytest.warns(UserWarning):
HydepSettings.fromFile(cfg, strict=False)