Skip to content

Commit

Permalink
テストを書いた
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-20 committed Dec 19, 2024
1 parent 5c23dfc commit 694d661
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 2 deletions.
11 changes: 11 additions & 0 deletions model/comments.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package model

import "fmt"

type Comment struct {
GormModel
ItemID int `gorm:"type:int;not null" json:"item_id"`
Expand All @@ -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,
Expand Down
64 changes: 64 additions & 0 deletions model/comments_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
8 changes: 6 additions & 2 deletions router/comments.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package router

import (
"fmt"
"strconv"

"github.com/labstack/echo/v4"
"github.com/traPtitech/booQ-v3/model"
)

type PostCommentBody struct {
Comment string `json:"comment"`
Text string `json:"text"`
}

type PostCommentResponse struct {
Expand All @@ -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 {
Expand Down
46 changes: 46 additions & 0 deletions router/comments_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
16 changes: 16 additions & 0 deletions router/test_common.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 694d661

Please sign in to comment.