|
|
@@ -0,0 +1,129 @@
|
|
|
+package com.etotem.cfc.controller.stats;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.controller.stats.StatsController;
|
|
|
+import com.etotem.cfc.mapper.*;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
+import org.mockito.junit.jupiter.MockitoSettings;
|
|
|
+import org.mockito.quality.Strictness;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * TDD 契约:/api/stats/dashboard 返回「新/总」统计(有效口径 + 近7天新增)。
|
|
|
+ * 纯 Mockito 单测(@SpringBootTest 需连库,本地不可用),不启动 Spring/DB。
|
|
|
+ * 存根用 argThat 按「状态参数 + created_at 边界」区分 total/new;若实现漏掉任一条件,
|
|
|
+ * 该存根不匹配 -> 返回 0 -> 断言失败。RED:新 key 未实现 -> 值缺失/失败;GREEN:全通过。
|
|
|
+ */
|
|
|
+@ExtendWith(MockitoExtension.class)
|
|
|
+@MockitoSettings(strictness = Strictness.LENIENT)
|
|
|
+public class StatsControllerDashboardContractTest {
|
|
|
+
|
|
|
+ @Mock private ThreadPoolTaskExecutor taskExecutor;
|
|
|
+ @Mock private FamilyMapper familyMapper;
|
|
|
+ @Mock private UserMapper userMapper;
|
|
|
+ @Mock private FamilyMemberMapper familyMemberMapper;
|
|
|
+ @Mock private GuidePackageMapper guidePackageMapper;
|
|
|
+ @Mock private TaskMapper taskMapper;
|
|
|
+ @Mock private AfterSalesRequestMapper afterSalesRequestMapper;
|
|
|
+ @Mock private ProductMapper productMapper;
|
|
|
+ @Mock private ProductOrderMapper productOrderMapper;
|
|
|
+ @Mock private ArticleMapper articleMapper;
|
|
|
+ @Mock private ActivityMapper activityMapper;
|
|
|
+
|
|
|
+ @InjectMocks private StatsController controller;
|
|
|
+
|
|
|
+ // —— wrapper 辅助判断(QueryWrapper 在 controller 中构造)——
|
|
|
+ private static boolean hasCreatedAt(Object w) {
|
|
|
+ return w != null && ((AbstractWrapper) w).getSqlSegment().toUpperCase().contains("CREATED_AT");
|
|
|
+ }
|
|
|
+ private static boolean hasParam(Object w, Object v) {
|
|
|
+ if (w == null) return false;
|
|
|
+ AbstractWrapper aw = (AbstractWrapper) w;
|
|
|
+ aw.getSqlSegment(); // MyBatis-Plus: 参数在渲染 SQL 时才物化到 paramNameValuePairs
|
|
|
+ return aw.getParamNameValuePairs().containsValue(v);
|
|
|
+ }
|
|
|
+ private static boolean hasSql(Object w, String frag) {
|
|
|
+ return w != null && ((AbstractWrapper) w).getSqlSegment().toUpperCase().contains(frag);
|
|
|
+ }
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() {
|
|
|
+ // 让 CompletableFuture.supplyAsync(supplier, taskExecutor) 内联同步执行
|
|
|
+ doAnswer(inv -> { ((Runnable) inv.getArgument(0)).run(); return null; })
|
|
|
+ .when(taskExecutor).execute(any(Runnable.class));
|
|
|
+
|
|
|
+ // 家庭:total=selectCount(null)=120,weekNew=带 created_at=12
|
|
|
+ when(familyMapper.selectCount(isNull())).thenReturn(120L);
|
|
|
+ when(familyMapper.selectCount(argThat(w -> hasCreatedAt(w)))).thenReturn(12L);
|
|
|
+ // 用户(家长账号):total(无created_at)=300,weekNew(带created_at)=30;teacher/pending 复用 total 存根
|
|
|
+ when(userMapper.selectCount(argThat(w -> !hasCreatedAt(w)))).thenReturn(300L);
|
|
|
+ when(userMapper.selectCount(argThat(w -> hasCreatedAt(w)))).thenReturn(30L);
|
|
|
+ // 非用户(userId IS NULL):total=88,weekNew=8
|
|
|
+ when(familyMemberMapper.selectCount(isNull())).thenReturn(0L); // 总孩子数(childFuture,null查询)
|
|
|
+ when(familyMemberMapper.selectCount(argThat(w -> hasSql(w, "IS NULL") && !hasCreatedAt(w)))).thenReturn(88L);
|
|
|
+ when(familyMemberMapper.selectCount(argThat(w -> hasSql(w, "IS NULL") && hasCreatedAt(w)))).thenReturn(8L);
|
|
|
+ // 规划师相关:任一 guidePackage/售后
|
|
|
+ when(guidePackageMapper.selectCount(any())).thenReturn(7L);
|
|
|
+ when(afterSalesRequestMapper.selectCount(any())).thenReturn(3L);
|
|
|
+ // 商品(有效=on_shelf):total=45,weekNew=5
|
|
|
+ when(productMapper.selectCount(argThat(w -> hasParam(w, "on_shelf") && !hasCreatedAt(w)))).thenReturn(45L);
|
|
|
+ when(productMapper.selectCount(argThat(w -> hasParam(w, "on_shelf") && hasCreatedAt(w)))).thenReturn(5L);
|
|
|
+ // 商品订单(有效=排除 cancelled/closed/refunded):total=210,weekNew=21
|
|
|
+ when(productOrderMapper.selectCount(argThat(w -> hasSql(w, "NOT IN") && !hasCreatedAt(w)))).thenReturn(210L);
|
|
|
+ when(productOrderMapper.selectCount(argThat(w -> hasSql(w, "NOT IN") && hasCreatedAt(w)))).thenReturn(21L);
|
|
|
+ // 文章(有效=published):total=60,weekNew=6
|
|
|
+ when(articleMapper.selectCount(argThat(w -> hasParam(w, "published") && !hasCreatedAt(w)))).thenReturn(60L);
|
|
|
+ when(articleMapper.selectCount(argThat(w -> hasParam(w, "published") && hasCreatedAt(w)))).thenReturn(6L);
|
|
|
+ // 活动(有效=published):total=25,weekNew=2
|
|
|
+ when(activityMapper.selectCount(argThat(w -> hasParam(w, "published") && !hasCreatedAt(w)))).thenReturn(25L);
|
|
|
+ when(activityMapper.selectCount(argThat(w -> hasParam(w, "published") && hasCreatedAt(w)))).thenReturn(2L);
|
|
|
+ // 任务列表(最近任务):空
|
|
|
+ when(taskMapper.selectList(any())).thenReturn(Collections.emptyList());
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> call() {
|
|
|
+ Result<Map<String, Object>> r = controller.getDashboardSummary();
|
|
|
+ assertNotNull(r, "返回 Result 不能为 null");
|
|
|
+ assertEquals(200, r.getCode(), "业务码应为200");
|
|
|
+ return r.getData();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void dashboard_returns_newtotal_counts_with_valid_filters() {
|
|
|
+ Map<String, Object> d = call();
|
|
|
+ // 家庭:新/总
|
|
|
+ assertEquals(120L, d.get("totalFamilies"), "家庭总数");
|
|
|
+ assertEquals(12L, d.get("weekNewFamilies"), "近7天新增家庭");
|
|
|
+ // 用户(家长账号):新/总
|
|
|
+ assertEquals(300L, d.get("totalParents"), "用户总数");
|
|
|
+ assertEquals(30L, d.get("weekNewParents"), "近7天新增用户");
|
|
|
+ // 非用户(userId为空成员):新/总
|
|
|
+ assertEquals(88L, d.get("totalNonUsers"), "非用户总数");
|
|
|
+ assertEquals(8L, d.get("weekNewNonUsers"), "近7天新增非用户");
|
|
|
+ // 商品(有效=on_shelf):新/总
|
|
|
+ assertEquals(45L, d.get("totalProducts"), "商品总数(仅上架)");
|
|
|
+ assertEquals(5L, d.get("weekNewProducts"), "近7天新增商品");
|
|
|
+ // 订单(有效=排除已取消/关闭/退款):新/总
|
|
|
+ assertEquals(210L, d.get("totalOrders"), "订单总数(仅有效)");
|
|
|
+ assertEquals(21L, d.get("weekNewOrders"), "近7天新增订单");
|
|
|
+ // 文章(有效=published):新/总
|
|
|
+ assertEquals(60L, d.get("totalArticles"), "文章总数(仅已发布)");
|
|
|
+ assertEquals(6L, d.get("weekNewArticles"), "近7天新增文章");
|
|
|
+ // 活动(有效=published):新/总
|
|
|
+ assertEquals(25L, d.get("totalActivities"), "活动总数(仅发布中)");
|
|
|
+ assertEquals(2L, d.get("weekNewActivities"), "近7天新增活动");
|
|
|
+ }
|
|
|
+}
|