Skip to content

Commit 085b5be

Browse files
committed
Merge branch 'main' into fix-multipole-numpy-serialization
2 parents 4b94e4a + 3382121 commit 085b5be

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,47 +1,41 @@
1-
import os
2-
31
import pytest
42

53
import pals
64

75

8-
def test_yaml():
6+
def test_yaml(tmp_path):
97
# Create one base element
108
element1 = pals.Marker(name="element1")
119
# Create one thick element
1210
element2 = pals.Drift(name="element2", length=2.0)
1311
# Create line with both elements
1412
line = pals.BeamLine(name="line", line=[element1, element2])
1513
# Serialize the BeamLine object to YAML
16-
test_file = "line.pals.yaml"
14+
test_file = tmp_path / "line.pals.yaml"
1715
line.to_file(test_file)
1816
# Read the YAML data from the test file
1917
loaded_line = pals.BeamLine.from_file(test_file)
20-
# Remove the test file
21-
os.remove(test_file)
2218
# Validate loaded BeamLine object
2319
assert line == loaded_line
2420

2521

26-
def test_json():
22+
def test_json(tmp_path):
2723
# Create one base element
2824
element1 = pals.Marker(name="element1")
2925
# Create one thick element
3026
element2 = pals.Drift(name="element2", length=2.0)
3127
# Create line with both elements
3228
line = pals.BeamLine(name="line", line=[element1, element2])
3329
# Serialize the BeamLine object to JSON
34-
test_file = "line.pals.json"
30+
test_file = tmp_path / "line.pals.json"
3531
line.to_file(test_file)
3632
# Read the JSON data from the test file
3733
loaded_line = pals.BeamLine.from_file(test_file)
38-
# Remove the test file
39-
os.remove(test_file)
4034
# Validate loaded BeamLine object
4135
assert line == loaded_line
4236

4337

44-
def test_comprehensive_lattice():
38+
def test_comprehensive_lattice(tmp_path):
4539
"""Test a comprehensive lattice using every PALS element at least once"""
4640

4741
# Create elements in alphabetical order for easy maintenance
@@ -216,7 +210,7 @@ def test_comprehensive_lattice():
216210
)
217211

218212
# Write to temporary file
219-
yaml_file = "comprehensive_lattice.pals.yaml"
213+
yaml_file = tmp_path / "comprehensive_lattice.pals.yaml"
220214
lattice.to_file(yaml_file)
221215

222216
# Read back from file
@@ -274,7 +268,7 @@ def test_comprehensive_lattice():
274268
assert unionele_loaded.elements[1].length == 0.1
275269

276270
# Write to temporary file
277-
json_file = "comprehensive_lattice.pals.json"
271+
json_file = tmp_path / "comprehensive_lattice.pals.json"
278272
lattice.to_file(json_file)
279273

280274
# Read back from file
@@ -331,10 +325,6 @@ def test_comprehensive_lattice():
331325
assert unionele_loaded_json.elements[1].kind == "Drift"
332326
assert unionele_loaded_json.elements[1].length == 0.1
333327

334-
# Clean up temporary files
335-
os.remove(yaml_file)
336-
os.remove(json_file)
337-
338328

339329
def _build_numpy_lattice(np):
340330
"""Build a small lattice using numpy-typed scalar values throughout."""

0 commit comments

Comments
 (0)