@@ -2,9 +2,11 @@ package controller_test
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
5
6
"net/http"
6
7
"net/http/httptest"
7
8
"os"
9
+ "strconv"
8
10
"testing"
9
11
10
12
"github.com/gin-gonic/gin"
@@ -16,6 +18,7 @@ import (
16
18
)
17
19
18
20
func TestMain (m * testing.M ) {
21
+ gin .SetMode (gin .TestMode )
19
22
// Setup the MySQL test database
20
23
models_test .SetupTestDB (config .AppConfig )
21
24
code := m .Run ()
@@ -31,8 +34,11 @@ type Response struct {
31
34
TotalPages int `json:"totalPages"`
32
35
}
33
36
37
+ type ShowResponse struct {
38
+ Blog model.Blog `json:"blog"`
39
+ }
40
+
34
41
func TestBlogIndex (t * testing.T ) {
35
- gin .SetMode (gin .TestMode )
36
42
req := httptest .NewRequest (http .MethodGet , "/blogs?format=json" , nil )
37
43
w := httptest .NewRecorder ()
38
44
ctx , _ := gin .CreateTestContext (w )
@@ -64,12 +70,11 @@ func TestBlogIndex(t *testing.T) {
64
70
}
65
71
66
72
func TestBlogIndexWithEmptyTable (t * testing.T ) {
67
- gin .SetMode (gin .TestMode )
68
73
req := httptest .NewRequest (http .MethodGet , "/blogs?format=json" , nil )
69
74
w := httptest .NewRecorder ()
70
75
ctx , _ := gin .CreateTestContext (w )
71
76
ctx .Request = req
72
- models_test .TRdb .Db .Exec ("DELETE * FROM blogs;" )
77
+ models_test .TRdb .Db .Exec ("DELETE FROM blogs;" )
73
78
74
79
store := blog .NewBlogStore (models_test .TRdb )
75
80
hdr := blog .NewHandler (store )
@@ -87,3 +92,79 @@ func TestBlogIndexWithEmptyTable(t *testing.T) {
87
92
// Check the length of the blogs array
88
93
assert .Len (t , result .Blogs , 0 )
89
94
}
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