Skip to content

Commit 3382121

Browse files
authored
Use tmp_path fixture in test_serialization.py (#72)
1 parent 1db2ef3 commit 3382121

1 file changed

Lines changed: 7 additions & 17 deletions

File tree

tests/test_serialization.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,39 @@
1-
import os
2-
31
import pals
42

53

6-
def test_yaml():
4+
def test_yaml(tmp_path):
75
# Create one base element
86
element1 = pals.Marker(name="element1")
97
# Create one thick element
108
element2 = pals.Drift(name="element2", length=2.0)
119
# Create line with both elements
1210
line = pals.BeamLine(name="line", line=[element1, element2])
1311
# Serialize the BeamLine object to YAML
14-
test_file = "line.pals.yaml"
12+
test_file = tmp_path / "line.pals.yaml"
1513
line.to_file(test_file)
1614
# Read the YAML data from the test file
1715
loaded_line = pals.BeamLine.from_file(test_file)
18-
# Remove the test file
19-
os.remove(test_file)
2016
# Validate loaded BeamLine object
2117
assert line == loaded_line
2218

2319

24-
def test_json():
20+
def test_json(tmp_path):
2521
# Create one base element
2622
element1 = pals.Marker(name="element1")
2723
# Create one thick element
2824
element2 = pals.Drift(name="element2", length=2.0)
2925
# Create line with both elements
3026
line = pals.BeamLine(name="line", line=[element1, element2])
3127
# Serialize the BeamLine object to JSON
32-
test_file = "line.pals.json"
28+
test_file = tmp_path / "line.pals.json"
3329
line.to_file(test_file)
3430
# Read the JSON data from the test file
3531
loaded_line = pals.BeamLine.from_file(test_file)
36-
# Remove the test file
37-
os.remove(test_file)
3832
# Validate loaded BeamLine object
3933
assert line == loaded_line
4034

4135

42-
def test_comprehensive_lattice():
36+
def test_comprehensive_lattice(tmp_path):
4337
"""Test a comprehensive lattice using every PALS element at least once"""
4438

4539
# Create elements in alphabetical order for easy maintenance
@@ -214,7 +208,7 @@ def test_comprehensive_lattice():
214208
)
215209

216210
# Write to temporary file
217-
yaml_file = "comprehensive_lattice.pals.yaml"
211+
yaml_file = tmp_path / "comprehensive_lattice.pals.yaml"
218212
lattice.to_file(yaml_file)
219213

220214
# Read back from file
@@ -272,7 +266,7 @@ def test_comprehensive_lattice():
272266
assert unionele_loaded.elements[1].length == 0.1
273267

274268
# Write to temporary file
275-
json_file = "comprehensive_lattice.pals.json"
269+
json_file = tmp_path / "comprehensive_lattice.pals.json"
276270
lattice.to_file(json_file)
277271

278272
# Read back from file
@@ -328,7 +322,3 @@ def test_comprehensive_lattice():
328322
assert unionele_loaded_json.elements[1].name == "union_drift"
329323
assert unionele_loaded_json.elements[1].kind == "Drift"
330324
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

Comments
 (0)