!311 test:添加测试

Merge pull request !311 from 王帅/main
This commit is contained in:
Michael Yang 2023-08-25 09:30:43 +00:00 committed by Gitee
commit cb2cebdff6
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 91 additions and 2 deletions

View File

@ -18,6 +18,7 @@ package com.mybatisflex.coretest;
import com.mybatisflex.core.keygen.IKeyGenerator;
import com.mybatisflex.core.keygen.KeyGeneratorFactory;
import com.mybatisflex.core.keygen.KeyGenerators;
import org.junit.Assert;
import org.junit.Test;
@ -33,9 +34,20 @@ public class IdGenTest {
@Test
public void snowFlakeID() {
int size = 10;
int size = 10_0000;
long[] ids = new long[size];
IKeyGenerator keyGenerator = KeyGeneratorFactory.getKeyGenerator("snowFlakeId");
IKeyGenerator keyGenerator = KeyGeneratorFactory.getKeyGenerator(KeyGenerators.snowFlakeId);
for (int i = 0; i < size; i++) {
ids[i] = (Long) keyGenerator.generate(null, null);
}
Assert.assertEquals(size, LongStream.of(ids).distinct().count());
}
@Test
public void flexID() {
int size = 100_0000;
long[] ids = new long[size];
IKeyGenerator keyGenerator = KeyGeneratorFactory.getKeyGenerator(KeyGenerators.flexId);
for (int i = 0; i < size; i++) {
ids[i] = (Long) keyGenerator.generate(null, null);
}

View File

@ -0,0 +1,77 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.test;
import com.mybatisflex.core.MybatisFlexBootstrap;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.RowUtil;
import com.mybatisflex.core.util.SqlUtil;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @author 王帅
* @since 2023-08-25
*/
public class BatchTester {
@BeforeClass
public static void init() {
DataSource dataSource = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema.sql")
.addScript("data.sql")
.build();
MybatisFlexBootstrap.getInstance()
.setDataSource(dataSource)
.setLogImpl(StdOutImpl.class)
.addMapper(AccountMapper.class)
.start();
}
@Test
public void testBatch() {
List<Account> accounts = initAccounts();
int[] ints = Db.executeBatch(accounts, AccountMapper.class, AccountMapper::insertSelective);
System.out.println(Arrays.toString(ints));
System.out.println(SqlUtil.toBool(ints));
RowUtil.printPretty(Db.selectAll("tb_account"));
}
private static List<Account> initAccounts() {
List<Account> accounts = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Account account = new Account();
account.setUserName("wangshuai" + i);
account.setAge(168 + i);
account.setBirthday(new Date());
accounts.add(account);
}
return accounts;
}
}