34 lines
2.4 KiB
Java
34 lines
2.4 KiB
Java
|
|
package com.ruoyi.projectdev;
|
||
|
|
|
||
|
|
import java.io.ByteArrayOutputStream;
|
||
|
|
import java.math.BigDecimal;
|
||
|
|
import java.util.*;
|
||
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||
|
|
import org.junit.jupiter.api.Test;
|
||
|
|
import org.springframework.mock.web.MockMultipartFile;
|
||
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
||
|
|
import com.ruoyi.production.service.BomService;
|
||
|
|
import com.ruoyi.warehouse.domain.ProductInfo;
|
||
|
|
import com.ruoyi.warehouse.mapper.ProductInfoMapper;
|
||
|
|
import com.ruoyi.web.controller.production.BomController;
|
||
|
|
import static org.junit.jupiter.api.Assertions.*;
|
||
|
|
import static org.mockito.Mockito.*;
|
||
|
|
|
||
|
|
class BomImportTest {
|
||
|
|
@Test void previewChecksIdentityQuantityAndDoesNotSave() throws Exception {
|
||
|
|
var controller=new BomController();var items=mock(ProductInfoMapper.class);var service=mock(BomService.class);
|
||
|
|
ReflectionTestUtils.setField(controller,"items",items);ReflectionTestUtils.setField(controller,"service",service);
|
||
|
|
for(long id=1;id<=4;id++){var item=new ProductInfo();item.setId(id);item.setProductCode("ITEM-"+id);item.setProductName("物品"+id);item.setUnit("件");when(items.checkProductCodeUnique(item.getProductCode())).thenReturn(item);}
|
||
|
|
try(var workbook=new XSSFWorkbook();var bytes=new ByteArrayOutputStream()){
|
||
|
|
var sheet=workbook.createSheet("用料");var header=sheet.createRow(0);header.createCell(0).setCellValue("物品编码");header.createCell(1).setCellValue("单台用量");
|
||
|
|
String[][] values={{"ITEM-2","0.0019"},{"ITEM-2","1"},{"ITEM-1","1"},{"ITEM-3","0"},{"ITEM-4","0.00001"},{"UNKNOWN","1"}};
|
||
|
|
for(int i=0;i<values.length;i++){var row=sheet.createRow(i+1);row.createCell(0).setCellValue(values[i][0]);row.createCell(1).setCellValue(values[i][1]);}
|
||
|
|
workbook.write(bytes);var file=new MockMultipartFile("file","bom.xlsx","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",bytes.toByteArray());
|
||
|
|
var rows=(List<Map<String,Object>>)controller.preview(file,1L).get("data");assertEquals(6,rows.size());assertNull(rows.get(0).get("error"));
|
||
|
|
assertEquals(new BigDecimal("0.0019"),((com.ruoyi.production.domain.BomLine)rows.get(0).get("row")).getUnitQuantity());
|
||
|
|
for(int i=1;i<rows.size();i++)assertNotNull(rows.get(i).get("error"));
|
||
|
|
verifyNoInteractions(service);verify(items,never()).insertProductInfo(any());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|