Skip to content
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 tests into CI #286

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: pytest

on: [push, pull_request]

jobs:
pytest:
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

- name: Run tests
run: |
pytest tagstudio/tests/
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ strict_optional = false
disable_error_code = ["union-attr", "annotation-unchecked", "import-untyped"]
explicit_package_bases = true
warn_unused_ignores = true
exclude = ['tests']
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pre-commit==3.7.0
pytest==8.2.0
Pyinstaller==6.6.0
mypy==1.10.0
syrupy==4.6.1
2 changes: 1 addition & 1 deletion tagstudio/src/core/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(
# self.word_count: int = None

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

def __repr__(self) -> str:
return self.__str__()
Expand Down
36 changes: 36 additions & 0 deletions tagstudio/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sys
import pathlib

import pytest
from syrupy.extensions.json import JSONSnapshotExtension

CWD = pathlib.Path(__file__).parent

sys.path.insert(0, str(CWD.parent))

from src.core.library import Tag, Library


@pytest.fixture
def test_tag():
yield Tag(
id=1,
name="Tag Name",
shorthand="TN",
aliases=["First A", "Second A"],
subtags_ids=[2, 3, 4],
color="",
)


@pytest.fixture
def test_library():
lib = Library()
ret_code = lib.open_library(CWD / "fixtures" / "library")
assert ret_code == 1
yield lib


@pytest.fixture
def snapshot_json(snapshot):
return snapshot.with_defaults(extension_class=JSONSnapshotExtension)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
[
"<ItemType.ENTRY: 0>",
2
]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
[
"<ItemType.ENTRY: 0>",
1
]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"{'id': 1, 'filename': 'foo.txt', 'path': '.', 'fields': [{6: [1001]}]}",
"{'id': 2, 'filename': 'bar.txt', 'path': '.', 'fields': [{6: [1000]}]}"
]
18 changes: 18 additions & 0 deletions tagstudio/tests/core/test_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest


def test_open_library(test_library, snapshot_json):
assert test_library.entries == snapshot_json


@pytest.mark.parametrize(
["query"],
[
("First",),
("Second",),
("--nomatch--",),
],
)
def test_library_search(test_library, query, snapshot_json):
res = test_library.search_library(query)
assert res == snapshot_json
26 changes: 8 additions & 18 deletions tagstudio/tests/core/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
from src.core.library import Tag


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


def test_empty_construction():
tag = Tag(id=1, name="", shorthand="", aliases=[], subtags_ids=[], color="")
assert tag
def test_subtag(test_tag):
test_tag.remove_subtag(2)
test_tag.remove_subtag(2)

test_tag.add_subtag(5)
# repeated add should not add the subtag
test_tag.add_subtag(5)
assert test_tag.subtag_ids == [3, 4, 5]
69 changes: 69 additions & 0 deletions tagstudio/tests/fixtures/library/.TagStudio/ts_library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"ts-version": "9.3.1",
"ext_list": [
".json",
".xmp",
".aae"
],
"is_exclude_list": true,
"tags": [
{
"id": 0,
"name": "Archived",
"aliases": [
"Archive"
],
"color": "Red"
},
{
"id": 1,
"name": "Favorite",
"aliases": [
"Favorited",
"Favorites"
],
"color": "Yellow"
},
{
"id": 1000,
"name": "first",
"shorthand": "first",
"color": "magenta"
},
{
"id": 1001,
"name": "second",
"shorthand": "second",
"color": "blue"
}
],
"collations": [],
"fields": [],
"macros": [],
"entries": [
{
"id": 1,
"filename": "foo.txt",
"path": ".",
"fields": [
{
"6": [
1001
]
}
]
},
{
"id": 2,
"filename": "bar.txt",
"path": ".",
"fields": [
{
"6": [
1000
]
}
]
}
]
}
Empty file.
Empty file.