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