Update test

- Test multiple clients
- Load testing configuration file using load_dotenv
- Add `requirements.dev.txt` file
This commit is contained in:
洛水居室 2023-05-08 09:21:32 +08:00 committed by GitHub
parent 72542c9676
commit 2a86d10640
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 195 additions and 40 deletions

3
requirements.dev.txt Normal file
View File

@ -0,0 +1,3 @@
pytest
pytest-asyncio
python-dotenv

4
tests/.env.example Normal file
View File

@ -0,0 +1,4 @@
COOKIES=
ACCOUNT_ID=
GENSHIN_PLAYER_ID=
STARRAIL_PLAYER_ID=

View File

@ -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

View File

@ -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

30
tests/test_genshin.py Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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