Skip to content

Commit

Permalink
add unit tests for tag class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
DJStowe committed May 20, 2024
1 parent 0641eb4 commit 62468de
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 2 deletions.
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ruff==0.4.2
pre-commit==3.7.0
pytest==8.2.0
Pyinstaller==6.6.0
mypy==1.10.0
pytest-mock 3.14.0
147 changes: 145 additions & 2 deletions tagstudio/tests/core/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
import sys
import os
import pytest

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..')))

from src.core.library import Tag

@pytest.fixture
def mock_library(mocker):
return mocker.Mock()

@pytest.fixture
def mock_subtag(mocker):
return mocker.Mock()

@pytest.fixture
def test_tag1():
return Tag(id=1, name="tag1 Name", shorthand="tag1", aliases=[], subtags_ids=[2], color="")

class TestTags:
@pytest.fixture
def test_tag2():
return Tag(id=2, name="tag2 Name", shorthand="tag2", aliases=[], subtags_ids=[3], color="")

@pytest.fixture
def test_tag3():
return Tag(id=3, name="tag3 Name", shorthand="tag3", aliases=[], subtags_ids=[], color="")

class TestTags():
def test_construction(self):
tag = Tag(
id=1,
Expand All @@ -12,7 +37,125 @@ def test_construction(self):
color="",
)
assert tag
assert tag.id == 1
assert tag.name == "Tag Name"
assert tag.shorthand == "TN"
assert tag.aliases == ["First A", "Second A"]
assert tag.subtag_ids == [2, 3, 4]
assert tag.color == ""

def test_empty_construction(self):
tag = Tag(id=1, name="", shorthand="", aliases=[], subtags_ids=[], color="")
assert tag
assert tag.id == 1
assert tag.name == ""
assert tag.shorthand == ""
assert tag.aliases == []
assert tag.subtag_ids == []
assert tag.color == ""

def test_add_subtag(self, test_tag1):
test_tag1.subtag_ids = []
assert test_tag1.subtag_ids == []
assert len(test_tag1.subtag_ids) == 0

test_tag1.add_subtag(2)
test_tag1.add_subtag(3)
assert test_tag1.subtag_ids == [2,3]
assert len(test_tag1.subtag_ids) == 2

#No Duplicates added
test_tag1.add_subtag(2)
assert len(test_tag1.subtag_ids) == 2
assert test_tag1.subtag_ids == [2,3]

def test_remove_subtag(self, test_tag1):
test_tag1.subtag_ids = [1,2,3,4,5]
assert len(test_tag1.subtag_ids) == 5

test_tag1.remove_subtag(3)
assert len(test_tag1.subtag_ids) == 4
assert test_tag1.subtag_ids == [1,2,4,5]

test_tag1.remove_subtag(2)
assert len(test_tag1.subtag_ids) == 3
assert test_tag1.subtag_ids == [1,4,5]

def test_remove_subtag_not_in_subtag_ids(self, test_tag1):
test_tag1.remove_subtag(1)
assert test_tag1.subtag_ids == [2]
test_tag1.remove_subtag(2)
assert test_tag1.subtag_ids == []
test_tag1.remove_subtag(2)
assert test_tag1.subtag_ids == []

def test_debug_name(self, test_tag1, test_tag2):
assert test_tag1.debug_name() == "tag1 Name (ID: 1)"
assert test_tag2.debug_name() == "tag2 Name (ID: 2)"

def test_display_name_no_shorthand(self, mock_library, test_tag1, test_tag2):
test_tag2.shorthand = ""
mock_library.get_tag.return_value = test_tag2
result = test_tag1.display_name(mock_library)
assert result == "tag1 Name (tag2 Name)"

def test_display_name_with_shorthand(self, mock_library, test_tag1, test_tag2):
mock_library.get_tag.return_value = test_tag2
result = test_tag1.display_name(mock_library)
assert result == "tag1 Name (tag2)"

def test_display_name_no_subtags(self, mock_library, test_tag1):
test_tag1.subtag_ids = []
result = test_tag1.display_name(mock_library)
assert result == "tag1 Name"
'''
#This probably isn't how we want display_names to work. But these tests pass if uncommented
def test_display_name_no_name(self, mock_library, test_tag1, test_tag2):
test_tag2.name = ""
test_tag1.name = ""
test_tag2.shorthand = ""
test_tag1.shorthand = ""
mock_library.get_tag.return_value = test_tag2
result = test_tag1.display_name(mock_library)
assert result == " ()"
def test_display_name_no_name_no_subtag_ids(self, mock_library, test_tag1):
test_tag1.name = ""
test_tag1.shorthand = ""
test_tag1.subtag_ids = []
result = test_tag1.display_name(mock_library)
assert result == ""
def test_display_name_no_name_with_subtag_name(self, mock_library, test_tag1, test_tag2):
test_tag1.name = ""
test_tag1.shorthand = ""
test_tag1.subtag_ids = [2]
mock_library.get_tag.return_value = test_tag2
result = test_tag1.display_name(mock_library)
assert result == " (tag2)"
def test_display_name_no_name_with_subtag_name_no_shorthand(self, mock_library, test_tag1, test_tag2):
test_tag1.name = ""
test_tag1.shorthand = ""
test_tag1.subtag_ids = [2]
test_tag2.shorthand = ""
mock_library.get_tag.return_value = test_tag2
result = test_tag1.display_name(mock_library)
assert result == " (tag2 Name)"
def test_display_name_2_subtags_no_shorthand(self, mock_library, test_tag1, test_tag2, test_tag3):
test_tag2.shorthand = ""
test_tag3.shorthand = ""
test_tag1.subtag_ids = [2,3]
mock_library.get_tag.return_value = test_tag2
result = test_tag1.display_name(mock_library)
assert result == "tag1 Name (tag2 Name)"
'''

if __name__ == "__main__":
pytest.main()

0 comments on commit 62468de

Please sign in to comment.