mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
84 lines
2.7 KiB
Go
84 lines
2.7 KiB
Go
package test
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
)
|
|
|
|
func TestKandangIntegration(t *testing.T) {
|
|
app, db := setupIntegrationApp(t)
|
|
areaID := createArea(t, app, "Area Kandang")
|
|
locationID := createLocation(t, app, "Location For Kandang", "Address", areaID)
|
|
|
|
t.Run("create kandang success", func(t *testing.T) {
|
|
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/kandangs", map[string]any{
|
|
"name": "Kandang OK",
|
|
"status": "ACTIVE",
|
|
"location_id": locationID,
|
|
"pic_id": 1,
|
|
})
|
|
if resp.StatusCode != fiber.StatusCreated {
|
|
t.Fatalf("expected 201, got %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
})
|
|
|
|
t.Run("create kandang with unknown location fails", func(t *testing.T) {
|
|
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/kandangs", map[string]any{
|
|
"name": "Kandang Fail",
|
|
"status": "ACTIVE",
|
|
"location_id": 999,
|
|
"pic_id": 1,
|
|
})
|
|
if resp.StatusCode != fiber.StatusNotFound {
|
|
t.Fatalf("expected 404, got %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
})
|
|
|
|
t.Run("cannot assign project floc with existing active kandang", func(t *testing.T) {
|
|
categoryID := createProductCategory(t, app, "DOC Category", "DOC1")
|
|
fcrID := createFcr(t, app, "FCR For Floc", []map[string]any{
|
|
{"weight": 1.0, "fcr_number": 1.5, "mortality": 2.0},
|
|
})
|
|
flocID := createFlock(t, app, "Floc Test")
|
|
|
|
projectFloc := entities.ProjectFlock{
|
|
FlockId: flocID,
|
|
AreaId: areaID,
|
|
ProductCategoryId: categoryID,
|
|
FcrId: fcrID,
|
|
LocationId: locationID,
|
|
Period: 1,
|
|
CreatedBy: 1,
|
|
}
|
|
if err := db.Create(&projectFloc).Error; err != nil {
|
|
t.Fatalf("failed to seed project floc: %v", err)
|
|
}
|
|
|
|
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/kandangs", map[string]any{
|
|
"name": "Kandang Active 1",
|
|
"status": "ACTIVE",
|
|
"location_id": locationID,
|
|
"pic_id": 1,
|
|
"project_flock_id": projectFloc.Id,
|
|
})
|
|
if resp.StatusCode != fiber.StatusCreated {
|
|
t.Fatalf("expected 201 when creating first kandang, got %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
resp, body = doJSONRequest(t, app, http.MethodPost, "/api/master-data/kandangs", map[string]any{
|
|
"name": "Kandang Active 2",
|
|
"status": "ACTIVE",
|
|
"location_id": locationID,
|
|
"pic_id": 1,
|
|
"project_flock_id": projectFloc.Id,
|
|
})
|
|
if resp.StatusCode != fiber.StatusConflict {
|
|
t.Fatalf("expected 409 when creating second active kandang, got %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
})
|
|
}
|