From 2a86d10640e99b78834b777d7265df9f90bed526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Mon, 8 May 2023 09:21:32 +0800 Subject: [PATCH] :white_check_mark: Update test - Test multiple clients - Load testing configuration file using load_dotenv - Add `requirements.dev.txt` file --- requirements.dev.txt | 3 ++ tests/.env.example | 4 ++ tests/conftest.py | 38 +++++++-------- tests/test_base_chronicle_client.py | 30 ++++++++++++ tests/test_genshin.py | 30 ++++++++++++ tests/test_genshin_battle_chronicle_client.py | 43 +++++++++++++++++ tests/test_starrail.py | 40 ++++++++-------- .../test_starrail_battle_chronicle_client.py | 47 +++++++++++++++++++ 8 files changed, 195 insertions(+), 40 deletions(-) create mode 100644 requirements.dev.txt create mode 100644 tests/.env.example create mode 100644 tests/test_base_chronicle_client.py create mode 100644 tests/test_genshin.py create mode 100644 tests/test_genshin_battle_chronicle_client.py create mode 100644 tests/test_starrail_battle_chronicle_client.py diff --git a/requirements.dev.txt b/requirements.dev.txt new file mode 100644 index 0000000..407b8a6 --- /dev/null +++ b/requirements.dev.txt @@ -0,0 +1,3 @@ +pytest +pytest-asyncio +python-dotenv \ No newline at end of file diff --git a/tests/.env.example b/tests/.env.example new file mode 100644 index 0000000..667f44c --- /dev/null +++ b/tests/.env.example @@ -0,0 +1,4 @@ +COOKIES= +ACCOUNT_ID= +GENSHIN_PLAYER_ID= +STARRAIL_PLAYER_ID= \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index e98d7be..6c2d703 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,14 +1,17 @@ import asyncio import os import warnings +from pathlib import Path import pytest -import pytest_asyncio +from dotenv import load_dotenv from simnet.client.cookies import Cookies -from simnet.client.starrail import StarRailClient from simnet.utils.cookies import parse_cookie -from simnet.utils.enum_ import Region + +env_path = Path(".env") +if env_path.exists(): + load_dotenv() @pytest.fixture(scope="session") @@ -34,10 +37,18 @@ def cookies() -> "Cookies": # skipcq: PY-D0003 @pytest.fixture(scope="session") -def player_id() -> int: # skipcq: PY-D0003 - _player_id = os.environ.get("PLAYER_ID") +def genshin_player_id() -> int: # skipcq: PY-D0003 + _player_id = os.environ.get("GENSHIN_PLAYER_ID") if not _player_id: - pytest.exit("No player id set", 1) + pytest.exit("No genshin player id set", 1) + return int(_player_id) + + +@pytest.fixture(scope="session") +def starrail_player_id() -> int: # skipcq: PY-D0003 + _player_id = os.environ.get("STARRAIL_PLAYER_ID") + if not _player_id: + pytest.exit("No genshin player id set", 1) return int(_player_id) @@ -45,18 +56,5 @@ def player_id() -> int: # skipcq: PY-D0003 def account_id() -> int: # skipcq: PY-D0003 _account_id = os.environ.get("ACCOUNT_ID") if not _account_id: - pytest.exit("No player id set", 1) + pytest.exit("No account id set", 1) return int(_account_id) - - -@pytest_asyncio.fixture -async def starrail_client( # skipcq: PY-D0003 - player_id: int, account_id: int, cookies: "Cookies" # skipcq: PYL-W0621 -): - async with StarRailClient( - player_id=player_id, - cookies=cookies, - account_id=account_id, - region=Region.CHINESE, - ) as client_instance: - yield client_instance diff --git a/tests/test_base_chronicle_client.py b/tests/test_base_chronicle_client.py new file mode 100644 index 0000000..ade4bef --- /dev/null +++ b/tests/test_base_chronicle_client.py @@ -0,0 +1,30 @@ +from typing import TYPE_CHECKING + +import pytest +import pytest_asyncio + +from simnet.client.components.chronicle.base import BaseChronicleClient +from simnet.utils.enum_ import Region + +if TYPE_CHECKING: + from simnet.client.cookies import Cookies + + +@pytest_asyncio.fixture +async def base_chronicle_client(account_id: int, cookies: "Cookies"): + async with BaseChronicleClient( + cookies=cookies, + account_id=account_id, + region=Region.CHINESE, + ) as client_instance: + yield client_instance + + +@pytest.mark.asyncio +class TestBaseChronicleClient: + @staticmethod + async def test_get_record_cards(base_chronicle_client: "BaseChronicleClient"): + record_cards = await base_chronicle_client.get_record_cards() + assert len(record_cards) > 0 + record_card = record_cards[0] + assert record_card.uid diff --git a/tests/test_genshin.py b/tests/test_genshin.py new file mode 100644 index 0000000..5e9ee48 --- /dev/null +++ b/tests/test_genshin.py @@ -0,0 +1,30 @@ +from typing import TYPE_CHECKING + +import pytest +import pytest_asyncio + +from simnet.client.genshin import GenshinClient +from simnet.utils.enum_ import Region, Game + +if TYPE_CHECKING: + from simnet.client.cookies import Cookies + + +@pytest_asyncio.fixture +async def genshin_client( + genshin_player_id: int, account_id: int, cookies: "Cookies" +): # skipcq: PY-D0003 # skipcq: PYL-W0621 + async with GenshinClient( + player_id=genshin_player_id, + cookies=cookies, + account_id=account_id, + region=Region.CHINESE, + ) as client_instance: + yield client_instance + + +@pytest.mark.asyncio +class TestGenshinClient: + @staticmethod + async def test_game(genshin_client: "GenshinClient"): + assert genshin_client.game == Game.GENSHIN diff --git a/tests/test_genshin_battle_chronicle_client.py b/tests/test_genshin_battle_chronicle_client.py new file mode 100644 index 0000000..9b5bf7d --- /dev/null +++ b/tests/test_genshin_battle_chronicle_client.py @@ -0,0 +1,43 @@ +from typing import TYPE_CHECKING + +import pytest +import pytest_asyncio + +from simnet.client.components.chronicle.genshin import GenshinBattleChronicleClient +from simnet.models.genshin.chronicle.stats import FullGenshinUserStats, Stats +from simnet.utils.enum_ import Region + +if TYPE_CHECKING: + from simnet.client.cookies import Cookies + + +@pytest_asyncio.fixture +async def genshin_client(genshin_player_id: int, account_id: int, cookies: "Cookies"): + async with GenshinBattleChronicleClient( + player_id=genshin_player_id, + cookies=cookies, + account_id=account_id, + region=Region.CHINESE, + ) as client_instance: + yield client_instance + + +@pytest.mark.asyncio +class TestGenshinBattleChronicleClient: + @staticmethod + async def test_get_battle_chronicle(genshin_client: GenshinBattleChronicleClient): + user = await genshin_client.get_genshin_user() + assert user.stats.days_active >= 0 + + @staticmethod + async def test_get_full_genshin_user(genshin_client: GenshinBattleChronicleClient): + user = await genshin_client.get_full_genshin_user() + assert isinstance(user, FullGenshinUserStats) + assert isinstance(user.stats, Stats) + + @staticmethod + async def test_get_partial_genshin_user( + genshin_client: GenshinBattleChronicleClient, + ): + user = await genshin_client.get_partial_genshin_user() + assert user.stats.days_active >= 0 diff --git a/tests/test_starrail.py b/tests/test_starrail.py index 4a7ee57..08c463e 100644 --- a/tests/test_starrail.py +++ b/tests/test_starrail.py @@ -1,30 +1,30 @@ from typing import TYPE_CHECKING import pytest +import pytest_asyncio + +from simnet.client.starrail import StarRailClient +from simnet.utils.enum_ import Region, Game if TYPE_CHECKING: - from simnet.client.starrail import StarRailClient + from simnet.client.cookies import Cookies + + +@pytest_asyncio.fixture +async def starrail_client( + starrail_player_id: int, account_id: int, cookies: "Cookies" +): # skipcq: PY-D0003 # skipcq: PYL-W0621 + async with StarRailClient( + player_id=starrail_player_id, + cookies=cookies, + account_id=account_id, + region=Region.CHINESE, + ) as client_instance: + yield client_instance @pytest.mark.asyncio class TestStarRailClient: @staticmethod - async def test_get_starrail_user(starrail_client: "StarRailClient"): - user = await starrail_client.get_starrail_user() - assert user is not None - assert user.stats.chest_num > 0 - assert len(user.characters) > 0 - character = user.characters[-1] - assert character.id > 0 - - @staticmethod - async def test_get_starrail_notes(starrail_client: "StarRailClient"): - notes = await starrail_client.get_starrail_notes() - assert notes is not None - - @staticmethod - async def test_get_starrail_characters(starrail_client: "StarRailClient"): - characters = await starrail_client.get_starrail_characters() - assert len(characters.avatar_list) > 0 - character = characters.avatar_list[-1] - assert character.id > 0 + async def test_game(starrail_client: "StarRailClient"): + assert starrail_client.game == Game.STARRAIL diff --git a/tests/test_starrail_battle_chronicle_client.py b/tests/test_starrail_battle_chronicle_client.py new file mode 100644 index 0000000..ebae61a --- /dev/null +++ b/tests/test_starrail_battle_chronicle_client.py @@ -0,0 +1,47 @@ +from typing import TYPE_CHECKING + +import pytest +import pytest_asyncio + +from simnet.client.components.chronicle.starrail import StarRailBattleChronicleClient +from simnet.utils.enum_ import Region + +if TYPE_CHECKING: + from simnet.client.cookies import Cookies + + +@pytest_asyncio.fixture +async def starrail_client(starrail_player_id: int, account_id: int, cookies: "Cookies"): + async with StarRailBattleChronicleClient( + player_id=starrail_player_id, + cookies=cookies, + account_id=account_id, + region=Region.CHINESE, + ) as client_instance: + yield client_instance + + +@pytest.mark.asyncio +class TestStarrailBattleChronicleClient: + @staticmethod + async def test_get_starrail_user(starrail_client: "StarRailBattleChronicleClient"): + user = await starrail_client.get_starrail_user() + assert user is not None + assert user.stats.chest_num > 0 + assert len(user.characters) > 0 + character = user.characters[-1] + assert character.id > 0 + + @staticmethod + async def test_get_starrail_notes(starrail_client: "StarRailBattleChronicleClient"): + notes = await starrail_client.get_starrail_notes() + assert notes is not None + + @staticmethod + async def test_get_starrail_characters( + starrail_client: "StarRailBattleChronicleClient", + ): + characters = await starrail_client.get_starrail_characters() + assert len(characters.avatar_list) > 0 + character = characters.avatar_list[-1] + assert character.id > 0