|
1 | | -import os |
2 | | - |
3 | 1 | import pytest |
4 | 2 |
|
5 | 3 | import pals |
6 | 4 |
|
7 | 5 |
|
8 | | -def test_yaml(): |
| 6 | +def test_yaml(tmp_path): |
9 | 7 | # Create one base element |
10 | 8 | element1 = pals.Marker(name="element1") |
11 | 9 | # Create one thick element |
12 | 10 | element2 = pals.Drift(name="element2", length=2.0) |
13 | 11 | # Create line with both elements |
14 | 12 | line = pals.BeamLine(name="line", line=[element1, element2]) |
15 | 13 | # Serialize the BeamLine object to YAML |
16 | | - test_file = "line.pals.yaml" |
| 14 | + test_file = tmp_path / "line.pals.yaml" |
17 | 15 | line.to_file(test_file) |
18 | 16 | # Read the YAML data from the test file |
19 | 17 | loaded_line = pals.BeamLine.from_file(test_file) |
20 | | - # Remove the test file |
21 | | - os.remove(test_file) |
22 | 18 | # Validate loaded BeamLine object |
23 | 19 | assert line == loaded_line |
24 | 20 |
|
25 | 21 |
|
26 | | -def test_json(): |
| 22 | +def test_json(tmp_path): |
27 | 23 | # Create one base element |
28 | 24 | element1 = pals.Marker(name="element1") |
29 | 25 | # Create one thick element |
30 | 26 | element2 = pals.Drift(name="element2", length=2.0) |
31 | 27 | # Create line with both elements |
32 | 28 | line = pals.BeamLine(name="line", line=[element1, element2]) |
33 | 29 | # Serialize the BeamLine object to JSON |
34 | | - test_file = "line.pals.json" |
| 30 | + test_file = tmp_path / "line.pals.json" |
35 | 31 | line.to_file(test_file) |
36 | 32 | # Read the JSON data from the test file |
37 | 33 | loaded_line = pals.BeamLine.from_file(test_file) |
38 | | - # Remove the test file |
39 | | - os.remove(test_file) |
40 | 34 | # Validate loaded BeamLine object |
41 | 35 | assert line == loaded_line |
42 | 36 |
|
43 | 37 |
|
44 | | -def test_comprehensive_lattice(): |
| 38 | +def test_comprehensive_lattice(tmp_path): |
45 | 39 | """Test a comprehensive lattice using every PALS element at least once""" |
46 | 40 |
|
47 | 41 | # Create elements in alphabetical order for easy maintenance |
@@ -216,7 +210,7 @@ def test_comprehensive_lattice(): |
216 | 210 | ) |
217 | 211 |
|
218 | 212 | # Write to temporary file |
219 | | - yaml_file = "comprehensive_lattice.pals.yaml" |
| 213 | + yaml_file = tmp_path / "comprehensive_lattice.pals.yaml" |
220 | 214 | lattice.to_file(yaml_file) |
221 | 215 |
|
222 | 216 | # Read back from file |
@@ -274,7 +268,7 @@ def test_comprehensive_lattice(): |
274 | 268 | assert unionele_loaded.elements[1].length == 0.1 |
275 | 269 |
|
276 | 270 | # Write to temporary file |
277 | | - json_file = "comprehensive_lattice.pals.json" |
| 271 | + json_file = tmp_path / "comprehensive_lattice.pals.json" |
278 | 272 | lattice.to_file(json_file) |
279 | 273 |
|
280 | 274 | # Read back from file |
@@ -331,10 +325,6 @@ def test_comprehensive_lattice(): |
331 | 325 | assert unionele_loaded_json.elements[1].kind == "Drift" |
332 | 326 | assert unionele_loaded_json.elements[1].length == 0.1 |
333 | 327 |
|
334 | | - # Clean up temporary files |
335 | | - os.remove(yaml_file) |
336 | | - os.remove(json_file) |
337 | | - |
338 | 328 |
|
339 | 329 | def _build_numpy_lattice(np): |
340 | 330 | """Build a small lattice using numpy-typed scalar values throughout.""" |
|
0 commit comments