From 58443ff90da419404a77386e7aa790ece4b12354 Mon Sep 17 00:00:00 2001 From: hsolbrig Date: Fri, 11 Jun 2021 13:09:44 -0500 Subject: [PATCH] Fix an omission on inline generation Enhanced unit tests a bit. Note that this is clearly NOT the way to do MissingRequiredField overrides if you have to restore to the default --- linkml_runtime/utils/yamlutils.py | 4 +++ tests/test_issues/input/issue_8a.yaml | 23 ++++++++++++++++++ tests/test_issues/test_issue_8.py | 35 +++++++++++++++++++++------ 3 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 tests/test_issues/input/issue_8a.yaml diff --git a/linkml_runtime/utils/yamlutils.py b/linkml_runtime/utils/yamlutils.py index 0f1e95df..27dbd7d0 100644 --- a/linkml_runtime/utils/yamlutils.py +++ b/linkml_runtime/utils/yamlutils.py @@ -111,6 +111,10 @@ def _normalize_inlined(self, slot_name: str, slot_type: Type, key_name: str, key @param is_list: True means inlined as list """ raw_slot: Union[list, dict, JsonObj] = self[slot_name] + if raw_slot is None: + raw_slot = [] + elif not isinstance(raw_slot, (dict, list, JsonObj)): + raw_slot = [raw_slot] cooked_slot = list() if is_list else dict() cooked_keys = set() diff --git a/tests/test_issues/input/issue_8a.yaml b/tests/test_issues/input/issue_8a.yaml new file mode 100644 index 00000000..86c4f4e7 --- /dev/null +++ b/tests/test_issues/input/issue_8a.yaml @@ -0,0 +1,23 @@ +# ID is omitted +# Name is omitted +description: >- + This is an example of an imported module +imports: + - linkml:types +prefixes: + linkml: https://w3id.org/linkml/ + include_core: https://w3id.org/mixs/include_core/ +default_prefix: include_core + +slots: + - name: id + identifier: true + required: true + +classes: + - name: root + description: >- + root class + slots: + - id + diff --git a/tests/test_issues/test_issue_8.py b/tests/test_issues/test_issue_8.py index ed75695e..cb8ca996 100644 --- a/tests/test_issues/test_issue_8.py +++ b/tests/test_issues/test_issue_8.py @@ -8,16 +8,17 @@ def override(cls: Type[YAMLRoot]): + orig = cls.MissingRequiredField def mrf(self, field_name: str) -> None: - if isinstance(self, SchemaDefinition) and field_name == "name": - self.name = self.id.rsplit('/', 1)[1] + if isinstance(self, SchemaDefinition) and field_name == "name" and self.id: + id_parts = self.id.replace('#', '/').rsplit('/') + self.name = id_parts[-1] elif isinstance(self, SlotDefinition) and field_name == "name": self.name = "id" elif isinstance(self, ClassDefinition) and field_name == "name": self.name = "core" else: - cls.MissingRequiredField(f"{self.__class__.__name}.{field_name}") - orig = cls.MissingRequiredField + orig(self, f"{type(self).__name__}.{field_name}") cls.MissingRequiredField = mrf return orig @@ -27,7 +28,7 @@ def mrf(self, field_name: str) -> None: def override2(): def mrf(self, field_name: str) -> None: - msgs.add(f"{self.__class__.__name__}.{field_name} is not supplied") + msgs.add(f"{type(self).__name__}.{field_name} is not supplied") orig = YAMLRoot.MissingRequiredField YAMLRoot.MissingRequiredField = mrf return orig @@ -35,6 +36,21 @@ def mrf(self, field_name: str) -> None: class TestErrorIntercept(unittest.TestCase): + def test_legitimate_error(self): + """ Test that legitimate errors are emitted correctly """ + test_file = env.input_path('issue_8a.yaml') + with self.assertRaises(ValueError) as e: + yaml_loader.load(test_file, SchemaDefinition) + self.assertEqual('name must be supplied', str(e.exception), "ValueError should be raised") + orig = override(SchemaDefinition) + try: + with self.assertRaises(ValueError) as e: + yaml_loader.load(test_file, SchemaDefinition) + self.assertEqual('SchemaDefinition.name must be supplied', str(e.exception)) + finally: + # SchemaDefinition.MissingRequiredField = orig + delattr(SchemaDefinition, "MissingRequiredField") + def test_missing_intercept(self): test_file = env.input_path('issue_8.yaml') with self.assertRaises(ValueError) as e: @@ -56,9 +72,12 @@ def test_missing_intercept(self): origcd = override(ClassDefinition) yaml_loader.load(test_file, SchemaDefinition) finally: - SchemaDefinition.MissingRequiredField = origschd - SlotDefinition.MissingRequiredField = origslotd - ClassDefinition.MissingRequiredField = origcd + delattr(SchemaDefinition, "MissingRequiredField") + delattr(SlotDefinition, "MissingRequiredField") + delattr(ClassDefinition, "MissingRequiredField") + # SchemaDefinition.MissingRequiredField = origschd + # SlotDefinition.MissingRequiredField = origslotd + # ClassDefinition.MissingRequiredField = origcd if __name__ == '__main__':