testcontainers reuse

This commit is contained in:
Martin7-1 2024-01-06 15:19:52 +08:00
parent b71c6587d8
commit 0fe079c0f4
No known key found for this signature in database
GPG Key ID: CD208062112C4569
12 changed files with 60 additions and 41 deletions

View File

@ -95,6 +95,12 @@
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -0,0 +1,33 @@
package com.mybatisflex.test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;
import java.util.Collections;
public class TestInfrastructure {
static final String FLEX_TEST_DDL = TestInfrastructure.class.getClassLoader().getResource("flex_test.sql").getPath();
static final String FIELD_MAPPING_TEST_DDL = TestInfrastructure.class.getClassLoader().getResource("field_mapping_test.sql").getPath();
static final String PATIENT_DATA_SPLIT_TEST_DDL = TestInfrastructure.class.getClassLoader().getResource("patient_data_split_test.sql").getPath();
static final String DOCKER_INITDB_PATH = "/docker-entrypoint-initdb.d/";
static GenericContainer<?> mysql = new MySQLContainer<>(DockerImageName.parse("mysql:8.2.0"))
.waitingFor(Wait.forLogMessage(".*ready for connections.*\\n", 1))
.withFileSystemBind(FLEX_TEST_DDL, DOCKER_INITDB_PATH + "flex_test.sql", BindMode.READ_ONLY)
.withFileSystemBind(FIELD_MAPPING_TEST_DDL, DOCKER_INITDB_PATH + "field_mapping_test.sql", BindMode.READ_ONLY)
.withFileSystemBind(PATIENT_DATA_SPLIT_TEST_DDL, DOCKER_INITDB_PATH + "patient_data_split_test.sql", BindMode.READ_ONLY)
.withDatabaseName("flex_test")
.withReuse(true)
.withPassword("123456");
@BeforeAll
public static void start() {
mysql.setPortBindings(Collections.singletonList("3306:3306"));
mysql.start();
}
}

View File

@ -19,18 +19,16 @@ package com.mybatisflex.test.mapper;
import com.mybatisflex.core.logicdelete.LogicDeleteManager; import com.mybatisflex.core.logicdelete.LogicDeleteManager;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db; import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row; import com.mybatisflex.test.TestInfrastructure;
import com.mybatisflex.test.model.Account; import com.mybatisflex.test.model.Account;
import com.mybatisflex.test.model.AccountVO; import com.mybatisflex.test.model.AccountVO;
import com.mybatisflex.test.model.AccountVO2; import com.mybatisflex.test.model.AccountVO2;
import lombok.val;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
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 java.util.Date; import java.util.Date;
import java.util.List;
import static com.mybatisflex.core.query.QueryMethods.*; import static com.mybatisflex.core.query.QueryMethods.*;
import static com.mybatisflex.test.model.table.AccountTableDef.ACCOUNT; import static com.mybatisflex.test.model.table.AccountTableDef.ACCOUNT;
@ -44,7 +42,7 @@ import static com.mybatisflex.test.model.table.UserTableDef.USER;
*/ */
@SpringBootTest @SpringBootTest
@SuppressWarnings("all") @SuppressWarnings("all")
class AccountMapperTest { class AccountMapperTest extends TestInfrastructure {
@Autowired @Autowired
private AccountMapper accountMapper; private AccountMapper accountMapper;

View File

@ -18,6 +18,7 @@ package com.mybatisflex.test.mapper;
import com.mybatisflex.core.mybatis.Mappers; import com.mybatisflex.core.mybatis.Mappers;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.test.TestInfrastructure;
import com.mybatisflex.test.model.Good; import com.mybatisflex.test.model.Good;
import com.mybatisflex.test.model.User; import com.mybatisflex.test.model.User;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
@ -34,7 +35,7 @@ import static com.mybatisflex.test.model.table.UserTableDef.USER;
* @since 2023-07-23 * @since 2023-07-23
*/ */
@SpringBootTest @SpringBootTest
class ActiveRecordTest { class ActiveRecordTest extends TestInfrastructure {
@Test @Test
void testMapper() { void testMapper() {

View File

@ -17,6 +17,7 @@
package com.mybatisflex.test.mapper; package com.mybatisflex.test.mapper;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.test.TestInfrastructure;
import com.mybatisflex.test.model.Account; import com.mybatisflex.test.model.Account;
import com.mybatisflex.test.model.Article; import com.mybatisflex.test.model.Article;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -26,7 +27,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import java.util.List; import java.util.List;
@SpringBootTest @SpringBootTest
public class CombinedMapperTest { public class CombinedMapperTest extends TestInfrastructure {
@Autowired @Autowired
private AccountMapper accountMapper; private AccountMapper accountMapper;

View File

@ -16,6 +16,7 @@
package com.mybatisflex.test.mapper; package com.mybatisflex.test.mapper;
import com.mybatisflex.test.TestInfrastructure;
import com.mybatisflex.test.model.Account; import com.mybatisflex.test.model.Account;
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;
@ -32,7 +33,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
* @since 2023/4/24 19:37 * @since 2023/4/24 19:37
*/ */
@SpringBootTest @SpringBootTest
class MyAccountMapperTest { class MyAccountMapperTest extends TestInfrastructure {
@Autowired @Autowired
private MyAccountMapper mapper; private MyAccountMapper mapper;

View File

@ -17,7 +17,7 @@
package com.mybatisflex.test.mapper; package com.mybatisflex.test.mapper;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.test.entity.Inner; import com.mybatisflex.test.TestInfrastructure;
import com.mybatisflex.test.entity.Outer; import com.mybatisflex.test.entity.Outer;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -32,7 +32,7 @@ import static com.mybatisflex.test.entity.table.OuterTableDef.OUTER;
* @since 2023-07-01 * @since 2023-07-01
*/ */
@SpringBootTest @SpringBootTest
class OuterMapperTest { class OuterMapperTest extends TestInfrastructure {
@Autowired @Autowired
private OuterMapper outerMapper; private OuterMapper outerMapper;

View File

@ -1,5 +1,6 @@
package com.mybatisflex.test.mapper; package com.mybatisflex.test.mapper;
import com.mybatisflex.test.TestInfrastructure;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@ -13,7 +14,7 @@ import javax.annotation.Resource;
*/ */
@SpringBootTest @SpringBootTest
@SuppressWarnings("all") @SuppressWarnings("all")
public class PatientMapperTest { public class PatientMapperTest extends TestInfrastructure {
@Resource @Resource
private PatientMapper patientMapper; private PatientMapper patientMapper;

View File

@ -20,20 +20,13 @@ import com.mybatisflex.core.field.QueryBuilder;
import com.mybatisflex.core.query.QueryChain; import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain; import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.test.TestInfrastructure;
import com.mybatisflex.test.model.Gender; import com.mybatisflex.test.model.Gender;
import com.mybatisflex.test.model.User; import com.mybatisflex.test.model.User;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
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.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;
import java.util.Collections;
import static com.mybatisflex.test.model.table.RoleTableDef.ROLE; import static com.mybatisflex.test.model.table.RoleTableDef.ROLE;
import static com.mybatisflex.test.model.table.UserRoleTableDef.USER_ROLE; import static com.mybatisflex.test.model.table.UserRoleTableDef.USER_ROLE;
@ -44,30 +37,11 @@ import static com.mybatisflex.test.model.table.UserTableDef.USER;
* @since 2023-08-08 * @since 2023-08-08
*/ */
@SpringBootTest @SpringBootTest
class QueryChainTest { class QueryChainTest extends TestInfrastructure {
static final String TEST_DDL = QueryChainTest.class.getClassLoader().getResource("flex_test.sql").getPath();
static GenericContainer<?> mysql = new GenericContainer<>(DockerImageName.parse("mysql:8.2.0"))
.waitingFor(Wait.forLogMessage(".*ready for connections.*\\n", 1))
// bind DDL to container /docker-entrypoint-initdb.d and it will execute automatically
.withFileSystemBind(TEST_DDL, "/docker-entrypoint-initdb.d/flex_test.sql", BindMode.READ_ONLY)
.withEnv("MYSQL_DATABASE", "flex_test")
.withEnv("MYSQL_ROOT_PASSWORD", "123456");
@Autowired @Autowired
UserMapper userMapper; UserMapper userMapper;
@BeforeAll
public static void start() {
mysql.setPortBindings(Collections.singletonList("3306:3306"));
mysql.start();
}
@AfterAll
public static void stop() {
mysql.stop();
}
@Test @Test
void testFields() { void testFields() {
QueryBuilder<User> builder = user -> QueryBuilder<User> builder = user ->

View File

@ -18,6 +18,7 @@ package com.mybatisflex.test.mapper;
import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.test.TestInfrastructure;
import com.mybatisflex.test.model.*; import com.mybatisflex.test.model.*;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -42,7 +43,7 @@ import static com.mybatisflex.test.model.table.UserTableDef.USER;
*/ */
@SpringBootTest @SpringBootTest
@SuppressWarnings("all") @SuppressWarnings("all")
class UserMapperTest { class UserMapperTest extends TestInfrastructure {
@Autowired @Autowired
private UserMapper userMapper; private UserMapper userMapper;

View File

@ -17,6 +17,7 @@
package com.mybatisflex.test.service; package com.mybatisflex.test.service;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.test.TestInfrastructure;
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;
@ -28,7 +29,7 @@ import static com.mybatisflex.test.model.table.ArticleTableDef.ARTICLE;
* @since 2023-07-22 * @since 2023-07-22
*/ */
@SpringBootTest @SpringBootTest
class ArticleServiceTest { class ArticleServiceTest extends TestInfrastructure {
@Autowired @Autowired
ArticleService articleService; ArticleService articleService;

View File

@ -2,6 +2,7 @@ package com.mybatisflex.test.service;
import com.mybatisflex.test.mapper.FieldMappingInnerMapper; import com.mybatisflex.test.mapper.FieldMappingInnerMapper;
import com.mybatisflex.test.mapper.FieldMappingMapper; import com.mybatisflex.test.mapper.FieldMappingMapper;
import com.mybatisflex.test.TestInfrastructure;
import com.mybatisflex.test.model.FieldMapping; import com.mybatisflex.test.model.FieldMapping;
import com.mybatisflex.test.model.FieldMappingInner; import com.mybatisflex.test.model.FieldMappingInner;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -11,11 +12,12 @@ import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date; import java.util.Date;
@SpringBootTest @SpringBootTest
public class FieldMappingTest { class FieldMappingTest extends TestInfrastructure {
@Autowired @Autowired
FieldMappingMapper fieldMappingMapper; FieldMappingMapper fieldMappingMapper;
@Autowired @Autowired
FieldMappingInnerMapper fieldMappingInnerMapper; FieldMappingInnerMapper fieldMappingInnerMapper;
@Test @Test
void testFieldMapping() { void testFieldMapping() {
String fieldId = FieldMapping.create().saveOpt().get().getId(); String fieldId = FieldMapping.create().saveOpt().get().getId();