Skip to content

Commit 0b02e43

Browse files
committed
Add unit test
1 parent 0d71eed commit 0b02e43

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/pals/kinds/Lattice.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ def unpack_json_structure(cls, data):
1919
"""Deserialize the JSON/YAML/...-like dict for Lattice elements"""
2020
from pals.kinds.mixin.all_element_mixin import unpack_element_list_structure
2121

22-
return unpack_element_list_structure(data, "line", "line")
22+
return unpack_element_list_structure(data, "branches", "branches")
2323

2424
def model_dump(self, *args, **kwargs):
2525
"""Custom model dump for Lattice to handle element list formatting"""
2626
from pals.kinds.mixin.all_element_mixin import dump_element_list
2727

28-
return dump_element_list(self, "line", *args, **kwargs)
28+
return dump_element_list(self, "branches", *args, **kwargs)
2929

3030
@staticmethod
3131
def from_file(filename: str) -> "Lattice":

tests/test_elements.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,29 @@ def test_UnionEle():
511511
assert len(element_with_children.elements) == 2
512512
assert element_with_children.elements[0].name == "m1"
513513
assert element_with_children.elements[1].name == "d1"
514+
515+
516+
def test_Lattice():
517+
# Create first line with one base element
518+
element1 = pals.Marker(name="element1")
519+
line1 = pals.BeamLine(name="line1", line=[element1])
520+
assert line1.line == [element1]
521+
# Extend first line with one thick element
522+
element2 = pals.Drift(name="element2", length=2.0)
523+
line1.line.extend([element2])
524+
assert line1.line == [element1, element2]
525+
# Create second line with one drift element
526+
element3 = pals.Drift(name="element3", length=3.0)
527+
line2 = pals.BeamLine(name="line2", line=[element3])
528+
# Extend first line with second line
529+
line1.line.extend(line2.line)
530+
assert line1.line == [element1, element2, element3]
531+
# Build lattice with two lines
532+
lattice = pals.Lattice(name="lattice", branches=[line1, line2])
533+
assert lattice.name == "lattice"
534+
assert lattice.kind == "Lattice"
535+
assert len(lattice.branches) == 2
536+
assert lattice.branches[0].name == "line1"
537+
assert lattice.branches[1].name == "line2"
538+
assert lattice.branches[0].line == [element1, element2, element3]
539+
assert lattice.branches[1].line == [element3]

0 commit comments

Comments
 (0)