|
|
@@ -13,6 +13,7 @@ import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.mockito.ArgumentCaptor;
|
|
|
import java.lang.reflect.Field;
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
|
@@ -96,4 +97,92 @@ class ProductUpdateClearTest {
|
|
|
|
|
|
verify(productMapper, never()).update(isNull(), any(Wrapper.class));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 营养字段为可空输入:管理端清空输入框提交 null 时,必须真正写入 NULL,
|
|
|
+ * 否则 updateById 忽略 null 会残留旧值。
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ void adminUpdate_营养字段清空时显式置NULL() {
|
|
|
+ Product existing = existing();
|
|
|
+ existing.setCaloriesKcal(new BigDecimal("320"));
|
|
|
+ existing.setProteinG(new BigDecimal("18.5"));
|
|
|
+ existing.setFatG(new BigDecimal("9"));
|
|
|
+ existing.setCarbG(new BigDecimal("36"));
|
|
|
+ existing.setFiberG(new BigDecimal("4.2"));
|
|
|
+ existing.setNutritionTags("低糖,高纤维");
|
|
|
+ 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(1L);
|
|
|
+ form.setDistributionSystemId(3L);
|
|
|
+ // 营养字段全部未填写(清空)
|
|
|
+
|
|
|
+ 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("calories_kcal"), sqlSet);
|
|
|
+ assertTrue(sqlSet.contains("protein_g"), sqlSet);
|
|
|
+ assertTrue(sqlSet.contains("fat_g"), sqlSet);
|
|
|
+ assertTrue(sqlSet.contains("carb_g"), sqlSet);
|
|
|
+ assertTrue(sqlSet.contains("fiber_g"), sqlSet);
|
|
|
+ assertTrue(sqlSet.contains("nutrition_tags"), sqlSet);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 原记录本来就没有营养数据、且表单也未提交营养字段时,不应触发多余的清空 UPDATE。
|
|
|
+ */
|
|
|
+ @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(1L);
|
|
|
+ form.setDistributionSystemId(3L);
|
|
|
+
|
|
|
+ assertEquals(200, service.adminUpdate(form).getCode());
|
|
|
+
|
|
|
+ verify(productMapper, never()).update(isNull(), any(Wrapper.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 营养标签传空串(含纯空格)时归一化为 NULL:库里不留空串,原有标签被真正清空。
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ void adminUpdate_营养标签传空串时归一化为NULL() {
|
|
|
+ Product existing = existing();
|
|
|
+ existing.setNutritionTags("低糖");
|
|
|
+ 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(1L);
|
|
|
+ form.setDistributionSystemId(3L);
|
|
|
+ form.setNutritionTags(" ");
|
|
|
+
|
|
|
+ assertEquals(200, service.adminUpdate(form).getCode());
|
|
|
+
|
|
|
+ ArgumentCaptor<Product> productCaptor = ArgumentCaptor.forClass(Product.class);
|
|
|
+ verify(productMapper).updateById(productCaptor.capture());
|
|
|
+ assertNull(productCaptor.getValue().getNutritionTags());
|
|
|
+
|
|
|
+ ArgumentCaptor<Wrapper> captor = ArgumentCaptor.forClass(Wrapper.class);
|
|
|
+ verify(productMapper).update(isNull(), captor.capture());
|
|
|
+ String sqlSet = ((LambdaUpdateWrapper<Product>) captor.getValue()).getSqlSet();
|
|
|
+ assertTrue(sqlSet.contains("nutrition_tags"), sqlSet);
|
|
|
+ }
|
|
|
}
|