From 7bb52d172ce0226158afdd963725387acf1c7667 Mon Sep 17 00:00:00 2001 From: yedpodtrzitko Date: Fri, 14 Jun 2024 03:31:24 +0800 Subject: [PATCH] add pytest to CI pipeline --- .github/workflows/pytest.yaml | 22 ++++++ pyproject.toml | 1 + requirements-dev.txt | 1 + tagstudio/src/core/library.py | 2 +- tagstudio/tests/conftest.py | 36 ++++++++++ .../test_library_search[--nomatch--].json | 1 + .../test_lib/test_library_search[First].json | 6 ++ .../test_lib/test_library_search[Second].json | 6 ++ .../test_lib/test_open_library.json | 4 ++ tagstudio/tests/core/test_lib.py | 18 +++++ tagstudio/tests/core/test_tags.py | 26 +++---- .../library/.TagStudio/ts_library.json | 69 +++++++++++++++++++ tagstudio/tests/fixtures/library/bar.txt | 0 tagstudio/tests/fixtures/library/foo.txt | 0 14 files changed, 173 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/pytest.yaml create mode 100644 tagstudio/tests/conftest.py create mode 100644 tagstudio/tests/core/__snapshots__/test_lib/test_library_search[--nomatch--].json create mode 100644 tagstudio/tests/core/__snapshots__/test_lib/test_library_search[First].json create mode 100644 tagstudio/tests/core/__snapshots__/test_lib/test_library_search[Second].json create mode 100644 tagstudio/tests/core/__snapshots__/test_lib/test_open_library.json create mode 100644 tagstudio/tests/core/test_lib.py create mode 100644 tagstudio/tests/fixtures/library/.TagStudio/ts_library.json create mode 100644 tagstudio/tests/fixtures/library/bar.txt create mode 100644 tagstudio/tests/fixtures/library/foo.txt diff --git a/.github/workflows/pytest.yaml b/.github/workflows/pytest.yaml new file mode 100644 index 000000000..e8a458aa8 --- /dev/null +++ b/.github/workflows/pytest.yaml @@ -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/ diff --git a/pyproject.toml b/pyproject.toml index 1cd4b4c89..d60908da6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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'] diff --git a/requirements-dev.txt b/requirements-dev.txt index 7b90e16cb..81de1b330 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -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 diff --git a/tagstudio/src/core/library.py b/tagstudio/src/core/library.py index 59f494010..9e9cf28bc 100644 --- a/tagstudio/src/core/library.py +++ b/tagstudio/src/core/library.py @@ -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__() diff --git a/tagstudio/tests/conftest.py b/tagstudio/tests/conftest.py new file mode 100644 index 000000000..036acd8c8 --- /dev/null +++ b/tagstudio/tests/conftest.py @@ -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) diff --git a/tagstudio/tests/core/__snapshots__/test_lib/test_library_search[--nomatch--].json b/tagstudio/tests/core/__snapshots__/test_lib/test_library_search[--nomatch--].json new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/tagstudio/tests/core/__snapshots__/test_lib/test_library_search[--nomatch--].json @@ -0,0 +1 @@ +[] diff --git a/tagstudio/tests/core/__snapshots__/test_lib/test_library_search[First].json b/tagstudio/tests/core/__snapshots__/test_lib/test_library_search[First].json new file mode 100644 index 000000000..e4e6902ca --- /dev/null +++ b/tagstudio/tests/core/__snapshots__/test_lib/test_library_search[First].json @@ -0,0 +1,6 @@ +[ + [ + "", + 2 + ] +] diff --git a/tagstudio/tests/core/__snapshots__/test_lib/test_library_search[Second].json b/tagstudio/tests/core/__snapshots__/test_lib/test_library_search[Second].json new file mode 100644 index 000000000..920ff4959 --- /dev/null +++ b/tagstudio/tests/core/__snapshots__/test_lib/test_library_search[Second].json @@ -0,0 +1,6 @@ +[ + [ + "", + 1 + ] +] diff --git a/tagstudio/tests/core/__snapshots__/test_lib/test_open_library.json b/tagstudio/tests/core/__snapshots__/test_lib/test_open_library.json new file mode 100644 index 000000000..a576676f5 --- /dev/null +++ b/tagstudio/tests/core/__snapshots__/test_lib/test_open_library.json @@ -0,0 +1,4 @@ +[ + "{'id': 1, 'filename': 'foo.txt', 'path': '.', 'fields': [{6: [1001]}]}", + "{'id': 2, 'filename': 'bar.txt', 'path': '.', 'fields': [{6: [1000]}]}" +] diff --git a/tagstudio/tests/core/test_lib.py b/tagstudio/tests/core/test_lib.py new file mode 100644 index 000000000..997598f22 --- /dev/null +++ b/tagstudio/tests/core/test_lib.py @@ -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 diff --git a/tagstudio/tests/core/test_tags.py b/tagstudio/tests/core/test_tags.py index e8e487545..43cde4270 100644 --- a/tagstudio/tests/core/test_tags.py +++ b/tagstudio/tests/core/test_tags.py @@ -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] diff --git a/tagstudio/tests/fixtures/library/.TagStudio/ts_library.json b/tagstudio/tests/fixtures/library/.TagStudio/ts_library.json new file mode 100644 index 000000000..eeab9fd64 --- /dev/null +++ b/tagstudio/tests/fixtures/library/.TagStudio/ts_library.json @@ -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 + ] + } + ] + } + ] +} diff --git a/tagstudio/tests/fixtures/library/bar.txt b/tagstudio/tests/fixtures/library/bar.txt new file mode 100644 index 000000000..e69de29bb diff --git a/tagstudio/tests/fixtures/library/foo.txt b/tagstudio/tests/fixtures/library/foo.txt new file mode 100644 index 000000000..e69de29bb