nonebot-bison/tests/platforms/test_ncm_artist.py
Azide 32e3bcc022
🐛 修正项目的代码警告 (#614)
* 🐛 调整ruff的pytest警告

* 🐛 调整导入关系警告

* 🐛 删除奇怪无用的赋值和取值逻辑

*  不同测试部分所用变量应加以区分

* 🐛 subs_io model添加默认值

* 🐛 修完所有的 ruff PT001 警告

* 🔧 按ruff建议修改ruff配置

warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `pyproject.toml`:
  - 'ignore' -> 'lint.ignore'
  - 'select' -> 'lint.select'

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-08-17 18:24:20 +08:00

71 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import time
import typing
import respx
import pytest
from httpx import Response
from nonebug.app import App
from .utils import get_json
if typing.TYPE_CHECKING:
from nonebot_bison.platform.ncm import NcmArtist
@pytest.fixture
def ncm_artist(app: App):
from nonebot_bison.platform import platform_manager
from nonebot_bison.utils import ProcessContext, DefaultClientManager
return platform_manager["ncm-artist"](ProcessContext(DefaultClientManager()))
@pytest.fixture(scope="module")
def ncm_artist_raw():
return get_json("ncm_siren.json")
@pytest.fixture(scope="module")
def ncm_artist_0(ncm_artist_raw):
return {**ncm_artist_raw, "hotAlbums": ncm_artist_raw["hotAlbums"][1:]}
@pytest.fixture(scope="module")
def ncm_artist_1(ncm_artist_raw: dict):
res = ncm_artist_raw.copy()
res["hotAlbums"] = ncm_artist_raw["hotAlbums"][:]
res["hotAlbums"][0]["publishTime"] = int(time.time() * 1000)
return res
@pytest.mark.asyncio
@respx.mock
async def test_fetch_new(ncm_artist, ncm_artist_0, ncm_artist_1, dummy_user_subinfo):
from nonebot_bison.types import Target, SubUnit
ncm_router = respx.get("https://music.163.com/api/artist/albums/32540734")
ncm_router.mock(return_value=Response(200, json=ncm_artist_0))
target = Target("32540734")
res = await ncm_artist.fetch_new_post(SubUnit(target, [dummy_user_subinfo]))
assert ncm_router.called
assert len(res) == 0
ncm_router.mock(return_value=Response(200, json=ncm_artist_1))
res2 = await ncm_artist.fetch_new_post(SubUnit(target, [dummy_user_subinfo]))
post = res2[0][1][0]
assert post.platform.platform_name == "ncm-artist"
assert post.content == "新专辑发布Y1K"
assert post.url == "https://music.163.com/#/album?id=131074504"
async def test_parse_target(ncm_artist: "NcmArtist"):
from nonebot_bison.platform.platform import Platform
res = await ncm_artist.parse_target("32540734")
assert res == "32540734"
res = await ncm_artist.parse_target("https://music.163.com/#/artist?id=32540734")
assert res == "32540734"
res = await ncm_artist.parse_target("music.163.com/#/artist?id=32540734")
assert res == "32540734"
with pytest.raises(Platform.ParseTargetException):
await ncm_artist.parse_target("music.163.com/#/rad?id=32540734")