Skip to content

Commit

Permalink
test: 修改测试,使得代码可以通过
Browse files Browse the repository at this point in the history
  • Loading branch information
BigOrangeQWQ committed Oct 12, 2024
1 parent 0edacca commit b01332e
Show file tree
Hide file tree
Showing 51 changed files with 3,032 additions and 1,753 deletions.
155 changes: 6 additions & 149 deletions poetry.lock

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ def pytest_configure(config: pytest.Config) -> None:
"github_repository": "owner/repo",
"github_run_id": "123456",
"github_event_path": "event_path",
"plugin_test_output": "test_output",
"plugin_test_result": False,
"github_apps": [],
}


@pytest.fixture(scope="session", autouse=True)
def load_plugin(nonebug_init: None) -> set["Plugin"]:
nonebot.get_driver().register_adapter(Adapter)
return nonebot.load_plugins(str(Path(__file__).parent.parent / "src" / "plugins"))
return nonebot.load_plugins(
str(Path(__file__).parent.parent.parent / "src" / "plugins")
)


@pytest.fixture()
Expand Down Expand Up @@ -88,6 +88,7 @@ async def app(app: App, tmp_path: Path, mocker: MockerFixture):
"name": "name",
"desc": "desc",
"author": "author",
"author_id": 1,
"homepage": "https://v2.nonebot.dev",
"tags": [],
"is_official": False,
Expand Down Expand Up @@ -203,4 +204,8 @@ def mocked_api(respx_mock: MockRouter):
},
],
)
respx_mock.get(
"https://api.github.com/user/1",
name="project_link_wordcloud",
).respond(json={"login": "he0119"})
return respx_mock
11 changes: 11 additions & 0 deletions tests/github/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest
from pytest_mock import MockerFixture


@pytest.fixture()
def mock_installation(mocker: MockerFixture):
mock_installation = mocker.MagicMock()
mock_installation.id = 123
mock_installation_resp = mocker.MagicMock()
mock_installation_resp.parsed_data = mock_installation
return mock_installation_resp
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from pathlib import Path
from typing import cast

from nonebot import get_adapter
from nonebot.adapters.github import Adapter, GitHubBot, PullRequestReviewSubmitted
from nonebot.adapters.github.config import GitHubApp
from nonebot.adapters.github import Adapter, PullRequestReviewSubmitted
from nonebug import App
from pytest_mock import MockerFixture

from tests.github.utils import get_github_bot

async def test_auto_merge(app: App, mocker: MockerFixture) -> None:

async def test_auto_merge(app: App, mocker: MockerFixture, mock_installation) -> None:
"""测试审查后自动合并
可直接合并的情况
Expand All @@ -17,38 +16,27 @@ async def test_auto_merge(app: App, mocker: MockerFixture) -> None:

mock_subprocess_run = mocker.patch("subprocess.run")

mock_installation = mocker.MagicMock()
mock_installation.id = 123
mock_installation_resp = mocker.MagicMock()
mock_installation_resp.parsed_data = mock_installation

mock_pull = mocker.MagicMock()
mock_pull.mergeable = True
mock_pull_resp = mocker.MagicMock()
mock_pull_resp.parsed_data = mock_pull

async with app.test_matcher(auto_merge_matcher) as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(
base=GitHubBot,
adapter=adapter,
self_id=GitHubApp(app_id="1", private_key="1"), # type: ignore
)
bot = cast(GitHubBot, bot)
adapter, bot = get_github_bot(ctx)
event_path = (
Path(__file__).parent.parent
Path(__file__).parent.parent.parent
/ "events"
/ "pull_request_review_submitted.json"
)
event = Adapter.payload_to_event(
event = adapter.payload_to_event(
"1", "pull_request_review", event_path.read_bytes()
)
assert isinstance(event, PullRequestReviewSubmitted)

ctx.should_call_api(
"rest.apps.async_get_repo_installation",
{"owner": "he0119", "repo": "action-test"},
mock_installation_resp,
mock_installation,
)
ctx.should_call_api(
"rest.pulls.async_get",
Expand Down Expand Up @@ -86,41 +74,31 @@ async def test_auto_merge(app: App, mocker: MockerFixture) -> None:
)


async def test_auto_merge_need_rebase(app: App, mocker: MockerFixture) -> None:
async def test_auto_merge_need_rebase(
app: App, mocker: MockerFixture, mock_installation
) -> None:
"""测试审查后自动合并
需要 rebase 的情况
"""
from src.plugins.github.plugins.publish import auto_merge_matcher
from src.plugins.github.models import GithubHandler, RepoInfo
from nonebot.adapters.github import Bot

mock_subprocess_run = mocker.patch("subprocess.run")
mock_resolve_conflict_pull_requests = mocker.patch(
"src.plugins.github.plugins.publish.resolve_conflict_pull_requests"
)

mock_installation = mocker.MagicMock()
mock_installation.id = 123
mock_installation_resp = mocker.MagicMock()
mock_installation_resp.parsed_data = mock_installation

mock_pull = mocker.MagicMock()
mock_pull.mergeable = False
mock_pull.head.ref = "publish/issue1"
mock_pull_resp = mocker.MagicMock()
mock_pull_resp.parsed_data = mock_pull

async with app.test_matcher(auto_merge_matcher) as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(
base=GitHubBot,
adapter=adapter,
self_id=GitHubApp(app_id="1", private_key="1"), # type: ignore
)
bot = cast(GitHubBot, bot)
adapter, bot = get_github_bot(ctx)
event_path = (
Path(__file__).parent.parent
Path(__file__).parent.parent.parent
/ "events"
/ "pull_request_review_submitted.json"
)
Expand All @@ -132,7 +110,7 @@ async def test_auto_merge_need_rebase(app: App, mocker: MockerFixture) -> None:
ctx.should_call_api(
"rest.apps.async_get_repo_installation",
{"owner": "he0119", "repo": "action-test"},
mock_installation_resp,
mock_installation,
)
ctx.should_call_api(
"rest.pulls.async_get",
Expand Down Expand Up @@ -186,15 +164,9 @@ async def test_auto_merge_not_publish(app: App, mocker: MockerFixture) -> None:
)

async with app.test_matcher(auto_merge_matcher) as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(
base=GitHubBot,
adapter=adapter,
self_id=GitHubApp(app_id="1", private_key="1"), # type: ignore
)
bot = cast(GitHubBot, bot)
adapter, bot = get_github_bot(ctx)
event_path = (
Path(__file__).parent.parent
Path(__file__).parent.parent.parent
/ "events"
/ "pull_request_review_submitted.json"
)
Expand Down Expand Up @@ -224,15 +196,9 @@ async def test_auto_merge_not_member(app: App, mocker: MockerFixture) -> None:
)

async with app.test_matcher(auto_merge_matcher) as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(
base=GitHubBot,
adapter=adapter,
self_id=GitHubApp(app_id="1", private_key="1"), # type: ignore
)
bot = cast(GitHubBot, bot)
adapter, bot = get_github_bot(ctx)
event_path = (
Path(__file__).parent.parent
Path(__file__).parent.parent.parent
/ "events"
/ "pull_request_review_submitted.json"
)
Expand Down Expand Up @@ -262,15 +228,9 @@ async def test_auto_merge_not_approve(app: App, mocker: MockerFixture) -> None:
)

async with app.test_matcher(auto_merge_matcher) as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(
base=GitHubBot,
adapter=adapter,
self_id=GitHubApp(app_id="1", private_key="1"), # type: ignore
)
bot = cast(GitHubBot, bot)
adapter, bot = get_github_bot(ctx)
event_path = (
Path(__file__).parent.parent
Path(__file__).parent.parent.parent
/ "events"
/ "pull_request_review_submitted.json"
)
Expand Down
Loading

0 comments on commit b01332e

Please sign in to comment.