test: 测试 UPDATE JOIN 表使用别名。

This commit is contained in:
王帅 2024-10-05 13:17:09 +08:00
parent c04b6be4dc
commit 539eeb16b8

View File

@ -57,17 +57,17 @@ public class UpdateChainTest implements WithAssertions {
@Before
public void init() {
this.dataSource = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema.sql")
.addScript("data.sql")
.setScriptEncoding("UTF-8")
.build();
.setType(EmbeddedDatabaseType.H2)
.addScript("schema.sql")
.addScript("data.sql")
.setScriptEncoding("UTF-8")
.build();
MybatisFlexBootstrap bootstrap = new MybatisFlexBootstrap()
.setDataSource(DATA_SOURCE_KEY, this.dataSource)
.setLogImpl(StdOutImpl.class)
.addMapper(AccountMapper.class)
.start();
.setDataSource(DATA_SOURCE_KEY, this.dataSource)
.setLogImpl(StdOutImpl.class)
.addMapper(AccountMapper.class)
.start();
DataSourceKey.use(DATA_SOURCE_KEY);
accountMapper = bootstrap.getMapper(AccountMapper.class);
@ -92,54 +92,55 @@ public class UpdateChainTest implements WithAssertions {
@SneakyThrows
public void testUpdateChain() {
UpdateChain.of(Account.class)
.set(Account::getUserName, "张三")
.setRaw(Account::getAge, "age + 1")
.where(Account::getId).eq(1)
.update();
.set(Account::getUserName, "张三")
.setRaw(Account::getAge, "age + 1")
.where(Account::getId).eq(1)
.update();
Account account = accountMapper.selectOneById(1);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String toParse = "2020-01-11";
assertThat(account).isNotNull()
.extracting(
Account::getUserName, Account::getAge,
Account::getSex, Account::getBirthday,
Account::getOptions, Account::getDelete,
Account::getArticles, Account::getTitle)
.containsExactly(
"张*", 19,
SexEnum.TYPE1, format.parse(toParse),
Collections.singletonMap("key", "value1"), false,
Collections.emptyList(), null);
.extracting(
Account::getUserName, Account::getAge,
Account::getSex, Account::getBirthday,
Account::getOptions, Account::getDelete,
Account::getArticles, Account::getTitle)
.containsExactly(
"张*", 19,
SexEnum.TYPE1, format.parse(toParse),
Collections.singletonMap("key", "value1"), false,
Collections.emptyList(), null);
}
@Test
public void testUpdateChain1() {
UpdateChain.of(Account.class)
.set(Account::getAge, ACCOUNT.AGE.add(1))
.where(Account::getId).ge(100)
.and(Account::getAge).eq(18)
.update();
.set(Account::getAge, ACCOUNT.AGE.add(1))
.where(Account::getId).ge(100)
.and(Account::getAge).eq(18)
.update();
List<Account> list = QueryChain.of(accountMapper).list();
assertThat(list).hasSize(2)
.extracting(Account::getId, Account::getUserName, Account::getAge)
.containsExactly(tuple(1L, "张*", 18), tuple(2L, "王麻**叔", 19));
.extracting(Account::getId, Account::getUserName, Account::getAge)
.containsExactly(tuple(1L, "张*", 18), tuple(2L, "王麻**叔", 19));
}
@Test
public void testUpdateChainToSql() {
ArticleTableDef ARTICLE1 = ArticleTableDef.ARTICLE.as("ar");
ArticleTableDef ar = ArticleTableDef.ARTICLE.as("ar");
String sql = UpdateChain.of(Account.class)
.set(ACCOUNT.AGE, 18)
.set(Article::getAccountId, 4)
.leftJoin(ARTICLE1).on(ACCOUNT.ID.eq(ARTICLE1.ACCOUNT_ID))
.where(ACCOUNT.ID.eq(4))
.toSQL();
.set(ACCOUNT.AGE, 18)
.set(Article::getAccountId, 4)
.leftJoin(ar).on(ACCOUNT.ID.eq(ar.ACCOUNT_ID))
.where(ACCOUNT.ID.eq(4))
.toSQL();
String expectSQL = "UPDATE `tb_account` " +
"LEFT JOIN `tb_article` AS `ar` ON `id` = `ar`.`account_id` " +
"SET `age` = 18 , `accountId` = 4 WHERE `id` = 4";
"LEFT JOIN `tb_article` AS `ar` ON `tb_account`.`id` = `ar`.`account_id` " +
"SET `age` = 18 , `accountId` = 4 WHERE `id` = 4";
assertThat(sql).isEqualTo(expectSQL);
}