Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 7 additions & 17 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
import os

import pals


def test_yaml():
def test_yaml(tmp_path):
# Create one base element
element1 = pals.Marker(name="element1")
# Create one thick element
element2 = pals.Drift(name="element2", length=2.0)
# Create line with both elements
line = pals.BeamLine(name="line", line=[element1, element2])
# Serialize the BeamLine object to YAML
test_file = "line.pals.yaml"
test_file = tmp_path / "line.pals.yaml"
line.to_file(test_file)
# Read the YAML data from the test file
loaded_line = pals.BeamLine.from_file(test_file)
# Remove the test file
os.remove(test_file)
# Validate loaded BeamLine object
assert line == loaded_line


def test_json():
def test_json(tmp_path):
# Create one base element
element1 = pals.Marker(name="element1")
# Create one thick element
element2 = pals.Drift(name="element2", length=2.0)
# Create line with both elements
line = pals.BeamLine(name="line", line=[element1, element2])
# Serialize the BeamLine object to JSON
test_file = "line.pals.json"
test_file = tmp_path / "line.pals.json"
line.to_file(test_file)
# Read the JSON data from the test file
loaded_line = pals.BeamLine.from_file(test_file)
# Remove the test file
os.remove(test_file)
# Validate loaded BeamLine object
assert line == loaded_line


def test_comprehensive_lattice():
def test_comprehensive_lattice(tmp_path):
"""Test a comprehensive lattice using every PALS element at least once"""

# Create elements in alphabetical order for easy maintenance
Expand Down Expand Up @@ -214,7 +208,7 @@ def test_comprehensive_lattice():
)

# Write to temporary file
yaml_file = "comprehensive_lattice.pals.yaml"
yaml_file = tmp_path / "comprehensive_lattice.pals.yaml"
lattice.to_file(yaml_file)

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

# Write to temporary file
json_file = "comprehensive_lattice.pals.json"
json_file = tmp_path / "comprehensive_lattice.pals.json"
lattice.to_file(json_file)

# Read back from file
Expand Down Expand Up @@ -328,7 +322,3 @@ def test_comprehensive_lattice():
assert unionele_loaded_json.elements[1].name == "union_drift"
assert unionele_loaded_json.elements[1].kind == "Drift"
assert unionele_loaded_json.elements[1].length == 0.1

# Clean up temporary files
os.remove(yaml_file)
os.remove(json_file)
Loading