Skip to content

Commit

Permalink
完善UpdateTest。
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy0v0 committed Nov 13, 2024
1 parent a01dbf1 commit 35111b3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 69 deletions.
5 changes: 3 additions & 2 deletions idea/Language Injections.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
<injection language="SQL" injector-id="java">
<display-name>JDBC (com.github.fantasy0v0.swift.jdbc)</display-name>
<single-file value="false" />
<place><![CDATA[psiParameter().ofMethod(0, psiMethod().withName("modify").withParameters("java.lang.String").definedInClass("com.github.fantasy0v0.swift.jdbc.JDBC"))]]></place>
<place><![CDATA[psiParameter().ofMethod(0, psiMethod().withName("select").withParameters("java.lang.String", "java.lang.Object...").definedInClass("com.github.fantasy0v0.swift.jdbc.JDBC"))]]></place>
<place><![CDATA[psiParameter().ofMethod(0, psiMethod().withName("select").withParameters("java.lang.String", "java.util.List").definedInClass("com.github.fantasy0v0.swift.jdbc.JDBC"))]]></place>
<place><![CDATA[psiParameter().ofMethod(0, psiMethod().withName("insert").withParameters("java.lang.String").definedInClass("com.github.fantasy0v0.swift.jdbc.JDBC"))]]></place>
<place><![CDATA[psiParameter().ofMethod(0, psiMethod().withName("update").withParameters("java.lang.String").definedInClass("com.github.fantasy0v0.swift.jdbc.JDBC"))]]></place>
</injection>
<injection language="SQL" injector-id="java">
<display-name>PaginateBuilder.count (com.github.fantasy0v0.swift.jdbc)</display-name>
<single-file value="false" />
<place><![CDATA[psiParameter().ofMethod(0, psiMethod().withName("count").withParameters("java.lang.String", "java.lang.Object...").definedInClass("com.github.fantasy0v0.swift.jdbc.PaginateBuilder"))]]></place>
</injection>
</LanguageInjectionConfiguration>
</LanguageInjectionConfiguration>
5 changes: 5 additions & 0 deletions jdbc/src/main/java/com/github/fantasy0v0/swift/jdbc/JDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,15 @@ public static SelectBuilder select(String sql, Object... params) {
return select(sql, Arrays.stream(params).toList());
}

@Deprecated
public static ModifyBuilder modify(String sql) {
return new ModifyBuilder(requireNonNull(dataSource), sql.trim());
}

public static UpdateBuilder update(String sql) {
return new UpdateBuilder(requireNonNull(dataSource), statementConfiguration, sql.trim());
}

public static void transaction(Integer level, Runnable runnable) {
TransactionBuilder<?> builder = TransactionBuilder.create(dataSource, level, runnable);
try {
Expand Down
54 changes: 25 additions & 29 deletions jdbc/src/test/java/test/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

import com.github.fantasy0v0.swift.jdbc.JDBC;
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 com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import test.container.ContainerUtil;
import test.container.JdbcContainer;
import test.container.JdbcTest;
import test.vo.Student;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -28,24 +28,17 @@ class SelectTest {

private final Logger log = LoggerFactory.getLogger(SelectTest.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();
@TestFactory
List<DynamicTest> testAllDatabase() {
return ContainerUtil.testAllContainers(() -> List.of(
new JdbcTest("testFetch", this::testFetch),
new JdbcTest("testFetchOne", this::testFetchOne),
new JdbcTest("testPredicate", this::testPredicate),
new JdbcTest("testJson", this::testJson)
));
}

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

for (Student student : students) {
Expand All @@ -57,13 +50,14 @@ void testFetch() {
String row = Arrays.stream(array).map(Object::toString).collect(Collectors.joining(", "));
log.debug("row: {}", row);
}
}

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

@Test
void testPredicate() {
void testPredicate(DataSource dataSource) {
String sql = "select * from student";
List<Object> parameters = new ArrayList<>();
Predicate predicate = and(
Expand All @@ -80,12 +74,14 @@ void testPredicate() {
Assertions.assertTrue(students.stream().allMatch(student -> 2 == student.status()));
}

@Test
void testJson() {
List<String> result = select("""
select '{ "test": 123}'::jsonb
""").fetch(row -> row.getString(1));
Assertions.assertEquals(1, result.size());
Assertions.assertEquals("{\"test\": 123}", result.getFirst());
void testJson(DataSource dataSource) throws SQLException {
String driverClassName = dataSource.unwrap(HikariDataSource.class).getDriverClassName();
if (driverClassName.contains("postgresql")) {
List<String> result = select("""
select '{ "test": 123}'::jsonb
""").fetch(row -> row.getString(1));
Assertions.assertEquals(1, result.size());
Assertions.assertEquals("{\"test\": 123}", result.getFirst());
}
}
}
80 changes: 42 additions & 38 deletions jdbc/src/test/java/test/UpdateTest.java
Original file line number Diff line number Diff line change
@@ -1,67 +1,71 @@
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import test.container.ContainerUtil;
import test.container.JdbcContainer;
import test.container.JdbcTest;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

public class UpdateTest {

private final Logger log = LoggerFactory.getLogger(UpdateTest.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();
@TestFactory
List<DynamicTest> testAllDatabase() {
return ContainerUtil.testAllContainers(() -> List.of(
new JdbcTest("test", this::test),
new JdbcTest("testArrayParams", this::testArrayParams),
new JdbcTest("testExecuteBatch", this::testExecuteBatch)
));
}

@Test
void test() {
int executed = JDBC.modify("""
update student set name = ? where id = ?
""").execute("测试修改", 1);
void test(DataSource dataSource) throws SQLException {
String driverClassName = dataSource.unwrap(HikariDataSource.class).getDriverClassName();
int executed = JDBC.update("""
update student set name = ? where id = ?
""").execute("测试修改", 1);
Assertions.assertEquals(1, executed);
executed = JDBC.modify("""
update student set name = ? where id = ?
""").execute("测试修改", 1);
executed = JDBC.update("""
update student set name = ? where id = ?
""").execute("测试修改", 1);
Assertions.assertEquals(1, executed);

List<Object[]> result = JDBC.modify("""
update student set name = ? where id = ? returning id
if (driverClassName.contains("postgresql")) {
List<Object[]> result = JDBC.update("""
update student set name = ? where id = ? returning id
""").fetch("测试修改1", 1);
Assertions.assertEquals(1, result.size());
Assertions.assertEquals(1L, result.getFirst()[0]);
Assertions.assertEquals(1, result.size());
Assertions.assertEquals(1L, result.getFirst()[0]);

result = JDBC.update("""
update student set name = ? returning id
""").fetch("测试修改1");
Assertions.assertTrue(result.size() > 1);
}

result = JDBC.modify("""
update student set name = ? returning id
""").fetch("测试修改1");
Assertions.assertTrue(result.size() > 1);
}

@Test
void testArrayParams() {
void testArrayParams(DataSource dataSource) {
Object[] params = {"测试修改", 1};
int executed = JDBC.modify("""
update student set name = ? where id = ?
""").execute(params);
int executed = JDBC.update("""
update student set name = ? where id = ?
""").execute(params);
Assertions.assertEquals(1, executed);
}

void testExecuteBatch(DataSource dataSource) {
JDBC.update("""
update student set name = ? where id = ?
""").executeBatch(
List.of(List.of("测试修改1", 1), List.of("测试修改2", 2))
);
}

}
2 changes: 2 additions & 0 deletions jdbc/src/test/java/test/container/ContainerUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package test.container;

import com.github.fantasy0v0.swift.jdbc.JDBC;
import org.junit.jupiter.api.DynamicTest;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.PostgreSQLContainer;
Expand Down Expand Up @@ -34,6 +35,7 @@ public static List<DynamicTest> testAllContainers(ContainerExecutable executable
.stream()
.map(test -> dynamicTest(name + " " + test.name(), () -> {
DataSource dataSource = container.start();
JDBC.configuration(dataSource);
try {
test.executable().execute(dataSource);
} finally {
Expand Down

0 comments on commit 35111b3

Please sign in to comment.