Skip to content

Commit

Permalink
✅ 补充未安装任何适配器时的测试 (#172)
Browse files Browse the repository at this point in the history
* 🐛 修正typing.Literal使用

* ✅ 补充未安装任何适配器时的测试

* ✅ 修改action名称

* ✅ 上报cov
  • Loading branch information
AzideCupric authored Aug 18, 2024
1 parent b859e94 commit 21ed320
Show file tree
Hide file tree
Showing 28 changed files with 118 additions and 29 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/test-no-adapters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Test Without Adapters

on:
push:
branches:
- main
- next
pull_request:
paths:
- "nonebot_plugin_saa/**"
- "tests/**"
- "pyproject.toml"
- "poetry.lock"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
name: Test
runs-on: ubuntu-latest
strategy:
fail-fast: false
env:
OS: ubuntu-latest
PYTHON_VERSION: 3.12

steps:
- uses: actions/checkout@v3

- name: Setup Python environment
uses: ./.github/actions/setup-python
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: Install prerequisites
run: poetry install --without adapters

- name: Run Pytest
run: poetry run pytest --cov-report xml --cov=./nonebot_plugin_saa

- name: Upload coverage report
uses: codecov/codecov-action@v3
with:
env_vars: OS,PYTHON_VERSION
token: ${{ secrets.CODECOV_TOKEN }}
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pathlib import Path
from functools import partial
from contextlib import suppress
from typing import Any, Optional, cast
from typing import Any, Literal, Optional, cast

from nonebot.adapters import Event
from nonebot.drivers import Request
Expand Down Expand Up @@ -53,7 +53,7 @@
MessageFactory.register_adapter_message(SupportedAdapters.discord, Message)

class DiscordMessageId(MessageId):
adapter_name: SupportedAdapters = adapter
adapter_name: Literal[SupportedAdapters.discord] = adapter
message_id: SnowflakeType
channel_id: Optional[SnowflakeType] = None

Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/dodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
MessageFactory.register_adapter_message(adapter, Message)

class DodoMessageId(MessageId):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.dodo] = adapter

message_id: str
reason: Optional[None] = None
Expand Down Expand Up @@ -178,7 +178,7 @@ def _gen_private(target: PlatformTarget) -> dict[str, Any]:
}

class DodoReceipt(Receipt):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.dodo] = adapter
message_id: str

async def revoke(self, reason: Optional[str] = None):
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/feishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
MessageFactory.register_adapter_message(adapter, Message)

class FeishuMessageId(MessageId):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.feishu] = adapter
message_id: str

@register_feishu(Text)
Expand Down Expand Up @@ -99,7 +99,7 @@ def _extract_channel_msg_event(event: Event) -> TargetFeishuGroup:

class FeishuReceipt(Receipt):
message_id: str
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.feishu] = adapter
data: dict[str, Any]

async def revoke(self):
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/kaiheila.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def wrapper(*args, **kwargs):
MessageFactory.register_adapter_message(SupportedAdapters.kaiheila, Message)

class KaiheilaMessageId(MessageId):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.kaiheila] = adapter
message_id: str

@register_kaiheila(Text)
Expand Down Expand Up @@ -115,7 +115,7 @@ def _gen_channel(target: PlatformTarget) -> dict[str, Any]:
}

class KaiheilaReceipt(Receipt):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.kaiheila] = adapter
data: MessageCreateReturn

async def revoke(self):
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/onebot_v11.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
MessageFactory.register_adapter_message(SupportedAdapters.onebot_v11, Message)

class OB11MessageId(MessageId):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.onebot_v11] = adapter

message_id: int

Expand Down Expand Up @@ -162,7 +162,7 @@ def _gen_group(target: PlatformTarget) -> dict[str, Any]:

class OB11Receipt(Receipt):
message_id: int
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.onebot_v11] = adapter

async def revoke(self):
return await cast(BotOB11, self._get_bot()).delete_msg(
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/onebot_v12.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
MessageFactory.register_adapter_message(adapter, Message)

class OB12MessageId(MessageId):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.onebot_v12] = adapter
message_id: str

@register_onebot_v12(Text)
Expand Down Expand Up @@ -249,7 +249,7 @@ def _to_unknow(target: PlatformTarget):

class OB12Receipt(Receipt):
message_id: str
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.onebot_v12] = adapter

async def revoke(self):
return await self._get_bot().delete_message(message_id=self.message_id)
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/qq.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
MessageFactory.register_adapter_message(adapter, Message)

class QQMessageId(MessageId):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.qq] = adapter
message_id: str

@register_qq(Text)
Expand Down Expand Up @@ -125,7 +125,7 @@ class QQReceipt(Receipt):
PostC2CFilesReturn,
PostGroupFilesReturn,
]
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.qq] = adapter

async def revoke(self, hidetip=False):
if not isinstance(self.msg_return, ApiMessage):
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/red.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
MessageFactory.register_adapter_message(SupportedAdapters.red, Message)

class RedMessageId(MessageId):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.red] = adapter
message_seq: str
message_id: Optional[str] = None
sender_uin: Optional[str] = None
Expand Down Expand Up @@ -111,7 +111,7 @@ def _gen_group(target: PlatformTarget) -> dict[str, Any]:
}

class RedReceipt(Receipt):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.red] = adapter
message: MessageModel

async def revoke(self):
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/satori.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class SatoriPlatform(str, Enum):
MessageFactory.register_adapter_message(SupportedAdapters.satori, Message)

class SatoriMessageId(MessageId):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.satori] = adapter

message_id: str

Expand Down Expand Up @@ -177,7 +177,7 @@ def _to_unknow(target: PlatformTarget):
return {"channel": target.channel_id}

class SatoriReceipt(Receipt):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.satori] = adapter
messages: list[SatoriMessage]

async def revoke(self):
Expand Down
4 changes: 2 additions & 2 deletions nonebot_plugin_saa/adapters/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
MessageFactory.register_adapter_message(SupportedAdapters.telegram, Message)

class TelegramMessageId(MessageId):
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.telegram] = adapter
message_id: int
chat_id: Union[int, str, None] = None

Expand Down Expand Up @@ -136,7 +136,7 @@ def build_mention_from_event(event: MessageEvent) -> MessageSegment:
class TelegramReceipt(Receipt):
chat_id: Union[int, str]
messages: list[MessageModel]
adapter_name: Literal[adapter] = adapter
adapter_name: Literal[SupportedAdapters.telegram] = adapter

async def revoke(self):
bot = cast(BotTG, self._get_bot())
Expand Down
27 changes: 18 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import pytest
import nonebot
from nonebug import NONEBOT_INIT_KWARGS, App
from nonebot.adapters.qq import Adapter as QQAdapter
from nonebot.adapters.red import Adapter as RedAdapter
from nonebot.adapters.dodo import Adapter as DoDoAdapter
from nonebot.adapters.feishu import Adapter as FeishuAdapter
from nonebot.adapters.satori import Adapter as SatoriAdapter
from nonebot.adapters.discord import Adapter as DiscordAdpter
from nonebot.adapters.telegram import Adapter as TelegramAdapter
from nonebot.adapters.onebot.v11 import Adapter as OnebotV11Adapter
from nonebot.adapters.onebot.v12 import Adapter as OnebotV12Adapter


def pytest_configure(config: pytest.Config) -> None:
Expand All @@ -28,6 +19,24 @@ def app(app: App):

@pytest.fixture(scope="session", autouse=True)
def load_adapters(nonebug_init: None): # noqa: PT004
pytest.importorskip("nonebot.adapters.qq")
pytest.importorskip("nonebot.adapters.red")
pytest.importorskip("nonebot.adapters.dodo")
pytest.importorskip("nonebot.adapters.feishu")
pytest.importorskip("nonebot.adapters.satori")
pytest.importorskip("nonebot.adapters.discord")
pytest.importorskip("nonebot.adapters.telegram")
pytest.importorskip("nonebot.adapters.onebot")
from nonebot.adapters.qq import Adapter as QQAdapter
from nonebot.adapters.red import Adapter as RedAdapter
from nonebot.adapters.dodo import Adapter as DoDoAdapter
from nonebot.adapters.feishu import Adapter as FeishuAdapter
from nonebot.adapters.satori import Adapter as SatoriAdapter
from nonebot.adapters.discord import Adapter as DiscordAdpter
from nonebot.adapters.telegram import Adapter as TelegramAdapter
from nonebot.adapters.onebot.v11 import Adapter as OnebotV11Adapter
from nonebot.adapters.onebot.v12 import Adapter as OnebotV12Adapter

driver = nonebot.get_driver()
driver.register_adapter(OnebotV11Adapter)
driver.register_adapter(OnebotV12Adapter)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_aggreated_send.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest
from nonebug import App

pytest.importorskip("nonebot.adapters.onebot")

from .utils import ob12_kwargs, mock_obv12_message_event


Expand Down
2 changes: 2 additions & 0 deletions tests/test_auto_select_qq_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from functools import partial

import pytest

pytest.importorskip("nonebot.adapters.qq")
from nonebug import App
from pytest_mock import MockerFixture
from nonebot import get_driver, get_adapter
Expand Down
2 changes: 2 additions & 0 deletions tests/test_custom_message_segment.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest

pytest.importorskip("nonebot.adapters.onebot")
from nonebug.app import App

from .utils import assert_ms, ob12_kwargs
Expand Down
2 changes: 2 additions & 0 deletions tests/test_discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import httpx
import respx
import pytest

pytest.importorskip("nonebot.adapters.discord")
from nonebug import App
from nonebot import get_driver
from pytest_mock import MockerFixture
Expand Down
2 changes: 2 additions & 0 deletions tests/test_dodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import respx
import pytest

pytest.importorskip("nonebot.adapters.dodo")
from nonebug import App
from httpx import Response
from nonebot.compat import model_dump
Expand Down
2 changes: 2 additions & 0 deletions tests/test_feishu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import respx
import pytest

pytest.importorskip("nonebot.adapters.feishu")
from nonebug import App
from httpx import Response
from nonebot import get_driver
Expand Down
2 changes: 2 additions & 0 deletions tests/test_message.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from io import BytesIO

import pytest

pytest.importorskip("nonebot.adapters.onebot")
from nonebug import App
from nonebot import get_driver
from nonebot.adapters.onebot.v11.bot import Bot
Expand Down
2 changes: 2 additions & 0 deletions tests/test_message_segment_build.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest

pytest.importorskip("nonebot.adapters.onebot")
from nonebug import App

from .utils import assert_ms, mock_obv11_message_event
Expand Down
2 changes: 2 additions & 0 deletions tests/test_onebot_v11.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from functools import partial

import pytest

pytest.importorskip("nonebot.adapters.onebot")
from nonebug import App
from nonebot import get_adapter
from pytest_mock import MockerFixture
Expand Down
2 changes: 2 additions & 0 deletions tests/test_onebot_v12.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from functools import partial

import pytest

pytest.importorskip("nonebot.adapters.onebot")
from nonebug import App
from pytest_mock import MockerFixture
from nonebot import get_driver, get_adapter
Expand Down
2 changes: 2 additions & 0 deletions tests/test_platform_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from typing import Literal

import pytest

pytest.importorskip("nonebot.adapters.onebot")
from nonebug import App
from pydantic import BaseModel
from nonebot.compat import model_dump, type_validate_python
Expand Down
2 changes: 2 additions & 0 deletions tests/test_qq.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from functools import partial

import pytest

pytest.importorskip("nonebot.adapters.qq")
from nonebug import App
from anyio import open_file
from nonebot import get_adapter
Expand Down
2 changes: 2 additions & 0 deletions tests/test_red.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import httpx
import pytest

pytest.importorskip("nonebot.adapters.red")
from nonebug import App
from respx import MockRouter
from nonebot import get_adapter
Expand Down
2 changes: 2 additions & 0 deletions tests/test_satori.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from typing import Any, Literal

import pytest

pytest.importorskip("nonebot.adapters.satori")
from nonebug import App
from nonebot import get_adapter
from pytest_mock import MockerFixture
Expand Down
2 changes: 2 additions & 0 deletions tests/test_send.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest

pytest.importorskip("nonebot.adapters.onebot")
from nonebug import App
from nonebot import get_driver, on_message

Expand Down
Loading

0 comments on commit 21ed320

Please sign in to comment.