Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove slot name redundancy in get_slots_by_enum method #294

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
: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 = []

Check warning on line 1518 in linkml_runtime/utils/schemaview.py

View check run for this annotation

Codecov / codecov/patch

linkml_runtime/utils/schemaview.py#L1518

Added line #L1518 was not covered by tests
for s in self.all_slots().values():
if s.range == enum_name and s not in enum_slots:
enum_slots.append(s)

Check warning on line 1521 in linkml_runtime/utils/schemaview.py

View check run for this annotation

Codecov / codecov/patch

linkml_runtime/utils/schemaview.py#L1521

Added line #L1521 was not covered by tests
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"