mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
120 lines
3.8 KiB
Go
120 lines
3.8 KiB
Go
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)
|
|
}
|