-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from linkml/issue_8
Issue 8
- Loading branch information
Showing
5 changed files
with
112 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
id: https://w3id.org/include_core | ||
# 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: | ||
# Slot name ("id") is missing | ||
- identifier: true | ||
required: true | ||
|
||
classes: | ||
# Class name (root) is missing | ||
- description: >- | ||
root class | ||
slots: | ||
- id | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import unittest | ||
from typing import Type | ||
|
||
from linkml_runtime.linkml_model import SchemaDefinition, SlotDefinition, ClassDefinition | ||
from linkml_runtime.loaders import yaml_loader | ||
from linkml_runtime.utils.yamlutils import YAMLRoot | ||
from tests.test_issues.environment import env | ||
|
||
|
||
def override(cls: Type[YAMLRoot]): | ||
def mrf(self, field_name: str) -> None: | ||
if isinstance(self, SchemaDefinition) and field_name == "name": | ||
self.name = self.id.rsplit('/', 1)[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 | ||
cls.MissingRequiredField = mrf | ||
return orig | ||
|
||
|
||
msgs = set() | ||
|
||
|
||
def override2(): | ||
def mrf(self, field_name: str) -> None: | ||
msgs.add(f"{self.__class__.__name__}.{field_name} is not supplied") | ||
orig = YAMLRoot.MissingRequiredField | ||
YAMLRoot.MissingRequiredField = mrf | ||
return orig | ||
|
||
|
||
class TestErrorIntercept(unittest.TestCase): | ||
|
||
def test_missing_intercept(self): | ||
test_file = env.input_path('issue_8.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") | ||
|
||
try: | ||
orig = override2() | ||
yaml_loader.load(test_file, SchemaDefinition) | ||
finally: | ||
YAMLRoot.MissingRequiredField = orig | ||
self.assertEqual({'ClassDefinition.name is not supplied', | ||
'SlotDefinition.name is not supplied', | ||
'SchemaDefinition.name is not supplied'}, msgs) | ||
|
||
try: | ||
origschd = override(SchemaDefinition) | ||
origslotd = override(SlotDefinition) | ||
origcd = override(ClassDefinition) | ||
yaml_loader.load(test_file, SchemaDefinition) | ||
finally: | ||
SchemaDefinition.MissingRequiredField = origschd | ||
SlotDefinition.MissingRequiredField = origslotd | ||
ClassDefinition.MissingRequiredField = origcd | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |