-
Notifications
You must be signed in to change notification settings - Fork 168
/
main_test.go
121 lines (109 loc) · 3.72 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"
"io/ioutil"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/gin-gonic/gin"
"github.com/guilhermeonrails/api-go-gin/controllers"
"github.com/guilhermeonrails/api-go-gin/database"
"github.com/guilhermeonrails/api-go-gin/models"
"github.com/stretchr/testify/assert"
)
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.Saudacoes)
req, _ := http.NewRequest("GET", "/gui", nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
assert.Equal(t, http.StatusOK, resposta.Code, "Deveriam ser iguais")
mockDaResposta := `{"API diz":"E ai gui, Tudo beleza?"}`
respostaBody, _ := ioutil.ReadAll(resposta.Body)
assert.Equal(t, mockDaResposta, string(respostaBody))
}
func TestListaTodosOsAlunosHanlder(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetupDasRotasDeTeste()
r.GET("/alunos", controllers.TodosAlunos)
req, _ := http.NewRequest("GET", "/alunos", nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
assert.Equal(t, http.StatusOK, resposta.Code)
}
func TestBucaAlunoPorCPFHandler(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)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
assert.Equal(t, http.StatusOK, resposta.Code)
}
func TestBuscaAlunoPorIDHandler(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetupDasRotasDeTeste()
r.GET("/alunos/:id", controllers.BuscarAlunoPorID)
pathDaBusca := "/alunos/" + strconv.Itoa(ID)
req, _ := http.NewRequest("GET", pathDaBusca, nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
var alunoMock models.Aluno
json.Unmarshal(resposta.Body.Bytes(), &alunoMock)
assert.Equal(t, "Nome do Aluno Teste", alunoMock.Nome, "Os nomes devem ser iguais")
assert.Equal(t, "12345678901", alunoMock.CPF)
assert.Equal(t, "123456789", alunoMock.RG)
assert.Equal(t, http.StatusOK, resposta.Code)
}
func TestDeletaAlunoHandler(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
r := SetupDasRotasDeTeste()
r.DELETE("/alunos/:id", controllers.DeletarAluno)
pathDeBusca := "/alunos/" + strconv.Itoa(ID)
req, _ := http.NewRequest("DELETE", pathDeBusca, nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
assert.Equal(t, http.StatusOK, resposta.Code)
}
func TestEditaUmAlunoHandler(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetupDasRotasDeTeste()
r.PATCH("/alunos/:id", controllers.EditarAluno)
aluno := models.Aluno{Nome: "Nome do Aluno Teste", CPF: "47123456789", RG: "123456700"}
valorJson, _ := json.Marshal(aluno)
pathParaEditar := "/alunos/" + strconv.Itoa(ID)
req, _ := http.NewRequest("PATCH", pathParaEditar, bytes.NewBuffer(valorJson))
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
var alunoMockAtualizado models.Aluno
json.Unmarshal(resposta.Body.Bytes(), &alunoMockAtualizado)
assert.Equal(t, "47123456789", alunoMockAtualizado.CPF)
assert.Equal(t, "123456700", alunoMockAtualizado.RG)
assert.Equal(t, "Nome do Aluno Teste", alunoMockAtualizado.Nome)
}