-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
121 lines (109 loc) · 3.91 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
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/JuanCampbsi/api-go-gin/controllers"
"github.com/JuanCampbsi/api-go-gin/database"
"github.com/JuanCampbsi/api-go-gin/models"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
var ID int
func SetupRoutsTest() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
routes := gin.Default()
return routes
}
func CreatedStudentMock() {
student := models.Student{Name: "Name Test", CPF: "12345678911", RG: "123456789"}
database.DB.Save(&student)
ID = int(student.ID)
}
func DeleteStudenMock() {
var student models.Student
database.DB.Delete(&student, ID)
}
func TestVerifySearchStudentsAll(t *testing.T) {
database.ConnectDataBase()
CreatedStudentMock()
defer DeleteStudenMock()
r := SetupRoutsTest()
r.GET("/students", controllers.ViewStudents)
req, _ := http.NewRequest("GET", "/students", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, req)
assert.Equal(t, http.StatusOK, response.Code, "They should be the same")
}
func TestVerifyStatusCodeSearchStudentsWithParams(t *testing.T) {
database.ConnectDataBase()
CreatedStudentMock()
defer DeleteStudenMock()
r := SetupRoutsTest()
r.GET("/students/:id", controllers.SearchStudentsId)
req, _ := http.NewRequest("GET", "/students/1", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, req)
assert.Equal(t, http.StatusOK, response.Code, "They should be the same")
}
func TestVerifyStatusCodeStudentsWithParamsCPFHandler(t *testing.T) {
database.ConnectDataBase()
CreatedStudentMock()
defer DeleteStudenMock()
r := SetupRoutsTest()
r.GET("/students/cpf/:cpf", controllers.SearchStudentsCPF)
req, _ := http.NewRequest("GET", "/students/cpf/00000000000", nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, req)
assert.Equal(t, http.StatusOK, response.Code, "They should be the same")
}
func TestSearchStudentIDHandler(t *testing.T) {
database.ConnectDataBase()
CreatedStudentMock()
defer DeleteStudenMock()
r := SetupRoutsTest()
r.GET("/students/:id", controllers.SearchStudentsId)
pathSearch := "/students/" + strconv.Itoa(ID)
req, _ := http.NewRequest("GET", pathSearch, nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, req)
var studentMock models.Student
json.Unmarshal(response.Body.Bytes(), &studentMock)
assert.Equal(t, "Name Test", studentMock.Name, "Names: they should be the same")
assert.Equal(t, "12345678911", studentMock.CPF, "CPF: they should be the same")
assert.Equal(t, "123456789", studentMock.RG, "RG: they should be the same")
assert.Equal(t, http.StatusOK, response.Code, "Status code: they should be the same")
}
func TestEditStudentHandler(t *testing.T) {
database.ConnectDataBase()
CreatedStudentMock()
defer DeleteStudenMock()
r := SetupRoutsTest()
r.PATCH("/students/:id", controllers.EditStudents)
student := models.Student{Name: "Edit Test", CPF: "42345678911", RG: "423456789"}
valueJson, _ := json.Marshal(student)
pathEdit := "/students/" + strconv.Itoa(ID)
req, _ := http.NewRequest("PATCH", pathEdit, bytes.NewBuffer(valueJson))
response := httptest.NewRecorder()
r.ServeHTTP(response, req)
var studentMock models.Student
json.Unmarshal(response.Body.Bytes(), &studentMock)
assert.Equal(t, "Edit Test", studentMock.Name, "Name: they should be the same")
assert.Equal(t, "42345678911", studentMock.CPF, "CPF: they should be the same")
assert.Equal(t, "423456789", studentMock.RG, "RG: they should be the same")
assert.Equal(t, http.StatusOK, response.Code, "Status Code: they should be the same")
}
func TestDeletStudentHandler(t *testing.T) {
database.ConnectDataBase()
CreatedStudentMock()
r := SetupRoutsTest()
r.DELETE("/students/:id", controllers.DeleteStudents)
pathSearch := "/students/" + strconv.Itoa(ID)
req, _ := http.NewRequest("DELETE", pathSearch, nil)
response := httptest.NewRecorder()
r.ServeHTTP(response, req)
assert.Equal(t, http.StatusOK, response.Code)
}