pytorch/.github/scripts/test_label_utils.py
Ning Xu 2c76838d7f Issue-88098: extract utils from check labels (#94597)
Fixes #88098

This is a mirror of the same PR (https://github.com/Goldspear/pytorch/pull/2) that has been reviewed in my fork (due to it's a stacked PR).

======================
## Context

This is the 2nd of the 3 PRs to address issue-88098.

## What Changed
1. Extract comment related utils from trymerge.py to github_utils.py
2. Extract label related utils from trymerge.py and check_labels.py to label_utils.py

## Tests
* pytorch-dummy repo [trymerge run ](https://github.com/Goldspear/pytorch-dummy/actions/runs/4118944174)merged the test PR [OK](https://github.com/Goldspear/pytorch-dummy/pull/2).

## Note to Reviewers
Due to higher degree of complexity involved to extract GitHubPR class, it's worth having a separate issue to handle that part of refactoring. This issue only focusing on refactoring where necessary to ship the functional diff.

* 1st PR: https://github.com/pytorch/pytorch/pull/94179
* 2nd PR: this one
* 3rd PR: https://github.com/Goldspear/pytorch/pull/3

Pull Request resolved: https://github.com/pytorch/pytorch/pull/94597
Approved by: https://github.com/ZainRizvi
2023-02-12 12:18:53 +00:00

76 lines
3.1 KiB
Python

from typing import Any
from unittest import TestCase, mock, main
from label_utils import (
get_last_page_num_from_header,
gh_get_labels,
has_required_labels,
)
from trymerge import GitHubPR
from test_trymerge import mocked_gh_graphql
release_notes_labels = [
"release notes: nn",
]
class TestLabelUtils(TestCase):
MOCK_HEADER_LINKS_TO_PAGE_NUMS = {
1: {"link": "<https://api.github.com/dummy/labels?per_page=10&page=1>; rel='last'"},
2: {"link": "<https://api.github.com/dummy/labels?per_page=1&page=2>;"},
3: {"link": "<https://api.github.com/dummy/labels?per_page=1&page=2&page=3>;"},
}
def test_get_last_page_num_from_header(self) -> None:
for expected_page_num, mock_header in self.MOCK_HEADER_LINKS_TO_PAGE_NUMS.items():
self.assertEqual(get_last_page_num_from_header(mock_header), expected_page_num)
MOCK_LABEL_INFO = '[{"name": "foo"}]'
@mock.patch("label_utils.get_last_page_num_from_header", return_value=3)
@mock.patch("label_utils.request_for_labels", return_value=(None, MOCK_LABEL_INFO))
def test_gh_get_labels(
self,
mock_request_for_labels: Any,
mock_get_last_page_num_from_header: Any,
) -> None:
res = gh_get_labels("mock_org", "mock_repo")
mock_get_last_page_num_from_header.assert_called_once()
self.assertEqual(res, ["foo"] * 3)
@mock.patch("label_utils.get_last_page_num_from_header", return_value=0)
@mock.patch("label_utils.request_for_labels", return_value=(None, MOCK_LABEL_INFO))
def test_gh_get_labels_raises_with_no_pages(
self,
mock_request_for_labels: Any,
get_last_page_num_from_header: Any,
) -> None:
with self.assertRaises(AssertionError) as err:
gh_get_labels("foo", "bar")
self.assertIn("number of pages of labels", str(err.exception))
@mock.patch('trymerge.gh_graphql', side_effect=mocked_gh_graphql)
@mock.patch('label_utils.get_release_notes_labels', return_value=release_notes_labels)
def test_pr_with_missing_labels(self, mocked_rn_labels: Any, mocked_gql: Any) -> None:
"Test PR with no 'release notes:' label or 'topic: not user facing' label"
pr = GitHubPR("pytorch", "pytorch", 82169)
self.assertFalse(has_required_labels(pr))
@mock.patch('trymerge.gh_graphql', side_effect=mocked_gh_graphql)
@mock.patch('label_utils.get_release_notes_labels', return_value=release_notes_labels)
def test_pr_with_release_notes_label(self, mocked_rn_labels: Any, mocked_gql: Any) -> None:
"Test PR with 'release notes: nn' label"
pr = GitHubPR("pytorch", "pytorch", 71759)
self.assertTrue(has_required_labels(pr))
@mock.patch('trymerge.gh_graphql', side_effect=mocked_gh_graphql)
@mock.patch('label_utils.get_release_notes_labels', return_value=release_notes_labels)
def test_pr_with_not_user_facing_label(self, mocked_rn_labels: Any, mocked_gql: Any) -> None:
"Test PR with 'topic: not user facing' label"
pr = GitHubPR("pytorch", "pytorch", 75095)
self.assertTrue(has_required_labels(pr))
if __name__ == "__main__":
main()