Skip to content

Commit

Permalink
Fix an omission on inline generation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hsolbrig committed Jun 11, 2021
1 parent d2c9ea6 commit 58443ff
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
4 changes: 4 additions & 0 deletions linkml_runtime/utils/yamlutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
23 changes: 23 additions & 0 deletions tests/test_issues/input/issue_8a.yaml
Original file line number Diff line number Diff line change
@@ -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

35 changes: 27 additions & 8 deletions tests/test_issues/test_issue_8.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -27,14 +28,29 @@ 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


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:
Expand All @@ -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__':
Expand Down

0 comments on commit 58443ff

Please sign in to comment.