Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 优化测试 #317

Open
he0119 opened this issue Dec 6, 2024 · 7 comments
Open

test: 优化测试 #317

he0119 opened this issue Dec 6, 2024 · 7 comments

Comments

@he0119
Copy link
Member

he0119 commented Dec 6, 2024

优化 mock 体验

提供统一且方便组合的 mock 体验,目前需要涉及到以下内容:

  • Event
  • Issue
  • Body

优化 snapshot 体验

  • 精简 ErrorDict,让其对人类友好
@he0119 he0119 changed the title Test: 优化测试 test: 优化测试 Dec 6, 2024
@BigOrangeQWQ
Copy link
Member

BigOrangeQWQ commented Dec 6, 2024

关于call_api我们是否需要也人类格式化一下,即储存一个api call列表,很多时候data不重要,而是call的api名字,用snapshot生成

@BigOrangeQWQ
Copy link
Member

可能json或者dict也可以通过节选key来精简化,而非snapshot梭哈

@BigOrangeQWQ
Copy link
Member

测试的修改无非是人类可视化和编写测试方便化两个方向,我们可以两头下功夫,讨论讨论

@he0119
Copy link
Member Author

he0119 commented Dec 6, 2024

关于call_api我们是否需要也人类格式化一下,即储存一个api call列表,很多时候data不重要,而是call的api名字,用snapshot生成

支持,重要是 api 名字和顺序。

@he0119
Copy link
Member Author

he0119 commented Dec 6, 2024

可能json或者dict也可以通过节选key来精简化,而非snapshot梭哈

我觉得还是需要 snapshot 完整内容来兜底,避免缺一些我们暂时没用到的东西。免得后面用到的时候发现原来在某一次修改中改错了。

精简和节选也可以一起做,类似于重点关注。

@BigOrangeQWQ
Copy link
Member

可能json或者dict也可以通过节选key来精简化,而非snapshot梭哈

我觉得还是需要 snapshot 完整内容来兜底,避免缺一些我们暂时没用到的东西。免得后面用到的时候发现原来在某一次修改中改错了。

精简和节选也可以一起做,类似于重点关注。

我觉得可以
完整的数据记录和增加人类可读性并不冲突
使增加人类可读的摘要快照更突出即可

@he0119
Copy link
Member Author

he0119 commented Dec 8, 2024

在之前的 PR 中尝试了两个简化方法,看起来还不错。

def should_call_apis(
ctx: MatcherContext, apis: list[GitHubApi], data: list[Any]
) -> None:
for n, api in enumerate(apis):
ctx.should_call_api(**api, data=data[n])
def assert_subprocess_run_calls(mock: MockType, commands: list[list[str]]):
calls = []
for command in commands:
command_str = " ".join(command)
calls.append(call(command, check=True, capture_output=True))
# 暂时不考虑报错的情况,仅涉及到 stdout
calls.append(call().stdout.decode())
calls.append(_Call(("().stdout.decode().__str__", (), {})))
if command_str.startswith("git diff"):
calls.append(_Call(("().stdout.__bool__", (), {})))
mock.assert_has_calls(calls)

should_call_apis

should_call_apis(
ctx,
[
GitHubApi(
api="rest.apps.async_get_repo_installation",
result=mock_installation,
),
GitHubApi(
api="rest.issues.async_get",
result=mock_issues_resp,
),
GitHubApi(
api="rest.issues.async_update",
result=True,
),
GitHubApi(
api="rest.pulls.async_list",
result=mock_pulls_resp,
),
GitHubApi(
api="rest.issues.async_get",
result=mock_publish_issue_resp,
),
GitHubApi(
api="rest.issues.async_get",
result=mock_remove_issue_resp,
),
],
snapshot(
{
0: {"owner": "he0119", "repo": "action-test"},
1: {"owner": "he0119", "repo": "action-test", "issue_number": 76},
2: {
"owner": "he0119",
"repo": "action-test",
"issue_number": 76,
"state": "closed",
"state_reason": "completed",
},
3: {"owner": "he0119", "repo": "action-test", "state": "open"},
4: {"owner": "he0119", "repo": "action-test", "issue_number": 100},
5: {"owner": "he0119", "repo": "action-test", "issue_number": 101},
}
),
)

assert_subprocess_run_calls

assert_subprocess_run_calls(
mock_subprocess_run,
[
["git", "config", "--global", "safe.directory", "*"],
["git", "push", "origin", "--delete", "publish/issue76"],
# 处理发布
["git", "checkout", "master"],
["git", "switch", "-C", "publish/issue100"],
["git", "config", "--global", "user.name", "test"],
[
"git",
"config",
"--global",
"user.email",
"[email protected]",
],
["git", "add", "-A"],
["git", "commit", "-m", ":beers: publish bot name (#100)"],
["git", "fetch", "origin"],
["git", "diff", "origin/publish/issue100", "publish/issue100"],
["git", "push", "origin", "publish/issue100", "-f"],
# 处理移除
["git", "checkout", "master"],
["git", "switch", "-C", "remove/issue101"],
["git", "config", "--global", "user.name", "test"],
[
"git",
"config",
"--global",
"user.email",
"[email protected]",
],
["git", "add", "-A"],
["git", "commit", "-m", ":hammer: remove name (#101)"],
["git", "fetch", "origin"],
["git", "diff", "origin/remove/issue101", "remove/issue101"],
["git", "push", "origin", "remove/issue101", "-f"],
],
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants