diff --git a/pyproject.toml b/pyproject.toml index 770538e..9436458 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "ontouml-py" -version = "0.1.0" +version = "0.1.1" description = "OntoUML Python Library" license = "Apache-2.0" authors = ["Pedro Paulo F. Barcelos "] diff --git a/tests/abstract_classes/test_relation.py b/tests/abstract_classes/test_relation.py new file mode 100644 index 0000000..bb873af --- /dev/null +++ b/tests/abstract_classes/test_relation.py @@ -0,0 +1,102 @@ +import pytest + +from ontouml_py.classes.abstract_classes.relation import Relation +from ontouml_py.classes.concrete_classes.binaryrelation import BinaryRelation +from ontouml_py.classes.concrete_classes.naryrelation import NaryRelation +from ontouml_py.classes.enumerations.relationstereotype import RelationStereotype + + +def test_relation_initialization_with_valid_stereotype() -> None: + """Test the initialization of a Relation subclass with a valid stereotype.""" + + class BinaryRelation(Relation): + def __init__(self, **data): + super().__init__(**data) + + relation = BinaryRelation(stereotype=RelationStereotype.MATERIAL) + assert ( + relation.stereotype == RelationStereotype.MATERIAL + ), "Relation should be initialized with the specified stereotype" + + +def test_relation_initialization_with_invalid_stereotype() -> None: + """Test the initialization of a Relation subclass with an invalid stereotype.""" + + class BinaryRelation(Relation): + def __init__(self, **data): + super().__init__(**data) + + with pytest.raises(ValueError): + BinaryRelation(stereotype="invalid_stereotype") + + +def test_relation_subclass_validation() -> None: + """Test the validation of allowed subclasses for Relation.""" + + class InvalidRelation(Relation): + def __init__(self, **data): + super().__init__(**data) + + with pytest.raises(ValueError): + InvalidRelation() + + +def test_binary_relation_as_valid_subclass() -> None: + """Test that BinaryRelation is a valid subclass of Relation.""" + binary_relation = BinaryRelation() + assert isinstance(binary_relation, Relation), "BinaryRelation should be a valid subclass of Relation" + + +def test_nary_relation_as_valid_subclass() -> None: + """Test that NaryRelation is a valid subclass of Relation.""" + nary_relation = NaryRelation() + assert isinstance(nary_relation, Relation), "NaryRelation should be a valid subclass of Relation" + + +def test_relation_default_values() -> None: + """Test the default values of attributes in the Relation class.""" + + class BinaryRelation(Relation): + def __init__(self, **data): + super().__init__(**data) + + relation = BinaryRelation() + assert relation.stereotype is None, "Default value of stereotype should be None" + + +def test_relation_attribute_assignment() -> None: + """Test attribute assignment in the Relation class.""" + + class BinaryRelation(Relation): + def __init__(self, **data): + super().__init__(**data) + + relation = BinaryRelation() + relation.stereotype = RelationStereotype.MATERIAL + assert relation.stereotype == RelationStereotype.MATERIAL, "Stereotype should be assignable in Relation class" + + +def test_relation_initialization_with_each_stereotype() -> None: + """Test the initialization of a Relation subclass with each stereotype in RelationStereotype.""" + + class BinaryRelation(Relation): + def __init__(self, **data): + super().__init__(**data) + + for stereotype in RelationStereotype: + relation = BinaryRelation(stereotype=stereotype) + assert relation.stereotype == stereotype, f"Relation should be initialized with the stereotype {stereotype}" + + +def test_relation_initialization_with_incorrect_type_for_stereotype() -> None: + """Test the initialization of a Relation subclass with incorrect type for stereotype.""" + + class BinaryRelation(Relation): + def __init__(self, **data): + super().__init__(**data) + + with pytest.raises(ValueError): + BinaryRelation(stereotype=123) # Using an integer instead of a RelationStereotype member + + with pytest.raises(ValueError): + BinaryRelation(stereotype="non_existent_stereotype") # Using a non-existent stereotype string diff --git a/tests/utils/test_nonemptyset.py b/tests/utils/test_nonemptyset.py index bc58583..30dfeaa 100644 --- a/tests/utils/test_nonemptyset.py +++ b/tests/utils/test_nonemptyset.py @@ -3,61 +3,61 @@ from ontouml_py.classes.utils.nonemptyset import NonEmptySet -def test_nonemptyset_initialization(): +def test_nonemptyset_initialization() -> None: """Test initialization of NonEmptySet with non-empty set.""" non_empty_set = NonEmptySet({1, 2, 3}) assert len(non_empty_set) == 3, "NonEmptySet should initialize with the correct number of elements" -def test_nonemptyset_initialization_empty_set(): +def test_nonemptyset_initialization_empty_set() -> None: """Test initialization of NonEmptySet with an empty set raises ValueError.""" with pytest.raises(ValueError, match="Initial set cannot be empty."): NonEmptySet(set()) -def test_nonemptyset_add_element(): +def test_nonemptyset_add_element() -> None: """Test adding an element to the NonEmptySet.""" non_empty_set = NonEmptySet({1}) non_empty_set.add(2) assert 2 in non_empty_set, "Element should be added to NonEmptySet" -def test_nonemptyset_remove_element(): +def test_nonemptyset_remove_element() -> None: """Test removing an element from the NonEmptySet.""" non_empty_set = NonEmptySet({1, 2}) non_empty_set.remove(1) assert 1 not in non_empty_set, "Element should be removed from NonEmptySet" -def test_nonemptyset_remove_last_element(): +def test_nonemptyset_remove_last_element() -> None: """Test removing the last element from the NonEmptySet raises ValueError.""" non_empty_set = NonEmptySet({1}) with pytest.raises(ValueError, match="Cannot remove the last element from the set."): non_empty_set.remove(1) -def test_nonemptyset_update(): +def test_nonemptyset_update() -> None: """Test updating NonEmptySet with another set.""" non_empty_set = NonEmptySet({1}) non_empty_set.update({2, 3}) assert non_empty_set == NonEmptySet({1, 2, 3}), "NonEmptySet should be updated correctly" -def test_nonemptyset_discard_existing_element(): +def test_nonemptyset_discard_existing_element() -> None: """Test discarding an existing element from NonEmptySet.""" non_empty_set = NonEmptySet({1, 2}) non_empty_set.discard(1) assert 1 not in non_empty_set, "Existing element should be discarded from NonEmptySet" -def test_nonemptyset_discard_last_element(): +def test_nonemptyset_discard_last_element() -> None: """Test discarding the last element from NonEmptySet raises ValueError.""" non_empty_set = NonEmptySet({1}) with pytest.raises(ValueError, match="Cannot discard the last element from the set."): non_empty_set.discard(1) -def test_nonemptyset_pop_element(): +def test_nonemptyset_pop_element() -> None: """Test popping an element from NonEmptySet.""" non_empty_set = NonEmptySet({1, 2}) popped = non_empty_set.pop() @@ -65,40 +65,40 @@ def test_nonemptyset_pop_element(): assert len(non_empty_set) == 1, "NonEmptySet should have one less element after pop" -def test_nonemptyset_pop_last_element(): +def test_nonemptyset_pop_last_element() -> None: """Test popping the last element from NonEmptySet raises ValueError.""" non_empty_set = NonEmptySet({1}) with pytest.raises(ValueError, match="Cannot pop the last element from the set."): non_empty_set.pop() -def test_nonemptyset_clear(): +def test_nonemptyset_clear() -> None: """Test clearing NonEmptySet raises ValueError.""" non_empty_set = NonEmptySet({1, 2, 3}) with pytest.raises(ValueError, match="Cannot clear a NonEmptySet."): non_empty_set.clear() -def test_nonemptyset_convert_set(): +def test_nonemptyset_convert_set() -> None: """Test converting a regular set to NonEmptySet.""" regular_set = {1, 2, 3} non_empty_set = NonEmptySet.convert_set(regular_set) assert non_empty_set == NonEmptySet({1, 2, 3}), "NonEmptySet should be correctly created from a regular set" -def test_nonemptyset_convert_empty_set(): +def test_nonemptyset_convert_empty_set() -> None: """Test converting an empty set to NonEmptySet raises ValueError.""" with pytest.raises(ValueError, match="Cannot create a NonEmptySet from an empty set"): NonEmptySet.convert_set(set()) -def test_nonemptyset_str_representation(): +def test_nonemptyset_str_representation() -> None: """Test the string representation of NonEmptySet.""" non_empty_set = NonEmptySet({1, 2, 3}) assert str(non_empty_set) == "{1, 2, 3}", "String representation of NonEmptySet should be correct" -def test_nonemptyset_repr_representation(): +def test_nonemptyset_repr_representation() -> None: """Test the official string representation of NonEmptySet.""" non_empty_set = NonEmptySet({1, 2}) assert (