-
-
Notifications
You must be signed in to change notification settings - Fork 2
test: pruebas unitarias para middleware ValidarSistema #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||||||||||||||||
| package middleware | ||||||||||||||||||||
|
|
||||||||||||||||||||
| import ( | ||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||
| "net/http" | ||||||||||||||||||||
| "net/http/httptest" | ||||||||||||||||||||
| "testing" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| "github.com/gin-gonic/gin" | ||||||||||||||||||||
| ) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| func init() { | ||||||||||||||||||||
| gin.SetMode(gin.TestMode) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| func setupRouter() *gin.Engine { | ||||||||||||||||||||
| r := gin.New() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| r.GET("/:sistema/test", ValidarSistema(), func(ctx *gin.Context) { | ||||||||||||||||||||
| sistema, _ := ctx.Get("sistemaValidado") | ||||||||||||||||||||
| ctx.JSON(http.StatusOK, gin.H{"sistema": sistema}) | ||||||||||||||||||||
| }) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| return r | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| func TestValidarSistema_Common(t *testing.T) { | ||||||||||||||||||||
| router := setupRouter() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| cases := []struct { | ||||||||||||||||||||
| descripcion string | ||||||||||||||||||||
| sistem string | ||||||||||||||||||||
| codigoEsperado int | ||||||||||||||||||||
| }{{"Metro es valid", "METRO", 200}, | ||||||||||||||||||||
| {"MB es valida", "MB", 200}, | ||||||||||||||||||||
| {"metro es valid", "metro", 200}, | ||||||||||||||||||||
| {"MEXIBÚS es valida", "MEXIBÚS", 200}, //Creo que en producción, las solicitudes se enviarán utilizando la opción "MEXIBUS"?. | ||||||||||||||||||||
| {"MEXIBUS es no valida", "MEXIBUS", 404}, | ||||||||||||||||||||
| {"Cadena vacia es no valida", "", 404}, | ||||||||||||||||||||
|
Comment on lines
+36
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Aclarar la intención del caso Este caso está codificando como comportamiento esperado un bug conocido (MEXIBUS sin acento devuelve 404). Cuando alguien lo corrija, el test fallará pero no quedará claro si el 404 era lo deseado o solo el estado actual. Sugiero marcar explícitamente que es un bug conocido (p. ej. en el nombre de la subprueba o con un comentario
Suggested change
|
||||||||||||||||||||
| {"Numero es no valida", "1", 404}, | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| for _, tc := range cases { | ||||||||||||||||||||
| t.Run(tc.descripcion, func(t *testing.T) { | ||||||||||||||||||||
| w := httptest.NewRecorder() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| req, _ := http.NewRequest("GET", "/"+tc.sistem+"/test", nil) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| router.ServeHTTP(w, req) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if w.Code != tc.codigoEsperado { | ||||||||||||||||||||
| t.Errorf("descripcion '%s' codigoEsperado '%d' codigo '%d' ", tc.descripcion, tc.codigoEsperado, w.Code) | ||||||||||||||||||||
| } | ||||||||||||||||||||
| }) | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| func TestValidarSistema_ContextValue(t *testing.T) { | ||||||||||||||||||||
| router := setupRouter() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| t.Run("ToUpper es ok", func(t *testing.T) { | ||||||||||||||||||||
| w := httptest.NewRecorder() | ||||||||||||||||||||
|
|
||||||||||||||||||||
| req, _ := http.NewRequest("GET", "/"+"metro"+"/test", nil) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| router.ServeHTTP(w, req) | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
Comment on lines
+58
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): En la prueba de contexto también conviene verificar el código de estado y la presencia de la clave Además de validar el valor en mayúsculas, sería útil (1) comprobar explícitamente que el código de respuesta sea 200 antes de decodificar el cuerpo, y (2) verificar que la clave "sistema" exista en el mapa usando el segundo valor de la indexación ( |
||||||||||||||||||||
| var body map[string]string | ||||||||||||||||||||
|
|
||||||||||||||||||||
| err := json.NewDecoder(w.Body).Decode(&body) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||
| t.Errorf("Error al decodificar el cuerpo de la respuesta %v", err) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| if body["sistema"] != "METRO" { | ||||||||||||||||||||
| t.Errorf("TopUpper no trabajo %v", body["sistema"]) | ||||||||||||||||||||
| } | ||||||||||||||||||||
| }) | ||||||||||||||||||||
| } | ||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Falta un caso de prueba para el sistema
CBB(y quizá otros) que se menciona en la descripción del PREn la descripción del PR indicas que agregas casos válidos para METRO, MB, CBB y otros, pero en las pruebas sólo están METRO, MB y MEXIBÚS/MEXIBUS. Propongo añadir al menos un caso explícito para
CBB(y cualquier otro sistema que se considere soportado) para que las pruebas coincidan con el alcance declarado y verifiquen esos caminos en el middleware.