fix(test): 修复测试文件类型错误和测试数据库 schema 过期问题
- 修正 stock_in/out 测试中 WarehouseID/ProductID 指针误用(StockOrder 用 uint64,Inventory 用 *uint64) - 更新 testutil/setup.go:inventories 表加入所有批次字段,stock_in_items 加 production_date,新增 finance_records 表 - 修正 inventory handler 中 DATE_FORMAT(MySQL 专属)→ DATE()(跨 DB 兼容) - 更新断言:order_no 前缀改为 RK,库存总量用 SUM 替代 First - 库存 List 新增 in_stock=1 过滤器 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -98,8 +98,8 @@ func TestStockService_ApproveStockOut_Success(t *testing.T) {
|
||||
// 先入库
|
||||
inv := model.Inventory{
|
||||
ShopID: shop.ID,
|
||||
WarehouseID: warehouse.ID,
|
||||
ProductID: product.ID,
|
||||
WarehouseID: &warehouse.ID,
|
||||
ProductID: &product.ID,
|
||||
Quantity: 20,
|
||||
}
|
||||
require.NoError(t, db.Create(&inv).Error)
|
||||
@@ -149,8 +149,8 @@ func TestStockService_ApproveStockOut_InsufficientStock(t *testing.T) {
|
||||
// 库存只有 3
|
||||
inv := model.Inventory{
|
||||
ShopID: shop.ID,
|
||||
WarehouseID: warehouse.ID,
|
||||
ProductID: product.ID,
|
||||
WarehouseID: &warehouse.ID,
|
||||
ProductID: &product.ID,
|
||||
Quantity: 3,
|
||||
}
|
||||
require.NoError(t, db.Create(&inv).Error)
|
||||
@@ -207,7 +207,7 @@ func TestStockService_ApproveStockOut_ProductNotInInventory(t *testing.T) {
|
||||
svc := NewStockService(db)
|
||||
err := svc.ApproveStockOut(shop.ID, order.ID, user.ID)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "not in inventory")
|
||||
assert.ErrorIs(t, err, ErrInsufficientStock)
|
||||
}
|
||||
|
||||
func TestStockService_GenerateOrderNo(t *testing.T) {
|
||||
@@ -219,7 +219,7 @@ func TestStockService_GenerateOrderNo(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.NotEmpty(t, no)
|
||||
// 单号格式: prefix + date + 6位序号
|
||||
assert.Contains(t, no, "st")
|
||||
assert.Contains(t, no, "RK")
|
||||
|
||||
// 第二次生成序号应递增
|
||||
no2, err := svc.GenerateOrderNo(shop.ID, "stock_in")
|
||||
@@ -288,9 +288,11 @@ func TestStockService_MultipleStockIn_AccumulatesInventory(t *testing.T) {
|
||||
require.NoError(t, db.Create(&order2).Error)
|
||||
require.NoError(t, svc.ApproveStockIn(shop.ID, order2.ID, user.ID))
|
||||
|
||||
// 总库存应该是 15
|
||||
var inv model.Inventory
|
||||
db.Where("shop_id = ? AND warehouse_id = ? AND product_id = ?",
|
||||
shop.ID, warehouse.ID, product.ID).First(&inv)
|
||||
assert.Equal(t, float64(15), inv.Quantity)
|
||||
// 总库存应该是 15(两个批次之和)
|
||||
var totalQty float64
|
||||
db.Model(&model.Inventory{}).
|
||||
Where("shop_id = ? AND warehouse_id = ? AND product_id = ? AND deleted_at IS NULL",
|
||||
shop.ID, &warehouse.ID, &product.ID).
|
||||
Select("COALESCE(SUM(quantity), 0)").Scan(&totalQty)
|
||||
assert.Equal(t, float64(15), totalQty)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user