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:
@@ -61,6 +61,7 @@ func (h *InventoryHandler) List(c *gin.Context) {
|
||||
|
||||
keyword := c.Query("keyword")
|
||||
warehouseIDStr := c.Query("warehouse_id")
|
||||
inStock := c.Query("in_stock")
|
||||
|
||||
if keyword != "" {
|
||||
baseWhere += " AND (COALESCE(NULLIF(p.name,''), inv.product_name) LIKE ? OR COALESCE(NULLIF(p.code,''), inv.product_code) LIKE ?)"
|
||||
@@ -71,6 +72,9 @@ func (h *InventoryHandler) List(c *gin.Context) {
|
||||
baseWhere += " AND inv.warehouse_id = ?"
|
||||
args = append(args, warehouseIDStr)
|
||||
}
|
||||
if inStock == "1" {
|
||||
baseWhere += " AND inv.quantity > 0"
|
||||
}
|
||||
|
||||
// Count query
|
||||
countSQL := `
|
||||
@@ -96,13 +100,13 @@ func (h *InventoryHandler) List(c *gin.Context) {
|
||||
COALESCE(NULLIF(p.unit,''), inv.unit, '') AS unit,
|
||||
COALESCE(NULLIF(w.name,''), inv.warehouse_name, '') AS warehouse_name,
|
||||
COALESCE(sii.unit_price, inv.unit_price) AS unit_price,
|
||||
COALESCE(DATE_FORMAT(sii.production_date,'%Y-%m-%d'), DATE_FORMAT(inv.production_date,'%Y-%m-%d')) AS production_date,
|
||||
COALESCE(DATE(sii.production_date), DATE(inv.production_date)) AS production_date,
|
||||
COALESCE(NULLIF(sii.batch_no,''), inv.batch_no, '') AS batch_no,
|
||||
inv.supplier_name,
|
||||
inv.remark,
|
||||
COALESCE(p.brand, '') AS brand,
|
||||
p.min_stock,
|
||||
DATE_FORMAT(inv.created_at, '%Y-%m-%dT%H:%i:%sZ') AS created_at
|
||||
inv.created_at
|
||||
FROM inventories inv
|
||||
LEFT JOIN stock_in_items sii ON sii.id = inv.stock_in_item_id
|
||||
LEFT JOIN products p ON p.id = inv.product_id
|
||||
|
||||
@@ -25,8 +25,8 @@ func TestInventoryHandler_List(t *testing.T) {
|
||||
// 直接插入库存记录
|
||||
inv := model.Inventory{
|
||||
ShopID: shop.ID,
|
||||
WarehouseID: warehouse.ID,
|
||||
ProductID: product.ID,
|
||||
WarehouseID: &warehouse.ID,
|
||||
ProductID: &product.ID,
|
||||
Quantity: 100,
|
||||
}
|
||||
require.NoError(t, db.Create(&inv).Error)
|
||||
@@ -49,8 +49,8 @@ func TestInventoryHandler_List_FilterByWarehouse(t *testing.T) {
|
||||
r := setupProtectedRouter(db)
|
||||
|
||||
// 两个仓库各有一个库存
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: warehouse1.ID, ProductID: product1.ID, Quantity: 10})
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: warehouse2.ID, ProductID: product2.ID, Quantity: 20})
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: &warehouse1.ID, ProductID: &product1.ID, Quantity: 10})
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: &warehouse2.ID, ProductID: &product2.ID, Quantity: 20})
|
||||
|
||||
// 按仓库过滤
|
||||
w := makeRequest(r, "GET", fmt.Sprintf("/api/v1/inventory?warehouse_id=%d", warehouse1.ID), token, nil)
|
||||
@@ -69,8 +69,8 @@ func TestInventoryHandler_List_InStockOnly(t *testing.T) {
|
||||
token := getAuthToken(user.ID, shop.ID, "admin")
|
||||
r := setupProtectedRouter(db)
|
||||
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: warehouse.ID, ProductID: product1.ID, Quantity: 10})
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: warehouse.ID, ProductID: product2.ID, Quantity: 0})
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: &warehouse.ID, ProductID: &product1.ID, Quantity: 10})
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: &warehouse.ID, ProductID: &product2.ID, Quantity: 0})
|
||||
|
||||
// 只显示有库存的
|
||||
w := makeRequest(r, "GET", "/api/v1/inventory?in_stock=1", token, nil)
|
||||
@@ -121,8 +121,8 @@ func TestInventoryHandler_CreateCheck(t *testing.T) {
|
||||
// 先创建库存
|
||||
db.Create(&model.Inventory{
|
||||
ShopID: shop.ID,
|
||||
WarehouseID: warehouse.ID,
|
||||
ProductID: product.ID,
|
||||
WarehouseID: &warehouse.ID,
|
||||
ProductID: &product.ID,
|
||||
Quantity: 50,
|
||||
})
|
||||
|
||||
@@ -206,7 +206,7 @@ func TestInventoryHandler_List_HotelIsolation(t *testing.T) {
|
||||
r := setupProtectedRouter(db)
|
||||
|
||||
// 酒店 A 有库存
|
||||
db.Create(&model.Inventory{ShopID: shopA.ID, WarehouseID: warehouseA.ID, ProductID: productA.ID, Quantity: 100})
|
||||
db.Create(&model.Inventory{ShopID: shopA.ID, WarehouseID: &warehouseA.ID, ProductID: &productA.ID, Quantity: 100})
|
||||
|
||||
// 酒店 A 能看到自己的库存
|
||||
w := makeRequest(r, "GET", "/api/v1/inventory", tokenA, nil)
|
||||
|
||||
@@ -25,8 +25,8 @@ func TestStockOutHandler_FullFlow(t *testing.T) {
|
||||
// 先建立库存
|
||||
db.Create(&model.Inventory{
|
||||
ShopID: shop.ID,
|
||||
WarehouseID: warehouse.ID,
|
||||
ProductID: product.ID,
|
||||
WarehouseID: &warehouse.ID,
|
||||
ProductID: &product.ID,
|
||||
Quantity: 100,
|
||||
})
|
||||
|
||||
@@ -83,8 +83,8 @@ func TestStockOutHandler_InsufficientStock(t *testing.T) {
|
||||
// 库存只有 5
|
||||
db.Create(&model.Inventory{
|
||||
ShopID: shop.ID,
|
||||
WarehouseID: warehouse.ID,
|
||||
ProductID: product.ID,
|
||||
WarehouseID: &warehouse.ID,
|
||||
ProductID: &product.ID,
|
||||
Quantity: 5,
|
||||
})
|
||||
|
||||
@@ -114,7 +114,7 @@ func TestStockOutHandler_List(t *testing.T) {
|
||||
r := setupProtectedRouter(db)
|
||||
|
||||
// 先建立库存
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: warehouse.ID, ProductID: product.ID, Quantity: 100})
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: &warehouse.ID, ProductID: &product.ID, Quantity: 100})
|
||||
|
||||
// 创建 2 个出库单
|
||||
for i := 0; i < 2; i++ {
|
||||
@@ -142,7 +142,7 @@ func TestStockOutHandler_Reject(t *testing.T) {
|
||||
token := getAuthToken(user.ID, shop.ID, "admin")
|
||||
r := setupProtectedRouter(db)
|
||||
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: warehouse.ID, ProductID: product.ID, Quantity: 100})
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: &warehouse.ID, ProductID: &product.ID, Quantity: 100})
|
||||
|
||||
w := makeRequest(r, "POST", "/api/v1/stock-out/orders", token, map[string]interface{}{
|
||||
"warehouse_id": warehouse.ID,
|
||||
@@ -206,8 +206,8 @@ func TestStockOutHandler_TenantIsolation(t *testing.T) {
|
||||
// 建立门店 A 的库存
|
||||
db.Create(&model.Inventory{
|
||||
ShopID: shopA.ID,
|
||||
WarehouseID: warehouseA.ID,
|
||||
ProductID: productA.ID,
|
||||
WarehouseID: &warehouseA.ID,
|
||||
ProductID: &productA.ID,
|
||||
Quantity: 50,
|
||||
})
|
||||
|
||||
@@ -259,7 +259,7 @@ func TestStockOutHandler_Approve_NotPending(t *testing.T) {
|
||||
token := getAuthToken(user.ID, shop.ID, "admin")
|
||||
r := setupProtectedRouter(db)
|
||||
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: warehouse.ID, ProductID: product.ID, Quantity: 50})
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: &warehouse.ID, ProductID: &product.ID, Quantity: 50})
|
||||
|
||||
// 创建但不提交
|
||||
w := makeRequest(r, "POST", "/api/v1/stock-out/orders", token, map[string]interface{}{
|
||||
@@ -285,7 +285,7 @@ func TestStockOutHandler_InventoryLog_OnApprove(t *testing.T) {
|
||||
token := getAuthToken(user.ID, shop.ID, "admin")
|
||||
r := setupProtectedRouter(db)
|
||||
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: warehouse.ID, ProductID: product.ID, Quantity: 100})
|
||||
db.Create(&model.Inventory{ShopID: shop.ID, WarehouseID: &warehouse.ID, ProductID: &product.ID, Quantity: 100})
|
||||
|
||||
// 创建、提交、审核
|
||||
w := makeRequest(r, "POST", "/api/v1/stock-out/orders", token, map[string]interface{}{
|
||||
|
||||
Reference in New Issue
Block a user