Skip to content

Commit

Permalink
test: add test for Post in Controller
Browse files Browse the repository at this point in the history
Signed-off-by: daz-3ux <[email protected]>
  • Loading branch information
Daz-3ux committed Oct 11, 2023
1 parent a2a3297 commit c09c5c3
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
83 changes: 83 additions & 0 deletions internal/dazBlog/controller/v1/post/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package post

import (
"bytes"
"encoding/json"
"github.com/Daz-3ux/dBlog/internal/dazBlog/biz"
"github.com/Daz-3ux/dBlog/internal/dazBlog/biz/post"
v1 "github.com/Daz-3ux/dBlog/pkg/api/dazBlog/v1"
"github.com/gin-gonic/gin"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)

type bodyLogWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}

func (w bodyLogWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}

func TestPostController_Create(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

want := &v1.CreatePostResponse{PostID: "post-daz1234"}

mockPostBiz := post.NewMockPostBiz(ctrl)
mockPostBiz.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any()).Return(want, nil).Times(1)

mockBiz := biz.NewMockIBiz(ctrl)
mockBiz.EXPECT().Posts().Return(mockPostBiz).AnyTimes()

c, _ := gin.CreateTestContext(httptest.NewRecorder())
body := bytes.NewBufferString(`{"title":"dBlog guide","content":"content"}`)
c.Request, _ = http.NewRequest("POST", "/v1/posts", body)
c.Request.Header.Set("Content-Type", "application/json")

blw := &bodyLogWriter{
body: bytes.NewBufferString(""),
ResponseWriter: c.Writer,
}
c.Writer = blw

type fields struct {
b biz.IBiz
}
type args struct {
c *gin.Context
}
tests := []struct {
name string
fields fields
args args
want *v1.CreatePostResponse
}{
{
name: "default",
fields: fields{b: mockBiz},
args: args{
c: c,
},
want: want,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := &PostController{
b: tt.fields.b,
}
ctrl.Create(tt.args.c)
var resp v1.CreatePostResponse
err := json.Unmarshal(blw.body.Bytes(), &resp)
assert.Nil(t, err)
assert.Equal(t, resp.PostID, want.PostID)
})
}
}
36 changes: 36 additions & 0 deletions internal/dazBlog/controller/v1/post/post_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package post

import (
"github.com/Daz-3ux/dBlog/internal/dazBlog/biz"
"github.com/Daz-3ux/dBlog/internal/dazBlog/store"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"testing"
)

func TestNew(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockStore := store.NewMockIStore(ctrl)

type args struct {
ds store.IStore
}
tests := []struct {
name string
args args
want *PostController
}{
{
name: "default",
args: args{mockStore},
want: &PostController{b: biz.NewBiz(mockStore)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, NewPostController(tt.args.ds))
})
}
}

0 comments on commit c09c5c3

Please sign in to comment.