From 56c1d5427a9ba4a2f583d9161e69cb464a22b206 Mon Sep 17 00:00:00 2001 From: Firq Date: Sun, 20 Oct 2024 21:38:22 +0200 Subject: [PATCH] Added testing --- .forgejo/workflows/check.yaml | 14 ++++++++ .gitignore | 1 + pyproject.toml | 11 ++++++ skyeweave/service/atlas.py | 10 +++--- skyeweave/service/compose.py | 6 ++-- tests/conftest.py | 15 ++++++++ tests/test_01_class.py | 36 +++++++++++++++++++ tests/test_02_download.py | 66 +++++++++++++++++++++++++++++++++++ 8 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 tests/conftest.py create mode 100644 tests/test_01_class.py create mode 100644 tests/test_02_download.py diff --git a/.forgejo/workflows/check.yaml b/.forgejo/workflows/check.yaml index 53d66b6..ffa1e64 100644 --- a/.forgejo/workflows/check.yaml +++ b/.forgejo/workflows/check.yaml @@ -32,3 +32,17 @@ jobs: run: | mypy --version mypy . + + tests: + runs-on: docker + container: forgejo.neshweb.net/ci-docker-images/python-neshweb:3.11 + steps: + - name: Checkout source code + uses: https://code.forgejo.org/actions/checkout@v3 + - name: Install packages + run: | + pip install -e .[testing] --disable-pip-version-check --no-cache-dir -q + python -m pip list --format=columns --disable-pip-version-check + - name: Run pytest + run: | + pytest diff --git a/.gitignore b/.gitignore index 838321e..fdd92d4 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ assets/ __pycache__/ *.egg-info/ .mypy_cache/ +.pytest_cache/ diff --git a/pyproject.toml b/pyproject.toml index bed9a13..5bdc627 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,9 @@ typing = [ "types-tqdm~=4.66.0", "types-requests~=2.32.0", ] +testing = [ + "pytest~=8.3.3" +] [project.scripts] skyeweave = "skyeweave.cli:run" @@ -35,6 +38,7 @@ skyeweave = "skyeweave.cli:run" [tool.setuptools.packages.find] where = ["."] include = ["skyeweave*"] +exclude = ["tests*"] [tool.setuptools.package-data] "*" = ["py.typed"] @@ -55,6 +59,13 @@ warn_return_any = true warn_unused_configs = true exclude = [ "test" ] +[tool.pytest.ini_options] +minversion = "8.0" +addopts = "-rA -v" +testpaths = [ + "tests", +] + [build-system] requires = ["setuptools >= 61.0"] build-backend = "setuptools.build_meta" diff --git a/skyeweave/service/atlas.py b/skyeweave/service/atlas.py index 7ca9595..3b232da 100644 --- a/skyeweave/service/atlas.py +++ b/skyeweave/service/atlas.py @@ -15,7 +15,7 @@ class ExtendData(TypedDict): faceSizeRect: NotRequired[Annotated[List[int], 2]] faceSize: NotRequired[int] -def fetch_config(chara_id: str) -> SpritesheetData: +def fetch_config(chara_id: int) -> SpritesheetData: url = f"https://api.atlasacademy.io/raw/JP/svtScript?charaId={chara_id}" LOGGER.debug(f"Loading data for {url}") @@ -62,7 +62,7 @@ def fetch_mstsvtjson(): break handle.write(block) -def fetch_expression_sheets(tempfolder: pathlib.Path, imageid: str): +def fetch_expression_sheets(tempfolder: pathlib.Path, imageid: int): atlasurl_base = f"https://static.atlasacademy.io/{AtlasAPIConfig.REGION}/CharaFigure/{imageid}" savefolder = tempfolder / str(imageid) @@ -106,7 +106,7 @@ def fetch_expression_sheets(tempfolder: pathlib.Path, imageid: str): return savefolder -def fetch_data(servantid: int) -> List[str]: +def fetch_data(servantid: int) -> List[int]: atlasurl = f"https://api.atlasacademy.io/nice/{AtlasAPIConfig.REGION}/servant/{servantid}?lore=false&lang=en" LOGGER.debug(f"Loading data for {atlasurl}") @@ -118,8 +118,8 @@ def fetch_data(servantid: int) -> List[str]: responsedata = response.json() svtname = responsedata["name"] - charascripts: List[dict[str, str]] = responsedata["charaScripts"] - chara_ids: List[str] = [chara["id"] for chara in charascripts] + charascripts: List[dict[str, int]] = responsedata["charaScripts"] + chara_ids: List[int] = [chara["id"] for chara in charascripts] LOGGER.debug(chara_ids) LOGGER.info(f"{svtname} ({servantid}) - {len(chara_ids)} charaIds") diff --git a/skyeweave/service/compose.py b/skyeweave/service/compose.py index 7296900..7e5259f 100644 --- a/skyeweave/service/compose.py +++ b/skyeweave/service/compose.py @@ -18,10 +18,10 @@ class CharaInfos(TypedDict): class SkyeWeave: output_folder: pathlib.Path image_folder: pathlib.Path - chara_ids: List[str] + chara_ids: List[int] chara_infos: Dict[str, CharaInfos] - def __init__(self, input_id: int, filters: Optional[List[str]] = None, output: Optional[pathlib.Path] = None, assets: Optional[pathlib.Path] = None): + def __init__(self, input_id: int, filters: Optional[List[int]] = None, output: Optional[pathlib.Path] = None, assets: Optional[pathlib.Path] = None): _output_folder = output or PathConfig.OUTPUT _image_folder = assets or PathConfig.IMAGES @@ -31,7 +31,7 @@ class SkyeWeave: self.image_folder = _image_folder / str(input_id) else: LOGGER.info(f"Processing manually uploaded charaId {input_id}") - _chara_ids = [str(input_id)] + _chara_ids = [input_id] self.output_folder = _output_folder / "manual" self.image_folder = _image_folder / "manual" diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..f597c97 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,15 @@ +import pathlib +import shutil +import pytest + +@pytest.fixture(scope="function") +def path_images(): + path = pathlib.Path(__file__).parent / "test_path_images" + yield path + shutil.rmtree(path) + +@pytest.fixture(scope="function") +def path_output(): + path = pathlib.Path(__file__).parent / "test_path_output" + yield path + shutil.rmtree(path) diff --git a/tests/test_01_class.py b/tests/test_01_class.py new file mode 100644 index 0000000..11d45fd --- /dev/null +++ b/tests/test_01_class.py @@ -0,0 +1,36 @@ +from pathlib import Path +from skyeweave import SkyeWeave + +def test_servantid_create(path_images: Path, path_output: Path): + test_id = 70 + + test_weaver = SkyeWeave( + test_id, + output=path_output, + assets=path_images + ) + + assert test_weaver.output_folder == path_output / str(test_id) + assert path_output.exists() + assert (path_output / str(test_id)).exists() + + assert test_weaver.image_folder == path_images / str(test_id) + assert path_images.exists() + assert (path_images / str(test_id)).exists() + +def test_charaid_create(path_images: Path, path_output: Path): + test_id = 3013002 + + test_weaver = SkyeWeave( + test_id, + output=path_output, + assets=path_images + ) + + assert test_weaver.output_folder == path_output / "manual" + assert path_output.exists() + assert (path_output / "manual").exists() + + assert test_weaver.image_folder == path_images / "manual" + assert path_images.exists() + assert (path_images / "manual").exists() diff --git a/tests/test_02_download.py b/tests/test_02_download.py new file mode 100644 index 0000000..92cfa6d --- /dev/null +++ b/tests/test_02_download.py @@ -0,0 +1,66 @@ +from pathlib import Path +from skyeweave import SkyeWeave + +def test_servantid_download(path_images: Path, path_output: Path): + test_id = 70 + + test_weaver = SkyeWeave( + test_id, + output=path_output, + assets=path_images + ) + test_weaver.download() + + expected_path = path_images / str(test_id) + expected_dirs = [ "3013000", "3013001", "3013002", "3013300", "1098204200", "1098264100", "1098290800" ] + + dirs = [f for f in expected_path.iterdir() if f.is_dir()] + assert set([d.name for d in dirs]) == set(expected_dirs) + + for d in dirs: + expected_files = ["0.png", "1.png"] + files = [f.name for f in d.iterdir() if f.is_file()] + assert set(files) == set(expected_files) + +def test_servantid_download_filter(path_images: Path, path_output: Path): + test_id = 70 + + test_weaver = SkyeWeave( + test_id, + filters=[ 3013000, 3013001, 1098290800], + output=path_output, + assets=path_images + ) + test_weaver.download() + + expected_path = path_images / str(test_id) + expected_dirs = [ "3013000", "3013001", "1098290800" ] + + dirs =[f for f in expected_path.iterdir() if f.is_dir()] + assert set([d.name for d in dirs]) == set(expected_dirs) + + for d in dirs: + expected_files = ["0.png", "1.png"] + files = [f.name for f in d.iterdir() if f.is_file()] + assert set(files) == set(expected_files) + +def test_charaid_download(path_images: Path, path_output: Path): + test_id = 3013000 + + test_weaver = SkyeWeave( + test_id, + output=path_output, + assets=path_images + ) + test_weaver.download() + + expected_path = path_images / "manual" + expected_dirs = [ "3013000" ] + + dirs = [f for f in expected_path.iterdir() if f.is_dir()] + assert set([d.name for d in dirs]) == set(expected_dirs) + + for d in dirs: + expected_files = ["0.png", "1.png"] + files = [f.name for f in d.iterdir() if f.is_file()] + assert set(files) == set(expected_files)