refactor: 动态数据源单元测试优化

This commit is contained in:
haoxr
2023-04-29 17:49:07 +08:00
parent cbd1621935
commit 1b1888c36d
4 changed files with 5 additions and 11 deletions

View File

@@ -24,7 +24,7 @@ public class DynamicDataSourceSample {
@Transactional @Transactional
public boolean updateDictTypeCode(Long dictTypeId,String newTypeCode) { public boolean updateMaster(Long dictTypeId,String newTypeCode) {
SysDictType dictType = dictTypeService.getById(dictTypeId); SysDictType dictType = dictTypeService.getById(dictTypeId);
String originalTypeCode = dictType.getCode(); String originalTypeCode = dictType.getCode();
@@ -32,7 +32,7 @@ public class DynamicDataSourceSample {
boolean result = dictTypeService.updateById(dictType); boolean result = dictTypeService.updateById(dictType);
if (result) { if (result) {
result = dictService.updateDictTypeCode(originalTypeCode, newTypeCode); result = dictService.updateSlave(originalTypeCode, newTypeCode);
} }
return result; return result;
} }

View File

@@ -73,5 +73,5 @@ public interface SysDictService extends IService<SysDict> {
* @param newTypeCode * @param newTypeCode
* @return * @return
*/ */
boolean updateDictTypeCode(String originalTypeCode, String newTypeCode); boolean updateSlave(String originalTypeCode, String newTypeCode);
} }

View File

@@ -170,7 +170,7 @@ public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> impl
*/ */
@DS("slave") @DS("slave")
@Override @Override
public boolean updateDictTypeCode(String originalTypeCode, String newTypeCode) { public boolean updateSlave(String originalTypeCode, String newTypeCode) {
boolean result = this.update(new LambdaUpdateWrapper<SysDict>() boolean result = this.update(new LambdaUpdateWrapper<SysDict>()
.eq(SysDict::getTypeCode, originalTypeCode) .eq(SysDict::getTypeCode, originalTypeCode)
.set(SysDict::getTypeCode, newTypeCode) .set(SysDict::getTypeCode, newTypeCode)

View File

@@ -1,17 +1,11 @@
package com.youlai.system; package com.youlai.system;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.youlai.system.pojo.form.RoleForm;
import com.youlai.system.pojo.form.UserForm;
import com.youlai.system.sample.DynamicDataSourceSample; import com.youlai.system.sample.DynamicDataSourceSample;
import com.youlai.system.service.SysRoleService;
import com.youlai.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
@SpringBootTest @SpringBootTest
@Slf4j @Slf4j
@@ -24,7 +18,7 @@ class DynamicDataSourceTest {
@Test @Test
void testDynamicDataSourceWithTransactional() { void testDynamicDataSourceWithTransactional() {
String newTypeCod = RandomUtil.randomString(RandomUtil.BASE_CHAR, 6).toUpperCase(); String newTypeCod = RandomUtil.randomString(RandomUtil.BASE_CHAR, 6).toUpperCase();
boolean result = dynamicDataSourceSample.updateDictTypeCode(2L, newTypeCod); boolean result = dynamicDataSourceSample.updateMaster(2L, newTypeCod);
log.info("testDynamicDataSourceWithTransactional result:{}", result); log.info("testDynamicDataSourceWithTransactional result:{}", result);
} }
} }