From 37f1741a77ecb588c57bd8723fb6cfa3b65c6752 Mon Sep 17 00:00:00 2001 From: fede Date: Wed, 18 Nov 2026 21:37:53 -0300 Subject: [PATCH] =?UTF-8?q?a=C3=B1adidos=20tests=20y=20hecho=20workflow=20?= =?UTF-8?q?de=20testing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fede --- .gitea/workflows/tests.yaml | 24 +++++++++ back/test/stock/actualizarobservacion.go | 49 ++++++++++++++++++ back/test/stock/ajustarstock.go | 35 +++++++++++++ back/test/stock/helpers.go | 66 ++++++++++++++++++++++++ back/test/stock/ingresarstock.go | 27 ++++++++++ back/test/stock/liberarreserva.go | 17 ++++++ back/test/stock/obtenerreservas.go | 15 ++++++ back/test/stock/obtenerstock.go | 33 ++++++++++++ back/test/stock/registrardevolucion.go | 25 +++++++++ back/test/stock/reservarstock.go | 30 +++++++++++ back/test/stock/suite_test.go | 44 ++++++++++++++++ back/test/stock/trasladarstock.go | 26 ++++++++++ 12 files changed, 391 insertions(+) create mode 100644 .gitea/workflows/tests.yaml create mode 100644 back/test/stock/actualizarobservacion.go create mode 100644 back/test/stock/ajustarstock.go create mode 100644 back/test/stock/helpers.go create mode 100644 back/test/stock/ingresarstock.go create mode 100644 back/test/stock/liberarreserva.go create mode 100644 back/test/stock/obtenerreservas.go create mode 100644 back/test/stock/obtenerstock.go create mode 100644 back/test/stock/registrardevolucion.go create mode 100644 back/test/stock/reservarstock.go create mode 100644 back/test/stock/suite_test.go create mode 100644 back/test/stock/trasladarstock.go diff --git a/.gitea/workflows/tests.yaml b/.gitea/workflows/tests.yaml new file mode 100644 index 0000000..f5ad5e3 --- /dev/null +++ b/.gitea/workflows/tests.yaml @@ -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 ./... diff --git a/back/test/stock/actualizarobservacion.go b/back/test/stock/actualizarobservacion.go new file mode 100644 index 0000000..c1541f4 --- /dev/null +++ b/back/test/stock/actualizarobservacion.go @@ -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()) + } +} diff --git a/back/test/stock/ajustarstock.go b/back/test/stock/ajustarstock.go new file mode 100644 index 0000000..a471c19 --- /dev/null +++ b/back/test/stock/ajustarstock.go @@ -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) + } +} diff --git a/back/test/stock/helpers.go b/back/test/stock/helpers.go new file mode 100644 index 0000000..962dcf0 --- /dev/null +++ b/back/test/stock/helpers.go @@ -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") + } +} diff --git a/back/test/stock/ingresarstock.go b/back/test/stock/ingresarstock.go new file mode 100644 index 0000000..fd4a693 --- /dev/null +++ b/back/test/stock/ingresarstock.go @@ -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) + } +} diff --git a/back/test/stock/liberarreserva.go b/back/test/stock/liberarreserva.go new file mode 100644 index 0000000..c174be3 --- /dev/null +++ b/back/test/stock/liberarreserva.go @@ -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) + } +} diff --git a/back/test/stock/obtenerreservas.go b/back/test/stock/obtenerreservas.go new file mode 100644 index 0000000..eb09d0c --- /dev/null +++ b/back/test/stock/obtenerreservas.go @@ -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()) + } +} diff --git a/back/test/stock/obtenerstock.go b/back/test/stock/obtenerstock.go new file mode 100644 index 0000000..851e87c --- /dev/null +++ b/back/test/stock/obtenerstock.go @@ -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()) + } +} diff --git a/back/test/stock/registrardevolucion.go b/back/test/stock/registrardevolucion.go new file mode 100644 index 0000000..def85aa --- /dev/null +++ b/back/test/stock/registrardevolucion.go @@ -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) + } +} diff --git a/back/test/stock/reservarstock.go b/back/test/stock/reservarstock.go new file mode 100644 index 0000000..2e4efbc --- /dev/null +++ b/back/test/stock/reservarstock.go @@ -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) + } +} diff --git a/back/test/stock/suite_test.go b/back/test/stock/suite_test.go new file mode 100644 index 0000000..3831761 --- /dev/null +++ b/back/test/stock/suite_test.go @@ -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) +} diff --git a/back/test/stock/trasladarstock.go b/back/test/stock/trasladarstock.go new file mode 100644 index 0000000..b3ff344 --- /dev/null +++ b/back/test/stock/trasladarstock.go @@ -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) + } +}