Skip to content

Commit

Permalink
使用新的单元测试方法。
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy0v0 committed Nov 20, 2024
1 parent 6edbeae commit 4ec1a21
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 162 deletions.
3 changes: 3 additions & 0 deletions jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<argLine>--add-opens swift.jdbc/test.container=ALL-UNNAMED</argLine>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Query count(String sql, List<Object> params) {
countParams.addAll(params);
}
return new Query(
"select count(1) from (%s)".formatted(sql),
"select count(1) from (%s) t".formatted(sql),
countParams
);
}
Expand All @@ -28,10 +28,10 @@ public Query paging(String sql, List<Object> params, long pageNumber, long pageS
pagingParams.addAll(params);
}
long offset = pageNumber * pageSize;
pagingParams.add(offset);
pagingParams.add(pageSize);
pagingParams.add(offset);
return new Query(
"select * from (%s) offset ? row fetch first ? row only".formatted(sql),
"select * from (%s) t limit ? offset ?".formatted(sql),
pagingParams
);
}
Expand Down
61 changes: 27 additions & 34 deletions jdbc/src/test/java/test/InsertTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package test;

import com.github.fantasy0v0.swift.jdbc.JDBC;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import test.container.ContainerUtil;
import test.container.JdbcContainer;
import test.container.SwiftJdbcExtension;
import test.exception.WorkException;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
Expand All @@ -20,26 +21,12 @@

import static com.github.fantasy0v0.swift.jdbc.JDBC.transaction;

@ExtendWith(SwiftJdbcExtension.class)
public class InsertTest {

private final Logger log = LoggerFactory.getLogger(InsertTest.class);

private final static JdbcContainer container = JdbcContainer.create(
ContainerUtil.PG, ContainerUtil.PG_LOCATIONS
);

@BeforeAll
static void beforeAll() {
DataSource dataSource = container.start();
JDBC.configuration(dataSource);
}

@AfterAll
static void afterAll() {
container.stop();
}

@Test
@TestTemplate
void test() {
Assertions.assertThrowsExactly(WorkException.class, () -> {
transaction(() -> {
Expand Down Expand Up @@ -68,7 +55,7 @@ insert into student(id, name, status, ext)
});
}

@Test
@TestTemplate
void testBatch() {
List<List<Object>> batchParams = new ArrayList<>();
batchParams.add(List.of(1000, "测试用户1", 0));
Expand All @@ -87,30 +74,36 @@ insert into student(id, name, status)
}
}

@Test
void fetch() {
List<Object[]> result = JDBC.modify("""
@TestTemplate
void fetch(DataSource dataSource) throws SQLException {
String driverClassName = dataSource.unwrap(HikariDataSource.class).getDriverClassName();
if (driverClassName.contains("postgresql")) {
List<Object[]> result = JDBC.modify("""
insert into student(id, name, status)
values(?, ?, ?)
returning id""").fetch(1000L, "测试学生", 0);
Assertions.assertEquals(1, result.size());
Assertions.assertEquals(1000L, result.getFirst()[0]);
Assertions.assertEquals(1, result.size());
Assertions.assertEquals(1000L, result.getFirst()[0]);
}
}

@Test
void testDateTime() {
OffsetDateTime offsetDateTime = OffsetDateTime.of(1500, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
LocalDateTime localDateTime = LocalDateTime.of(1600, 1, 1, 0, 0, 0, 0);
List<Object[]> objects = JDBC.modify("""
@TestTemplate
void testDateTime(DataSource dataSource) throws SQLException {
String driverClassName = dataSource.unwrap(HikariDataSource.class).getDriverClassName();
if (driverClassName.contains("postgresql")) {
OffsetDateTime offsetDateTime = OffsetDateTime.of(1500, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
LocalDateTime localDateTime = LocalDateTime.of(1600, 1, 1, 0, 0, 0, 0);
List<Object[]> objects = JDBC.modify("""
insert into datetime_test(id, date)
values(?, ?) returning date
""").fetch(1, offsetDateTime);
log.debug("value: {}", objects.getFirst()[0]);
log.debug("value: {}", objects.getFirst()[0]);

objects = JDBC.modify("""
objects = JDBC.modify("""
insert into datetime_test(id, date)
values(?, ?) returning date
""").fetch(1, localDateTime);
log.debug("value: {}", objects.getFirst()[0]);
log.debug("value: {}", objects.getFirst()[0]);
}
}
}
23 changes: 6 additions & 17 deletions jdbc/src/test/java/test/JDBCTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import test.container.ContainerUtil;
import test.container.JdbcContainer;
import test.container.SwiftJdbcExtension;

import javax.sql.DataSource;
import java.util.List;
Expand All @@ -16,27 +19,13 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(SwiftJdbcExtension.class)
class JDBCTest {

private final Logger log = LoggerFactory.getLogger(JDBCTest.class);

private final static JdbcContainer container = JdbcContainer.create(
ContainerUtil.PG, ContainerUtil.PG_LOCATIONS
);

@BeforeAll
static void beforeAll() {
DataSource dataSource = container.start();
JDBC.configuration(dataSource);
}

@AfterAll
static void afterAll() {
container.stop();
}

@Test
void testStatement() {
@TestTemplate
void testStatement(DataSource dataSource) {
List<Object[]> data = select("""
select * from student;
""").fetch();
Expand Down
31 changes: 8 additions & 23 deletions jdbc/src/test/java/test/PagingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import com.github.fantasy0v0.swift.jdbc.JDBC;
import com.github.fantasy0v0.swift.jdbc.PageData;
import com.github.fantasy0v0.swift.jdbc.predicate.Predicate;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import test.container.ContainerUtil;
import test.container.JdbcContainer;
import test.container.SwiftJdbcExtension;
import test.vo.Student;

import javax.sql.DataSource;
Expand All @@ -19,27 +18,13 @@
import static com.github.fantasy0v0.swift.jdbc.clauses.Clauses.where;
import static com.github.fantasy0v0.swift.jdbc.predicate.Predicates.exp;

@ExtendWith(SwiftJdbcExtension.class)
class PagingTest {

private final Logger log = LoggerFactory.getLogger(PagingTest.class);

private final static JdbcContainer container = JdbcContainer.create(
ContainerUtil.PG, ContainerUtil.PG_LOCATIONS
);

@BeforeAll
static void beforeAll() {
DataSource dataSource = container.start();
JDBC.configuration(dataSource);
}

@AfterAll
static void afterAll() {
container.stop();
}

@Test
void testWithOutWhere() {
@TestTemplate
void testWithOutWhere(DataSource dataSource) {
PageData<Student> data = select("select * from student")
.paginate(0, 10)
.fetch(Student::from);
Expand Down Expand Up @@ -71,7 +56,7 @@ void testWithOutWhere() {
Assertions.assertEquals(5, data.data().size());
}

@Test
@TestTemplate
void testWithWhere() {
String sql = "select * from student";
Predicate predicate = exp("status = ?", 2);
Expand All @@ -87,7 +72,7 @@ void testWithWhere() {
Assertions.assertEquals(3, data.data().size());
}

@Test
@TestTemplate
void customizeCount() {
PageData<Student> data = select("select * from student")
.paginate(0, 10)
Expand Down
16 changes: 7 additions & 9 deletions jdbc/src/test/java/test/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.github.fantasy0v0.swift.jdbc.predicate.Predicate;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import test.container.ContainerUtil;
import test.container.JdbcTest;
import test.container.SwiftJdbcExtension;
import test.vo.Student;

import javax.sql.DataSource;
Expand All @@ -22,20 +24,13 @@
import static com.github.fantasy0v0.swift.jdbc.predicate.Predicates.and;
import static com.github.fantasy0v0.swift.jdbc.predicate.Predicates.exp;

@ExtendWith(SwiftJdbcExtension.class)
class SelectTest {

private final Logger log = LoggerFactory.getLogger(SelectTest.class);

@TestFactory
List<DynamicTest> testAllDatabase() {
return ContainerUtil.testAllContainers(() -> List.of(
JdbcTest.of("testFetch", this::testFetch),
JdbcTest.of("testFetchOne", this::testFetchOne),
JdbcTest.of("testPredicate", this::testPredicate),
JdbcTest.of("testJson", this::testJson)
));
}

@TestTemplate
void testFetch(DataSource dataSource) {
List<Student> students = select("select * from student").fetch(Student::from);

Expand All @@ -50,11 +45,13 @@ void testFetch(DataSource dataSource) {
}
}

@TestTemplate
void testFetchOne(DataSource dataSource) {
Object[] row = select("select * from student limit 1").fetchOne();
Assertions.assertNotNull(row);
}

@TestTemplate
void testPredicate(DataSource dataSource) {
String sql = "select * from student";
Predicate predicate = and(
Expand All @@ -71,6 +68,7 @@ void testPredicate(DataSource dataSource) {
Assertions.assertTrue(students.stream().allMatch(student -> 2 == student.status()));
}

@TestTemplate
void testJson(DataSource dataSource) throws SQLException {
String driverClassName = dataSource.unwrap(HikariDataSource.class).getDriverClassName();
if (driverClassName.contains("postgresql")) {
Expand Down
23 changes: 6 additions & 17 deletions jdbc/src/test/java/test/TransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,24 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import test.container.ContainerUtil;
import test.container.JdbcContainer;
import test.container.SwiftJdbcExtension;

import javax.sql.DataSource;

import static com.github.fantasy0v0.swift.jdbc.JDBC.*;

@ExtendWith(SwiftJdbcExtension.class)
public class TransactionTest {

private final Logger log = LoggerFactory.getLogger(TransactionTest.class);

private final static JdbcContainer container = JdbcContainer.create(
ContainerUtil.PG, ContainerUtil.PG_LOCATIONS
);

@BeforeAll
static void beforeAll() {
DataSource dataSource = container.start();
JDBC.configuration(dataSource);
}

@AfterAll
static void afterAll() {
container.stop();
}

@Test
@TestTemplate
void test() {
// 不能在事务交易过程中改变事物交易隔绝等级
// transaction(() -> {
Expand All @@ -58,7 +47,7 @@ void test() {
});
}

@Test
@TestTemplate
void rollback() {
transaction(() -> {
modify("update student set name = ? where id = ?")
Expand Down
Loading

0 comments on commit 4ec1a21

Please sign in to comment.