|
1 | 1 | from pydantic import ValidationError |
2 | 2 |
|
| 3 | +import yaml |
| 4 | + |
3 | 5 | from schema.BaseElement import BaseElement |
4 | 6 | from schema.ThickElement import ThickElement |
5 | 7 | from schema.Line import Line |
6 | 8 |
|
7 | 9 |
|
8 | 10 | def test_BaseElement(): |
9 | | - # Create base element with custom name |
| 11 | + # Create one base element with custom name |
10 | 12 | element_name = "base_element" |
11 | 13 | element = BaseElement(name=element_name) |
12 | 14 | assert element.name == element_name |
13 | 15 |
|
14 | 16 |
|
15 | 17 | def test_ThickElement(): |
16 | | - # Create thick element with custom name and length |
| 18 | + # Create one thick element with custom name and length |
17 | 19 | element_name = "thick_element" |
18 | 20 | element_length = 1.0 |
19 | 21 | element = ThickElement( |
@@ -49,3 +51,23 @@ def test_Line(): |
49 | 51 | # Extend first line with second line |
50 | 52 | line1.line.extend(line2.line) |
51 | 53 | assert line1.line == [element1, element2, element3] |
| 54 | + |
| 55 | + |
| 56 | +def test_yaml(): |
| 57 | + # Create one base element |
| 58 | + element1 = BaseElement(name="element1") |
| 59 | + # Create one thick element |
| 60 | + element2 = ThickElement(name="element2", length=2.0) |
| 61 | + # Create line with both elements |
| 62 | + line = Line(line=[element1, element2]) |
| 63 | + # Serialize the Line object to YAML |
| 64 | + yaml_data = yaml.dump(line.model_dump(), default_flow_style=False) |
| 65 | + # Write the YAML data to a file |
| 66 | + with open("line.yaml", "w") as file: |
| 67 | + file.write(yaml_data) |
| 68 | + # Read the YAML data from the file |
| 69 | + with open("line.yaml", "r") as file: |
| 70 | + yaml_data = yaml.safe_load(file) |
| 71 | + # Parse the YAML data back into a Line object |
| 72 | + loaded_line = Line(**yaml_data) |
| 73 | + assert line == loaded_line |
0 commit comments