|
4 | 4 | package issues_test |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "fmt" |
7 | 8 | "testing" |
| 9 | + "time" |
8 | 10 |
|
9 | 11 | "code.gitea.io/gitea/models/db" |
10 | 12 | issues_model "code.gitea.io/gitea/models/issues" |
11 | 13 | repo_model "code.gitea.io/gitea/models/repo" |
12 | 14 | "code.gitea.io/gitea/models/unittest" |
13 | 15 | user_model "code.gitea.io/gitea/models/user" |
14 | 16 | "code.gitea.io/gitea/modules/setting" |
| 17 | + "code.gitea.io/gitea/tests" |
15 | 18 |
|
16 | 19 | "github.com/stretchr/testify/assert" |
17 | 20 | ) |
@@ -156,6 +159,100 @@ func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) { |
156 | 159 | } |
157 | 160 | } |
158 | 161 |
|
| 162 | +func TestGetUnmergedPullRequestsByHeadInfoMax(t *testing.T) { |
| 163 | + defer tests.AddFixtures("models/fixtures/TestGetUnmergedPullRequestsByHeadInfoMax/")() |
| 164 | + assert.NoError(t, unittest.PrepareTestDatabase()) |
| 165 | + |
| 166 | + repoID := int64(1) |
| 167 | + olderThan := int64(0) |
| 168 | + |
| 169 | + // for NULL created field the olderThan condition is ignored |
| 170 | + prs, err := issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, olderThan, "branch2") |
| 171 | + assert.NoError(t, err) |
| 172 | + assert.Equal(t, int64(1), prs[0].HeadRepoID) |
| 173 | + |
| 174 | + // test for when the created field is set |
| 175 | + branch := "branchmax" |
| 176 | + prs, err = issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, olderThan, branch) |
| 177 | + assert.NoError(t, err) |
| 178 | + assert.Len(t, prs, 0) |
| 179 | + olderThan = time.Now().UnixNano() |
| 180 | + assert.NoError(t, err) |
| 181 | + prs, err = issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, olderThan, branch) |
| 182 | + assert.NoError(t, err) |
| 183 | + assert.Len(t, prs, 1) |
| 184 | + for _, pr := range prs { |
| 185 | + assert.Equal(t, int64(1), pr.HeadRepoID) |
| 186 | + assert.Equal(t, branch, pr.HeadBranch) |
| 187 | + } |
| 188 | + pr := prs[0] |
| 189 | + |
| 190 | + for _, testCase := range []struct { |
| 191 | + table string |
| 192 | + field string |
| 193 | + id int64 |
| 194 | + match any |
| 195 | + nomatch any |
| 196 | + }{ |
| 197 | + { |
| 198 | + table: "issue", |
| 199 | + field: "is_closed", |
| 200 | + id: pr.IssueID, |
| 201 | + match: false, |
| 202 | + nomatch: true, |
| 203 | + }, |
| 204 | + { |
| 205 | + table: "pull_request", |
| 206 | + field: "flow", |
| 207 | + id: pr.ID, |
| 208 | + match: issues_model.PullRequestFlowGithub, |
| 209 | + nomatch: issues_model.PullRequestFlowAGit, |
| 210 | + }, |
| 211 | + { |
| 212 | + table: "pull_request", |
| 213 | + field: "head_repo_id", |
| 214 | + id: pr.ID, |
| 215 | + match: pr.HeadRepoID, |
| 216 | + nomatch: 0, |
| 217 | + }, |
| 218 | + { |
| 219 | + table: "pull_request", |
| 220 | + field: "head_branch", |
| 221 | + id: pr.ID, |
| 222 | + match: pr.HeadBranch, |
| 223 | + nomatch: "something else", |
| 224 | + }, |
| 225 | + { |
| 226 | + table: "pull_request", |
| 227 | + field: "has_merged", |
| 228 | + id: pr.ID, |
| 229 | + match: false, |
| 230 | + nomatch: true, |
| 231 | + }, |
| 232 | + } { |
| 233 | + t.Run(testCase.field, func(t *testing.T) { |
| 234 | + update := fmt.Sprintf("UPDATE `%s` SET `%s` = ? WHERE `id` = ?", testCase.table, testCase.field) |
| 235 | + |
| 236 | + // expect no match |
| 237 | + _, err = db.GetEngine(db.DefaultContext).Exec(update, testCase.nomatch, testCase.id) |
| 238 | + assert.NoError(t, err) |
| 239 | + prs, err = issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, olderThan, branch) |
| 240 | + assert.NoError(t, err) |
| 241 | + assert.Len(t, prs, 0) |
| 242 | + |
| 243 | + // expect one match |
| 244 | + _, err = db.GetEngine(db.DefaultContext).Exec(update, testCase.match, testCase.id) |
| 245 | + assert.NoError(t, err) |
| 246 | + prs, err = issues_model.GetUnmergedPullRequestsByHeadInfoMax(db.DefaultContext, repoID, olderThan, branch) |
| 247 | + assert.NoError(t, err) |
| 248 | + assert.Len(t, prs, 1) |
| 249 | + |
| 250 | + // identical to the known PR |
| 251 | + assert.Equal(t, pr.ID, prs[0].ID) |
| 252 | + }) |
| 253 | + } |
| 254 | +} |
| 255 | + |
159 | 256 | func TestGetUnmergedPullRequestsByBaseInfo(t *testing.T) { |
160 | 257 | assert.NoError(t, unittest.PrepareTestDatabase()) |
161 | 258 | prs, err := issues_model.GetUnmergedPullRequestsByBaseInfo(db.DefaultContext, 1, "master") |
|
0 commit comments