Skip to content

Commit d0f479e

Browse files
committed
add unit tests for tag class methods
1 parent 491ebb6 commit d0f479e

File tree

2 files changed

+157
-13
lines changed

2 files changed

+157
-13
lines changed

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ pre-commit==3.7.0
33
pytest==8.2.0
44
Pyinstaller==6.6.0
55
mypy==1.10.0
6+
pytest-mock 3.14.0

tagstudio/tests/core/test_tags.py

Lines changed: 156 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,161 @@
1+
import sys
2+
import os
3+
import pytest
4+
5+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))
6+
17
from src.core.library import Tag
28

9+
@pytest.fixture
10+
def mock_library(mocker):
11+
return mocker.Mock()
12+
13+
@pytest.fixture
14+
def mock_subtag(mocker):
15+
return mocker.Mock()
16+
17+
@pytest.fixture
18+
def test_tag1():
19+
return Tag(id=1, name="tag1 Name", shorthand="tag1", aliases=[], subtags_ids=[2], color="")
20+
21+
@pytest.fixture
22+
def test_tag2():
23+
return Tag(id=2, name="tag2 Name", shorthand="tag2", aliases=[], subtags_ids=[3], color="")
24+
25+
@pytest.fixture
26+
def test_tag3():
27+
return Tag(id=3, name="tag3 Name", shorthand="tag3", aliases=[], subtags_ids=[], color="")
28+
29+
class TestTags():
30+
def test_construction(self):
31+
tag = Tag(
32+
id=1,
33+
name="Tag Name",
34+
shorthand="TN",
35+
aliases=["First A", "Second A"],
36+
subtags_ids=[2, 3, 4],
37+
color="",
38+
)
39+
assert tag
40+
assert tag.id == 1
41+
assert tag.name == "Tag Name"
42+
assert tag.shorthand == "TN"
43+
assert tag.aliases == ["First A", "Second A"]
44+
assert tag.subtag_ids == [2, 3, 4]
45+
assert tag.color == ""
46+
47+
def test_empty_construction(self):
48+
tag = Tag(id=1, name="", shorthand="", aliases=[], subtags_ids=[], color="")
49+
assert tag.id == 1
50+
assert tag.name == ""
51+
assert tag.shorthand == ""
52+
assert tag.aliases == []
53+
assert tag.subtag_ids == []
54+
assert tag.color == ""
55+
56+
def test_add_subtag(self, test_tag1):
57+
test_tag1.subtag_ids = []
58+
assert test_tag1.subtag_ids == []
59+
assert len(test_tag1.subtag_ids) == 0
60+
61+
test_tag1.add_subtag(2)
62+
test_tag1.add_subtag(3)
63+
assert test_tag1.subtag_ids == [2,3]
64+
assert len(test_tag1.subtag_ids) == 2
65+
66+
#No Duplicates added
67+
test_tag1.add_subtag(2)
68+
assert len(test_tag1.subtag_ids) == 2
69+
assert test_tag1.subtag_ids == [2,3]
70+
71+
def test_remove_subtag(self, test_tag1):
72+
test_tag1.subtag_ids = [1,2,3,4,5]
73+
assert len(test_tag1.subtag_ids) == 5
74+
75+
test_tag1.remove_subtag(3)
76+
assert len(test_tag1.subtag_ids) == 4
77+
assert test_tag1.subtag_ids == [1,2,4,5]
78+
79+
test_tag1.remove_subtag(2)
80+
assert len(test_tag1.subtag_ids) == 3
81+
assert test_tag1.subtag_ids == [1,4,5]
82+
83+
def test_remove_subtag_not_in_subtag_ids(self, test_tag1):
84+
test_tag1.remove_subtag(1)
85+
assert test_tag1.subtag_ids == [2]
86+
test_tag1.remove_subtag(2)
87+
assert test_tag1.subtag_ids == []
88+
test_tag1.remove_subtag(2)
89+
assert test_tag1.subtag_ids == []
90+
91+
def test_debug_name(self, test_tag1, test_tag2):
92+
assert test_tag1.debug_name() == "tag1 Name (ID: 1)"
93+
assert test_tag2.debug_name() == "tag2 Name (ID: 2)"
94+
95+
def test_display_name_no_shorthand(self, mock_library, test_tag1, test_tag2):
96+
test_tag2.shorthand = ""
97+
mock_library.get_tag.return_value = test_tag2
98+
result = test_tag1.display_name(mock_library)
99+
assert result == "tag1 Name (tag2 Name)"
100+
101+
def test_display_name_with_shorthand(self, mock_library, test_tag1, test_tag2):
102+
mock_library.get_tag.return_value = test_tag2
103+
result = test_tag1.display_name(mock_library)
104+
assert result == "tag1 Name (tag2)"
105+
106+
def test_display_name_no_subtags(self, mock_library, test_tag1):
107+
test_tag1.subtag_ids = []
108+
result = test_tag1.display_name(mock_library)
109+
assert result == "tag1 Name"
110+
'''
111+
#This probably isn't how we want display_names to work. But these tests pass if uncommented
112+
def test_display_name_no_name(self, mock_library, test_tag1, test_tag2):
113+
test_tag2.name = ""
114+
test_tag1.name = ""
115+
test_tag2.shorthand = ""
116+
test_tag1.shorthand = ""
117+
118+
mock_library.get_tag.return_value = test_tag2
119+
result = test_tag1.display_name(mock_library)
120+
assert result == " ()"
121+
122+
def test_display_name_no_name_no_subtag_ids(self, mock_library, test_tag1):
123+
test_tag1.name = ""
124+
test_tag1.shorthand = ""
125+
test_tag1.subtag_ids = []
126+
127+
result = test_tag1.display_name(mock_library)
128+
assert result == ""
129+
130+
def test_display_name_no_name_with_subtag_name(self, mock_library, test_tag1, test_tag2):
131+
test_tag1.name = ""
132+
test_tag1.shorthand = ""
133+
test_tag1.subtag_ids = [2]
134+
135+
mock_library.get_tag.return_value = test_tag2
136+
result = test_tag1.display_name(mock_library)
137+
assert result == " (tag2)"
138+
139+
def test_display_name_no_name_with_subtag_name_no_shorthand(self, mock_library, test_tag1, test_tag2):
140+
test_tag1.name = ""
141+
test_tag1.shorthand = ""
142+
test_tag1.subtag_ids = [2]
143+
test_tag2.shorthand = ""
144+
145+
mock_library.get_tag.return_value = test_tag2
146+
result = test_tag1.display_name(mock_library)
147+
assert result == " (tag2 Name)"
148+
149+
def test_display_name_2_subtags_no_shorthand(self, mock_library, test_tag1, test_tag2, test_tag3):
150+
test_tag2.shorthand = ""
151+
test_tag3.shorthand = ""
152+
test_tag1.subtag_ids = [2,3]
3153
4-
def test_construction():
5-
tag = Tag(
6-
id=1,
7-
name="Tag Name",
8-
shorthand="TN",
9-
aliases=["First A", "Second A"],
10-
subtags_ids=[2, 3, 4],
11-
color="",
12-
)
13-
assert tag
154+
mock_library.get_tag.return_value = test_tag2
155+
result = test_tag1.display_name(mock_library)
156+
assert result == "tag1 Name (tag2 Name)"
157+
'''
14158

159+
if __name__ == "__main__":
160+
pytest.main()
15161

16-
def test_empty_construction():
17-
tag = Tag(id=1, name="", shorthand="", aliases=[], subtags_ids=[], color="")
18-
assert tag

0 commit comments

Comments
 (0)