Skip to content

Commit ea1ef21

Browse files
committed
refactor tests
1 parent d68c1e0 commit ea1ef21

File tree

1 file changed

+113
-97
lines changed

1 file changed

+113
-97
lines changed

tests/code/test_pypeec.py

+113-97
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,136 @@
1313
import scisave
1414
import pypeec
1515

16+
# crash on warnings and ignore deprecation
17+
warnings.filterwarnings("error")
18+
warnings.filterwarnings("ignore", category=DeprecationWarning)
19+
warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
20+
1621
# disable logging
1722
scilogger.disable()
1823

1924
# get the path the folder
2025
PATH_ROOT = os.path.dirname(__file__)
2126

2227

23-
def _create_temp_file():
28+
def _get_run_mesher(use_script, file_geometry, file_voxel):
2429
"""
25-
Get a temporary file.
30+
Run the mesher.
2631
"""
2732

28-
(_, filename) = tempfile.mkstemp(suffix=".mpk")
29-
30-
return filename
33+
if use_script:
34+
status = pypeec.run_arguments(
35+
[
36+
"--quiet",
37+
"mesher",
38+
"--geometry",
39+
file_geometry,
40+
"--voxel",
41+
file_voxel,
42+
]
43+
)
44+
if status != 0:
45+
raise RuntimeError("invalid return code for the mesher")
46+
else:
47+
pypeec.run_mesher_file(
48+
file_geometry=file_geometry,
49+
file_voxel=file_voxel,
50+
)
3151

3252

33-
def _delete_temp_file(filename):
53+
def _get_run_solver(use_script, file_problem, file_tolerance, file_voxel, file_solution):
3454
"""
35-
Delete a temporary file.
55+
Run the solver.
3656
"""
3757

38-
try:
39-
os.remove(filename)
40-
except OSError:
41-
pass
42-
58+
if use_script:
59+
status = pypeec.run_arguments(
60+
[
61+
"--quiet",
62+
"solver",
63+
"--voxel",
64+
file_voxel,
65+
"--problem",
66+
file_problem,
67+
"--tolerance",
68+
file_tolerance,
69+
"--solution",
70+
file_solution,
71+
]
72+
)
73+
if status != 0:
74+
raise RuntimeError("invalid return code for the solver")
75+
else:
76+
pypeec.run_solver_file(
77+
file_voxel=file_voxel,
78+
file_problem=file_problem,
79+
file_tolerance=file_tolerance,
80+
file_solution=file_solution,
81+
)
82+
83+
84+
def _get_run_viewer(use_script, file_viewer, file_voxel):
85+
"""
86+
Run the viewer.
87+
"""
4388

44-
def _run_script(argv):
89+
if use_script:
90+
status = pypeec.run_arguments(
91+
[
92+
"--quiet",
93+
"viewer",
94+
"--voxel",
95+
file_voxel,
96+
"--viewer",
97+
file_viewer,
98+
"--plot_mode",
99+
"debug",
100+
]
101+
)
102+
if status != 0:
103+
raise RuntimeError("invalid return code for the viewer")
104+
else:
105+
pypeec.run_viewer_file(
106+
file_voxel=file_voxel,
107+
file_viewer=file_viewer,
108+
plot_mode="debug",
109+
)
110+
111+
112+
def _get_run_plotter(use_script, file_plotter, file_solution):
45113
"""
46-
Run the command line script with arguments.
114+
Run the plotter.
47115
"""
48116

49-
status = pypeec.run_arguments(argv)
50-
if status != 0:
51-
raise RuntimeError("invalid return code for the script")
117+
if use_script:
118+
status = pypeec.run_arguments(
119+
[
120+
"--quiet",
121+
"plotter",
122+
"--solution",
123+
file_solution,
124+
"--plotter",
125+
file_plotter,
126+
"--plot_mode",
127+
"debug",
128+
]
129+
)
130+
if status != 0:
131+
raise RuntimeError("invalid return code for the plotter")
132+
else:
133+
pypeec.run_plotter_file(
134+
file_solution=file_solution,
135+
file_plotter=file_plotter,
136+
plot_mode="debug",
137+
)
52138

53139

54140
def run_workflow(name, use_script):
55141
"""
56142
Run the complete workflow:
57143
- Run the mesher.
58-
- Run the viewer.
59144
- Run the solver.
145+
- Run the viewer.
60146
- Run the plotter.
61147
62148
The intermediate file are stored with temporary files.
@@ -67,11 +153,6 @@ def run_workflow(name, use_script):
67153
- With the API (pypeec.main).
68154
"""
69155

70-
# crash on warnings and ignore deprecation
71-
warnings.filterwarnings("error")
72-
warnings.filterwarnings("ignore", category=DeprecationWarning)
73-
warnings.filterwarnings("ignore", category=PendingDeprecationWarning)
74-
75156
# construct the folder path for the examples
76157
folder_examples = os.path.join(PATH_ROOT, "..", "..", "examples")
77158

@@ -85,88 +166,23 @@ def run_workflow(name, use_script):
85166
file_tolerance = os.path.join(folder_examples, "config", "tolerance.yaml")
86167

87168
# get the temporary files
88-
file_voxel = _create_temp_file()
89-
file_solution = _create_temp_file()
169+
(_, file_voxel) = tempfile.mkstemp(suffix=".mpk")
170+
(_, file_solution) = tempfile.mkstemp(suffix=".mpk")
90171

91-
# run the code
172+
# run the workflow and load the results
92173
try:
93174
# run the workflow
94-
if use_script:
95-
# get the arguments
96-
argv_me = [
97-
"--quiet",
98-
"mesher",
99-
"--geometry",
100-
file_geometry,
101-
"--voxel",
102-
file_voxel,
103-
]
104-
argv_vi = [
105-
"--quiet",
106-
"viewer",
107-
"--voxel",
108-
file_voxel,
109-
"--viewer",
110-
file_viewer,
111-
"--plot_mode",
112-
"debug",
113-
]
114-
argv_so = [
115-
"--quiet",
116-
"solver",
117-
"--voxel",
118-
file_voxel,
119-
"--problem",
120-
file_problem,
121-
"--tolerance",
122-
file_tolerance,
123-
"--solution",
124-
file_solution,
125-
]
126-
argv_pl = [
127-
"--quiet",
128-
"plotter",
129-
"--solution",
130-
file_solution,
131-
"--plotter",
132-
file_plotter,
133-
"--plot_mode",
134-
"debug",
135-
]
136-
137-
# run the scripts
138-
_run_script(argv_me)
139-
_run_script(argv_vi)
140-
_run_script(argv_so)
141-
_run_script(argv_pl)
142-
else:
143-
pypeec.run_mesher_file(
144-
file_geometry=file_geometry,
145-
file_voxel=file_voxel,
146-
)
147-
pypeec.run_solver_file(
148-
file_voxel=file_voxel,
149-
file_problem=file_problem,
150-
file_tolerance=file_tolerance,
151-
file_solution=file_solution,
152-
)
153-
pypeec.run_viewer_file(
154-
file_voxel=file_voxel,
155-
file_viewer=file_viewer,
156-
plot_mode="debug",
157-
)
158-
pypeec.run_plotter_file(
159-
file_solution=file_solution,
160-
file_plotter=file_plotter,
161-
plot_mode="debug",
162-
)
175+
_get_run_mesher(use_script, file_geometry, file_voxel)
176+
_get_run_solver(use_script, file_problem, file_tolerance, file_voxel, file_solution)
177+
_get_run_plotter(use_script, file_plotter, file_solution)
178+
_get_run_viewer(use_script, file_viewer, file_voxel)
163179

164180
# load the files
165181
data_voxel = scisave.load_data(file_voxel)
166182
data_solution = scisave.load_data(file_solution)
167183
finally:
168184
# close the temporary files
169-
_delete_temp_file(file_voxel)
170-
_delete_temp_file(file_solution)
185+
os.remove(file_voxel)
186+
os.remove(file_solution)
171187

172188
return data_voxel, data_solution

0 commit comments

Comments
 (0)