|
|
@@ -0,0 +1,111 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.etotem.cfc.entity.Family;
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.entity.GuideFamily;
|
|
|
+import com.etotem.cfc.mapper.FamilyMapper;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.mapper.GuideFamilyMapper;
|
|
|
+import com.etotem.cfc.service.GuideFamilyQueryService;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+class GuideFamilyQueryServiceTest {
|
|
|
+
|
|
|
+ private GuideFamilyQueryService service;
|
|
|
+ private GuideFamilyMapper gfMapper;
|
|
|
+ private FamilyMapper familyMapper;
|
|
|
+ private FamilyMemberMapper fmMapper;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ service = new GuideFamilyQueryService();
|
|
|
+ gfMapper = mock(GuideFamilyMapper.class);
|
|
|
+ familyMapper = mock(FamilyMapper.class);
|
|
|
+ fmMapper = mock(FamilyMemberMapper.class);
|
|
|
+ set("guideFamilyMapper", gfMapper);
|
|
|
+ set("familyMapper", familyMapper);
|
|
|
+ set("familyMemberMapper", fmMapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void set(String n, Object v) throws Exception {
|
|
|
+ Field f = GuideFamilyQueryService.class.getDeclaredField(n);
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(service, v);
|
|
|
+ }
|
|
|
+
|
|
|
+ private GuideFamily binding(long familyId) {
|
|
|
+ GuideFamily gf = new GuideFamily();
|
|
|
+ gf.setFamilyId(familyId);
|
|
|
+ gf.setServiceType("paid");
|
|
|
+ gf.setServicePrice(199);
|
|
|
+ return gf;
|
|
|
+ }
|
|
|
+
|
|
|
+ private FamilyMember member(long familyId) {
|
|
|
+ FamilyMember m = new FamilyMember();
|
|
|
+ m.setId(1L);
|
|
|
+ m.setFamilyId(familyId);
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 无绑定时返回空列表() {
|
|
|
+ when(gfMapper.selectList(any(QueryWrapper.class))).thenReturn(Collections.emptyList());
|
|
|
+ assertTrue(service.listMyBoundFamilies(1L).isEmpty());
|
|
|
+ verify(familyMapper, never()).selectBatchIds(any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 返回绑定家庭的名称与成员数() {
|
|
|
+ when(gfMapper.selectList(any(QueryWrapper.class))).thenReturn(Collections.singletonList(binding(10L)));
|
|
|
+
|
|
|
+ Family family = new Family();
|
|
|
+ family.setId(10L);
|
|
|
+ family.setName("张家");
|
|
|
+ when(familyMapper.selectBatchIds(any())).thenReturn(Collections.singletonList(family));
|
|
|
+ when(fmMapper.selectList(any(QueryWrapper.class)))
|
|
|
+ .thenReturn(Arrays.asList(member(10L), member(10L), member(10L)));
|
|
|
+
|
|
|
+ List<Map<String, Object>> out = service.listMyBoundFamilies(1L);
|
|
|
+
|
|
|
+ assertEquals(1, out.size());
|
|
|
+ assertEquals(10L, out.get(0).get("familyId"));
|
|
|
+ assertEquals("张家", out.get(0).get("familyName"));
|
|
|
+ assertEquals(3, out.get(0).get("memberCount"));
|
|
|
+ assertEquals("paid", out.get(0).get("serviceType"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 多家庭成员数分别统计() {
|
|
|
+ when(gfMapper.selectList(any(QueryWrapper.class)))
|
|
|
+ .thenReturn(Arrays.asList(binding(10L), binding(20L)));
|
|
|
+ when(familyMapper.selectBatchIds(any())).thenReturn(Collections.emptyList());
|
|
|
+ when(fmMapper.selectList(any(QueryWrapper.class)))
|
|
|
+ .thenReturn(Arrays.asList(member(10L), member(10L), member(20L)));
|
|
|
+
|
|
|
+ List<Map<String, Object>> out = service.listMyBoundFamilies(1L);
|
|
|
+
|
|
|
+ assertEquals(2, out.size());
|
|
|
+ assertEquals(2, out.get(0).get("memberCount"));
|
|
|
+ assertEquals(1, out.get(1).get("memberCount"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 成员列表按familyId查询() {
|
|
|
+ when(fmMapper.selectList(any(QueryWrapper.class))).thenReturn(Collections.emptyList());
|
|
|
+ assertNotNull(service.listFamilyMembers(10L));
|
|
|
+ verify(fmMapper).selectList(any(QueryWrapper.class));
|
|
|
+ }
|
|
|
+}
|