Skip to content

Commit 6d237ed

Browse files
committed
add pytest to CI pipeline
1 parent 5bc80a0 commit 6d237ed

13 files changed

+106
-19
lines changed

.github/workflows/pytest.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: unit_tests
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
pytest:
7+
name: Run tests
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- name: Checkout repo
12+
uses: actions/checkout@v4
13+
14+
- name: Install dependencies
15+
run: |
16+
python -m pip install --upgrade pip
17+
pip install -r requirements.txt
18+
pip install -r requirements-dev.txt
19+
20+
- name: Run tests
21+
run: |
22+
pytest tagstudio/tests/

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ strict_optional = false
66
disable_error_code = ["union-attr", "annotation-unchecked", "import-untyped"]
77
explicit_package_bases = true
88
warn_unused_ignores = true
9+
exclude = ['tests']

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+
syrupy==4.6.1

tagstudio/src/core/library.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def __init__(
8080
# self.word_count: int = None
8181

8282
def __str__(self) -> str:
83-
return f"\n{self.compressed_dict()}\n"
83+
return str(self.compressed_dict())
8484

8585
def __repr__(self) -> str:
8686
return self.__str__()

tagstudio/tests/conftest.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
import pathlib
3+
4+
import pytest
5+
from syrupy.extensions.json import JSONSnapshotExtension
6+
7+
CWD = pathlib.Path(__file__).parent
8+
9+
sys.path.insert(0, str(CWD.parent))
10+
11+
from src.core.library import Tag, Library
12+
13+
14+
@pytest.fixture
15+
def test_tag():
16+
yield Tag(
17+
id=1,
18+
name="Tag Name",
19+
shorthand="TN",
20+
aliases=["First A", "Second A"],
21+
subtags_ids=[2, 3, 4],
22+
color="",
23+
)
24+
25+
26+
@pytest.fixture
27+
def test_library():
28+
lib = Library()
29+
ret_code = lib.open_library(CWD / "fixtures" / "library")
30+
print("CWD", CWD)
31+
print(CWD.glob("*"))
32+
assert ret_code == 1
33+
yield lib
34+
35+
36+
@pytest.fixture
37+
def snapshot_json(snapshot):
38+
return snapshot.with_defaults(extension_class=JSONSnapshotExtension)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
[
3+
"<ItemType.ENTRY: 0>",
4+
2
5+
]
6+
]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
[
3+
"<ItemType.ENTRY: 0>",
4+
1
5+
]
6+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
"{'id': 1, 'filename': 'foo.txt', 'path': '.', 'fields': [{6: [1001]}]}",
3+
"{'id': 2, 'filename': 'bar.txt', 'path': '.', 'fields': [{6: [1000]}]}"
4+
]

tagstudio/tests/core/test_lib.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
3+
4+
def test_open_library(test_library, snapshot_json):
5+
assert test_library.entries == snapshot_json
6+
7+
8+
@pytest.mark.parametrize(
9+
["query"],
10+
[
11+
("First",),
12+
("Second",),
13+
("--nomatch--",),
14+
],
15+
)
16+
def test_library_search(test_library, query, snapshot_json):
17+
res = test_library.search_library(query)
18+
assert res == snapshot_json

tagstudio/tests/core/test_tags.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
from src.core.library import Tag
2-
3-
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
14-
15-
16-
def test_empty_construction():
17-
tag = Tag(id=1, name="", shorthand="", aliases=[], subtags_ids=[], color="")
18-
assert tag
1+
def test_subtag(test_tag):
2+
test_tag.remove_subtag(2)
3+
test_tag.remove_subtag(2)
4+
5+
test_tag.add_subtag(5)
6+
# repeated add should not add the subtag
7+
test_tag.add_subtag(5)
8+
assert test_tag.subtag_ids == [3, 4, 5]

tagstudio/tests/fixtures/library/bar.txt

Whitespace-only changes.

tagstudio/tests/fixtures/library/foo.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)