Skip to content

Commit

Permalink
添加单测
Browse files Browse the repository at this point in the history
  • Loading branch information
ssttkkl committed Oct 19, 2024
1 parent fbe3924 commit 7d94b9c
Show file tree
Hide file tree
Showing 10 changed files with 962 additions and 74 deletions.
13 changes: 13 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom: ['https://afdian.net/a/ssttkkl']
4 changes: 4 additions & 0 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
template: |
## What’s Changed
$CHANGES
31 changes: 31 additions & 0 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: PR Check

on:
pull_request:

jobs:
test:

strategy:
fail-fast: false
matrix:
os: [ macos-latest, windows-latest, ubuntu-latest ]
python-version: ["3.9", "3.10", "3.11", "3.12"]

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install requirements
run: |
pip install poetry
poetry export --with=dev --without-hashes -o requirements.txt
pip install -r requirements.txt
- name: Test with pytest
run: pytest
54 changes: 54 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Release

on:
push:
tags:
- v*

jobs:
publish-pypi-github:
runs-on: ubuntu-latest
environment:
name: release
url: https://pypi.org/p/nonebot-plugin-access-control
permissions:
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
# write permission is required to create a github release
contents: write
# write permission is required for autolabeler
# otherwise, read permission is required at least
pull-requests: write

steps:
- uses: actions/checkout@v3

- name: Setup Python environment
uses: actions/setup-python@v3
with:
python-version: "3.11"

- name: Setup Poetry
run: |
pip install poetry
- run: |
echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
- uses: release-drafter/release-drafter@v5
with:
name: Release ${{ env.TAG_NAME }}
tag: ${{ env.TAG_NAME }}
publish: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Publish PyPI and Github
run: |
poetry build
ls dist/
gh release upload --clobber ${{ env.TAG_NAME }} dist/*.tar.gz dist/*.whl
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Publish pypi
uses: pypa/gh-action-pypi-publish@release/v1
822 changes: 749 additions & 73 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ python = "^3.9"
nonebot2 = "^2.0"

[tool.poetry.group.dev.dependencies]
nonebot2 = { extras = ["httpx"], version = "^2.0" }
nonebot2 = {extras = ["fastapi"], version = "^2.3.3"}
nonebot-adapter-onebot = "^2.2.2"
pytest = "^8.3.3"
nonebug = "^0.3.7"
pytest-asyncio = "^0.24.0"
nonebot-plugin-send-anything-anywhere = "^0.7.1"

[build-system]
requires = ["poetry-core"]
Expand Down
Empty file added tests/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import nonebot
import pytest
from nonebot.adapters.onebot.v11 import Adapter


@pytest.fixture(scope="session", autouse=True)
def load_bot():
# 加载适配器
driver = nonebot.get_driver()
driver.register_adapter(Adapter)

# 加载插件
nonebot.load_plugin("nonebot_plugin_escape_url")
33 changes: 33 additions & 0 deletions tests/test_onebot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
from nonebot import on_message
from nonebot.adapters.onebot.v11 import Message
from nonebug import App

from tests.utils import mock_obv11_message_event, create_obv11_bot


@pytest.mark.asyncio
async def test_onebot(app: App):
from nonebot_plugin_saa import Text, MessageFactory

matcher = on_message()

@matcher.handle()
async def process():
await MessageFactory(Text("https://github.com")).send()

async with app.test_matcher(matcher) as ctx:
bot = create_obv11_bot(ctx)
msg_event = mock_obv11_message_event(Message("321"))
ctx.receive_event(bot, msg_event)
ctx.should_call_api(
"send_msg",
data={
"message": Message("github。com"),
"user_id": 2233,
"message_type": "private",
},
result={
"message_id": 667788,
},
)
60 changes: 60 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from typing import TYPE_CHECKING

from nonebug.mixin.call_api import ApiContext

if TYPE_CHECKING:
from nonebot.adapters.onebot.v11 import Message as OB11Message
from nonebot.adapters.onebot.v11 import Bot as OB11Bot


def create_obv11_bot(ctx: ApiContext) -> "OB11Bot":
from nonebot import get_adapter
from nonebot.adapters.onebot.v11 import Adapter as OB11Adapter
from nonebot.adapters.onebot.v11 import Bot as OB11Bot

return ctx.create_bot(
base=OB11Bot,
adapter=get_adapter(OB11Adapter.get_name())
)


def mock_obv11_message_event(message: "OB11Message", group: bool = False, message_id: int = 1234):
from nonebot.adapters.onebot.v11.event import Sender
from nonebot.adapters.onebot.v11 import GroupMessageEvent as OB11GroupMessageEvent
from nonebot.adapters.onebot.v11 import (
PrivateMessageEvent as OB11PrivateMessageEvent,
)

if not group:
return OB11PrivateMessageEvent(
time=1122,
self_id=2233,
post_type="message",
sub_type="",
user_id=2233,
message_type="private",
message_id=message_id,
message=message,
original_message=message,
raw_message=str(message),
font=1,
sender=Sender(user_id=2233),
to_me=False,
)
else:
return OB11GroupMessageEvent(
time=1122,
self_id=2233,
group_id=3344,
post_type="message",
sub_type="",
user_id=2233,
message_type="group",
message_id=message_id,
message=message,
original_message=message,
raw_message=str(message),
font=1,
sender=Sender(user_id=2233),
to_me=False,
)

0 comments on commit 7d94b9c

Please sign in to comment.