From d590889c422bdc4e5d394929e4fc86caad3d06b9 Mon Sep 17 00:00:00 2001 From: Jeremy McCormick Date: Fri, 23 Aug 2024 13:00:23 -0500 Subject: [PATCH] Add test of ID generation Load a schema without IDs and set them, and load the same schema with ID generation turned off to check that an exception is thrown. --- tests/test_datamodel.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_datamodel.py b/tests/test_datamodel.py index 4db5369b..4ab2a6cf 100644 --- a/tests/test_datamodel.py +++ b/tests/test_datamodel.py @@ -484,6 +484,19 @@ def test_model_validate(self) -> None: data = yaml.safe_load(test_yaml) Schema.model_validate(data) + def test_id_generation(self) -> None: + """Test ID generation.""" + test_path = os.path.join(TESTDIR, "data", "test_id_generation.yaml") + with open(test_path) as test_yaml: + yaml_data = yaml.safe_load(test_yaml) + # Generate IDs for objects in the test schema. + Schema.model_validate(yaml_data, context={"id_generation": True}) + with open(test_path) as test_yaml: + yaml_data = yaml.safe_load(test_yaml) + # Test that an error is raised when id generation is disabled. + with self.assertRaises(ValidationError): + Schema.model_validate(yaml_data, context={"id_generation": False}) + class SchemaVersionTest(unittest.TestCase): """Test the schema version."""