Skip to content

Commit

Permalink
Merge pull request #294 from linkml/schemaview_enum_slots
Browse files Browse the repository at this point in the history
remove slot name redundancy in get_slots_by_enum method
  • Loading branch information
sierra-moxon authored Jan 9, 2024
2 parents 0c55320 + 801973d commit 6763067
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 91 deletions.
7 changes: 5 additions & 2 deletions linkml_runtime/utils/schemaview.py
Original file line number Diff line number Diff line change
Expand Up @@ -1515,11 +1515,14 @@ def get_slots_by_enum(self, enum_name: ENUM_NAME = None) -> List[SlotDefinition]
:param enum_name: enum in consideration
:return: list of slots, either schem or both class attribute defined
"""
enum_slots = [s for s in self.all_slots().values() if s.range == enum_name]
enum_slots = []
for s in self.all_slots().values():
if s.range == enum_name and s not in enum_slots:
enum_slots.append(s)
for class_definition in self.all_classes().values():
if class_definition.slot_usage:
for slot_definition in class_definition.slot_usage.values():
if slot_definition.range == enum_name:
if slot_definition.range == enum_name and slot_definition not in enum_slots:
enum_slots.append(slot_definition)
return enum_slots

Expand Down
56 changes: 0 additions & 56 deletions tests/test_issues/input/linkml_issue_998.yaml

This file was deleted.

121 changes: 88 additions & 33 deletions tests/test_issues/test_linkml_issue_998.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,98 @@
import logging
import unittest
from unittest import TestCase
import pytest
from linkml_runtime.utils.schemaview import SchemaView

from tests.test_issues.environment import env
schema = """
id: https://w3id.org/linkml/examples/personinfo
name: personinfo
prefixes:
linkml: https://w3id.org/linkml/
schema: http://schema.org/
imports:
- linkml:types
default_range: string
classes:
Person:
class_uri: schema:Person
attributes:
id:
identifier: true
employed:
range: EmploymentStatusEnum
past_relationship:
range: RelationshipStatusEnum
slots:
- type
- past_employer
Programmer:
attributes:
employed:
range: EmploymentStatusEnum
slots:
- type
- past_employer
slot_usage:
type:
range: TypeEnum
slots:
status:
range: PersonStatusEnum
relationship:
range: RelationshipStatusEnum
type:
past_employer:
range: EmploymentStatusEnum
enums:
PersonStatusEnum:
permissible_values:
ALIVE:
DEAD:
UNKNOWN:
EmployedStatusEnum:
permissible_values:
EMPLOYED:
UNEMPLOYED:
UNKNOWN:
RelationshipStatusEnum:
permissible_values:
UNKNOWN:
TypeEnum:
permissible_values:
UNKNOWN:
"""

class Issue998TestCase(TestCase):
"""
https://github.com/linkml/linkml/issues/998
"""
env = env

def test_issue_998_schema_slot(self):
view = SchemaView(env.input_path('linkml_issue_998.yaml'))
enum_slots = view.get_slots_by_enum("PersonStatusEnum")
# assert type(enum_slots) is List[SlotDefinition]
assert len(enum_slots) == 1
assert enum_slots[0].name == "status"
@pytest.fixture
def view():
return SchemaView(schema)

def test_issue_998_attribute_slot(self):
view = SchemaView(env.input_path('linkml_issue_998.yaml'))
enum_slots = view.get_slots_by_enum("EmploymentStatusEnum")
assert len(enum_slots) == 1
assert enum_slots[0].name == "employed"

def test_issue_998_schema_and_atribute_slots(self):
view = SchemaView(env.input_path('linkml_issue_998.yaml'))
enum_slots = view.get_slots_by_enum("RelationshipStatusEnum")
assert len(enum_slots) == 2
assert enum_slots[0].name == "relationship"
assert enum_slots[1].name == "past_relationship"
def test_issue_998_schema_slot(view):
enum_slots = view.get_slots_by_enum("EmploymentStatusEnum")
assert len(enum_slots) == 2

def test_issue_998_slot_usage_range(self):
view = SchemaView(env.input_path('linkml_issue_998.yaml'))
enum_slots = view.get_slots_by_enum("TypeEnum")
assert len(enum_slots) == 1
assert enum_slots[0].name == "type"

def test_slots_are_not_duplicated(view):
enum_slots = view.get_slots_by_enum("PersonStatusEnum")
assert len(enum_slots) == 1
assert enum_slots[0].name == "status"

if __name__ == "__main__":
unittest.main()

def test_issue_998_attribute_slot(view):
enum_slots = view.get_slots_by_enum("EmploymentStatusEnum")
assert len(enum_slots) == 1
assert enum_slots[0].name == "employed"


def test_issue_998_schema_and_attribute_slots(view):
enum_slots = view.get_slots_by_enum("RelationshipStatusEnum")
assert len(enum_slots) == 2
assert enum_slots[0].name == "relationship"
assert enum_slots[1].name == "past_relationship"


def test_issue_998_slot_usage_range(view):
enum_slots = view.get_slots_by_enum("TypeEnum")
assert len(enum_slots) == 1
assert enum_slots[0].name == "type"

0 comments on commit 6763067

Please sign in to comment.