Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update TypeHandlerTest.java #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.baomidou.mybatisplus.samples.typehandler;

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.samples.typehandler.entity.Currency;
import com.baomidou.mybatisplus.samples.typehandler.entity.User;
import com.baomidou.mybatisplus.samples.typehandler.entity.Wallet;
import com.baomidou.mybatisplus.samples.typehandler.mapper.UserMapper;
import org.junit.jupiter.api.Assertions;
import java.util.List;
import javax.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.Arrays;

/**
* <p>
* 内置 类型处理器 演示
Expand All @@ -23,7 +24,7 @@
* @since 2018-08-11
*/
@SpringBootTest
public class TypeHandlerTest {
class TypeHandlerTest {

@Resource
private UserMapper userMapper;
Expand All @@ -32,27 +33,51 @@ public class TypeHandlerTest {
* 自定义类型处理器演示
*/
@Test
public void test() {
User Jone = userMapper.selectById(1);
Assertions.assertEquals("Jone", Jone.getName());
Assertions.assertEquals(2, Jone.getWallets().size());
Assertions.assertEquals("微信钱包", Jone.getWallets().get(1).getName());

User Jack = userMapper.selectById(2);
Assertions.assertEquals("Jack", Jack.getName());
Assertions.assertEquals(1, Jack.getWallets().size());
Assertions.assertEquals("银联钱包", Jack.getWallets().get(0).getName());

// wrapper typeHandler 测试
LambdaUpdateWrapper<User> wrapper = Wrappers.<User>lambdaUpdate().set(User::getWallets, Arrays.asList(new Wallet("Tom",
Arrays.asList(new Currency("RMB", 1000d)))), "typeHandler=com.baomidou.mybatisplus.samples.typehandler.WalletListTypeHandler");
wrapper.eq(User::getId, 2L);
Assertions.assertEquals(userMapper.update(new User().setAge(99), wrapper), 1);
System.err.println(userMapper.selectById(2));
void testCommon() {
User jone = userMapper.selectById(1);
assertThat(jone)
.hasFieldOrPropertyWithValue("name", "Jone");
assertThat(jone.getWallets())
.hasSize(2);
assertThat(jone.getWallets().get(1))
.hasFieldOrPropertyWithValue("name", "微信钱包");

User jack = userMapper.selectById(2);
assertThat(jack)
.hasFieldOrPropertyWithValue("name", "Jack");
assertThat(jack.getWallets())
.hasSize(1)
.first()
.hasFieldOrPropertyWithValue("name", "银联钱包");

}

@Test
void wrapperTypeHandler() {
List<Wallet> wallets = singletonList(new Wallet("Tom",
singletonList(new Currency("RMB", 1000d))));
LambdaUpdateWrapper<User> wrapper = Wrappers.<User>lambdaUpdate()
.set(User::getWallets,
wallets,
"typeHandler=com.baomidou.mybatisplus.samples.typehandler.WalletListTypeHandler")
.set(User::getAge, 99)
.eq(User::getId, 2L);
assertThat(userMapper.update(null, wrapper))
.isEqualTo(1);

User user = userMapper.selectById(2L);
assertThat(user)
.extracting(User::getWallets)
.isEqualTo(wallets);
}

@Test
void pageTest() {
// 分页测试
Page<User> userPage = userMapper.selectPage(new Page<>(1, 20), null);
Assertions.assertEquals(userPage.getTotal(), 2);
Assertions.assertEquals(userPage.getRecords().size(), 2);
assertThat(userPage)
.extracting(Page::getTotal, page -> page.getRecords().size())
.containsExactly(2L, 2);
}

}