1
0

fix(jpa): 优化测试用例

This commit is contained in:
2026-01-22 22:36:57 +08:00
parent 7b555492ee
commit 015016a2da

View File

@@ -16,13 +16,13 @@ import com.lanyuanxiaoyao.service.template.database.jpa.repository.EmployeeRepos
import com.lanyuanxiaoyao.service.template.database.jpa.repository.ReportRepository;
import com.querydsl.core.types.dsl.CaseBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Session;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
@@ -44,7 +44,8 @@ public class TestApplication extends AbstractTestApplication {
private final CompanyRepository companyRepository;
private final EmployeeRepository employeeRepository;
private final ReportRepository reportRepository;
private final Session session;
private final EntityManager manager;
private final JPAQueryFactory factory;
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
@@ -88,8 +89,6 @@ public class TestApplication extends AbstractTestApplication {
}
private void testQuery() {
var factory = new JPAQueryFactory(session);
formatLog("准备 Specification 查询的测试数据");
var company1 = companyRepository.save(Company.builder().name("TechCorp").members(100).build());
var company2 = companyRepository.save(Company.builder().name("DataInc").members(50).build());
@@ -192,7 +191,7 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(result1_querydsl.size() == 1, "基本比较操作符查询失败 %d".formatted(result1_querydsl.size()));
formatLog("1. 基本比较操作符查询 HQL");
var result1_hql = session.createQuery(
var result1_hql = manager.createQuery(
"""
from Employee employee
where employee.name = 'Bob'
@@ -203,7 +202,7 @@ public class TestApplication extends AbstractTestApplication {
and employee.salary <= 45000.00
""",
Employee.class
).list();
).getResultList();
Assert.isTrue(result1_hql.size() == 1, "基本比较操作符查询失败 %d".formatted(result1_hql.size()));
formatLog("2. 区间和集合操作符查询 JPA");
@@ -235,13 +234,12 @@ public class TestApplication extends AbstractTestApplication {
.and(QEmployee.employee.age.between(40, 50).not())
.and(QEmployee.employee.age.in(25, 30, 35))
.and(QEmployee.employee.role.in(Employee.Role.USER, Employee.Role.ADMIN))
.and(QEmployee.employee.name.in("Charlie", "David").not())
.and(QEmployee.employee.name.in(List.of("Charlie", "David")).not())
);
Assert.isTrue(result2_querydsl.size() == 2, "区间和集合操作符查询失败 %d".formatted(result2_querydsl.size()));
formatLog("2. 区间和集合操作符查询 HQL");
var result2_hql = session.createQuery(
var result2_hql = manager.createQuery(
"""
from Employee employee
where employee.age between 25 and 30
@@ -251,7 +249,7 @@ public class TestApplication extends AbstractTestApplication {
and employee.name not in ('Charlie', 'David')
""",
Employee.class
).list();
).getResultList();
Assert.isTrue(result2_hql.size() == 2, "区间和集合操作符查询失败 %d".formatted(result2_hql.size()));
formatLog("3. 字符串操作符查询 JPA");
@@ -293,7 +291,7 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(result3_querydsl.size() == 1, "字符串操作符查询失败 %d".formatted(result3_querydsl.size()));
formatLog("3. 字符串操作符查询 HQL");
var result3_hql = session.createQuery(
var result3_hql = manager.createQuery(
"""
from Employee employee
where employee.name like 'A%'
@@ -305,7 +303,7 @@ public class TestApplication extends AbstractTestApplication {
and substring(employee.name, 1, 3) = 'Ali'
""",
Employee.class
).list();
).getResultList();
Assert.isTrue(result3_hql.size() == 1, "字符串操作符查询失败 %d".formatted(result3_hql.size()));
formatLog("4. NULL 和布尔操作符查询 JPA");
@@ -318,9 +316,6 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(!result4_jpa.isEmpty(), "NULL 和布尔操作符查询失败 %d".formatted(result4_jpa.size()));
formatLog("4. NULL 和布尔操作符查询 Fenix");
log.info("Fenix框架当前版本不支持以下布尔操作符:");
log.info(" - cb.isTrue() / cb.isFalse() - 专用布尔判断函数");
log.info("Fenix通过equals/notEquals处理布尔字段");
var result4_fenix = employeeRepository.findAll(
builder -> builder.andEquals(Employee.Fields.active, true)
.andIsNotNull(Employee.Fields.bonus)
@@ -338,7 +333,7 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(!result4_querydsl.isEmpty(), "NULL 和布尔操作符查询失败 %d".formatted(result4_querydsl.size()));
formatLog("4. NULL 和布尔操作符查询 HQL");
var result4_hql = session.createQuery(
var result4_hql = manager.createQuery(
"""
from Employee employee
where employee.active is true
@@ -346,7 +341,7 @@ public class TestApplication extends AbstractTestApplication {
and employee.code not in ('E999')
""",
Employee.class
).list();
).getResultList();
Assert.isTrue(!result4_hql.isEmpty(), "NULL 和布尔操作符查询失败 %d".formatted(result4_hql.size()));
formatLog("5. 集合操作符查询 JPA");
@@ -380,7 +375,7 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(result5_querydsl.size() == 2, "集合操作符查询失败 %d".formatted(result5_querydsl.size()));
formatLog("5. 集合操作符查询 HQL");
var result5_hql = session.createQuery(
var result5_hql = manager.createQuery(
"""
from Employee employee
where employee.skills is not empty
@@ -391,7 +386,7 @@ public class TestApplication extends AbstractTestApplication {
and size(employee.skills) < 4
""",
Employee.class
).list();
).getResultList();
Assert.isTrue(result5_hql.size() == 2, "集合操作符查询失败 %d".formatted(result5_hql.size()));
formatLog("6. 逻辑操作符查询 JPA");
@@ -429,14 +424,14 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(result6_querydsl.size() == 2, "逻辑操作符查询失败 %d".formatted(result6_querydsl.size()));
formatLog("6. 逻辑操作符查询 HQL");
var result6_hql = session.createQuery(
var result6_hql = manager.createQuery(
"""
from Employee employee
where (employee.name = 'Alice' or employee.name = 'Bob')
and not (employee.name = 'Charlie' or employee.name = 'David')
""",
Employee.class
).list();
).getResultList();
Assert.isTrue(result6_hql.size() == 2, "逻辑操作符查询失败 %d".formatted(result6_hql.size()));
formatLog("7. Specification 链式调用查询 JPA");
@@ -472,7 +467,7 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(result7_querydsl.size() == 2, "Specification 链式调用失败 %d".formatted(result7_querydsl.size()));
formatLog("7. Specification 链式调用查询 HQL");
var result7_hql = session.createQuery(
var result7_hql = manager.createQuery(
"""
from Employee employee
where employee.active is true
@@ -482,7 +477,7 @@ public class TestApplication extends AbstractTestApplication {
or employee.name = 'Charlie'
""",
Employee.class
).list();
).getResultList();
Assert.isTrue(result7_hql.size() == 2, "Specification 链式调用失败 %d".formatted(result7_hql.size()));
formatLog("8. Join 操作查询 JPA");
@@ -524,7 +519,7 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(result8_querydsl.size() == 1, "Join 操作查询失败 %d".formatted(result8_querydsl.size()));
formatLog("8. Join 操作查询 HQL");
var result8_hql = session.createQuery(
var result8_hql = manager.createQuery(
"""
from Employee employee
join employee.company as company
@@ -538,7 +533,7 @@ public class TestApplication extends AbstractTestApplication {
and value(prop) != 'Junior'
""",
Employee.class
).list();
).getResultList();
Assert.isTrue(result8_hql.size() == 1, "Join 操作查询失败 %d".formatted(result8_hql.size()));
formatLog("9. 子查询和聚合函数查询 JPA");
@@ -594,7 +589,7 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(result9_querydsl.isEmpty(), "子查询(聚合函数)+ 数学运算失败 %d".formatted(result9_querydsl.size()));
formatLog("9. 子查询和聚合函数查询 HQL");
var result9_hql = session.createQuery(
var result9_hql = manager.createQuery(
"""
from Employee employee
where employee.salary > (select avg(e.salary) from Employee e)
@@ -607,7 +602,7 @@ public class TestApplication extends AbstractTestApplication {
and employee.role != com.lanyuanxiaoyao.service.template.database.jpa.entity.Employee.Role.USER
""",
Employee.class
).list();
).getResultList();
Assert.isTrue(result9_hql.isEmpty(), "子查询(聚合函数)+ 数学运算失败 %d".formatted(result9_hql.size()));
formatLog("10. 排序查询 JPA");
@@ -654,7 +649,7 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(result10_querydsl.size() == 3, "排序查询失败 %d".formatted(result10_querydsl.size()));
formatLog("10. 排序查询 HQL");
var result10_hql = session.createQuery(
var result10_hql = manager.createQuery(
"""
from Employee employee
where employee.active is true
@@ -664,7 +659,7 @@ public class TestApplication extends AbstractTestApplication {
order by employee.age desc, employee.name asc
""",
Employee.class
).list();
).getResultList();
Assert.isTrue(result10_hql.size() == 3, "排序查询失败 %d".formatted(result10_hql.size()));
formatLog("11. 分页查询 JPA");
@@ -711,7 +706,7 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(page11_querydsl.getTotalElements() == 3, "总元素数不正确 %d".formatted(page11_querydsl.getTotalElements()));
formatLog("11. 分页查询 HQL");
var page11_hql = session.createQuery(
var page11_hql = manager.createQuery(
"""
from Employee employee
where employee.active is true
@@ -721,8 +716,8 @@ public class TestApplication extends AbstractTestApplication {
order by employee.age
""",
Employee.class
).setFirstResult(0).setMaxResults(2).list();
var total11_hql = session.createQuery(
).setFirstResult(0).setMaxResults(2).getResultList();
var total11_hql = manager.createQuery(
"""
from Employee employee
where employee.active is true
@@ -731,7 +726,7 @@ public class TestApplication extends AbstractTestApplication {
and employee.salary < 60000.00
""",
Employee.class
).list().size();
).getResultList().size();
Assert.isTrue(page11_hql.size() == 2, "分页大小不正确 %d".formatted(page11_hql.size()));
Assert.isTrue(total11_hql == 3, "总元素数不正确 %d".formatted(total11_hql));
@@ -790,7 +785,7 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(result12_querydsl.size() == 2, "CASE WHEN 查询失败 %d".formatted(result12_querydsl.size()));
formatLog("12. CASE WHEN 条件表达式查询 HQL");
var result12_hql = session.createQuery(
var result12_hql = manager.createQuery(
"""
from Employee employee
where (case when employee.age > 30 then 'Senior'
@@ -803,7 +798,7 @@ public class TestApplication extends AbstractTestApplication {
end) != 'Junior'
""",
Employee.class
).list();
).getResultList();
Assert.isTrue(result12_hql.size() == 2, "CASE WHEN 查询失败 %d".formatted(result12_hql.size()));
formatLog("13. 综合多条件查询 JPA");
@@ -874,7 +869,7 @@ public class TestApplication extends AbstractTestApplication {
Assert.isTrue(result13_querydsl.size() == 1 && result13_querydsl.get(0).getName().equals("Alice"), "综合多条件查询失败 %d".formatted(result13_querydsl.size()));
formatLog("13. 综合多条件查询 HQL");
var result13_hql = session.createQuery(
var result13_hql = manager.createQuery(
"""
select distinct employee
from Employee employee
@@ -897,7 +892,7 @@ public class TestApplication extends AbstractTestApplication {
and employee.role != com.lanyuanxiaoyao.service.template.database.jpa.entity.Employee.Role.USER
""",
Employee.class
).list();
).getResultList();
Assert.isTrue(result13_hql.size() == 1 && result13_hql.get(0).getName().equals("Alice"), "综合多条件查询失败 %d".formatted(result13_hql.size()));
formatLog("清理测试数据");