mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 21:41:55 +00:00
FIX[BE]: name duplicate flock,projectflock category change,menerapkan dto seperti warehouse di projectflock
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
)
|
||||
|
||||
func TestKandangIntegration(t *testing.T) {
|
||||
@@ -51,20 +52,19 @@ func TestKandangIntegration(t *testing.T) {
|
||||
})
|
||||
|
||||
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,
|
||||
FlockId: flocID,
|
||||
AreaId: areaID,
|
||||
Category: string(utils.ProjectFlockCategoryGrowing),
|
||||
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)
|
||||
|
||||
@@ -19,19 +19,18 @@ func TestProjectFlockSummary(t *testing.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,
|
||||
"kandang_ids": []uint{kandangID},
|
||||
"flock_id": flockID,
|
||||
"area_id": areaID,
|
||||
"category": "growing",
|
||||
"fcr_id": fcrID,
|
||||
"location_id": locationID,
|
||||
"kandang_ids": []uint{kandangID},
|
||||
}
|
||||
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/production/project_flocks", createPayload)
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
@@ -40,9 +39,10 @@ func TestProjectFlockSummary(t *testing.T) {
|
||||
|
||||
var createResp struct {
|
||||
Data struct {
|
||||
Id uint `json:"id"`
|
||||
Period int `json:"period"`
|
||||
Flock struct {
|
||||
Id uint `json:"id"`
|
||||
Period int `json:"period"`
|
||||
Category string `json:"category"`
|
||||
Flock struct {
|
||||
Id uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
} `json:"flock"`
|
||||
@@ -50,11 +50,6 @@ func TestProjectFlockSummary(t *testing.T) {
|
||||
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"`
|
||||
@@ -86,19 +81,27 @@ func TestProjectFlockSummary(t *testing.T) {
|
||||
if createResp.Data.Area.Id != areaID || createResp.Data.Area.Name == "" {
|
||||
t.Fatalf("expected area detail to be present, got %+v", createResp.Data.Area)
|
||||
}
|
||||
if createResp.Data.Category != string(utils.ProjectFlockCategoryGrowing) {
|
||||
t.Fatalf("expected category to be %s, got %s", utils.ProjectFlockCategoryGrowing, createResp.Data.Category)
|
||||
}
|
||||
if createResp.Data.Location.Id != 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)
|
||||
}
|
||||
if createResp.Data.Kandangs[0].Status == "" {
|
||||
t.Fatalf("expected kandang status to be present, got %+v", createResp.Data.Kandangs[0])
|
||||
if createResp.Data.Kandangs[0].Status != string(utils.KandangStatusPengajuan) {
|
||||
t.Fatalf("expected kandang status to be PENGAJUAN, got %s", createResp.Data.Kandangs[0].Status)
|
||||
}
|
||||
if createResp.Data.Period != 1 {
|
||||
t.Fatalf("expected period 1 to be assigned automatically, got %d", createResp.Data.Period)
|
||||
}
|
||||
|
||||
createdKandang := fetchKandang(t, db, kandangID)
|
||||
if createdKandang.Status != string(utils.KandangStatusPengajuan) {
|
||||
t.Fatalf("expected kandang status in DB to be PENGAJUAN, got %s", createdKandang.Status)
|
||||
}
|
||||
|
||||
var pivotRecords []entities.ProjectFlockKandang
|
||||
if err := db.Where("project_flock_id = ?", createResp.Data.Id).Find(&pivotRecords).Error; err != nil {
|
||||
t.Fatalf("failed to fetch pivot records: %v", err)
|
||||
@@ -116,12 +119,12 @@ func TestProjectFlockSummary(t *testing.T) {
|
||||
|
||||
secondKandangID := createKandang(t, app, "Kandang Summary 2", locationID, 1)
|
||||
secondPayload := map[string]any{
|
||||
"flock_id": flockID,
|
||||
"area_id": areaID,
|
||||
"product_category_id": categoryID,
|
||||
"fcr_id": fcrID,
|
||||
"location_id": locationID,
|
||||
"kandang_ids": []uint{secondKandangID},
|
||||
"flock_id": flockID,
|
||||
"area_id": areaID,
|
||||
"category": "laying",
|
||||
"fcr_id": fcrID,
|
||||
"location_id": locationID,
|
||||
"kandang_ids": []uint{secondKandangID},
|
||||
}
|
||||
resp, body = doJSONRequest(t, app, http.MethodPost, "/api/production/project_flocks", secondPayload)
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
@@ -129,8 +132,9 @@ func TestProjectFlockSummary(t *testing.T) {
|
||||
}
|
||||
var createRespSecond struct {
|
||||
Data struct {
|
||||
Id uint `json:"id"`
|
||||
Period int `json:"period"`
|
||||
Id uint `json:"id"`
|
||||
Period int `json:"period"`
|
||||
Category string `json:"category"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &createRespSecond); err != nil {
|
||||
@@ -139,6 +143,9 @@ func TestProjectFlockSummary(t *testing.T) {
|
||||
if createRespSecond.Data.Period != 2 {
|
||||
t.Fatalf("expected second period to be 2, got %d", createRespSecond.Data.Period)
|
||||
}
|
||||
if createRespSecond.Data.Category != string(utils.ProjectFlockCategoryLaying) {
|
||||
t.Fatalf("expected category to be %s, got %s", utils.ProjectFlockCategoryLaying, createRespSecond.Data.Category)
|
||||
}
|
||||
|
||||
pivotRecords = nil
|
||||
if err := db.Where("project_flock_id = ?", createRespSecond.Data.Id).Find(&pivotRecords).Error; err != nil {
|
||||
@@ -155,6 +162,11 @@ func TestProjectFlockSummary(t *testing.T) {
|
||||
t.Fatalf("expected second pivot DetachedAt to be nil, got %v", secondPivotRecord.DetachedAt)
|
||||
}
|
||||
|
||||
secondKandang := fetchKandang(t, db, secondKandangID)
|
||||
if secondKandang.Status != string(utils.KandangStatusPengajuan) {
|
||||
t.Fatalf("expected second kandang status in DB to be PENGAJUAN, got %s", secondKandang.Status)
|
||||
}
|
||||
|
||||
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))
|
||||
@@ -202,7 +214,7 @@ func TestProjectFlockSummary(t *testing.T) {
|
||||
t.Fatalf("expected 200 when deleting second project flock, got %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
secondKandang := fetchKandang(t, db, secondKandangID)
|
||||
secondKandang = fetchKandang(t, db, secondKandangID)
|
||||
if secondKandang.ProjectFlockId != nil {
|
||||
t.Fatalf("expected second project_flock_id to be nil after delete, got %v", *secondKandang.ProjectFlockId)
|
||||
}
|
||||
@@ -245,19 +257,18 @@ func TestProjectFlockSearchByRelatedFields(t *testing.T) {
|
||||
areaID := createArea(t, app, "Area Search Target")
|
||||
locationID := createLocation(t, app, "Location Search Target", "Location Address Target", areaID)
|
||||
flockID := createFlock(t, app, "Flock Search Target")
|
||||
categoryID := createProductCategory(t, app, "Category Search Target", "CATGT")
|
||||
fcrID := createFcr(t, app, "FCR Search Target", []map[string]any{
|
||||
{"weight": 1.0, "fcr_number": 1.5, "mortality": 2.0},
|
||||
})
|
||||
kandangID := createKandang(t, app, "Kandang Search Target", locationID, 1)
|
||||
|
||||
createPayload := map[string]any{
|
||||
"flock_id": flockID,
|
||||
"area_id": areaID,
|
||||
"product_category_id": categoryID,
|
||||
"fcr_id": fcrID,
|
||||
"location_id": locationID,
|
||||
"kandang_ids": []uint{kandangID},
|
||||
"flock_id": flockID,
|
||||
"area_id": areaID,
|
||||
"category": "growing",
|
||||
"fcr_id": fcrID,
|
||||
"location_id": locationID,
|
||||
"kandang_ids": []uint{kandangID},
|
||||
}
|
||||
|
||||
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/production/project_flocks", createPayload)
|
||||
@@ -277,8 +288,8 @@ func TestProjectFlockSearchByRelatedFields(t *testing.T) {
|
||||
searchTerms := []string{
|
||||
"Flock Search Target",
|
||||
"Area Search Target",
|
||||
"Category Search Target",
|
||||
"CATGT",
|
||||
string(utils.ProjectFlockCategoryGrowing),
|
||||
"growing",
|
||||
"FCR Search Target",
|
||||
"Kandang Search Target",
|
||||
"Location Search Target",
|
||||
@@ -329,7 +340,6 @@ func TestProjectFlockSorting(t *testing.T) {
|
||||
flockOne := createFlock(t, app, "Flock Sort One")
|
||||
flockTwo := createFlock(t, app, "Flock Sort Two")
|
||||
|
||||
categoryID := createProductCategory(t, app, "Category Sort", "CSORT")
|
||||
fcrID := createFcr(t, app, "FCR Sort", []map[string]any{
|
||||
{"weight": 1.0, "fcr_number": 1.5, "mortality": 2.0},
|
||||
})
|
||||
@@ -339,12 +349,12 @@ func TestProjectFlockSorting(t *testing.T) {
|
||||
kandangThree := createKandang(t, app, "Kandang Sort Three", locationB, 1)
|
||||
|
||||
projectOnePayload := map[string]any{
|
||||
"flock_id": flockOne,
|
||||
"area_id": areaA,
|
||||
"product_category_id": categoryID,
|
||||
"fcr_id": fcrID,
|
||||
"location_id": locationA,
|
||||
"kandang_ids": []uint{kandangOne},
|
||||
"flock_id": flockOne,
|
||||
"area_id": areaA,
|
||||
"category": "growing",
|
||||
"fcr_id": fcrID,
|
||||
"location_id": locationA,
|
||||
"kandang_ids": []uint{kandangOne},
|
||||
}
|
||||
resp, body := doJSONRequest(t, app, http.MethodPost, "/api/production/project_flocks", projectOnePayload)
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
@@ -353,12 +363,12 @@ func TestProjectFlockSorting(t *testing.T) {
|
||||
projectOneID := parseProjectFlockID(t, body)
|
||||
|
||||
projectTwoPayload := map[string]any{
|
||||
"flock_id": flockTwo,
|
||||
"area_id": areaB,
|
||||
"product_category_id": categoryID,
|
||||
"fcr_id": fcrID,
|
||||
"location_id": locationB,
|
||||
"kandang_ids": []uint{kandangTwo, kandangThree},
|
||||
"flock_id": flockTwo,
|
||||
"area_id": areaB,
|
||||
"category": "laying",
|
||||
"fcr_id": fcrID,
|
||||
"location_id": locationB,
|
||||
"kandang_ids": []uint{kandangTwo, kandangThree},
|
||||
}
|
||||
resp, body = doJSONRequest(t, app, http.MethodPost, "/api/production/project_flocks", projectTwoPayload)
|
||||
if resp.StatusCode != fiber.StatusCreated {
|
||||
|
||||
Reference in New Issue
Block a user