Skip to content

Commit 694d661

Browse files
committed
テストを書いた
1 parent 5c23dfc commit 694d661

File tree

5 files changed

+143
-2
lines changed

5 files changed

+143
-2
lines changed

model/comments.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package model
22

3+
import "fmt"
4+
35
type Comment struct {
46
GormModel
57
ItemID int `gorm:"type:int;not null" json:"item_id"`
@@ -18,6 +20,15 @@ type CreateCommentPayload struct {
1820
}
1921

2022
func CreateComment(p *CreateCommentPayload) (*Comment, error) {
23+
if p.ItemID == 0 {
24+
return nil, fmt.Errorf("ItemID is required")
25+
}
26+
if p.UserID == "" {
27+
return nil, fmt.Errorf("UserID is required")
28+
}
29+
if p.Comment == "" {
30+
return nil, fmt.Errorf("Comment is required")
31+
}
2132
c := &Comment{
2233
ItemID: p.ItemID,
2334
UserID: p.UserID,

model/comments_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package model
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestCreateComment(t *testing.T) {
10+
cases := []struct {
11+
name string
12+
payload *CreateCommentPayload
13+
fail bool
14+
}{
15+
{
16+
name: "正常系",
17+
payload: &CreateCommentPayload{
18+
ItemID: 1,
19+
UserID: "user1",
20+
Comment: "comment1",
21+
},
22+
fail: false,
23+
},
24+
{
25+
name: "異常系: ItemIDが存在しない",
26+
payload: &CreateCommentPayload{
27+
UserID: "user1",
28+
Comment: "comment1",
29+
},
30+
fail: true,
31+
},
32+
{
33+
name: "異常系: UserIDが存在しない",
34+
payload: &CreateCommentPayload{
35+
ItemID: 1,
36+
Comment: "comment1",
37+
},
38+
fail: true,
39+
},
40+
{
41+
name: "異常系: Commentが存在しない",
42+
payload: &CreateCommentPayload{
43+
ItemID: 1,
44+
UserID: "user1",
45+
},
46+
fail: true,
47+
},
48+
}
49+
50+
assert := assert.New(t)
51+
for _, tt := range cases {
52+
t.Run(tt.name, func(t *testing.T) {
53+
c, err := CreateComment(tt.payload)
54+
if tt.fail {
55+
assert.Error(err)
56+
} else {
57+
assert.NoError(err)
58+
assert.Equal(tt.payload.ItemID, c.ItemID)
59+
assert.Equal(tt.payload.UserID, c.UserID)
60+
assert.Equal(tt.payload.Comment, c.Comment)
61+
}
62+
})
63+
}
64+
}

router/comments.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package router
22

33
import (
4+
"fmt"
45
"strconv"
56

67
"github.com/labstack/echo/v4"
78
"github.com/traPtitech/booQ-v3/model"
89
)
910

1011
type PostCommentBody struct {
11-
Comment string `json:"comment"`
12+
Text string `json:"text"`
1213
}
1314

1415
type PostCommentResponse struct {
@@ -29,11 +30,14 @@ func PostComment(c echo.Context) error {
2930
if err := c.Bind(&body); err != nil {
3031
return invalidRequest(c, err)
3132
}
33+
if body.Text == "" {
34+
return invalidRequest(c, fmt.Errorf("text is empty"))
35+
}
3236

3337
payload := model.CreateCommentPayload{
3438
ItemID: itemID,
3539
UserID: me,
36-
Comment: body.Comment,
40+
Comment: body.Text,
3741
}
3842
comment, err := model.CreateComment(&payload)
3943
if err != nil {

router/comments_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package router
2+
3+
import (
4+
"testing"
5+
6+
"github.com/labstack/echo/v4"
7+
"github.com/stretchr/testify/assert"
8+
"github.com/traPtitech/booQ-v3/model"
9+
)
10+
11+
func TestPostComment(t *testing.T) {
12+
model.PrepareTestDatabase()
13+
14+
e := echo.New()
15+
SetupRouting(e, CreateUserProvider("s9"))
16+
17+
cases := []struct {
18+
name string
19+
payload string
20+
expected int
21+
}{
22+
{
23+
name: "正常系",
24+
payload: `{"text":"テストコメント"}`,
25+
expected: 201,
26+
},
27+
{
28+
name: "異常系: 空文字列",
29+
payload: `{"text":""}`,
30+
expected: 400,
31+
},
32+
{
33+
name: "異常系: パラメータ不足",
34+
payload: `{}`,
35+
expected: 400,
36+
},
37+
}
38+
39+
for _, tc := range cases {
40+
t.Run(tc.name, func(t *testing.T) {
41+
assert := assert.New(t)
42+
rec := PerformMutation(e, "POST", "/api/items/1/comments", tc.payload)
43+
assert.Equal(tc.expected, rec.Code)
44+
})
45+
}
46+
}

router/test_common.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package router
2+
3+
import (
4+
"net/http/httptest"
5+
"strings"
6+
7+
"github.com/labstack/echo/v4"
8+
)
9+
10+
func PerformMutation(e *echo.Echo, method, path, payload string) *httptest.ResponseRecorder {
11+
req := httptest.NewRequest(method, path, strings.NewReader(payload))
12+
req.Header.Set("Content-Type", "application/json")
13+
rec := httptest.NewRecorder()
14+
e.ServeHTTP(rec, req)
15+
return rec
16+
}

0 commit comments

Comments
 (0)