Refactor integration test DecodeJSON calls to use generic return value (#37432)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: wxiaoguang <2114189+wxiaoguang@users.noreply.github.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -35,8 +35,7 @@ func TestAPIModifyLabels(t *testing.T) {
|
||||
Description: "test label",
|
||||
}).AddTokenAuth(token)
|
||||
resp := MakeRequest(t, req, http.StatusCreated)
|
||||
apiLabel := new(api.Label)
|
||||
DecodeJSON(t, resp, &apiLabel)
|
||||
apiLabel := DecodeJSON(t, resp, &api.Label{})
|
||||
dbLabel := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: apiLabel.ID, RepoID: repo.ID})
|
||||
assert.Equal(t, dbLabel.Name, apiLabel.Name)
|
||||
assert.Equal(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
|
||||
@@ -57,8 +56,7 @@ func TestAPIModifyLabels(t *testing.T) {
|
||||
req = NewRequest(t, "GET", urlStr).
|
||||
AddTokenAuth(token)
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
var apiLabels []*api.Label
|
||||
DecodeJSON(t, resp, &apiLabels)
|
||||
apiLabels := DecodeJSON(t, resp, []*api.Label{})
|
||||
assert.Len(t, apiLabels, 2)
|
||||
|
||||
// GetLabel
|
||||
@@ -66,7 +64,7 @@ func TestAPIModifyLabels(t *testing.T) {
|
||||
req = NewRequest(t, "GET", singleURLStr).
|
||||
AddTokenAuth(token)
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &apiLabel)
|
||||
apiLabel = DecodeJSON(t, resp, &api.Label{})
|
||||
assert.Equal(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
|
||||
|
||||
// EditLabel
|
||||
@@ -78,7 +76,7 @@ func TestAPIModifyLabels(t *testing.T) {
|
||||
Color: &newColor,
|
||||
}).AddTokenAuth(token)
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &apiLabel)
|
||||
apiLabel = DecodeJSON(t, resp, &api.Label{})
|
||||
assert.Equal(t, newColor, apiLabel.Color)
|
||||
req = NewRequestWithJSON(t, "PATCH", singleURLStr, &api.EditLabelOption{
|
||||
Color: &newColorWrong,
|
||||
@@ -107,8 +105,7 @@ func TestAPIAddIssueLabels(t *testing.T) {
|
||||
Labels: []any{1, 2},
|
||||
}).AddTokenAuth(token)
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
var apiLabels []*api.Label
|
||||
DecodeJSON(t, resp, &apiLabels)
|
||||
apiLabels := DecodeJSON(t, resp, []*api.Label{})
|
||||
assert.Len(t, apiLabels, unittest.GetCount(t, &issues_model.IssueLabel{IssueID: issue.ID}))
|
||||
|
||||
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: 2})
|
||||
@@ -132,8 +129,7 @@ func TestAPIAddIssueLabelsWithLabelNames(t *testing.T) {
|
||||
Labels: []any{repoLabel.Name, orgLabel.Name},
|
||||
}).AddTokenAuth(token)
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
var apiLabels []*api.Label
|
||||
DecodeJSON(t, resp, &apiLabels)
|
||||
apiLabels := DecodeJSON(t, resp, []*api.Label{})
|
||||
assert.Len(t, apiLabels, unittest.GetCount(t, &issues_model.IssueLabel{IssueID: issue.ID}))
|
||||
var apiLabelNames []string
|
||||
for _, label := range apiLabels {
|
||||
@@ -162,8 +158,7 @@ func TestAPIReplaceIssueLabels(t *testing.T) {
|
||||
Labels: []any{label.ID},
|
||||
}).AddTokenAuth(token)
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
var apiLabels []*api.Label
|
||||
DecodeJSON(t, resp, &apiLabels)
|
||||
apiLabels := DecodeJSON(t, resp, []*api.Label{})
|
||||
if assert.Len(t, apiLabels, 1) {
|
||||
assert.Equal(t, label.ID, apiLabels[0].ID)
|
||||
}
|
||||
@@ -188,8 +183,7 @@ func TestAPIReplaceIssueLabelsWithLabelNames(t *testing.T) {
|
||||
Labels: []any{label.Name},
|
||||
}).AddTokenAuth(token)
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
var apiLabels []*api.Label
|
||||
DecodeJSON(t, resp, &apiLabels)
|
||||
apiLabels := DecodeJSON(t, resp, []*api.Label{})
|
||||
if assert.Len(t, apiLabels, 1) {
|
||||
assert.Equal(t, label.Name, apiLabels[0].Name)
|
||||
}
|
||||
@@ -212,8 +206,7 @@ func TestAPIModifyOrgLabels(t *testing.T) {
|
||||
Description: "test label",
|
||||
}).AddTokenAuth(token)
|
||||
resp := MakeRequest(t, req, http.StatusCreated)
|
||||
apiLabel := new(api.Label)
|
||||
DecodeJSON(t, resp, &apiLabel)
|
||||
apiLabel := DecodeJSON(t, resp, &api.Label{})
|
||||
dbLabel := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: apiLabel.ID, OrgID: owner.ID})
|
||||
assert.Equal(t, dbLabel.Name, apiLabel.Name)
|
||||
assert.Equal(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
|
||||
@@ -234,8 +227,7 @@ func TestAPIModifyOrgLabels(t *testing.T) {
|
||||
req = NewRequest(t, "GET", urlStr).
|
||||
AddTokenAuth(token)
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
var apiLabels []*api.Label
|
||||
DecodeJSON(t, resp, &apiLabels)
|
||||
apiLabels := DecodeJSON(t, resp, []*api.Label{})
|
||||
assert.Len(t, apiLabels, 4)
|
||||
|
||||
// GetLabel
|
||||
@@ -243,7 +235,7 @@ func TestAPIModifyOrgLabels(t *testing.T) {
|
||||
req = NewRequest(t, "GET", singleURLStr).
|
||||
AddTokenAuth(token)
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &apiLabel)
|
||||
apiLabel = DecodeJSON(t, resp, &api.Label{})
|
||||
assert.Equal(t, strings.TrimLeft(dbLabel.Color, "#"), apiLabel.Color)
|
||||
|
||||
// EditLabel
|
||||
@@ -255,7 +247,7 @@ func TestAPIModifyOrgLabels(t *testing.T) {
|
||||
Color: &newColor,
|
||||
}).AddTokenAuth(token)
|
||||
resp = MakeRequest(t, req, http.StatusOK)
|
||||
DecodeJSON(t, resp, &apiLabel)
|
||||
apiLabel = DecodeJSON(t, resp, &api.Label{})
|
||||
assert.Equal(t, newColor, apiLabel.Color)
|
||||
req = NewRequestWithJSON(t, "PATCH", singleURLStr, &api.EditLabelOption{
|
||||
Color: &newColorWrong,
|
||||
|
||||
Reference in New Issue
Block a user