Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 983f370

Browse files
committed
added test_relation.py
1 parent b3a4431 commit 983f370

File tree

3 files changed

+118
-16
lines changed

3 files changed

+118
-16
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ontouml-py"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
description = "OntoUML Python Library"
55
license = "Apache-2.0"
66
authors = ["Pedro Paulo F. Barcelos <[email protected]>"]
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import pytest
2+
3+
from ontouml_py.classes.abstract_classes.relation import Relation
4+
from ontouml_py.classes.concrete_classes.binaryrelation import BinaryRelation
5+
from ontouml_py.classes.concrete_classes.naryrelation import NaryRelation
6+
from ontouml_py.classes.enumerations.relationstereotype import RelationStereotype
7+
8+
9+
def test_relation_initialization_with_valid_stereotype() -> None:
10+
"""Test the initialization of a Relation subclass with a valid stereotype."""
11+
12+
class BinaryRelation(Relation):
13+
def __init__(self, **data):
14+
super().__init__(**data)
15+
16+
relation = BinaryRelation(stereotype=RelationStereotype.MATERIAL)
17+
assert (
18+
relation.stereotype == RelationStereotype.MATERIAL
19+
), "Relation should be initialized with the specified stereotype"
20+
21+
22+
def test_relation_initialization_with_invalid_stereotype() -> None:
23+
"""Test the initialization of a Relation subclass with an invalid stereotype."""
24+
25+
class BinaryRelation(Relation):
26+
def __init__(self, **data):
27+
super().__init__(**data)
28+
29+
with pytest.raises(ValueError):
30+
BinaryRelation(stereotype="invalid_stereotype")
31+
32+
33+
def test_relation_subclass_validation() -> None:
34+
"""Test the validation of allowed subclasses for Relation."""
35+
36+
class InvalidRelation(Relation):
37+
def __init__(self, **data):
38+
super().__init__(**data)
39+
40+
with pytest.raises(ValueError):
41+
InvalidRelation()
42+
43+
44+
def test_binary_relation_as_valid_subclass() -> None:
45+
"""Test that BinaryRelation is a valid subclass of Relation."""
46+
binary_relation = BinaryRelation()
47+
assert isinstance(binary_relation, Relation), "BinaryRelation should be a valid subclass of Relation"
48+
49+
50+
def test_nary_relation_as_valid_subclass() -> None:
51+
"""Test that NaryRelation is a valid subclass of Relation."""
52+
nary_relation = NaryRelation()
53+
assert isinstance(nary_relation, Relation), "NaryRelation should be a valid subclass of Relation"
54+
55+
56+
def test_relation_default_values() -> None:
57+
"""Test the default values of attributes in the Relation class."""
58+
59+
class BinaryRelation(Relation):
60+
def __init__(self, **data):
61+
super().__init__(**data)
62+
63+
relation = BinaryRelation()
64+
assert relation.stereotype is None, "Default value of stereotype should be None"
65+
66+
67+
def test_relation_attribute_assignment() -> None:
68+
"""Test attribute assignment in the Relation class."""
69+
70+
class BinaryRelation(Relation):
71+
def __init__(self, **data):
72+
super().__init__(**data)
73+
74+
relation = BinaryRelation()
75+
relation.stereotype = RelationStereotype.MATERIAL
76+
assert relation.stereotype == RelationStereotype.MATERIAL, "Stereotype should be assignable in Relation class"
77+
78+
79+
def test_relation_initialization_with_each_stereotype() -> None:
80+
"""Test the initialization of a Relation subclass with each stereotype in RelationStereotype."""
81+
82+
class BinaryRelation(Relation):
83+
def __init__(self, **data):
84+
super().__init__(**data)
85+
86+
for stereotype in RelationStereotype:
87+
relation = BinaryRelation(stereotype=stereotype)
88+
assert relation.stereotype == stereotype, f"Relation should be initialized with the stereotype {stereotype}"
89+
90+
91+
def test_relation_initialization_with_incorrect_type_for_stereotype() -> None:
92+
"""Test the initialization of a Relation subclass with incorrect type for stereotype."""
93+
94+
class BinaryRelation(Relation):
95+
def __init__(self, **data):
96+
super().__init__(**data)
97+
98+
with pytest.raises(ValueError):
99+
BinaryRelation(stereotype=123) # Using an integer instead of a RelationStereotype member
100+
101+
with pytest.raises(ValueError):
102+
BinaryRelation(stereotype="non_existent_stereotype") # Using a non-existent stereotype string

tests/utils/test_nonemptyset.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,102 +3,102 @@
33
from ontouml_py.classes.utils.nonemptyset import NonEmptySet
44

55

6-
def test_nonemptyset_initialization():
6+
def test_nonemptyset_initialization() -> None:
77
"""Test initialization of NonEmptySet with non-empty set."""
88
non_empty_set = NonEmptySet({1, 2, 3})
99
assert len(non_empty_set) == 3, "NonEmptySet should initialize with the correct number of elements"
1010

1111

12-
def test_nonemptyset_initialization_empty_set():
12+
def test_nonemptyset_initialization_empty_set() -> None:
1313
"""Test initialization of NonEmptySet with an empty set raises ValueError."""
1414
with pytest.raises(ValueError, match="Initial set cannot be empty."):
1515
NonEmptySet(set())
1616

1717

18-
def test_nonemptyset_add_element():
18+
def test_nonemptyset_add_element() -> None:
1919
"""Test adding an element to the NonEmptySet."""
2020
non_empty_set = NonEmptySet({1})
2121
non_empty_set.add(2)
2222
assert 2 in non_empty_set, "Element should be added to NonEmptySet"
2323

2424

25-
def test_nonemptyset_remove_element():
25+
def test_nonemptyset_remove_element() -> None:
2626
"""Test removing an element from the NonEmptySet."""
2727
non_empty_set = NonEmptySet({1, 2})
2828
non_empty_set.remove(1)
2929
assert 1 not in non_empty_set, "Element should be removed from NonEmptySet"
3030

3131

32-
def test_nonemptyset_remove_last_element():
32+
def test_nonemptyset_remove_last_element() -> None:
3333
"""Test removing the last element from the NonEmptySet raises ValueError."""
3434
non_empty_set = NonEmptySet({1})
3535
with pytest.raises(ValueError, match="Cannot remove the last element from the set."):
3636
non_empty_set.remove(1)
3737

3838

39-
def test_nonemptyset_update():
39+
def test_nonemptyset_update() -> None:
4040
"""Test updating NonEmptySet with another set."""
4141
non_empty_set = NonEmptySet({1})
4242
non_empty_set.update({2, 3})
4343
assert non_empty_set == NonEmptySet({1, 2, 3}), "NonEmptySet should be updated correctly"
4444

4545

46-
def test_nonemptyset_discard_existing_element():
46+
def test_nonemptyset_discard_existing_element() -> None:
4747
"""Test discarding an existing element from NonEmptySet."""
4848
non_empty_set = NonEmptySet({1, 2})
4949
non_empty_set.discard(1)
5050
assert 1 not in non_empty_set, "Existing element should be discarded from NonEmptySet"
5151

5252

53-
def test_nonemptyset_discard_last_element():
53+
def test_nonemptyset_discard_last_element() -> None:
5454
"""Test discarding the last element from NonEmptySet raises ValueError."""
5555
non_empty_set = NonEmptySet({1})
5656
with pytest.raises(ValueError, match="Cannot discard the last element from the set."):
5757
non_empty_set.discard(1)
5858

5959

60-
def test_nonemptyset_pop_element():
60+
def test_nonemptyset_pop_element() -> None:
6161
"""Test popping an element from NonEmptySet."""
6262
non_empty_set = NonEmptySet({1, 2})
6363
popped = non_empty_set.pop()
6464
assert popped in {1, 2}, "Popped element should be from NonEmptySet"
6565
assert len(non_empty_set) == 1, "NonEmptySet should have one less element after pop"
6666

6767

68-
def test_nonemptyset_pop_last_element():
68+
def test_nonemptyset_pop_last_element() -> None:
6969
"""Test popping the last element from NonEmptySet raises ValueError."""
7070
non_empty_set = NonEmptySet({1})
7171
with pytest.raises(ValueError, match="Cannot pop the last element from the set."):
7272
non_empty_set.pop()
7373

7474

75-
def test_nonemptyset_clear():
75+
def test_nonemptyset_clear() -> None:
7676
"""Test clearing NonEmptySet raises ValueError."""
7777
non_empty_set = NonEmptySet({1, 2, 3})
7878
with pytest.raises(ValueError, match="Cannot clear a NonEmptySet."):
7979
non_empty_set.clear()
8080

8181

82-
def test_nonemptyset_convert_set():
82+
def test_nonemptyset_convert_set() -> None:
8383
"""Test converting a regular set to NonEmptySet."""
8484
regular_set = {1, 2, 3}
8585
non_empty_set = NonEmptySet.convert_set(regular_set)
8686
assert non_empty_set == NonEmptySet({1, 2, 3}), "NonEmptySet should be correctly created from a regular set"
8787

8888

89-
def test_nonemptyset_convert_empty_set():
89+
def test_nonemptyset_convert_empty_set() -> None:
9090
"""Test converting an empty set to NonEmptySet raises ValueError."""
9191
with pytest.raises(ValueError, match="Cannot create a NonEmptySet from an empty set"):
9292
NonEmptySet.convert_set(set())
9393

9494

95-
def test_nonemptyset_str_representation():
95+
def test_nonemptyset_str_representation() -> None:
9696
"""Test the string representation of NonEmptySet."""
9797
non_empty_set = NonEmptySet({1, 2, 3})
9898
assert str(non_empty_set) == "{1, 2, 3}", "String representation of NonEmptySet should be correct"
9999

100100

101-
def test_nonemptyset_repr_representation():
101+
def test_nonemptyset_repr_representation() -> None:
102102
"""Test the official string representation of NonEmptySet."""
103103
non_empty_set = NonEmptySet({1, 2})
104104
assert (

0 commit comments

Comments
 (0)