mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
Merge branch 'dev/hafizh' into 'feat/BE/Sprint-6'
unfinish: fifo system See merge request mbugroup/lti-api!75
This commit is contained in:
@@ -0,0 +1,446 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
|
||||
commonRepo "gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
commonSvc "gitlab.com/mbugroup/lti-api.git/internal/common/service"
|
||||
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
||||
rProductWarehouse "gitlab.com/mbugroup/lti-api.git/internal/modules/inventory/product-warehouses/repositories"
|
||||
recordingRepo "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/repositories"
|
||||
servicePkg "gitlab.com/mbugroup/lti-api.git/internal/modules/production/recordings/services"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils"
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/utils/fifo"
|
||||
)
|
||||
|
||||
func TestRecordingFIFO_CreatePendingWithoutStock(t *testing.T) {
|
||||
db, svc, _, _ := setupRecordingFIFOTableTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
recordingID := uint(1)
|
||||
productWarehouse := createProductWarehouseRow(t, db, 0)
|
||||
stock := createRecordingStockRow(t, db, recordingID, productWarehouse.Id, 10)
|
||||
|
||||
if err := svc.ConsumeRecordingStocks(ctx, db, []entity.RecordingStock{stock}); err != nil {
|
||||
t.Fatalf("consumeRecordingStocks (pending) failed: %v", err)
|
||||
}
|
||||
|
||||
updated := fetchRecordingStock(t, db, stock.Id)
|
||||
assertFloatEqual(t, 0, updated.UsageQty, "usage_qty should remain zero when no stock is available")
|
||||
assertFloatEqual(t, 10, updated.PendingQty, "pending_qty should capture the entire request")
|
||||
assertWarehouseQuantity(t, db, productWarehouse.Id, 0)
|
||||
assertAllocationCount(t, db, 0)
|
||||
|
||||
assertAllocationCount(t, db, 0)
|
||||
}
|
||||
|
||||
func TestRecordingFIFO_EditReallocatesUsage(t *testing.T) {
|
||||
db, svc, fifoSvc, stockableKey := setupRecordingFIFOTableTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
recordingID := uint(1)
|
||||
productWarehouse := createProductWarehouseRow(t, db, 0)
|
||||
stock := createRecordingStockRow(t, db, recordingID, productWarehouse.Id, 10)
|
||||
lot := createStockLot(t, db, productWarehouse.Id)
|
||||
|
||||
if _, err := fifoSvc.Replenish(ctx, commonSvc.StockReplenishRequest{
|
||||
StockableKey: stockableKey,
|
||||
StockableID: lot.Id,
|
||||
ProductWarehouseID: productWarehouse.Id,
|
||||
Quantity: 12,
|
||||
}); err != nil {
|
||||
t.Fatalf("replenish failed: %v", err)
|
||||
}
|
||||
|
||||
if err := svc.ConsumeRecordingStocks(ctx, db, []entity.RecordingStock{stock}); err != nil {
|
||||
t.Fatalf("consumeRecordingStocks (initial) failed: %v", err)
|
||||
}
|
||||
|
||||
assertWarehouseQuantity(t, db, productWarehouse.Id, 2)
|
||||
|
||||
desired := 4.0
|
||||
stock.UsageQty = &desired
|
||||
|
||||
if err := svc.ConsumeRecordingStocks(ctx, db, []entity.RecordingStock{stock}); err != nil {
|
||||
t.Fatalf("consumeRecordingStocks (edit) failed: %v", err)
|
||||
}
|
||||
|
||||
updated := fetchRecordingStock(t, db, stock.Id)
|
||||
assertFloatEqual(t, 4, updated.UsageQty, "usage_qty should reflect edited request")
|
||||
assertFloatEqual(t, 0, updated.PendingQty, "pending_qty should remain zero after downsize")
|
||||
assertWarehouseQuantity(t, db, productWarehouse.Id, 8)
|
||||
|
||||
alloc := fetchSingleAllocation(t, db, stock.Id)
|
||||
if alloc.Status != entity.StockAllocationStatusActive {
|
||||
t.Fatalf("expected ACTIVE allocation, got %s", alloc.Status)
|
||||
}
|
||||
if mathAbs(alloc.Qty-4) > 1e-6 {
|
||||
t.Fatalf("expected allocation qty 4, got %.3f", alloc.Qty)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecordingFIFO_DeleteReleasesStock(t *testing.T) {
|
||||
db, svc, fifoSvc, stockableKey := setupRecordingFIFOTableTest(t)
|
||||
ctx := context.Background()
|
||||
|
||||
recordingID := uint(1)
|
||||
productWarehouse := createProductWarehouseRow(t, db, 0)
|
||||
stock := createRecordingStockRow(t, db, recordingID, productWarehouse.Id, 10)
|
||||
lot := createStockLot(t, db, productWarehouse.Id)
|
||||
|
||||
if _, err := fifoSvc.Replenish(ctx, commonSvc.StockReplenishRequest{
|
||||
StockableKey: stockableKey,
|
||||
StockableID: lot.Id,
|
||||
ProductWarehouseID: productWarehouse.Id,
|
||||
Quantity: 10,
|
||||
}); err != nil {
|
||||
t.Fatalf("replenish failed: %v", err)
|
||||
}
|
||||
if err := svc.ConsumeRecordingStocks(ctx, db, []entity.RecordingStock{stock}); err != nil {
|
||||
t.Fatalf("consumeRecordingStocks failed: %v", err)
|
||||
}
|
||||
|
||||
if err := svc.ReleaseRecordingStocks(ctx, db, []entity.RecordingStock{stock}); err != nil {
|
||||
t.Fatalf("releaseRecordingStocks failed: %v", err)
|
||||
}
|
||||
|
||||
updated := fetchRecordingStock(t, db, stock.Id)
|
||||
assertFloatEqual(t, 0, updated.UsageQty, "usage_qty should be cleared after delete")
|
||||
assertFloatEqual(t, 0, updated.PendingQty, "pending_qty should be cleared after delete")
|
||||
assertWarehouseQuantity(t, db, productWarehouse.Id, 10)
|
||||
|
||||
alloc := fetchSingleAllocation(t, db, stock.Id)
|
||||
if alloc.Status != entity.StockAllocationStatusReleased {
|
||||
t.Fatalf("expected allocation to be released, got %s", alloc.Status)
|
||||
}
|
||||
}
|
||||
|
||||
// --- helpers ----------------------------------------------------------------
|
||||
|
||||
type recordingStockTable struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
RecordingId uint `gorm:"column:recording_id;not null"`
|
||||
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null"`
|
||||
UsageQty *float64 `gorm:"column:usage_qty"`
|
||||
PendingQty *float64 `gorm:"column:pending_qty"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (recordingStockTable) TableName() string { return "recording_stocks" }
|
||||
|
||||
type productWarehouseTable struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
ProductId uint `gorm:"column:product_id"`
|
||||
WarehouseId uint `gorm:"column:warehouse_id"`
|
||||
Quantity float64 `gorm:"column:quantity"`
|
||||
CreatedBy uint `gorm:"column:created_by"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
}
|
||||
|
||||
func (productWarehouseTable) TableName() string { return "product_warehouses" }
|
||||
|
||||
type stockAllocationTable struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
ProductWarehouseId uint `gorm:"not null"`
|
||||
StockableType string `gorm:"size:100"`
|
||||
StockableId uint
|
||||
UsableType string `gorm:"size:100"`
|
||||
UsableId uint
|
||||
Qty float64 `gorm:"column:qty"`
|
||||
Status string `gorm:"size:20"`
|
||||
Note *string `gorm:"type:text"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ReleasedAt *time.Time
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
}
|
||||
|
||||
func (stockAllocationTable) TableName() string { return "stock_allocations" }
|
||||
|
||||
type testStockSource struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
ProductWarehouseId uint `gorm:"column:product_warehouse_id;not null"`
|
||||
TotalQty float64 `gorm:"column:total_qty"`
|
||||
TotalUsedQty float64 `gorm:"column:total_used_qty"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (testStockSource) TableName() string { return "test_fifo_stockables" }
|
||||
|
||||
func setupRecordingFIFOTableTest(t *testing.T) (*gorm.DB, servicePkg.RecordingFIFOIntegrationService, commonSvc.FifoService, fifo.StockableKey) {
|
||||
t.Helper()
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
|
||||
if err := db.AutoMigrate(
|
||||
&recordingStockTable{},
|
||||
&productWarehouseTable{},
|
||||
&stockAllocationTable{},
|
||||
&testStockSource{},
|
||||
); err != nil {
|
||||
t.Fatalf("auto migrate: %v", err)
|
||||
}
|
||||
|
||||
if err := db.AutoMigrate(
|
||||
&entity.ProductWarehouse{},
|
||||
&entity.StockAllocation{},
|
||||
&entity.RecordingStock{},
|
||||
); err != nil {
|
||||
t.Fatalf("auto migrate entities: %v", err)
|
||||
}
|
||||
|
||||
stockAllocRepo := newFifoTestStockAllocationRepo(db)
|
||||
productWarehouseRepo := rProductWarehouse.NewProductWarehouseRepository(db)
|
||||
fifoSvc := commonSvc.NewFifoService(db, stockAllocRepo, productWarehouseRepo, utils.Log)
|
||||
|
||||
registerRecordingUsable(t, fifoSvc)
|
||||
|
||||
key := fifo.StockableKey(fmt.Sprintf("TEST_STOCKABLE_%s_%d", sanitizeKey(t.Name()), time.Now().UnixNano()))
|
||||
if err := fifoSvc.RegisterStockable(fifo.StockableConfig{
|
||||
Key: key,
|
||||
Table: "test_fifo_stockables",
|
||||
Columns: fifo.StockableColumns{
|
||||
ID: "id",
|
||||
ProductWarehouseID: "product_warehouse_id",
|
||||
TotalQuantity: "total_qty",
|
||||
TotalUsedQuantity: "total_used_qty",
|
||||
CreatedAt: "created_at",
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("register stockable: %v", err)
|
||||
}
|
||||
|
||||
svc := servicePkg.NewRecordingFIFOIntegrationService(
|
||||
recordingRepo.NewRecordingRepository(db),
|
||||
productWarehouseRepo,
|
||||
fifoSvc,
|
||||
)
|
||||
|
||||
return db, svc, fifoSvc, key
|
||||
}
|
||||
|
||||
func registerRecordingUsable(t *testing.T, fifoSvc commonSvc.FifoService) {
|
||||
t.Helper()
|
||||
err := fifoSvc.RegisterUsable(fifo.UsableConfig{
|
||||
Key: fifo.UsableKeyRecordingStock,
|
||||
Table: "recording_stocks",
|
||||
Columns: fifo.UsableColumns{
|
||||
ID: "id",
|
||||
ProductWarehouseID: "product_warehouse_id",
|
||||
UsageQuantity: "usage_qty",
|
||||
PendingQuantity: "pending_qty",
|
||||
CreatedAt: "created_at",
|
||||
},
|
||||
})
|
||||
if err != nil && !strings.Contains(strings.ToLower(err.Error()), "already registered") {
|
||||
t.Fatalf("register usable: %v", err)
|
||||
}
|
||||
if _, ok := fifo.Usable(fifo.UsableKeyRecordingStock); !ok {
|
||||
t.Fatal("recording stock usable key not registered")
|
||||
}
|
||||
}
|
||||
|
||||
func createProductWarehouseRow(t *testing.T, db *gorm.DB, qty float64) entity.ProductWarehouse {
|
||||
t.Helper()
|
||||
pw := entity.ProductWarehouse{
|
||||
ProductId: 1,
|
||||
WarehouseId: 1,
|
||||
Quantity: qty,
|
||||
CreatedBy: 1,
|
||||
}
|
||||
if err := db.Create(&pw).Error; err != nil {
|
||||
t.Fatalf("create product warehouse: %v", err)
|
||||
}
|
||||
return pw
|
||||
}
|
||||
|
||||
func createRecordingStockRow(t *testing.T, db *gorm.DB, recordingID, productWarehouseID uint, desired float64) entity.RecordingStock {
|
||||
t.Helper()
|
||||
stock := entity.RecordingStock{
|
||||
RecordingId: recordingID,
|
||||
ProductWarehouseId: productWarehouseID,
|
||||
UsageQty: floatPtr(0),
|
||||
PendingQty: floatPtr(0),
|
||||
}
|
||||
if err := db.Create(&stock).Error; err != nil {
|
||||
t.Fatalf("create recording stock: %v", err)
|
||||
}
|
||||
stock.UsageQty = floatPtr(desired)
|
||||
return stock
|
||||
}
|
||||
|
||||
func createStockLot(t *testing.T, db *gorm.DB, productWarehouseID uint) testStockSource {
|
||||
t.Helper()
|
||||
lot := testStockSource{
|
||||
ProductWarehouseId: productWarehouseID,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
if err := db.Create(&lot).Error; err != nil {
|
||||
t.Fatalf("create stock lot: %v", err)
|
||||
}
|
||||
return lot
|
||||
}
|
||||
|
||||
func fetchRecordingStock(t *testing.T, db *gorm.DB, id uint) entity.RecordingStock {
|
||||
t.Helper()
|
||||
var stock entity.RecordingStock
|
||||
if err := db.First(&stock, id).Error; err != nil {
|
||||
t.Fatalf("fetch recording stock: %v", err)
|
||||
}
|
||||
return stock
|
||||
}
|
||||
|
||||
func fetchSingleAllocation(t *testing.T, db *gorm.DB, usableID uint) entity.StockAllocation {
|
||||
t.Helper()
|
||||
var alloc entity.StockAllocation
|
||||
if err := db.Where("usable_id = ?", usableID).Order("created_at ASC").First(&alloc).Error; err != nil {
|
||||
t.Fatalf("fetch allocation: %v", err)
|
||||
}
|
||||
return alloc
|
||||
}
|
||||
|
||||
func assertAllocationCount(t *testing.T, db *gorm.DB, expected int64) {
|
||||
t.Helper()
|
||||
var count int64
|
||||
if err := db.Model(&entity.StockAllocation{}).Count(&count).Error; err != nil {
|
||||
t.Fatalf("count allocations: %v", err)
|
||||
}
|
||||
if count != expected {
|
||||
t.Fatalf("expected %d allocations, got %d", expected, count)
|
||||
}
|
||||
}
|
||||
|
||||
func assertWarehouseQuantity(t *testing.T, db *gorm.DB, id uint, expected float64) {
|
||||
t.Helper()
|
||||
var pw entity.ProductWarehouse
|
||||
if err := db.First(&pw, id).Error; err != nil {
|
||||
t.Fatalf("fetch product warehouse: %v", err)
|
||||
}
|
||||
if mathAbs(pw.Quantity-expected) > 1e-6 {
|
||||
t.Fatalf("expected warehouse quantity %.3f, got %.3f", expected, pw.Quantity)
|
||||
}
|
||||
}
|
||||
|
||||
func assertFloatEqual(t *testing.T, expected float64, value *float64, msg string) {
|
||||
t.Helper()
|
||||
if value == nil {
|
||||
t.Fatalf("expected %s %.3f, got nil", msg, expected)
|
||||
}
|
||||
if mathAbs(*value-expected) > 1e-6 {
|
||||
t.Fatalf("%s: expected %.3f, got %.3f", msg, expected, *value)
|
||||
}
|
||||
}
|
||||
|
||||
func floatPtr(v float64) *float64 {
|
||||
p := new(float64)
|
||||
*p = v
|
||||
return p
|
||||
}
|
||||
|
||||
func mathAbs(v float64) float64 {
|
||||
if v < 0 {
|
||||
return -v
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func sanitizeKey(name string) string {
|
||||
if name == "" {
|
||||
return "CASE"
|
||||
}
|
||||
clean := strings.Map(func(r rune) rune {
|
||||
if (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') {
|
||||
return r
|
||||
}
|
||||
if r >= 'a' && r <= 'z' {
|
||||
return r - 32
|
||||
}
|
||||
return '_'
|
||||
}, name)
|
||||
return clean
|
||||
}
|
||||
|
||||
type fifoTestStockAllocationRepo struct {
|
||||
commonRepo.StockAllocationRepository
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func newFifoTestStockAllocationRepo(db *gorm.DB) commonRepo.StockAllocationRepository {
|
||||
return &fifoTestStockAllocationRepo{
|
||||
StockAllocationRepository: commonRepo.NewStockAllocationRepository(db),
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *fifoTestStockAllocationRepo) PatchOne(
|
||||
ctx context.Context,
|
||||
id uint,
|
||||
updates map[string]any,
|
||||
modifier func(*gorm.DB) *gorm.DB,
|
||||
) error {
|
||||
base := r.db
|
||||
|
||||
setClauses := make([]string, 0, len(updates))
|
||||
args := make([]any, 0, len(updates)+1)
|
||||
for column, value := range updates {
|
||||
colName := column
|
||||
if strings.EqualFold(column, "quantity") {
|
||||
colName = "qty"
|
||||
}
|
||||
setClauses = append(setClauses, fmt.Sprintf("%s = ?", colName))
|
||||
args = append(args, value)
|
||||
}
|
||||
args = append(args, id)
|
||||
sql := fmt.Sprintf("UPDATE stock_allocations SET %s WHERE id = ?", strings.Join(setClauses, ", "))
|
||||
|
||||
result := base.Exec(sql, args...)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return gorm.ErrRecordNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *fifoTestStockAllocationRepo) ReleaseByUsable(
|
||||
ctx context.Context,
|
||||
usableType string,
|
||||
usableID uint,
|
||||
note *string,
|
||||
modifier func(*gorm.DB) *gorm.DB,
|
||||
) error {
|
||||
base := r.db
|
||||
|
||||
setClause := "status = ?, released_at = ?"
|
||||
args := []any{entity.StockAllocationStatusReleased, time.Now()}
|
||||
if note != nil {
|
||||
setClause += ", note = ?"
|
||||
args = append(args, *note)
|
||||
}
|
||||
args = append(args, usableType, usableID, entity.StockAllocationStatusActive)
|
||||
sql := fmt.Sprintf(
|
||||
"UPDATE stock_allocations SET %s WHERE usable_type = ? AND usable_id = ? AND status = ?",
|
||||
setClause,
|
||||
)
|
||||
|
||||
result := base.Exec(sql, args...)
|
||||
return result.Error
|
||||
}
|
||||
Reference in New Issue
Block a user