-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
164 lines (109 loc) · 3.51 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/gin-gonic/gin"
"github.com/jadson-medeiros/gin-rest/controllers"
"github.com/jadson-medeiros/gin-rest/database"
"github.com/jadson-medeiros/gin-rest/models"
"github.com/stretchr/testify/assert"
)
var ID int
func SetupTestRoutes() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
routes := gin.Default()
return routes
}
func CreateStudentMock() {
student := models.Student{Name: "Test", CPG: "12345678901"}
database.DB.Create(&student)
ID = int(student.ID)
}
func DeleteStudentMock() {
var student models.Student
database.DB.Delete(&student, ID)
}
func TestCheckStatusCodeWelcomeWithParams(t *testing.T) {
r := SetupTestRoutes()
r.GET("/:name", controllers.Welcome)
req, _ := http.NewRequest("GET", "/test", nil)
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code, "should be equals")
mock := `{"API:":"Hi test, welcome :D"}`
resBody, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, mock, string(resBody))
}
func TestGetAllStudentesHandler(t *testing.T) {
database.ConnectionDB()
CreateStudentMock()
defer DeleteStudentMock()
r := SetupTestRoutes()
r.GET("/students", controllers.ShowAllStudents)
req, _ := http.NewRequest("GET", "/students", nil)
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
}
func TestSearchStudentByCPGHandler(t *testing.T) {
database.ConnectionDB()
CreateStudentMock()
defer DeleteStudentMock()
r := SetupTestRoutes()
r.GET("/students/cpg/:cpg", controllers.SearchStudentByCPG)
req, _ := http.NewRequest("GET", "/students/cpg/12345678901", nil)
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
}
func TestSearchStudentByIDHandler(t *testing.T) {
database.ConnectionDB()
CreateStudentMock()
defer DeleteStudentMock()
r := SetupTestRoutes()
r.GET("/students/:id", controllers.SearchStudentByID)
path := "/students/" + strconv.Itoa(ID)
req, _ := http.NewRequest("GET", path, nil)
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
var studentMock models.Student
json.Unmarshal(res.Body.Bytes(), &studentMock)
assert.Equal(t, "Test", studentMock.Name, "The names should match")
assert.Equal(t, "12345678901", studentMock.CPG)
assert.Equal(t, http.StatusOK, res.Code)
}
func TestDeleteStudentHandler(t *testing.T) {
database.ConnectionDB()
CreateStudentMock()
r := SetupTestRoutes()
r.DELETE("/students/:id", controllers.DeleteStudent)
path := "/students/" + strconv.Itoa(ID)
req, _ := http.NewRequest("DELETE", path, nil)
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
}
func TestUpdateStudentHandler(t *testing.T) {
database.ConnectionDB()
CreateStudentMock()
defer DeleteStudentMock()
r := SetupTestRoutes()
r.PATCH("/students/:id", controllers.EditStudent)
student := models.Student{Name: "Test2", CPG: "46123456789"}
studentJson, _ := json.Marshal(student)
path := "/students/" + strconv.Itoa(ID)
req, _ := http.NewRequest("PATCH", path, bytes.NewReader(studentJson))
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
var studentUpdated models.Student
json.Unmarshal(res.Body.Bytes(), &studentUpdated)
assert.Equal(t, http.StatusOK, res.Code)
assert.Equal(t, "Test2", studentUpdated.Name, "The names should match")
assert.Equal(t, "46123456789", studentUpdated.CPG, "The CPG should match")
}