|
|
@@ -0,0 +1,82 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.etotem.cfc.controller.admin.AdminController;
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
+import org.junit.jupiter.api.BeforeAll;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测:管理端孩子列表支持按 familyId 过滤(能力键 admin.children.familyFilter)。
|
|
|
+ *
|
|
|
+ * <p>需求/设计锚点:{@code docs/superpowers/specs/2026-11-15-family-workbench-tabs-design.md} 改造3——
|
|
|
+ * 家庭工作台 tab「孩子管理」需锁定当前家庭,POST /api/admin/children 必须支持 {familyId} 过滤;
|
|
|
+ * 不传 familyId 时保持全量(向后兼容,现有管理端全局孩子列表不受影响)。</p>
|
|
|
+ */
|
|
|
+class AdminChildrenFamilyFilterContractTest {
|
|
|
+
|
|
|
+ private final FamilyMemberMapper mapper = mock(FamilyMemberMapper.class);
|
|
|
+ private AdminController controller;
|
|
|
+
|
|
|
+ @BeforeAll
|
|
|
+ static void initMybatisPlusTableInfo() {
|
|
|
+ MapperBuilderAssistant assistant = new MapperBuilderAssistant(new MybatisConfiguration(), "");
|
|
|
+ TableInfoHelper.initTableInfo(assistant, FamilyMember.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ controller = new AdminController();
|
|
|
+ Field f = AdminController.class.getDeclaredField("familyMemberMapper");
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(controller, mapper);
|
|
|
+ when(mapper.selectPage(any(Page.class), any())).thenReturn(new Page<>());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流:POST /api/admin/children 传 familyId 时按 family_id 过滤(tab 锁定家庭)")
|
|
|
+ void familyIdFiltersWrapper() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("page", 1);
|
|
|
+ params.put("size", 10);
|
|
|
+ params.put("familyId", 5001);
|
|
|
+ controller.getChildren(params, "admin", null);
|
|
|
+ verify(mapper).selectPage(
|
|
|
+ any(Page.class),
|
|
|
+ org.mockito.ArgumentMatchers.argThat(w -> {
|
|
|
+ if (!(w instanceof LambdaQueryWrapper)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ LambdaQueryWrapper<?> lw = (LambdaQueryWrapper<?>) w;
|
|
|
+ String sql = lw.getSqlSegment() == null ? "" : lw.getSqlSegment();
|
|
|
+ return sql.contains("family_id") && sql.contains("=");
|
|
|
+ })
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("兼容流:不传 familyId 时 wrapper 为 null(全局列表行为不变)")
|
|
|
+ void noFamilyIdKeepsAll() {
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("page", 1);
|
|
|
+ params.put("size", 10);
|
|
|
+ controller.getChildren(params, "admin", null);
|
|
|
+ verify(mapper).selectPage(any(Page.class), org.mockito.ArgumentMatchers.isNull());
|
|
|
+ }
|
|
|
+}
|