|
1 | | -import json |
2 | | -import os |
3 | | -import sys |
4 | | -import yaml |
5 | | - |
6 | | -# Add the parent directory to sys.path |
7 | | -sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../src/"))) |
8 | | - |
9 | 1 | from pydantic import ValidationError |
10 | 2 |
|
11 | 3 | from pals import MagneticMultipoleParameters |
@@ -68,6 +60,8 @@ def test_Drift(): |
68 | 60 |
|
69 | 61 |
|
70 | 62 | def test_Quadrupole(): |
| 63 | + import yaml |
| 64 | + |
71 | 65 | # Create one drift element with custom name and length |
72 | 66 | element_name = "quadrupole_element" |
73 | 67 | element_length = 1.0 |
@@ -118,53 +112,3 @@ def test_BeamLine(): |
118 | 112 | # Extend first line with second line |
119 | 113 | line1.line.extend(line2.line) |
120 | 114 | assert line1.line == [element1, element2, element3] |
121 | | - |
122 | | - |
123 | | -def test_yaml(): |
124 | | - # Create one base element |
125 | | - element1 = BaseElement(name="element1") |
126 | | - # Create one thick element |
127 | | - element2 = ThickElement(name="element2", length=2.0) |
128 | | - # Create line with both elements |
129 | | - line = BeamLine(name="line", line=[element1, element2]) |
130 | | - # Serialize the BeamLine object to YAML |
131 | | - yaml_data = yaml.dump(line.model_dump(), default_flow_style=False) |
132 | | - print(f"\n{yaml_data}") |
133 | | - # Write the YAML data to a test file |
134 | | - test_file = "line.yaml" |
135 | | - with open(test_file, "w") as file: |
136 | | - file.write(yaml_data) |
137 | | - # Read the YAML data from the test file |
138 | | - with open(test_file, "r") as file: |
139 | | - yaml_data = yaml.safe_load(file) |
140 | | - # Parse the YAML data back into a BeamLine object |
141 | | - loaded_line = BeamLine(**yaml_data) |
142 | | - # Remove the test file |
143 | | - os.remove(test_file) |
144 | | - # Validate loaded BeamLine object |
145 | | - assert line == loaded_line |
146 | | - |
147 | | - |
148 | | -def test_json(): |
149 | | - # Create one base element |
150 | | - element1 = BaseElement(name="element1") |
151 | | - # Create one thick element |
152 | | - element2 = ThickElement(name="element2", length=2.0) |
153 | | - # Create line with both elements |
154 | | - line = BeamLine(name="line", line=[element1, element2]) |
155 | | - # Serialize the BeamLine object to JSON |
156 | | - json_data = json.dumps(line.model_dump(), sort_keys=True, indent=2) |
157 | | - print(f"\n{json_data}") |
158 | | - # Write the JSON data to a test file |
159 | | - test_file = "line.json" |
160 | | - with open(test_file, "w") as file: |
161 | | - file.write(json_data) |
162 | | - # Read the JSON data from the test file |
163 | | - with open(test_file, "r") as file: |
164 | | - json_data = json.loads(file.read()) |
165 | | - # Parse the JSON data back into a BeamLine object |
166 | | - loaded_line = BeamLine(**json_data) |
167 | | - # Remove the test file |
168 | | - os.remove(test_file) |
169 | | - # Validate loaded BeamLine object |
170 | | - assert line == loaded_line |
0 commit comments