|
@@ -0,0 +1,99 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
|
|
+import com.etotem.cfc.entity.Product;
|
|
|
|
|
+import com.etotem.cfc.mapper.ProductMapper;
|
|
|
|
|
+import com.etotem.cfc.service.NutritionProductService;
|
|
|
|
|
+import com.etotem.cfc.service.ProductService;
|
|
|
|
|
+import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
|
|
+import java.lang.reflect.Field;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.isNull;
|
|
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 商品编辑:分类/所属体系为 clearable 下拉,清空后必须真正写入 NULL,
|
|
|
|
|
+ * 不能因为 MyBatis-Plus updateById 忽略 null 字段而被静默保留。
|
|
|
|
|
+ */
|
|
|
|
|
+class ProductUpdateClearTest {
|
|
|
|
|
+
|
|
|
|
|
+ private ProductService service;
|
|
|
|
|
+ private ProductMapper productMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() throws Exception {
|
|
|
|
|
+ // LambdaUpdateWrapper 解析列名依赖 MyBatis-Plus 的 TableInfo 缓存
|
|
|
|
|
+ MapperBuilderAssistant assistant =
|
|
|
|
|
+ new MapperBuilderAssistant(new MybatisConfiguration(), "");
|
|
|
|
|
+ TableInfoHelper.initTableInfo(assistant, Product.class);
|
|
|
|
|
+
|
|
|
|
|
+ service = new ProductService();
|
|
|
|
|
+ productMapper = mock(ProductMapper.class);
|
|
|
|
|
+ set("productMapper", productMapper);
|
|
|
|
|
+ set("nutritionProductService", mock(NutritionProductService.class));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void set(String name, Object value) throws Exception {
|
|
|
|
|
+ Field f = ProductService.class.getDeclaredField(name);
|
|
|
|
|
+ f.setAccessible(true);
|
|
|
|
|
+ f.set(service, value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private Product existing() {
|
|
|
|
|
+ Product p = new Product();
|
|
|
|
|
+ p.setId(7L);
|
|
|
|
|
+ p.setName("还原水机滤芯");
|
|
|
|
|
+ p.setStatus("on_shelf");
|
|
|
|
|
+ p.setCategoryId(1L);
|
|
|
|
|
+ p.setDistributionSystemId(3L);
|
|
|
|
|
+ return p;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void adminUpdate_分类与体系为空时显式置NULL() {
|
|
|
|
|
+ Product existing = existing();
|
|
|
|
|
+ when(productMapper.selectById(7L)).thenReturn(existing);
|
|
|
|
|
+ when(productMapper.updateById(any(Product.class))).thenReturn(1);
|
|
|
|
|
+ when(productMapper.update(isNull(), any(Wrapper.class))).thenReturn(1);
|
|
|
|
|
+
|
|
|
|
|
+ Product form = new Product();
|
|
|
|
|
+ form.setId(7L);
|
|
|
|
|
+ form.setName("还原水机滤芯");
|
|
|
|
|
+ form.setCategoryId(null);
|
|
|
|
|
+ form.setDistributionSystemId(null);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(200, service.adminUpdate(form).getCode());
|
|
|
|
|
+
|
|
|
|
|
+ ArgumentCaptor<Wrapper> captor = ArgumentCaptor.forClass(Wrapper.class);
|
|
|
|
|
+ verify(productMapper).update(isNull(), captor.capture());
|
|
|
|
|
+ String sqlSet = ((LambdaUpdateWrapper<Product>) captor.getValue()).getSqlSet();
|
|
|
|
|
+ assertTrue(sqlSet.contains("category_id"), sqlSet);
|
|
|
|
|
+ assertTrue(sqlSet.contains("distribution_system_id"), sqlSet);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void adminUpdate_选择了分类与体系时不额外置NULL() {
|
|
|
|
|
+ Product existing = existing();
|
|
|
|
|
+ when(productMapper.selectById(7L)).thenReturn(existing);
|
|
|
|
|
+ when(productMapper.updateById(any(Product.class))).thenReturn(1);
|
|
|
|
|
+ when(productMapper.update(isNull(), any(Wrapper.class))).thenReturn(1);
|
|
|
|
|
+
|
|
|
|
|
+ Product form = new Product();
|
|
|
|
|
+ form.setId(7L);
|
|
|
|
|
+ form.setName("还原水机滤芯");
|
|
|
|
|
+ form.setCategoryId(2L);
|
|
|
|
|
+ form.setDistributionSystemId(4L);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(200, service.adminUpdate(form).getCode());
|
|
|
|
|
+
|
|
|
|
|
+ verify(productMapper, never()).update(isNull(), any(Wrapper.class));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|