Skip to content

Commit

Permalink
Simplify and fix serialization test case
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyMcCormick committed Feb 24, 2025
1 parent 1af26bc commit ec2c56a
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions tests/test_datamodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,44 +888,37 @@ class SchemaSerializationTest(unittest.TestCase):

def test_serialization(self) -> None:
"""Test serialization of the schema data model."""
sch_out = Schema.from_uri(TEST_SERIALIZATION)
model_data = sch_out.model_dump(by_alias=True, exclude_none=True, exclude_defaults=True)
# Read the original YAML content from the test_serialization.yaml file
with open(TEST_SERIALIZATION) as file:
original_yaml_content = file.read()

# Debug print to see what's in model_data
print("Model data:", model_data)
# Load the schema from the original YAML content
schema_out = Schema.from_uri(TEST_SERIALIZATION)
serialized_data = schema_out.model_dump(by_alias=True, exclude_none=True, exclude_defaults=True)

# Write the serialized data to a temporary YAML file
with tempfile.NamedTemporaryFile(delete=False, suffix=".yaml", mode="w+") as temp_file:
print(f"Dumping schema to {temp_file.name}")
yaml.dump(model_data, temp_file, default_flow_style=False, sort_keys=False)
yaml.dump(serialized_data, temp_file, default_flow_style=False, sort_keys=False)
temp_file.seek(0)
# Read the original YAML content
original_yaml = temp_file.read()
print("Original YAML:\n", original_yaml)
temp_file.seek(0)
print(f"Reading schema back from {temp_file.name}\n")
sch_in = Schema.from_uri(temp_file.name)
deserialized_yaml = yaml.dump(
sch_in.model_dump(by_alias=True, exclude_none=True, exclude_defaults=True),
default_flow_style=False,
sort_keys=False,
)
print("Deserialized YAML:\n", deserialized_yaml)

# Show the differences between the original and deserialized YAML
diff = difflib.unified_diff(
original_yaml.splitlines(keepends=True),
deserialized_yaml.splitlines(keepends=True),
fromfile="original.yaml",
tofile="deserialized.yaml",
)
print("Differences:\n", "".join(diff))
# Read the deserialized YAML content from the temporary file
deserialized_yaml_content = temp_file.read()

# Show the differences between the original and deserialized YAML
diff = difflib.unified_diff(
original_yaml_content.splitlines(keepends=True),
deserialized_yaml_content.splitlines(keepends=True),
fromfile="original.yaml",
tofile="deserialized.yaml",
)
print("Differences:\n", "".join(diff))

# Assert that the original and deserialized YAML are the same
self.assertEqual(
yaml.safe_load(original_yaml),
yaml.safe_load(deserialized_yaml),
"The original and deserialized YAML contents should be the same",
)
# Assert that the original and deserialized YAML are the same
self.assertEqual(
yaml.safe_load(original_yaml_content),
yaml.safe_load(deserialized_yaml_content),
"The original and deserialized YAML contents should be the same",
)


if __name__ == "__main__":
Expand Down

0 comments on commit ec2c56a

Please sign in to comment.