File tree Expand file tree Collapse file tree 5 files changed +26
-22
lines changed Expand file tree Collapse file tree 5 files changed +26
-22
lines changed Original file line number Diff line number Diff line change 1
1
package model
2
2
3
- import "fmt "
3
+ import "errors "
4
4
5
5
type Comment struct {
6
6
GormModel
@@ -21,21 +21,21 @@ type CreateCommentPayload struct {
21
21
22
22
func CreateComment (p * CreateCommentPayload ) (* Comment , error ) {
23
23
if p .ItemID == 0 {
24
- return nil , fmt . Errorf ("ItemID is required" )
24
+ return nil , errors . New ("ItemID is required" )
25
25
}
26
26
if p .UserID == "" {
27
- return nil , fmt . Errorf ("UserID is required" )
27
+ return nil , errors . New ("UserID is required" )
28
28
}
29
29
if p .Comment == "" {
30
- return nil , fmt . Errorf ("Comment is required" )
30
+ return nil , errors . New ("Comment is required" )
31
31
}
32
- c := & Comment {
32
+ c := Comment {
33
33
ItemID : p .ItemID ,
34
34
UserID : p .UserID ,
35
35
Comment : p .Comment ,
36
36
}
37
- if err := db .Create (c ).Error ; err != nil {
37
+ if err := db .Create (& c ).Error ; err != nil {
38
38
return nil , err
39
39
}
40
- return c , nil
40
+ return & c , nil
41
41
}
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ func TestCreateComment(t *testing.T) {
12
12
cases := []struct {
13
13
name string
14
14
payload * CreateCommentPayload
15
- fail bool
15
+ ok bool
16
16
}{
17
17
{
18
18
name : "正常系" ,
@@ -21,39 +21,39 @@ func TestCreateComment(t *testing.T) {
21
21
UserID : "user1" ,
22
22
Comment : "comment1" ,
23
23
},
24
- fail : false ,
24
+ ok : true ,
25
25
},
26
26
{
27
27
name : "異常系: ItemIDが存在しない" ,
28
28
payload : & CreateCommentPayload {
29
29
UserID : "user1" ,
30
30
Comment : "comment1" ,
31
31
},
32
- fail : true ,
32
+ ok : false ,
33
33
},
34
34
{
35
35
name : "異常系: UserIDが存在しない" ,
36
36
payload : & CreateCommentPayload {
37
37
ItemID : 1 ,
38
38
Comment : "comment1" ,
39
39
},
40
- fail : true ,
40
+ ok : false ,
41
41
},
42
42
{
43
43
name : "異常系: Commentが存在しない" ,
44
44
payload : & CreateCommentPayload {
45
45
ItemID : 1 ,
46
46
UserID : "user1" ,
47
47
},
48
- fail : true ,
48
+ ok : false ,
49
49
},
50
50
}
51
51
52
52
assert := assert .New (t )
53
53
for _ , tt := range cases {
54
54
t .Run (tt .name , func (t * testing.T ) {
55
55
_ , err := CreateComment (tt .payload )
56
- if tt .fail {
56
+ if tt .ok {
57
57
assert .Error (err )
58
58
} else {
59
59
assert .NoError (err )
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package router
2
2
3
3
import (
4
4
"fmt"
5
+ "net/http"
5
6
"strconv"
6
7
7
8
"github.com/labstack/echo/v4"
@@ -18,8 +19,8 @@ type PostCommentResponse struct {
18
19
19
20
// PostComment POST /items/:id/comments
20
21
func PostComment (c echo.Context ) error {
21
- itemIDRaw := c .Param ("id" )
22
- itemID , err := strconv .Atoi (itemIDRaw )
22
+ itemIDStr := c .Param ("id" )
23
+ itemID , err := strconv .Atoi (itemIDStr )
23
24
if err != nil {
24
25
return invalidRequest (c , err )
25
26
}
@@ -44,5 +45,5 @@ func PostComment(c echo.Context) error {
44
45
return internalServerError (c , err )
45
46
}
46
47
47
- return c .JSON (201 , PostCommentResponse {ID : comment .ID })
48
+ return c .JSON (http . StatusCreated , PostCommentResponse {ID : comment .ID })
48
49
}
Original file line number Diff line number Diff line change 1
1
package router
2
2
3
3
import (
4
+ "net/http"
4
5
"testing"
5
6
6
7
"github.com/labstack/echo/v4"
@@ -12,7 +13,7 @@ func TestPostComment(t *testing.T) {
12
13
model .PrepareTestDatabase ()
13
14
14
15
e := echo .New ()
15
- SetupRouting (e , CreateUserProvider ("s9" ))
16
+ SetupRouting (e , CreateUserProvider (TEST_USER ))
16
17
17
18
cases := []struct {
18
19
name string
@@ -22,24 +23,24 @@ func TestPostComment(t *testing.T) {
22
23
{
23
24
name : "正常系" ,
24
25
payload : `{"text":"テストコメント"}` ,
25
- expected : 201 ,
26
+ expected : http . StatusCreated ,
26
27
},
27
28
{
28
29
name : "異常系: 空文字列" ,
29
30
payload : `{"text":""}` ,
30
- expected : 400 ,
31
+ expected : http . StatusBadRequest ,
31
32
},
32
33
{
33
34
name : "異常系: パラメータ不足" ,
34
35
payload : `{}` ,
35
- expected : 400 ,
36
+ expected : http . StatusBadRequest ,
36
37
},
37
38
}
38
39
39
40
for _ , tc := range cases {
40
41
t .Run (tc .name , func (t * testing.T ) {
41
42
assert := assert .New (t )
42
- rec := PerformMutation (e , "POST" , "/api/items/1/comments" , tc .payload )
43
+ rec := performMutation (e , "POST" , "/api/items/1/comments" , tc .payload )
43
44
assert .Equal (tc .expected , rec .Code )
44
45
})
45
46
}
Original file line number Diff line number Diff line change 7
7
"github.com/labstack/echo/v4"
8
8
)
9
9
10
- func PerformMutation (e * echo.Echo , method , path , payload string ) * httptest.ResponseRecorder {
10
+ var TEST_USER = "s9"
11
+
12
+ func performMutation (e * echo.Echo , method , path , payload string ) * httptest.ResponseRecorder {
11
13
req := httptest .NewRequest (method , path , strings .NewReader (payload ))
12
14
req .Header .Set ("Content-Type" , "application/json" )
13
15
rec := httptest .NewRecorder ()
You can’t perform that action at this time.
0 commit comments