-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
136 lines (110 loc) · 3.64 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/gin-gonic/gin"
"github.com/marianaoliveira-mb/api-go-gin/controllers"
"github.com/marianaoliveira-mb/api-go-gin/database"
"github.com/marianaoliveira-mb/api-go-gin/models"
"github.com/stretchr/testify/assert"
)
var ID int
func SetapDasRotasDeTeste() *gin.Engine {
gin.SetMode(gin.ReleaseMode) //Exibe a resposta de test com uma forma simplificada
rotas := gin.Default()
return rotas
}
func CriaAlunoMock() {
aluno := models.Aluno{Nome: "Nome Teste", CPF: "12345678910", RG: "123456789"}
database.DB.Create(&aluno)
ID = int(aluno.ID)
}
func DeletaAlunoMock() {
var aluno models.Aluno
database.DB.Delete(&aluno, ID)
}
func TestVerificaStatusCodeSaudacao(t *testing.T) {
r := SetapDasRotasDeTeste()
r.GET("/:nome", controllers.Saudacao)
req, _ := http.NewRequest("GET", "/Mariana", nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
assert.Equal(t, http.StatusOK, resposta.Code, "Deveriam ser iguais")
mockDaResposta := `{"API diz:":"E ai Mariana, tudo beleza?"}`
respostaBody, _ := io.ReadAll(resposta.Body)
assert.Equal(t, mockDaResposta, string(respostaBody))
}
func TestExibeAlunos(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetapDasRotasDeTeste()
r.GET("/alunos", controllers.ExibeAlunos)
req, _ := http.NewRequest("GET", "/alunos", nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
assert.Equal(t, http.StatusOK, resposta.Code)
}
func TestBuscaAlunoPorCPF(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetapDasRotasDeTeste()
r.GET("/alunos/cpf/:cpf", controllers.BuscaAlunoPorCPF)
req, _ := http.NewRequest("GET", "/alunos/cpf/89609458755", nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
assert.Equal(t, http.StatusOK, resposta.Code)
}
func TestBuscaAlunoPorID(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetapDasRotasDeTeste()
r.GET("/alunos/:id", controllers.ExibeAluno)
pathBusca := "/alunos/" + strconv.Itoa(ID)
req, _ := http.NewRequest("GET", pathBusca, nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
var alunoMock models.Aluno
json.Unmarshal(resposta.Body.Bytes(), &alunoMock)
fmt.Println(alunoMock.Nome)
assert.Equal(t, "Nome Teste", alunoMock.Nome)
assert.Equal(t, "12345678910", alunoMock.CPF)
assert.Equal(t, "123456789", alunoMock.RG)
}
func TestDeletaAluno(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
r := SetapDasRotasDeTeste()
r.DELETE("/alunos/:id", controllers.DeletaAluno)
pathBusca := "/alunos/" + strconv.Itoa(ID)
req, _ := http.NewRequest("DELETE", pathBusca, nil)
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
assert.Equal(t, http.StatusOK, resposta.Code)
}
func TestEditaAluno(t *testing.T) {
database.ConectaComBancoDeDados()
CriaAlunoMock()
defer DeletaAlunoMock()
r := SetapDasRotasDeTeste()
r.PATCH("/alunos/:id", controllers.EditaAluno)
aluno := models.Aluno{Nome: "Nome Teste", CPF: "23345678910", RG: "123456700"}
valorJson, _ := json.Marshal(aluno)
pathEditar := "/alunos/" + strconv.Itoa(ID)
req, _ := http.NewRequest("PATCH", pathEditar, bytes.NewBuffer(valorJson))
resposta := httptest.NewRecorder()
r.ServeHTTP(resposta, req)
var alunoMockAtualizado models.Aluno
json.Unmarshal(resposta.Body.Bytes(), &alunoMockAtualizado)
assert.Equal(t, "23345678910", alunoMockAtualizado.CPF)
assert.Equal(t, "123456700", alunoMockAtualizado.RG)
assert.Equal(t, "Nome Teste", alunoMockAtualizado.Nome)
}