-
Notifications
You must be signed in to change notification settings - Fork 380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add unit tests for Tag class #199
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d0f479e
add unit tests for tag class methods
DJStowe bf67617
merge test-tag-class into main
DJStowe 3d7447b
Merge branch 'main' into test-tag-class
DJStowe f3ca7f2
Update requirements-dev.txt
DJStowe 7e3c97f
Merge branch 'main' of github.com:TagStudioDev/TagStudio
DJStowe feef907
Merge branch 'main' into test-tag-class
DJStowe 931d172
add integration job
DJStowe f66e62c
Merge branch 'test-tag-class' of github.com:DJStowe/TagStudio into te…
DJStowe 6c9e36b
test workflow
DJStowe 7c52462
fix path to test_tags.py
DJStowe d719e57
test workflow with test_tags.py
DJStowe 62a4ded
Merge branch 'TagStudioDev:main' into test-tag-class
DJStowe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: unit_tests | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
unit_tests: | ||
name: Run_tests | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repo | ||
uses: actions/checkout@v4 | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt | ||
pip install -r requirements-dev.txt | ||
pip install pytest-mock==3.14.0 | ||
|
||
- name: Run tests | ||
run: | | ||
pytest tagstudio/tests/core/test_tags.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ pre-commit==3.7.0 | |
pytest==8.2.0 | ||
Pyinstaller==6.6.0 | ||
mypy==1.10.0 | ||
pytest-mock==3.14.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,157 @@ | ||
import sys | ||
import os | ||
import pytest | ||
Check failure on line 3 in tagstudio/tests/core/test_tags.py GitHub Actions / Run MyPy
|
||
|
||
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="") | ||
|
||
@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, | ||
name="Tag Name", | ||
shorthand="TN", | ||
aliases=["First A", "Second A"], | ||
subtags_ids=[2, 3, 4], | ||
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.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 = "" | ||
|
||
def test_construction(): | ||
tag = Tag( | ||
id=1, | ||
name="Tag Name", | ||
shorthand="TN", | ||
aliases=["First A", "Second A"], | ||
subtags_ids=[2, 3, 4], | ||
color="", | ||
) | ||
assert tag | ||
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] | ||
|
||
def test_empty_construction(): | ||
tag = Tag(id=1, name="", shorthand="", aliases=[], subtags_ids=[], color="") | ||
assert tag | ||
mock_library.get_tag.return_value = test_tag2 | ||
result = test_tag1.display_name(mock_library) | ||
assert result == "tag1 Name (tag2 Name)" | ||
''' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isnt this wrapping class superfluous?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment yes, but it's needed for one of the commented out tests. I think display_name should probably be changed, but I wanted to have tests for it if we want to keep it as is. Maybe I'll comment that fixture out for now and open an issue to change display_name?