-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
124 lines (111 loc) · 3.4 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/mthsnts/go-gin-API/controllers"
"github.com/mthsnts/go-gin-API/database"
"github.com/mthsnts/go-gin-API/models"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"strconv"
"testing"
)
var ID int
func SetupDasRotasDeTeste() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
rotas := gin.Default()
return rotas
}
func CriaAlunoMock() {
aluno := models.Aluno{Nome: "Nome do Aluno Teste", CPF: "12345678901", RG: "123456789"}
database.DB.Create(&aluno)
ID = int(aluno.ID)
}
func DeletaAlunoMock() {
var aluno models.Aluno
database.DB.Delete(&aluno, ID)
}
func TestVerificaStatusCodeDaSaudacaoComParametro(t *testing.T) {
r := SetupDasRotasDeTeste()
r.GET("/:nome", controllers.Saudacao)
req, _ := http.NewRequest("GET", "/gui", nil)
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code, "Deveriam ser iguais")
mockDaResposta := `{"API diz:":"E ai gui, tudo beleza ?"}`
repostaBody, _ := ioutil.ReadAll(res.Body)
assert.Equal(t, mockDaResposta, string(repostaBody))
}
func TestListandoTodosOsAlunosHandler(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetupDasRotasDeTeste()
r.GET("/alunos", controllers.ExibeTodosAlunos)
req, _ := http.NewRequest("GET", "/alunos", nil)
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
fmt.Println(res.Body)
}
func TestBuscaPorCPF(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetupDasRotasDeTeste()
r.GET("/alunos/cpf/:cpf", controllers.BuscaAlunoPorCPF)
req, _ := http.NewRequest("GET", "/alunos/cpf/12345678901", nil)
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
fmt.Println(res.Body)
}
func TestBuscaPorID(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetupDasRotasDeTeste()
r.GET("/alunos/:id", controllers.BuscaAlunoPorId)
req, _ := http.NewRequest("GET", "/alunos/5", nil)
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
fmt.Println(res.Body)
}
func TestDeletaAlunoHandler(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
r := SetupDasRotasDeTeste()
r.DELETE("/alunos/delete/:id", controllers.DeletaAluno)
pathDeBusca := "/alunos/delete/" + strconv.Itoa(ID)
req, _ := http.NewRequest("DELETE", pathDeBusca, nil)
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
fmt.Println(res.Body)
}
func TestEditaUmAlunoHandler(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetupDasRotasDeTeste()
r.PATCH("/alunos/edit/:id", controllers.EditaAluno)
aluno := models.Aluno{Nome: "Nome do Aluno Teste", CPF: "12345678912", RG: "123456709"}
valorJson, _ := json.Marshal(aluno)
pathDeEdicao := "/alunos/edit/" + strconv.Itoa(ID)
req, _ := http.NewRequest("PATCH", pathDeEdicao, bytes.NewBuffer(valorJson))
res := httptest.NewRecorder()
r.ServeHTTP(res, req)
var alunoMockAtualizado models.Aluno
err := json.Unmarshal(res.Body.Bytes(), &alunoMockAtualizado)
if err != nil {
return
}
fmt.Println(res.Body)
assert.Equal(t, "12345678912", alunoMockAtualizado.CPF)
assert.Equal(t, "123456709", alunoMockAtualizado.RG)
}