1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
# SPDX-FileCopyrightText: 2024-present Hinrich Mahler <chango@mahlerhome.de>
#
# SPDX-License-Identifier: MIT
import pytest
from chango.action import ChanGoActionData, LinkedIssue
from chango.concrete.sections import GitHubSectionChangeNote, PullRequest, Section
class DummyChangNote(
GitHubSectionChangeNote.with_sections([Section(uid="req", title="Req", is_required=True)])
):
OWNER = "my-username"
REPOSITORY = "my-repo"
class DummyChangNoteNoOwner(
GitHubSectionChangeNote.with_sections([Section(uid="req", title="Req", is_required=True)])
):
REPOSITORY = "my-repo"
class DummyChangNoteNoRepository(
GitHubSectionChangeNote.with_sections([Section(uid="req", title="Req", is_required=True)])
):
OWNER = "my-username"
class FromGitHubEvent(
GitHubSectionChangeNote.with_sections(
[
Section(uid="opt_0", title="Opt", is_required=False, sort_order=0),
Section(uid="req_1", title="Req", is_required=True, sort_order=1),
Section(uid="req_0", title="Req", is_required=True, sort_order=0),
]
)
):
pass
class TestGitHubSectionChangeNote:
"""Since TestSectionChangeNote is an abstract base class, we are testing with
GitHubTestSectionChangeNote as a simple implementation.
Note that we do *not* test abstract methods, as that is the responsibility of the concrete
implementations.
"""
def test_class_variables(self):
assert DummyChangNote.OWNER == "my-username"
assert DummyChangNote.REPOSITORY == "my-repo"
def test_get_pull_request_url(self):
assert (
DummyChangNote.get_pull_request_url("123")
== "https://github.com/my-username/my-repo/pull/123"
)
def test_get_pull_request_url_invalid(self):
with pytest.raises(ValueError, match="OWNER must be set as class variable."):
DummyChangNoteNoOwner.get_pull_request_url("123")
with pytest.raises(ValueError, match="REPOSITORY must be set as class variable."):
DummyChangNoteNoRepository.get_pull_request_url("123")
def test_get_thread_url(self):
assert (
DummyChangNote.get_thread_url("123")
== "https://github.com/my-username/my-repo/issues/123"
)
def test_get_thread_url_invalid(self):
with pytest.raises(ValueError, match="OWNER must be set as class variable."):
DummyChangNoteNoOwner.get_thread_url("123")
with pytest.raises(ValueError, match="REPOSITORY must be set as class variable."):
DummyChangNoteNoRepository.get_thread_url("123")
def test_get_author_url(self):
assert DummyChangNote.get_author_url("123") == "https://github.com/123"
def test_get_sections_has_required(self):
assert FromGitHubEvent.get_sections(None, None) == {"req_0", "req_1"}
def test_get_sections_no_required(self):
NoRequired = GitHubSectionChangeNote.with_sections(
[
Section(uid="opt_0", title="Opt", is_required=False, sort_order=5),
Section(uid="opt_1", title="Opt", is_required=False, sort_order=42),
Section(uid="opt_2", title="Opt", is_required=False, sort_order=-3),
]
)
assert NoRequired.get_sections(None, None) == {"opt_2"}
def test_build_from_github_event_missing_data(self):
with pytest.raises(ValueError, match="required data"):
FromGitHubEvent.build_from_github_event({})
def test_build_from_github_event_basic(self):
event_data = {
"pull_request": {
"html_url": "https://example.com/pull/42",
"number": 42,
"title": "example title",
"user": {"login": "author_uid"},
"labels": [{"name": "label1"}, {"name": "label2"}],
}
}
change_note = FromGitHubEvent.build_from_github_event(event_data)
assert change_note.req_0 == "example title"
assert change_note.pull_requests == (
PullRequest(uid="42", author_uids=("author_uid",), closes_threads=()),
)
assert change_note.slug == "0042"
def test_build_from_github_event_custom_get_sections(self):
class CustomGetSections(FromGitHubEvent):
@classmethod
def get_sections(cls, event_data, sections): # noqa: ARG003
return {"opt_0", "req_1", "req_0"}
event_data = {
"pull_request": {
"html_url": "https://example.com/pull/42",
"number": 42,
"title": "example title",
"user": {"login": "author_uid"},
"labels": [{"name": "label1"}, {"name": "label2"}],
}
}
change_note = CustomGetSections.build_from_github_event(event_data)
assert change_note.req_0 == "example title"
assert change_note.req_1 == "example title"
assert change_note.opt_0 == "example title"
assert change_note.pull_requests == (
PullRequest(uid="42", author_uids=("author_uid",), closes_threads=()),
)
assert change_note.slug == "0042"
def test_build_from_github_event_chango_action_data_no_linked_issues(self, monkeypatch):
received_data = {}
def get_sections(labels, issue_types):
received_data["labels"] = labels
received_data["issue_types"] = issue_types
return {"req_0", "req_1"}
monkeypatch.setattr(FromGitHubEvent, "get_sections", get_sections)
event_data = {
"pull_request": {
"html_url": "https://example.com/pull/42",
"number": 42,
"title": "example title",
"user": {"login": "author_uid"},
"labels": [],
}
}
data = ChanGoActionData(linked_issues=None, parent_pull_request=None)
FromGitHubEvent.build_from_github_event(event_data, data)
assert received_data["labels"] == set()
assert received_data["issue_types"] == set()
def test_build_from_github_event_chango_action_data(self, monkeypatch):
received_data = {}
def get_sections(labels, issue_types):
received_data["labels"] = labels
received_data["issue_types"] = issue_types
return {"req_0", "req_1"}
monkeypatch.setattr(FromGitHubEvent, "get_sections", get_sections)
event_data = {
"pull_request": {
"html_url": "https://example.com/pull/42",
"number": 42,
"title": "example title",
"user": {"login": "author_uid"},
"labels": [{"name": "pr_label1"}, {"name": "pr_label2"}],
}
}
data = ChanGoActionData(
linked_issues=(
LinkedIssue(number=1, title="issue_title", labels=None, issue_type="issue_type"),
LinkedIssue(
number=2,
title="issue_title",
labels=("issue_label1", "issue_label2"),
issue_type=None,
),
),
parent_pull_request=None,
)
FromGitHubEvent.build_from_github_event(event_data, data)
assert received_data["labels"] == {
"pr_label1",
"pr_label2",
"issue_label1",
"issue_label2",
}
assert received_data["issue_types"] == {"issue_type"}
|