|
| 1 | +from typing import Optional, List, Union, Any, Dict |
| 2 | +from dataclasses import fields |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from linkml_runtime.utils.schema_builder import SchemaBuilder |
| 7 | +from linkml_runtime.linkml_model import ClassDefinition, SlotDefinition |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize("replace_if_present", [True, False]) |
| 11 | +def test_add_existing_class(replace_if_present): |
| 12 | + """ |
| 13 | + Test the case of adding a class with a name that is the same as a class that already |
| 14 | + exists in the schema |
| 15 | + """ |
| 16 | + builder = SchemaBuilder() |
| 17 | + |
| 18 | + cls_name = "Person" |
| 19 | + |
| 20 | + # Add a class to the schema |
| 21 | + cls = ClassDefinition(name=cls_name, slots=["name"]) |
| 22 | + builder.add_class(cls) |
| 23 | + |
| 24 | + # Add another class with the same name to the schema |
| 25 | + cls_repeat = ClassDefinition(name=cls_name, slots=["age"]) |
| 26 | + |
| 27 | + if replace_if_present: |
| 28 | + builder.add_class(cls_repeat, replace_if_present=True) |
| 29 | + assert builder.schema.classes[cls_name].slots == ["age"] |
| 30 | + else: |
| 31 | + with pytest.raises(ValueError, match=f"Class {cls_name} already exists"): |
| 32 | + builder.add_class(cls_repeat) |
| 33 | + assert builder.schema.classes[cls_name].slots == ["name"] |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + "slots", |
| 38 | + [ |
| 39 | + None, |
| 40 | + ["name", "age"], |
| 41 | + ["name", SlotDefinition(name="age")], |
| 42 | + [SlotDefinition(name="name"), SlotDefinition(name="age")], |
| 43 | + ], |
| 44 | +) |
| 45 | +@pytest.mark.parametrize("use_attributes", [True, False]) |
| 46 | +def test_add_class_with_slot_additions( |
| 47 | + slots: Optional[List[Union[str, SlotDefinition]]], use_attributes: bool |
| 48 | +): |
| 49 | + """ |
| 50 | + Test adding a class with separate additional slots specification |
| 51 | + """ |
| 52 | + # If `slots` is None, it should be treated as if it were an empty list |
| 53 | + if slots is None: |
| 54 | + slots = [] |
| 55 | + |
| 56 | + slot_names = {s if isinstance(s, str) else s.name for s in slots} |
| 57 | + |
| 58 | + builder = SchemaBuilder() |
| 59 | + |
| 60 | + cls_name = "Person" |
| 61 | + |
| 62 | + # Add a class to the schema |
| 63 | + cls = ClassDefinition(name=cls_name) |
| 64 | + |
| 65 | + if use_attributes: |
| 66 | + # === The case where the slots specified should be added to the `attributes` |
| 67 | + # meta slot of the class === |
| 68 | + if any(not isinstance(s, SlotDefinition) for s in slots): |
| 69 | + with pytest.raises( |
| 70 | + ValueError, |
| 71 | + match="If use_attributes=True then slots must be SlotDefinitions", |
| 72 | + ): |
| 73 | + builder.add_class(cls, slots=slots, use_attributes=use_attributes) |
| 74 | + else: |
| 75 | + builder.add_class(cls, slots=slots, use_attributes=use_attributes) |
| 76 | + assert builder.schema.classes[cls_name].attributes.keys() == slot_names |
| 77 | + else: |
| 78 | + # === The case where the slots specified should be added to the `slots` |
| 79 | + # meta slot of the schema === |
| 80 | + builder.add_class(cls, slots=slots, use_attributes=use_attributes) |
| 81 | + assert builder.schema.slots.keys() == slot_names |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize( |
| 85 | + ("cls", "extra_kwargs", "expected_added_class"), |
| 86 | + [ |
| 87 | + ("Person", {}, ClassDefinition(name="Person")), |
| 88 | + (2, {}, None), # Invalid type for `cls` |
| 89 | + ("Person", {"tree_root": True}, ClassDefinition(name="Person", tree_root=True)), |
| 90 | + ("Person", {"ijk": True}, None), # Invalid extra kwarg |
| 91 | + ( |
| 92 | + {"name": "Person", "tree_root": False}, |
| 93 | + {"tree_root": True}, |
| 94 | + ClassDefinition(name="Person", tree_root=True), |
| 95 | + ), |
| 96 | + ( |
| 97 | + {"name": "Person", "tree_root": False}, |
| 98 | + {"ijk": True}, # Invalid extra kwarg |
| 99 | + None, |
| 100 | + ), |
| 101 | + ( |
| 102 | + ClassDefinition(name="Person", tree_root=False), |
| 103 | + {"tree_root": True}, |
| 104 | + ClassDefinition(name="Person", tree_root=True), |
| 105 | + ), |
| 106 | + ( |
| 107 | + ClassDefinition(name="Person", tree_root=False), |
| 108 | + {"ijk": True}, # Invalid extra kwarg |
| 109 | + ClassDefinition(name="Person", tree_root=True), |
| 110 | + ), |
| 111 | + ], |
| 112 | +) |
| 113 | +def test_add_class_with_extra_kwargs( |
| 114 | + cls: Union[ClassDefinition, Dict, str], |
| 115 | + extra_kwargs: Dict[str, Any], |
| 116 | + expected_added_class: Optional[ClassDefinition], |
| 117 | +): |
| 118 | + """ |
| 119 | + Test adding a class with extra kwargs |
| 120 | + """ |
| 121 | + # The meta slots or fields of `ClassDefinition` |
| 122 | + class_meta_slots = {f.name for f in fields(ClassDefinition)} |
| 123 | + |
| 124 | + builder = SchemaBuilder() |
| 125 | + |
| 126 | + if not isinstance(cls, (str, dict, ClassDefinition)): |
| 127 | + with pytest.raises(TypeError, match="cls must be"): |
| 128 | + builder.add_class(cls, **extra_kwargs) |
| 129 | + elif extra_kwargs.keys() - class_meta_slots: |
| 130 | + # Handle the case of extra kwargs include a key that is not a meta slot of |
| 131 | + # `ClassDefinition` |
| 132 | + with pytest.raises(ValueError): |
| 133 | + builder.add_class(cls, **extra_kwargs) |
| 134 | + else: |
| 135 | + builder.add_class(cls, **extra_kwargs) |
| 136 | + |
| 137 | + if isinstance(cls, str): |
| 138 | + class_name = cls |
| 139 | + elif isinstance(cls, dict): |
| 140 | + class_name = cls["name"] |
| 141 | + else: |
| 142 | + class_name = cls.name |
| 143 | + |
| 144 | + added_class = builder.schema.classes[class_name] |
| 145 | + |
| 146 | + assert added_class == expected_added_class |
0 commit comments