feat/BE/US-74/pengajuan-flock

This commit is contained in:
ragilap
2025-10-16 10:06:18 +07:00
parent 7392d8a679
commit 6c7ab8a0f8
37 changed files with 2038 additions and 99 deletions
+48 -1
View File
@@ -5,16 +5,19 @@ import (
"testing"
"github.com/gofiber/fiber/v2"
"gitlab.com/mbugroup/lti-api.git/internal/entities"
)
func TestKandangIntegration(t *testing.T) {
app, _ := setupIntegrationApp(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,
})
@@ -26,6 +29,7 @@ func TestKandangIntegration(t *testing.T) {
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,
})
@@ -33,4 +37,47 @@ func TestKandangIntegration(t *testing.T) {
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))
}
})
}
@@ -40,6 +40,8 @@ func setupIntegrationApp(t *testing.T) (*fiber.App, *gorm.DB) {
&entities.User{},
&entities.Area{},
&entities.Location{},
&entities.Flock{},
&entities.ProjectFlock{},
&entities.Kandang{},
&entities.Warehouse{},
&entities.Uom{},
@@ -152,6 +154,7 @@ func createKandang(t *testing.T, app *fiber.App, name string, locationID, picID
t.Helper()
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/kandangs", map[string]any{
"name": name,
"status": "ACTIVE",
"location_id": locationID,
"pic_id": picID,
})
@@ -291,6 +294,17 @@ func createFcr(t *testing.T, app *fiber.App, name string, standards []map[string
return parseID(t, body)
}
func createFlock(t *testing.T, app *fiber.App, name string) uint {
t.Helper()
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/master-data/flocks", map[string]any{
"name": name,
})
if resp.StatusCode != fiber.StatusCreated {
t.Fatalf("expected 201 when creating flock, got %d: %s", resp.StatusCode, string(body))
}
return parseID(t, body)
}
func fetchFcr(t *testing.T, db *gorm.DB, id uint) entities.Fcr {
t.Helper()
var fcr entities.Fcr
@@ -0,0 +1,119 @@
package test
import (
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/gofiber/fiber/v2"
)
func TestProjectFlockSummary(t *testing.T) {
app, _ := setupIntegrationApp(t)
areaID := createArea(t, app, "Area Project")
locationID := createLocation(t, app, "Location Project", "Address", areaID)
flockID := createFlock(t, app, "Flock Summary")
categoryID := createProductCategory(t, app, "DOC Summary", "DOCS")
fcrID := createFcr(t, app, "FCR Summary", []map[string]any{
{"weight": 1.0, "fcr_number": 1.5, "mortality": 2.0},
})
kandangID := createKandang(t, app, "Kandang Summary", locationID, 1)
createPayload := map[string]any{
"flock_id": flockID,
"area_id": areaID,
"product_category_id": categoryID,
"fcr_id": fcrID,
"location_id": locationID,
"period": 1,
"kandang_ids": []uint{kandangID},
}
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/production/project_flocks", createPayload)
if resp.StatusCode != fiber.StatusCreated {
t.Fatalf("expected 201 when creating project flock, got %d: %s", resp.StatusCode, string(body))
}
var createResp struct {
Data struct {
Id uint `json:"id"`
FlockId uint `json:"flock_id"`
AreaId uint `json:"area_id"`
ProductCategoryId uint `json:"product_category_id"`
FcrId uint `json:"fcr_id"`
LocationId uint `json:"location_id"`
Period int `json:"period"`
Flock struct {
Id uint `json:"id"`
Name string `json:"name"`
} `json:"flock"`
Area struct {
Id uint `json:"id"`
Name string `json:"name"`
} `json:"area"`
ProductCategory struct {
Id uint `json:"id"`
Name string `json:"name"`
Code string `json:"code"`
} `json:"product_category"`
Fcr struct {
Id uint `json:"id"`
Name string `json:"name"`
} `json:"fcr"`
Location struct {
Id uint `json:"id"`
Name string `json:"name"`
Address string `json:"address"`
} `json:"location"`
Kandangs []struct {
Id uint `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
} `json:"kandangs"`
CreatedUser struct {
Id uint `json:"id"`
IdUser uint `json:"id_user"`
Email string `json:"email"`
Name string `json:"name"`
} `json:"created_user"`
} `json:"data"`
}
if err := json.Unmarshal(body, &createResp); err != nil {
t.Fatalf("failed to parse create response: %v", err)
}
if createResp.Data.FlockId != flockID || createResp.Data.Flock.Name == "" {
t.Fatalf("expected flock detail to be present, got %+v", createResp.Data.Flock)
}
if createResp.Data.AreaId != areaID || createResp.Data.Area.Name == "" {
t.Fatalf("expected area detail to be present, got %+v", createResp.Data.Area)
}
if createResp.Data.LocationId != locationID || createResp.Data.Location.Name == "" {
t.Fatalf("expected location detail to be present, got %+v", createResp.Data.Location)
}
if len(createResp.Data.Kandangs) != 1 || createResp.Data.Kandangs[0].Id != kandangID {
t.Fatalf("expected kandang detail to be present, got %+v", createResp.Data.Kandangs)
}
resp, body = doJSONRequest(t, app, http.MethodGet, "/api/production/project_flocks/flocks/"+uintToString(flockID)+"/periods", nil)
if resp.StatusCode != fiber.StatusOK {
t.Fatalf("expected 200 when fetching summary, got %d: %s", resp.StatusCode, string(body))
}
var summary struct {
Data struct {
NextPeriod int `json:"next_period"`
} `json:"data"`
}
if err := json.Unmarshal(body, &summary); err != nil {
t.Fatalf("failed to parse summary response: %v", err)
}
if summary.Data.NextPeriod != 2 {
t.Fatalf("expected next_period 2, got %d", summary.Data.NextPeriod)
}
}
func uintToString(v uint) string {
return fmt.Sprintf("%d", v)
}