Skip to content

Commit f5ead6e

Browse files
committed
feat: add test case for show route
add test case for show route add test case for show route
1 parent f37ac6a commit f5ead6e

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ run: build
77
@./bin/main
88

99
coverage:
10-
@go test -v -cover ./...
10+
@go test -v -cover ./tests/...
1111

1212
test:
13-
@go test -v ./...
13+
@go test -v ./tests/...

internal/service/blog/route.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func (handler *Handler) RegisterRoute(router *gin.RouterGroup) {
2727
func (handler *Handler) BlogIndex(ctx *gin.Context) {
2828
blogs, err := handler.store.FindAll(ctx)
2929
if err != nil {
30+
if err.Error() == "record not found" {
31+
util.WriteError(ctx.Writer, http.StatusNotFound, err)
32+
return
33+
}
3034
util.WriteError(ctx.Writer, http.StatusInternalServerError, err)
3135
return
3236
}
@@ -60,6 +64,10 @@ func (handler *Handler) BlogShow(ctx *gin.Context) {
6064
}
6165
blog, err := handler.store.Find(ctx, id)
6266
if err != nil {
67+
if err.Error() == "record not found" {
68+
util.WriteError(ctx.Writer, http.StatusNotFound, err)
69+
return
70+
}
6371
util.WriteError(ctx.Writer, http.StatusInternalServerError, err)
6472
return
6573
}

tests/controllers/blogs_controller_test.go

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package controller_test
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"net/http"
67
"net/http/httptest"
78
"os"
9+
"strconv"
810
"testing"
911

1012
"github.com/gin-gonic/gin"
@@ -16,6 +18,7 @@ import (
1618
)
1719

1820
func TestMain(m *testing.M) {
21+
gin.SetMode(gin.TestMode)
1922
// Setup the MySQL test database
2023
models_test.SetupTestDB(config.AppConfig)
2124
code := m.Run()
@@ -31,8 +34,11 @@ type Response struct {
3134
TotalPages int `json:"totalPages"`
3235
}
3336

37+
type ShowResponse struct {
38+
Blog model.Blog `json:"blog"`
39+
}
40+
3441
func TestBlogIndex(t *testing.T) {
35-
gin.SetMode(gin.TestMode)
3642
req := httptest.NewRequest(http.MethodGet, "/blogs?format=json", nil)
3743
w := httptest.NewRecorder()
3844
ctx, _ := gin.CreateTestContext(w)
@@ -64,12 +70,11 @@ func TestBlogIndex(t *testing.T) {
6470
}
6571

6672
func TestBlogIndexWithEmptyTable(t *testing.T) {
67-
gin.SetMode(gin.TestMode)
6873
req := httptest.NewRequest(http.MethodGet, "/blogs?format=json", nil)
6974
w := httptest.NewRecorder()
7075
ctx, _ := gin.CreateTestContext(w)
7176
ctx.Request = req
72-
models_test.TRdb.Db.Exec("DELETE * FROM blogs;")
77+
models_test.TRdb.Db.Exec("DELETE FROM blogs;")
7378

7479
store := blog.NewBlogStore(models_test.TRdb)
7580
hdr := blog.NewHandler(store)
@@ -87,3 +92,79 @@ func TestBlogIndexWithEmptyTable(t *testing.T) {
8792
// Check the length of the blogs array
8893
assert.Len(t, result.Blogs, 0)
8994
}
95+
96+
func TestBlogShow(t *testing.T) {
97+
// gin.SetMode(gin.TestMode)
98+
// Seed the table
99+
blogTest := model.Blog{Title: "Test Blog", Content: "This is a test blog content"}
100+
models_test.TRdb.Db.Create(&blogTest)
101+
102+
store := blog.NewBlogStore(models_test.TRdb)
103+
hdr := blog.NewHandler(store)
104+
t.Run("Valid Blog ID", func(t *testing.T) {
105+
blogID := strconv.FormatUint(uint64(blogTest.ID), 10)
106+
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/blogs/%s?format=json", blogID), nil)
107+
w := httptest.NewRecorder()
108+
ctx, _ := gin.CreateTestContext(w)
109+
ctx.Request = req
110+
ctx.Params = []gin.Param{
111+
{
112+
Key: "id",
113+
Value: blogID,
114+
},
115+
}
116+
117+
hdr.BlogShow(ctx)
118+
119+
res := w.Result()
120+
defer res.Body.Close()
121+
// Check the status code
122+
assert.Equal(t, http.StatusOK, res.StatusCode)
123+
//Read data from
124+
var returnedBlog ShowResponse
125+
err := json.NewDecoder(res.Body).Decode(&returnedBlog)
126+
assert.NoError(t, err)
127+
128+
// // Check the returned blog match
129+
assert.Equal(t, blogTest.Title, returnedBlog.Blog.Title)
130+
assert.Equal(t, blogTest.Content, returnedBlog.Blog.Content)
131+
})
132+
t.Run("Invalid Blog ID", func(t *testing.T) {
133+
blogID := "invalidID"
134+
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/blogs/%s?format=json", blogID), nil)
135+
w := httptest.NewRecorder()
136+
ctx, _ := gin.CreateTestContext(w)
137+
ctx.Request = req
138+
ctx.Params = []gin.Param{
139+
{
140+
Key: "id",
141+
Value: blogID,
142+
},
143+
}
144+
hdr.BlogShow(ctx)
145+
146+
res := w.Result()
147+
defer res.Body.Close()
148+
// Check Response Status
149+
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
150+
})
151+
t.Run("Non-Existed Blog ID", func(t *testing.T) {
152+
blogID := "99999"
153+
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/blogs/%s?format=json", blogID), nil)
154+
w := httptest.NewRecorder()
155+
ctx, _ := gin.CreateTestContext(w)
156+
ctx.Request = req
157+
ctx.Params = []gin.Param{
158+
{
159+
Key: "id",
160+
Value: blogID,
161+
},
162+
}
163+
hdr.BlogShow(ctx)
164+
165+
res := w.Result()
166+
defer res.Body.Close()
167+
// Check Response Status
168+
assert.Equal(t, http.StatusNotFound, res.StatusCode)
169+
})
170+
}

0 commit comments

Comments
 (0)