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