añadidos tests y hecho workflow de testing

Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
fede committed 2026-11-19 08:55:08 -03:00
1 parent 1e74e4363e
commit 37f1741a77
12 files changed
+391

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
name: Tests post-merge
on:
push:
branches:
- master
jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout código
run: |
git clone https://${{ secrets.GIT_USER }}:${{ secrets.REGISTRY_PASS }}@fedesrv.ddns.net/git/${{ github.repository }}.git .
git checkout ${{ github.sha }}
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.27'
- name: Tests backend
working-directory: ./back
run: go test ./...
+49
View File
@@ -0,0 +1,49 @@
package stock
import (
"testing"
ctrl "repuestera_back/controllers/stock"
"github.com/gin-gonic/gin"
)
func TestActualizarObservacion_Validacion(t *testing.T) {
texto := ""
for i := 0; i < 61; i++ {
texto += "a"
}
context, recorder := newStockContext(t, "PATCH", "/stock/1/observacion", map[string]any{
"observacion": texto,
}, gin.Params{{Key: "id", Value: "1"}})
ctrl.ActualizarObservacion(context)
if recorder.Code != 400 {
t.Fatalf("observación larga: esperado 400, obtenido %d", recorder.Code)
}
context, recorder = newStockContext(t, "PATCH", "/stock/abc/observacion", map[string]any{
"observacion": "ok",
}, gin.Params{{Key: "id", Value: "abc"}})
ctrl.ActualizarObservacion(context)
if recorder.Code != 400 {
t.Fatalf("id inválido: esperado 400, obtenido %d", recorder.Code)
}
}
func TestActualizarObservacion_Integracion(t *testing.T) {
context, recorder := newIntegrationContext(t, "PATCH", "/stock/1/observacion", map[string]any{
"observacion": "test integracion",
}, gin.Params{{Key: "id", Value: "1"}})
ctrl.ActualizarObservacion(context)
if recorder.Code != 200 {
t.Fatalf("actualizar observación: esperado 200, obtenido %d: %s", recorder.Code, recorder.Body.String())
}
context, recorder = newIntegrationContext(t, "PATCH", "/stock/1/observacion", map[string]any{
"observacion": "",
}, gin.Params{{Key: "id", Value: "1"}})
ctrl.ActualizarObservacion(context)
if recorder.Code != 200 {
t.Fatalf("vaciar observación: esperado 200, obtenido %d: %s", recorder.Code, recorder.Body.String())
}
}
+35
View File
@@ -0,0 +1,35 @@
package stock
import (
"testing"
ctrl "repuestera_back/controllers/stock"
)
func TestAjustarStock_Validacion(t *testing.T) {
context, recorder := newStockContext(t, "POST", "/stock/ajustes", map[string]any{}, nil)
ctrl.AjustarStock(context)
if recorder.Code != 400 {
t.Fatalf("ajuste sin datos: esperado 400, obtenido %d", recorder.Code)
}
context, recorder = newStockContext(t, "POST", "/stock/ajustes", map[string]any{
"stockId": 1,
"cantidadReal": -1,
"motivo": "test",
}, nil)
ctrl.AjustarStock(context)
if recorder.Code != 400 {
t.Fatalf("ajuste negativo: esperado 400, obtenido %d", recorder.Code)
}
context, recorder = newStockContext(t, "POST", "/stock/ajustes", map[string]any{
"stockId": 1,
"cantidadReal": 10,
"motivo": "",
}, nil)
ctrl.AjustarStock(context)
if recorder.Code != 400 {
t.Fatalf("ajuste sin motivo: esperado 400, obtenido %d", recorder.Code)
}
}
+66
View File
@@ -0,0 +1,66 @@
package stock
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"repuestera_back/utils"
"github.com/gin-gonic/gin"
)
var dbAvailable bool
func newStockContext(t *testing.T, method, path string, body any, params gin.Params) (*gin.Context, *httptest.ResponseRecorder) {
t.Helper()
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
context, _ := gin.CreateTestContext(recorder)
var payload []byte
if body != nil {
payload, _ = json.Marshal(body)
}
request, err := http.NewRequest(method, path, bytes.NewReader(payload))
if err != nil {
t.Fatalf("no se pudo crear la request: %v", err)
}
request.Header.Set("Content-Type", "application/json")
context.Request = request
context.Params = params
context.Set("userID", uint(1))
context.Set(utils.ContextEmpresaIDKey, uint(0))
return context, recorder
}
func newIntegrationContext(t *testing.T, method, path string, body any, params gin.Params) (*gin.Context, *httptest.ResponseRecorder) {
t.Helper()
if !dbAvailable {
t.Skip("base de datos no disponible")
}
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
context, _ := gin.CreateTestContext(recorder)
var payload []byte
if body != nil {
payload, _ = json.Marshal(body)
}
request, err := http.NewRequest(method, path, bytes.NewReader(payload))
if err != nil {
t.Fatalf("no se pudo crear la request: %v", err)
}
request.Header.Set("Content-Type", "application/json")
context.Request = request
context.Params = params
context.Set("userID", uint(1))
context.Set(utils.ContextEmpresaIDKey, uint(1))
return context, recorder
}
func requireDB(t *testing.T) {
if !dbAvailable {
t.Skip("base de datos no disponible")
}
}
+27
View File
@@ -0,0 +1,27 @@
package stock
import (
"testing"
ctrl "repuestera_back/controllers/stock"
)
func TestIngresarStock_Validacion(t *testing.T) {
context, recorder := newStockContext(t, "POST", "/stock/ingresos", map[string]any{
"proveedorId": 1,
"numero": "TEST",
"fechaEmision": "2026-01-01T00:00:00Z",
"almacenDestinoId": 1,
"items": []any{},
}, nil)
ctrl.IngresarStock(context)
if recorder.Code != 400 {
t.Fatalf("ingreso sin items: esperado 400, obtenido %d", recorder.Code)
}
context, recorder = newStockContext(t, "POST", "/stock/ingresos", map[string]any{}, nil)
ctrl.IngresarStock(context)
if recorder.Code != 400 {
t.Fatalf("ingreso vacío: esperado 400, obtenido %d", recorder.Code)
}
}
+17
View File
@@ -0,0 +1,17 @@
package stock
import (
"testing"
ctrl "repuestera_back/controllers/stock"
"github.com/gin-gonic/gin"
)
func TestLiberarReserva_Validacion(t *testing.T) {
context, recorder := newStockContext(t, "PATCH", "/stock/reservas/abc/liberar", nil, gin.Params{{Key: "id", Value: "abc"}})
ctrl.LiberarReserva(context)
if recorder.Code != 400 {
t.Fatalf("liberar id inválido: esperado 400, obtenido %d", recorder.Code)
}
}
+15
View File
@@ -0,0 +1,15 @@
package stock
import (
"testing"
ctrl "repuestera_back/controllers/stock"
)
func TestObtenerReservas_Integracion(t *testing.T) {
context, recorder := newIntegrationContext(t, "GET", "/stock/reservas", nil, nil)
ctrl.ObtenerReservas(context)
if recorder.Code != 200 {
t.Fatalf("obtener reservas: esperado 200, obtenido %d: %s", recorder.Code, recorder.Body.String())
}
}
+33
View File
@@ -0,0 +1,33 @@
package stock
import (
"testing"
ctrl "repuestera_back/controllers/stock"
"github.com/gin-gonic/gin"
)
func TestObtenerStock_Integracion(t *testing.T) {
context, recorder := newIntegrationContext(t, "GET", "/stock/", nil, nil)
ctrl.ObtenerStock(context)
if recorder.Code != 200 {
t.Fatalf("obtener stock: esperado 200, obtenido %d: %s", recorder.Code, recorder.Body.String())
}
}
func TestObtenerStockPorID_Validacion(t *testing.T) {
context, recorder := newStockContext(t, "GET", "/stock/abc", nil, gin.Params{{Key: "id", Value: "abc"}})
ctrl.ObtenerStockPorID(context)
if recorder.Code != 400 {
t.Fatalf("id inválido: esperado 400, obtenido %d", recorder.Code)
}
}
func TestObtenerStockPorID_Integracion(t *testing.T) {
context, recorder := newIntegrationContext(t, "GET", "/stock/1", nil, gin.Params{{Key: "id", Value: "1"}})
ctrl.ObtenerStockPorID(context)
if recorder.Code != 200 {
t.Fatalf("obtener stock por id: esperado 200, obtenido %d: %s", recorder.Code, recorder.Body.String())
}
}
+25
View File
@@ -0,0 +1,25 @@
package stock
import (
"testing"
ctrl "repuestera_back/controllers/stock"
)
func TestRegistrarDevolucion_Validacion(t *testing.T) {
context, recorder := newStockContext(t, "POST", "/stock/devoluciones", map[string]any{
"facturaId": 1,
"motivo": "test",
"items": []any{},
}, nil)
ctrl.RegistrarDevolucion(context)
if recorder.Code != 400 {
t.Fatalf("devolución sin items: esperado 400, obtenido %d", recorder.Code)
}
context, recorder = newStockContext(t, "POST", "/stock/devoluciones", map[string]any{}, nil)
ctrl.RegistrarDevolucion(context)
if recorder.Code != 400 {
t.Fatalf("devolución vacía: esperado 400, obtenido %d", recorder.Code)
}
}
+30
View File
@@ -0,0 +1,30 @@
package stock
import (
"testing"
ctrl "repuestera_back/controllers/stock"
)
func TestReservarStock_Validacion(t *testing.T) {
context, recorder := newStockContext(t, "POST", "/stock/reservas", map[string]any{
"repuestoId": 1,
"almacenId": 1,
"cantidad": 1,
}, nil)
ctrl.ReservarStock(context)
if recorder.Code != 400 {
t.Fatalf("reserva sin cliente: esperado 400, obtenido %d", recorder.Code)
}
context, recorder = newStockContext(t, "POST", "/stock/reservas", map[string]any{
"repuestoId": 1,
"almacenId": 1,
"cantidad": 0,
"clienteId": 1,
}, nil)
ctrl.ReservarStock(context)
if recorder.Code != 400 {
t.Fatalf("reserva cantidad cero: esperado 400, obtenido %d", recorder.Code)
}
}
+44
View File
@@ -0,0 +1,44 @@
package stock
import (
"os"
"strings"
"testing"
"github.com/joho/godotenv"
"repuestera_back/config"
"repuestera_back/repositorios"
)
func TestMain(m *testing.M) {
_ = godotenv.Load("../../.env")
if dbString := os.Getenv("DBSTRING"); dbString != "" {
os.Setenv("DBSTRING", strings.ReplaceAll(dbString, "mysql:3306", "localhost:3306"))
}
if err := config.InitDB(); err == nil && config.DB != nil {
repositorios.InicializarRepositorios(config.DB)
dbAvailable = true
} else {
repositorios.InicializarRepositorios(nil)
}
os.Exit(m.Run())
}
func TestStockSuite(t *testing.T) {
t.Run("ActualizarObservacion/Validacion", TestActualizarObservacion_Validacion)
t.Run("AjustarStock/Validacion", TestAjustarStock_Validacion)
t.Run("IngresarStock/Validacion", TestIngresarStock_Validacion)
t.Run("LiberarReserva/Validacion", TestLiberarReserva_Validacion)
t.Run("ReservarStock/Validacion", TestReservarStock_Validacion)
t.Run("RegistrarDevolucion/Validacion", TestRegistrarDevolucion_Validacion)
t.Run("TrasladarStock/Validacion", TestTrasladarStock_Validacion)
t.Run("ObtenerStockPorID/Validacion", TestObtenerStockPorID_Validacion)
if !dbAvailable {
t.Skip("base de datos no disponible para tests de integración")
}
t.Run("ActualizarObservacion/Integracion", TestActualizarObservacion_Integracion)
t.Run("ObtenerReservas/Integracion", TestObtenerReservas_Integracion)
t.Run("ObtenerStock/Integracion", TestObtenerStock_Integracion)
t.Run("ObtenerStockPorID/Integracion", TestObtenerStockPorID_Integracion)
}
+26
View File
@@ -0,0 +1,26 @@
package stock
import (
"testing"
ctrl "repuestera_back/controllers/stock"
)
func TestTrasladarStock_Validacion(t *testing.T) {
context, recorder := newStockContext(t, "POST", "/stock/traslados", map[string]any{}, nil)
ctrl.TrasladarStock(context)
if recorder.Code != 400 {
t.Fatalf("traslado sin datos: esperado 400, obtenido %d", recorder.Code)
}
context, recorder = newStockContext(t, "POST", "/stock/traslados", map[string]any{
"repuestoId": 1,
"almacenOrigenId": 1,
"almacenDestinoId": 1,
"cantidad": 1,
}, nil)
ctrl.TrasladarStock(context)
if recorder.Code != 422 {
t.Fatalf("traslado mismo almacén: esperado 422, obtenido %d", recorder.Code)
}
}