From 694d661371502cd75866471b54f4b214f8713af5 Mon Sep 17 00:00:00 2001 From: cp-20 Date: Thu, 19 Dec 2024 16:57:33 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E6=9B=B8?= =?UTF-8?q?=E3=81=84=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/comments.go | 11 +++++++ model/comments_test.go | 64 +++++++++++++++++++++++++++++++++++++++++ router/comments.go | 8 ++++-- router/comments_test.go | 46 +++++++++++++++++++++++++++++ router/test_common.go | 16 +++++++++++ 5 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 model/comments_test.go create mode 100644 router/comments_test.go create mode 100644 router/test_common.go diff --git a/model/comments.go b/model/comments.go index 9afc4aa..e170d13 100644 --- a/model/comments.go +++ b/model/comments.go @@ -1,5 +1,7 @@ package model +import "fmt" + type Comment struct { GormModel ItemID int `gorm:"type:int;not null" json:"item_id"` @@ -18,6 +20,15 @@ type CreateCommentPayload struct { } func CreateComment(p *CreateCommentPayload) (*Comment, error) { + if p.ItemID == 0 { + return nil, fmt.Errorf("ItemID is required") + } + if p.UserID == "" { + return nil, fmt.Errorf("UserID is required") + } + if p.Comment == "" { + return nil, fmt.Errorf("Comment is required") + } c := &Comment{ ItemID: p.ItemID, UserID: p.UserID, diff --git a/model/comments_test.go b/model/comments_test.go new file mode 100644 index 0000000..fd01202 --- /dev/null +++ b/model/comments_test.go @@ -0,0 +1,64 @@ +package model + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCreateComment(t *testing.T) { + cases := []struct { + name string + payload *CreateCommentPayload + fail bool + }{ + { + name: "正常系", + payload: &CreateCommentPayload{ + ItemID: 1, + UserID: "user1", + Comment: "comment1", + }, + fail: false, + }, + { + name: "異常系: ItemIDが存在しない", + payload: &CreateCommentPayload{ + UserID: "user1", + Comment: "comment1", + }, + fail: true, + }, + { + name: "異常系: UserIDが存在しない", + payload: &CreateCommentPayload{ + ItemID: 1, + Comment: "comment1", + }, + fail: true, + }, + { + name: "異常系: Commentが存在しない", + payload: &CreateCommentPayload{ + ItemID: 1, + UserID: "user1", + }, + fail: true, + }, + } + + assert := assert.New(t) + for _, tt := range cases { + t.Run(tt.name, func(t *testing.T) { + c, err := CreateComment(tt.payload) + if tt.fail { + assert.Error(err) + } else { + assert.NoError(err) + assert.Equal(tt.payload.ItemID, c.ItemID) + assert.Equal(tt.payload.UserID, c.UserID) + assert.Equal(tt.payload.Comment, c.Comment) + } + }) + } +} diff --git a/router/comments.go b/router/comments.go index 66f338e..43fd7b0 100644 --- a/router/comments.go +++ b/router/comments.go @@ -1,6 +1,7 @@ package router import ( + "fmt" "strconv" "github.com/labstack/echo/v4" @@ -8,7 +9,7 @@ import ( ) type PostCommentBody struct { - Comment string `json:"comment"` + Text string `json:"text"` } type PostCommentResponse struct { @@ -29,11 +30,14 @@ func PostComment(c echo.Context) error { if err := c.Bind(&body); err != nil { return invalidRequest(c, err) } + if body.Text == "" { + return invalidRequest(c, fmt.Errorf("text is empty")) + } payload := model.CreateCommentPayload{ ItemID: itemID, UserID: me, - Comment: body.Comment, + Comment: body.Text, } comment, err := model.CreateComment(&payload) if err != nil { diff --git a/router/comments_test.go b/router/comments_test.go new file mode 100644 index 0000000..d358c33 --- /dev/null +++ b/router/comments_test.go @@ -0,0 +1,46 @@ +package router + +import ( + "testing" + + "github.com/labstack/echo/v4" + "github.com/stretchr/testify/assert" + "github.com/traPtitech/booQ-v3/model" +) + +func TestPostComment(t *testing.T) { + model.PrepareTestDatabase() + + e := echo.New() + SetupRouting(e, CreateUserProvider("s9")) + + cases := []struct { + name string + payload string + expected int + }{ + { + name: "正常系", + payload: `{"text":"テストコメント"}`, + expected: 201, + }, + { + name: "異常系: 空文字列", + payload: `{"text":""}`, + expected: 400, + }, + { + name: "異常系: パラメータ不足", + payload: `{}`, + expected: 400, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert := assert.New(t) + rec := PerformMutation(e, "POST", "/api/items/1/comments", tc.payload) + assert.Equal(tc.expected, rec.Code) + }) + } +} diff --git a/router/test_common.go b/router/test_common.go new file mode 100644 index 0000000..8152b1d --- /dev/null +++ b/router/test_common.go @@ -0,0 +1,16 @@ +package router + +import ( + "net/http/httptest" + "strings" + + "github.com/labstack/echo/v4" +) + +func PerformMutation(e *echo.Echo, method, path, payload string) *httptest.ResponseRecorder { + req := httptest.NewRequest(method, path, strings.NewReader(payload)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + e.ServeHTTP(rec, req) + return rec +}