Skip to content

Commit 5af475d

Browse files
Merge pull request #474 from linkml/schemaview_eq_tests
schemaview.py: tests for checking SchemaView equality
2 parents 3c42fc7 + ae9b463 commit 5af475d

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

tests/test_utils/test_schemaview.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,56 @@ def test_metamodel_in_schemaview() -> None:
725725
assert exp_slot_uri is not None
726726

727727

728+
def test_eq_true_false() -> None:
729+
"""Test that __eq__ returns True or False appropriately."""
730+
schema = SchemaDefinition(id="test-schema", name="TestSchema")
731+
view = SchemaView(schema)
732+
733+
# Create a new SchemaView with the same schema
734+
view_copy = SchemaView(schema)
735+
assert view.schema.id == view_copy.schema.id
736+
assert view.modifications == view_copy.modifications
737+
# the new schema will have a unique UUID
738+
assert view.uuid != view_copy.uuid
739+
# the two schemas will therefore not be equal
740+
assert view != view_copy
741+
742+
# copy over the uuid and modifications from the original schema
743+
view_copy.uuid = view.uuid
744+
view_copy.modifications = view.modifications
745+
746+
# the schemas are now equal. Hurrah!
747+
assert view == view_copy
748+
749+
# alter the modification count
750+
view_copy.modifications += 1
751+
assert view != view_copy
752+
753+
# Create a new SchemaView with a different schema
754+
diff_schema = SchemaDefinition(id="different-schema", name="DifferentSchema")
755+
diff_view = SchemaView(diff_schema)
756+
assert view != diff_view
757+
758+
# copy over the UUID and modifications from the original schema
759+
diff_view.uuid = view.uuid
760+
diff_view.modifications = view.modifications
761+
762+
# schemas have different IDs so will still be different
763+
assert diff_view != view
764+
765+
766+
def test_eq_not_implemented() -> None:
767+
"""Test that __eq__ returns NotImplemented for non-SchemaView objects."""
768+
schema = SchemaDefinition(id="test-schema", name="TestSchema")
769+
view = SchemaView(schema)
770+
771+
# Compare with a string
772+
assert view.__eq__("not-a-schemaview") is NotImplemented
773+
774+
# Compare with a different object
775+
assert view.__eq__(object()) is NotImplemented
776+
777+
728778
def test_in_schema(schema_view_with_imports: SchemaView) -> None:
729779
"""Test the in_schema function for determining the source schema of a class or slot."""
730780
view = schema_view_with_imports
@@ -734,6 +784,8 @@ def test_in_schema(schema_view_with_imports: SchemaView) -> None:
734784
assert view.in_schema(SlotDefinitionName("name")) == "core"
735785
assert view.in_schema(SlotDefinitionName(ACTIVITY)) == "core"
736786
assert view.in_schema(SlotDefinitionName("string")) == "types"
787+
with pytest.raises(ValueError, match="Element fake_element not in any schema"):
788+
view.in_schema("fake_element")
737789

738790

739791
CREATURE_EXPECTED = {
@@ -1108,6 +1160,12 @@ def test_all_classes_ordered_by(sv_ordering_tests: SchemaView, ordered_by: str)
11081160
assert list(sv_ordering_tests.all_classes(ordered_by=ordered_by).keys()) == ORDERING_TESTS[ordered_by]
11091161

11101162

1163+
def test_all_classes_ordered_by_error(sv_ordering_tests: SchemaView) -> None:
1164+
"""Test the ordered_by method throws an error when appropriate."""
1165+
with pytest.raises(ValueError, match="ordered_by must be in OrderedBy or None, got whatever"):
1166+
sv_ordering_tests.all_classes(ordered_by="whatever")
1167+
1168+
11111169
def test_all_classes_class_induced_slots(schema_view_with_imports: SchemaView) -> None:
11121170
"""Test all_classes and class_induced_slots."""
11131171
view = schema_view_with_imports

0 commit comments

Comments
 (0)