Skip to content

Commit 3390169

Browse files
committed
Add unit test to write to/read from YAML file
1 parent eba6ca7 commit 3390169

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

tests/test_schema.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
from pydantic import ValidationError
22

3+
import yaml
4+
35
from schema.BaseElement import BaseElement
46
from schema.ThickElement import ThickElement
57
from schema.Line import Line
68

79

810
def test_BaseElement():
9-
# Create base element with custom name
11+
# Create one base element with custom name
1012
element_name = "base_element"
1113
element = BaseElement(name=element_name)
1214
assert element.name == element_name
1315

1416

1517
def test_ThickElement():
16-
# Create thick element with custom name and length
18+
# Create one thick element with custom name and length
1719
element_name = "thick_element"
1820
element_length = 1.0
1921
element = ThickElement(
@@ -49,3 +51,23 @@ def test_Line():
4951
# Extend first line with second line
5052
line1.line.extend(line2.line)
5153
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

Comments
 (0)