File tree Expand file tree Collapse file tree 5 files changed +143
-2
lines changed Expand file tree Collapse file tree 5 files changed +143
-2
lines changed Original file line number Diff line number Diff line change 1
1
package model
2
2
3
+ import "fmt"
4
+
3
5
type Comment struct {
4
6
GormModel
5
7
ItemID int `gorm:"type:int;not null" json:"item_id"`
@@ -18,6 +20,15 @@ type CreateCommentPayload struct {
18
20
}
19
21
20
22
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
+ }
21
32
c := & Comment {
22
33
ItemID : p .ItemID ,
23
34
UserID : p .UserID ,
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
package router
2
2
3
3
import (
4
+ "fmt"
4
5
"strconv"
5
6
6
7
"github.com/labstack/echo/v4"
7
8
"github.com/traPtitech/booQ-v3/model"
8
9
)
9
10
10
11
type PostCommentBody struct {
11
- Comment string `json:"comment "`
12
+ Text string `json:"text "`
12
13
}
13
14
14
15
type PostCommentResponse struct {
@@ -29,11 +30,14 @@ func PostComment(c echo.Context) error {
29
30
if err := c .Bind (& body ); err != nil {
30
31
return invalidRequest (c , err )
31
32
}
33
+ if body .Text == "" {
34
+ return invalidRequest (c , fmt .Errorf ("text is empty" ))
35
+ }
32
36
33
37
payload := model.CreateCommentPayload {
34
38
ItemID : itemID ,
35
39
UserID : me ,
36
- Comment : body .Comment ,
40
+ Comment : body .Text ,
37
41
}
38
42
comment , err := model .CreateComment (& payload )
39
43
if err != nil {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments