|
4 | 4 | package issues_test
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "fmt" |
7 | 8 | "testing"
|
8 | 9 |
|
9 | 10 | "code.gitea.io/gitea/models/db"
|
@@ -158,6 +159,91 @@ func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
|
158 | 159 | }
|
159 | 160 | }
|
160 | 161 |
|
| 162 | +func TestGetUnmergedPullRequestsByHeadInfoMax(t *testing.T) { |
| 163 | + assert.NoError(t, unittest.PrepareTestDatabase()) |
| 164 | + |
| 165 | + repoID := int64(1) |
| 166 | + maxPR := int64(0) |
| 167 | + prs, err := issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, maxPR, "branch2") |
| 168 | + assert.NoError(t, err) |
| 169 | + assert.Len(t, prs, 0) |
| 170 | + maxPR, err = issues_model.GetMaxIssueIndexForRepo(db.DefaultContext, repoID) |
| 171 | + assert.NoError(t, err) |
| 172 | + prs, err = issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, maxPR, "branch2") |
| 173 | + assert.NoError(t, err) |
| 174 | + assert.Len(t, prs, 1) |
| 175 | + for _, pr := range prs { |
| 176 | + assert.Equal(t, int64(1), pr.HeadRepoID) |
| 177 | + assert.Equal(t, "branch2", pr.HeadBranch) |
| 178 | + } |
| 179 | + pr := prs[0] |
| 180 | + |
| 181 | + for _, testCase := range []struct { |
| 182 | + table string |
| 183 | + field string |
| 184 | + id int64 |
| 185 | + match any |
| 186 | + nomatch any |
| 187 | + }{ |
| 188 | + { |
| 189 | + table: "issue", |
| 190 | + field: "is_closed", |
| 191 | + id: pr.IssueID, |
| 192 | + match: false, |
| 193 | + nomatch: true, |
| 194 | + }, |
| 195 | + { |
| 196 | + table: "pull_request", |
| 197 | + field: "flow", |
| 198 | + id: pr.ID, |
| 199 | + match: issues_model.PullRequestFlowGithub, |
| 200 | + nomatch: issues_model.PullRequestFlowAGit, |
| 201 | + }, |
| 202 | + { |
| 203 | + table: "pull_request", |
| 204 | + field: "head_repo_id", |
| 205 | + id: pr.ID, |
| 206 | + match: pr.HeadRepoID, |
| 207 | + nomatch: 0, |
| 208 | + }, |
| 209 | + { |
| 210 | + table: "pull_request", |
| 211 | + field: "head_branch", |
| 212 | + id: pr.ID, |
| 213 | + match: pr.HeadBranch, |
| 214 | + nomatch: "something else", |
| 215 | + }, |
| 216 | + { |
| 217 | + table: "pull_request", |
| 218 | + field: "has_merged", |
| 219 | + id: pr.ID, |
| 220 | + match: false, |
| 221 | + nomatch: true, |
| 222 | + }, |
| 223 | + } { |
| 224 | + t.Run(testCase.field, func(t *testing.T) { |
| 225 | + update := fmt.Sprintf("UPDATE `%s` SET `%s` = ? WHERE `id` = ?", testCase.table, testCase.field) |
| 226 | + |
| 227 | + // expect no match |
| 228 | + _, err = db.GetEngine(db.DefaultContext).Exec(update, testCase.nomatch, testCase.id) |
| 229 | + assert.NoError(t, err) |
| 230 | + prs, err = issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, maxPR, "branch2") |
| 231 | + assert.NoError(t, err) |
| 232 | + assert.Len(t, prs, 0) |
| 233 | + |
| 234 | + // expect one match |
| 235 | + _, err = db.GetEngine(db.DefaultContext).Exec(update, testCase.match, testCase.id) |
| 236 | + assert.NoError(t, err) |
| 237 | + prs, err = issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, maxPR, "branch2") |
| 238 | + assert.NoError(t, err) |
| 239 | + assert.Len(t, prs, 1) |
| 240 | + |
| 241 | + // identical to the known PR |
| 242 | + assert.Equal(t, pr.ID, prs[0].ID) |
| 243 | + }) |
| 244 | + } |
| 245 | +} |
| 246 | + |
161 | 247 | func TestGetUnmergedPullRequestsByBaseInfo(t *testing.T) {
|
162 | 248 | assert.NoError(t, unittest.PrepareTestDatabase())
|
163 | 249 | prs, err := issues_model.GetUnmergedPullRequestsByBaseInfo(db.DefaultContext, 1, "master")
|
|
0 commit comments