fix: 修正可用库存过滤与测试

This commit is contained in:
zhouz
2026-07-22 14:39:49 +08:00
parent 1fcc152dca
commit 3d180b4948
3 changed files with 84 additions and 29 deletions

View File

@@ -0,0 +1,78 @@
package cn.code.nl.module.wms.service.iostorinv;
import cn.code.nl.module.wms.controller.admin.iostorinv.vo.AvailableInventoryRespVO;
import cn.code.nl.module.wms.controller.admin.iostorinv.vo.ExpandAvailableInventoryReqVO;
import cn.code.nl.module.wms.dal.mysql.iostorinvdtl.IostorinvDtlMapper;
import org.junit.jupiter.api.Test;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Proxy;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
class IostorInvServiceImplTest {
@Test
@SuppressWarnings("unchecked")
void expandAvailableInventoryShouldDeduplicateBoxesAndKeepWarehouseIsolation() throws Exception {
List<String> queriedVehicleCodes = new ArrayList<>();
IostorinvDtlMapper mapper = (IostorinvDtlMapper) Proxy.newProxyInstance(
IostorinvDtlMapper.class.getClassLoader(), new Class<?>[]{IostorinvDtlMapper.class},
(proxy, method, args) -> {
if (!"selectAvailableInventoryByVehicleCodes".equals(method.getName())) {
throw new UnsupportedOperationException(method.getName());
}
assertEquals("STOR-001", args[0]);
queriedVehicleCodes.addAll((Collection<String>) args[1]);
return List.of(inventory("BOX-001", "PCSN-001", "STOR-001"),
inventory("BOX-001", "PCSN-002", "STOR-001"));
});
IostorInvServiceImpl service = new IostorInvServiceImpl();
Field mapperField = IostorInvServiceImpl.class.getDeclaredField("iostorinvDtlMapper");
mapperField.setAccessible(true);
mapperField.set(service, mapper);
ExpandAvailableInventoryReqVO reqVO = new ExpandAvailableInventoryReqVO();
reqVO.setStorId("STOR-001");
reqVO.setVehicleCodes(List.of("BOX-001", "BOX-001"));
List<AvailableInventoryRespVO> result = service.expandAvailableInventory(reqVO);
assertEquals(List.of("BOX-001"), queriedVehicleCodes);
assertEquals(2, result.size());
assertTrue(result.stream().allMatch(item -> "BOX-001".equals(item.getVehicleCode())));
assertTrue(result.stream().allMatch(item -> "STOR-001".equals(item.getStorId())));
assertEquals(2, result.stream().map(AvailableInventoryRespVO::getPcsn).distinct().count());
}
@Test
void availableInventorySqlShouldFilterWarehouseStatusDeletedAndAvailableQuantity() throws Exception {
String mapperResource = "mapper/iostorinvdtl/IostorinvDtlMapper.xml";
try (InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(mapperResource)) {
assertNotNull(input, "Mapper XML 应存在于测试类路径");
String sql = new String(input.readAllBytes(), StandardCharsets.UTF_8);
assertTrue(sql.contains("sa.stor_id = #{storId}"));
assertTrue(sql.contains("gp.status = '可用'"));
assertTrue(sql.contains("gp.qty - gp.frozen_qty &gt; 0"));
assertTrue(sql.contains("gp.deleted = 0"));
assertTrue(sql.contains("sa.deleted = 0"));
assertTrue(sql.contains("mb.deleted = 0"));
}
}
private static AvailableInventoryRespVO inventory(String vehicleCode, String pcsn, String storId) {
AvailableInventoryRespVO inventory = new AvailableInventoryRespVO();
inventory.setVehicleCode(vehicleCode);
inventory.setPcsn(pcsn);
inventory.setStorId(storId);
return inventory;
}
}

View File

@@ -1,22 +1,17 @@
package cn.code.nl.module.wms.service.iostorinv;
import cn.code.nl.module.wms.controller.admin.iostorinv.vo.IostorInvCreateReqVO;
import cn.code.nl.module.wms.controller.admin.iostorinv.vo.AvailableInventoryRespVO;
import cn.code.nl.module.wms.controller.admin.iostorinv.vo.ExpandAvailableInventoryReqVO;
import jakarta.annotation.Resource;
import jakarta.validation.ConstraintViolationException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Disabled;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
@ActiveProfiles("local")
@@ -40,22 +35,4 @@ class IostorInvServiceLocalSpringTest {
.anyMatch(violation -> violation.getPropertyPath().toString().contains("details")));
}
/**
* 本地库数据前置:仓库 stor-1 的 BOX-001 有两个可用子卷,另一仓库也存在 BOX-001。
* 当前仓库没有可移植的 WMS 本地测试数据集,避免向开发库写入固定业务数据,准备好上述数据后可手动启用。
*/
@Test
@Disabled("需要 local 数据库预置跨仓库 BOX-001 测试数据")
void expandAvailableInventoryShouldReturnAllChildrenOnceAndOnlyFromRequestedWarehouse() {
ExpandAvailableInventoryReqVO reqVO = new ExpandAvailableInventoryReqVO();
reqVO.setStorId("stor-1");
reqVO.setVehicleCodes(List.of("BOX-001", "BOX-001"));
List<AvailableInventoryRespVO> result = iostorInvService.expandAvailableInventory(reqVO);
assertEquals(2, result.size());
assertTrue(result.stream().allMatch(item -> "BOX-001".equals(item.getVehicleCode())));
assertTrue(result.stream().allMatch(item -> "stor-1".equals(item.getStorId())));
assertEquals(2, result.stream().map(AvailableInventoryRespVO::getPcsn).distinct().count());
}
}