From 904ea3e52bdd8279fc226eacc44627565c9a4bd3 Mon Sep 17 00:00:00 2001 From: H1rono Date: Tue, 31 Dec 2024 10:36:51 +0900 Subject: [PATCH 1/4] :recycle: Use `json.Marshal` in tests --- router/transaction_test.go | 39 ++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/router/transaction_test.go b/router/transaction_test.go index eafff5b9..5f7dac19 100644 --- a/router/transaction_test.go +++ b/router/transaction_test.go @@ -6,7 +6,6 @@ import ( "fmt" "net/http" "net/http/httptest" - "strings" "testing" "time" @@ -543,14 +542,24 @@ func TestHandlers_PostTransaction(t *testing.T) { group := tx1.Group.ID e := echo.New() - // FIXME: json.Marshalを使う - reqBody := fmt.Sprintf( - `{"title": "%s", "amount": %d, "targets": ["%s"], "tags": ["%s"], "group": "%s"}`, - tx1.Title, tx1.Amount, tx1.Target, tag.ID, group) + reqBody, err := json.Marshal(&struct { + Title string `json:"title"` + Amount int `json:"amount"` + Targets []string `json:"targets"` + Tags []uuid.UUID `json:"tags"` + Group *uuid.UUID `json:"group"` + }{ + Title: tx1.Title, + Amount: tx1.Amount, + Targets: []string{tx1.Target}, + Tags: []uuid.UUID{tag.ID}, + Group: &group, + }) + require.NoError(t, err) req, err := http.NewRequest( http.MethodPost, "/api/transactions", - strings.NewReader(reqBody)) + bytes.NewReader(reqBody)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() @@ -788,6 +797,7 @@ func TestHandlers_PutTransaction(t *testing.T) { updated := &model.TransactionResponse{ ID: tx.ID, + Title: random.AlphaNumeric(t, 20), Amount: random.Numeric(t, 1000000), Target: random.AlphaNumeric(t, 20), Tags: []*model.Tag{ @@ -810,13 +820,22 @@ func TestHandlers_PutTransaction(t *testing.T) { }) e := echo.New() - reqBody := fmt.Sprintf( - `{"amount": %d, "target": "%s", "tags": ["%s"]}`, - updated.Amount, updated.Target, updatedTag.ID) + reqBody, err := json.Marshal(&struct { + Title string `json:"title"` + Amount int `json:"amount"` + Target string `json:"target"` + Tags []uuid.UUID `json:"tags"` + }{ + Title: updated.Title, + Amount: updated.Amount, + Target: updated.Target, + Tags: []uuid.UUID{updatedTag.ID}, + }) + require.NoError(t, err) req, err := http.NewRequest( http.MethodPut, fmt.Sprintf("/api/transactions/%s", tx.ID), - strings.NewReader(reqBody)) + bytes.NewReader(reqBody)) require.NoError(t, err) req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) rec := httptest.NewRecorder() From 7682abbb2640c0a0d35e45f39895ba82d1dbfcc4 Mon Sep 17 00:00:00 2001 From: H1rono Date: Tue, 31 Dec 2024 10:37:31 +0900 Subject: [PATCH 2/4] :pencil: Fix comment --- router/request_test.go | 62 +----------------------------------------- 1 file changed, 1 insertion(+), 61 deletions(-) diff --git a/router/request_test.go b/router/request_test.go index 2aad393e..0de35a90 100644 --- a/router/request_test.go +++ b/router/request_test.go @@ -3655,64 +3655,4 @@ func TestHandlers_PutStatus(t *testing.T) { }) } -// func TestHandlers_PostComment(t *testing.T) { -// t.Parallel() - -// t.Run("Success", func(t *testing.T) { -// t.Parallel() -// ctrl := gomock.NewController(t) - -// date := time.Now() - -// comment := &model.Comment{ -// ID: uuid.New(), -// User: uuid.New(), -// Comment: random.AlphaNumeric(t, 20), -// CreatedAt: date, -// UpdatedAt: date, -// } - -// requestID := uuid.New() -// reqComment := Comment{ -// Comment: comment.Comment, -// } -// reqBody, err := json.Marshal(reqComment) -// require.NoError(t, err) - -// e := echo.New() -// req, err := http.NewRequest( -// http.MethodPost, -// fmt.Sprintf("/api/requests/%s/comments", requestID), -// bytes.NewReader(reqBody)) -// assert.NoError(t, err) -// req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) -// rec := httptest.NewRecorder() -// c := e.NewContext(req, rec) -// c.SetPath("api/requests/:requestID/comments") -// c.SetParamNames("requestID") -// c.SetParamValues(requestID.String()) - -// h, err := NewTestHandlers(t, ctrl) -// assert.NoError(t, err) - -// h.Repository.MockCommentRepository. -// EXPECT(). -// CreateComment(c.Request().Context(), comment.Comment, requestID, comment.User). -// Return(comment, nil) - -// res := &CommentDetail{ -// ID: comment.ID, -// User: comment.User, -// Comment: comment.Comment, -// CreatedAt: comment.CreatedAt, -// UpdatedAt: comment.UpdatedAt, -// } -// resBody, err := json.Marshal(res) -// require.NoError(t, err) - -// if assert.NoError(t, h.Handlers.PostComment(c)) { -// assert.Equal(t, http.StatusOK, rec.Code) -// assert.Equal(t, string(resBody), strings.TrimRight(rec.Body.String(), "\n")) -// } -// }) -// } +// TODO: TestHandlers_PostComment From b74aaec7060ab5db2c433f36f85a60e843457e9e Mon Sep 17 00:00:00 2001 From: H1rono Date: Tue, 31 Dec 2024 10:41:28 +0900 Subject: [PATCH 3/4] :recycle: Ensure `assert.Equal` only in HTTP status code --- router/admin_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/router/admin_test.go b/router/admin_test.go index 95b53553..0ca7a7ce 100644 --- a/router/admin_test.go +++ b/router/admin_test.go @@ -14,6 +14,7 @@ import ( "github.com/stretchr/testify/require" "github.com/traPtitech/Jomon/ent" "github.com/traPtitech/Jomon/model" + "github.com/traPtitech/Jomon/testutil" "go.uber.org/mock/gomock" ) @@ -49,11 +50,11 @@ func TestHandler_GetAdmins(t *testing.T) { require.NoError(t, err) assert.NoError(t, h.Handlers.GetAdmins(c)) - assert.Equal(t, http.StatusOK, rec.Code) + testutil.AssertEqual(t, http.StatusOK, rec.Code) var res []uuid.UUID err = json.Unmarshal(rec.Body.Bytes(), &res) require.NoError(t, err) - assert.Equal(t, []uuid.UUID{admin.ID}, res) + testutil.RequireEqual(t, []uuid.UUID{admin.ID}, res) }) t.Run("Success2", func(t *testing.T) { @@ -78,11 +79,11 @@ func TestHandler_GetAdmins(t *testing.T) { require.NoError(t, err) assert.NoError(t, h.Handlers.GetAdmins(c)) - assert.Equal(t, http.StatusOK, rec.Code) + testutil.AssertEqual(t, http.StatusOK, rec.Code) var res []uuid.UUID err = json.Unmarshal(rec.Body.Bytes(), &res) require.NoError(t, err) - assert.Equal(t, []uuid.UUID{}, res) + assert.Empty(t, res) }) t.Run("FailedWithError", func(t *testing.T) { From 0ed1a52ab22d0b2c4fe77d8408ed76f4426609b8 Mon Sep 17 00:00:00 2001 From: H1rono Date: Tue, 31 Dec 2024 10:42:22 +0900 Subject: [PATCH 4/4] :recycle: `Equal` with 0 into `Empty` --- model/group_impl_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model/group_impl_test.go b/model/group_impl_test.go index c45247e8..4969748f 100644 --- a/model/group_impl_test.go +++ b/model/group_impl_test.go @@ -42,7 +42,7 @@ func TestEntRepository_GetGroups(t *testing.T) { t.Parallel() groups, err := repo2.GetGroups(ctx) require.NoError(t, err) - assert.Equal(t, 0, len(groups)) + assert.Empty(t, groups) }) } @@ -317,7 +317,7 @@ func TestEntRepository_GetMembers(t *testing.T) { got, err := repo2.GetMembers(ctx, group.ID) assert.NoError(t, err) - assert.Equal(t, got, []*Member{}) + assert.Empty(t, got) }) }