Skip to content

Commit 6763067

Browse files
authored
Merge pull request #294 from linkml/schemaview_enum_slots
remove slot name redundancy in get_slots_by_enum method
2 parents 0c55320 + 801973d commit 6763067

File tree

3 files changed

+93
-91
lines changed

3 files changed

+93
-91
lines changed

linkml_runtime/utils/schemaview.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,11 +1515,14 @@ def get_slots_by_enum(self, enum_name: ENUM_NAME = None) -> List[SlotDefinition]
15151515
:param enum_name: enum in consideration
15161516
:return: list of slots, either schem or both class attribute defined
15171517
"""
1518-
enum_slots = [s for s in self.all_slots().values() if s.range == enum_name]
1518+
enum_slots = []
1519+
for s in self.all_slots().values():
1520+
if s.range == enum_name and s not in enum_slots:
1521+
enum_slots.append(s)
15191522
for class_definition in self.all_classes().values():
15201523
if class_definition.slot_usage:
15211524
for slot_definition in class_definition.slot_usage.values():
1522-
if slot_definition.range == enum_name:
1525+
if slot_definition.range == enum_name and slot_definition not in enum_slots:
15231526
enum_slots.append(slot_definition)
15241527
return enum_slots
15251528

tests/test_issues/input/linkml_issue_998.yaml

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 88 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,98 @@
1-
import logging
2-
import unittest
3-
from unittest import TestCase
1+
import pytest
42
from linkml_runtime.utils.schemaview import SchemaView
53

6-
from tests.test_issues.environment import env
4+
schema = """
5+
id: https://w3id.org/linkml/examples/personinfo
6+
name: personinfo
7+
prefixes:
8+
linkml: https://w3id.org/linkml/
9+
schema: http://schema.org/
10+
imports:
11+
- linkml:types
12+
default_range: string
713
14+
classes:
15+
Person:
16+
class_uri: schema:Person
17+
attributes:
18+
id:
19+
identifier: true
20+
employed:
21+
range: EmploymentStatusEnum
22+
past_relationship:
23+
range: RelationshipStatusEnum
24+
slots:
25+
- type
26+
- past_employer
27+
Programmer:
28+
attributes:
29+
employed:
30+
range: EmploymentStatusEnum
31+
slots:
32+
- type
33+
- past_employer
34+
slot_usage:
35+
type:
36+
range: TypeEnum
37+
38+
slots:
39+
status:
40+
range: PersonStatusEnum
41+
relationship:
42+
range: RelationshipStatusEnum
43+
type:
44+
past_employer:
45+
range: EmploymentStatusEnum
46+
enums:
47+
PersonStatusEnum:
48+
permissible_values:
49+
ALIVE:
50+
DEAD:
51+
UNKNOWN:
52+
EmployedStatusEnum:
53+
permissible_values:
54+
EMPLOYED:
55+
UNEMPLOYED:
56+
UNKNOWN:
57+
RelationshipStatusEnum:
58+
permissible_values:
59+
UNKNOWN:
60+
TypeEnum:
61+
permissible_values:
62+
UNKNOWN:
63+
"""
864

9-
class Issue998TestCase(TestCase):
10-
"""
11-
https://github.com/linkml/linkml/issues/998
12-
"""
13-
env = env
1465

15-
def test_issue_998_schema_slot(self):
16-
view = SchemaView(env.input_path('linkml_issue_998.yaml'))
17-
enum_slots = view.get_slots_by_enum("PersonStatusEnum")
18-
# assert type(enum_slots) is List[SlotDefinition]
19-
assert len(enum_slots) == 1
20-
assert enum_slots[0].name == "status"
66+
@pytest.fixture
67+
def view():
68+
return SchemaView(schema)
2169

22-
def test_issue_998_attribute_slot(self):
23-
view = SchemaView(env.input_path('linkml_issue_998.yaml'))
24-
enum_slots = view.get_slots_by_enum("EmploymentStatusEnum")
25-
assert len(enum_slots) == 1
26-
assert enum_slots[0].name == "employed"
2770

28-
def test_issue_998_schema_and_atribute_slots(self):
29-
view = SchemaView(env.input_path('linkml_issue_998.yaml'))
30-
enum_slots = view.get_slots_by_enum("RelationshipStatusEnum")
31-
assert len(enum_slots) == 2
32-
assert enum_slots[0].name == "relationship"
33-
assert enum_slots[1].name == "past_relationship"
71+
def test_issue_998_schema_slot(view):
72+
enum_slots = view.get_slots_by_enum("EmploymentStatusEnum")
73+
assert len(enum_slots) == 2
3474

35-
def test_issue_998_slot_usage_range(self):
36-
view = SchemaView(env.input_path('linkml_issue_998.yaml'))
37-
enum_slots = view.get_slots_by_enum("TypeEnum")
38-
assert len(enum_slots) == 1
39-
assert enum_slots[0].name == "type"
4075

76+
def test_slots_are_not_duplicated(view):
77+
enum_slots = view.get_slots_by_enum("PersonStatusEnum")
78+
assert len(enum_slots) == 1
79+
assert enum_slots[0].name == "status"
4180

42-
if __name__ == "__main__":
43-
unittest.main()
81+
82+
def test_issue_998_attribute_slot(view):
83+
enum_slots = view.get_slots_by_enum("EmploymentStatusEnum")
84+
assert len(enum_slots) == 1
85+
assert enum_slots[0].name == "employed"
86+
87+
88+
def test_issue_998_schema_and_attribute_slots(view):
89+
enum_slots = view.get_slots_by_enum("RelationshipStatusEnum")
90+
assert len(enum_slots) == 2
91+
assert enum_slots[0].name == "relationship"
92+
assert enum_slots[1].name == "past_relationship"
93+
94+
95+
def test_issue_998_slot_usage_range(view):
96+
enum_slots = view.get_slots_by_enum("TypeEnum")
97+
assert len(enum_slots) == 1
98+
assert enum_slots[0].name == "type"

0 commit comments

Comments
 (0)