forked from opengaussexamples/examples
test: Add unit and integration tests for limit/offset support in OpenGaussDialect
- Implemented unit tests for the `OpenGaussDialect` limit/offset support using `LimitHandler`. - Tested SQL generation for queries with and without offset. - `testProcessSqlWithOffset`: Validates SQL query with limit and offset values. - `testProcessSqlWithoutOffset`: Validates SQL query with only limit value. - `testSupportsLimit`: Ensures `supportsLimit()` returns true. - Structured tests in `OpenGaussDialectLimitHandlerTest.java`. - Verified the proper handling of limit and offset logic in OpenGaussDialect.
This commit is contained in:
parent
18ba24a730
commit
092d34510f
|
|
@ -0,0 +1 @@
|
|||
/target/
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package org.hibernate.dialect.entity.pagination;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "test_pagination")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class TestEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String name;
|
||||
}
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
package org.hibernate.dialect.integration;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.dialect.entity.pagination.TestEntity;
|
||||
import org.hibernate.dialect.util.HibernateUtil;
|
||||
import org.hibernate.query.Query;
|
||||
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 java.util.List;
|
||||
|
||||
public class OpenGaussDialectPaginationIntegrationTest {
|
||||
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
@BeforeAll
|
||||
public static void init() {
|
||||
sessionFactory = HibernateUtil.getSessionFactory(TestEntity.class);
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
session.createQuery("delete from TestEntity").executeUpdate();
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
TestEntity entity = new TestEntity();
|
||||
entity.setName("Name " + i);
|
||||
session.save(entity);
|
||||
}
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void tearDown() {
|
||||
if (sessionFactory != null) {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaginationWithOffset() {
|
||||
Session session = sessionFactory.openSession();
|
||||
int pageSize = 10;
|
||||
int pageNumber = 3;
|
||||
String hql = "from TestEntity order by id";
|
||||
Query<TestEntity> query = session.createQuery(hql, TestEntity.class);
|
||||
query.setFirstResult(pageNumber * pageSize); // 设置 offset
|
||||
query.setMaxResults(pageSize); // 设置 limit
|
||||
List<TestEntity> results = query.list();
|
||||
Assertions.assertEquals(pageSize, results.size());
|
||||
Assertions.assertEquals(31L, results.get(0).getId());
|
||||
Assertions.assertEquals(40L, results.get(results.size() - 1).getId());
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPaginationWithoutOffset() {
|
||||
Session session = sessionFactory.openSession();
|
||||
int pageSize = 10;
|
||||
String hql = "from TestEntity order by id";
|
||||
Query<TestEntity> query = session.createQuery(hql, TestEntity.class);
|
||||
query.setMaxResults(pageSize);
|
||||
List<TestEntity> results = query.list();
|
||||
Assertions.assertEquals(pageSize, results.size());
|
||||
Assertions.assertEquals(1L, results.get(0).getId());
|
||||
Assertions.assertEquals(10L, results.get(results.size() - 1).getId());
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package org.hibernate.dialect.unit;
|
||||
|
||||
import org.hibernate.dialect.OpenGaussDialect;
|
||||
import org.hibernate.dialect.pagination.LimitHandler;
|
||||
import org.hibernate.engine.spi.RowSelection;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class OpenGaussDialectLimitHandlerTest {
|
||||
@Test
|
||||
public void testProcessSqlWithOffset() {
|
||||
OpenGaussDialect dialect = new OpenGaussDialect();
|
||||
LimitHandler limitHandler = dialect.getLimitHandler();
|
||||
String originalSql = "SELECT * FROM test_table";
|
||||
RowSelection selection = new RowSelection();
|
||||
selection.setFirstRow(10); // offset
|
||||
selection.setMaxRows(20); // limit
|
||||
String processedSql = limitHandler.processSql(originalSql, selection);
|
||||
String expectedSql = "SELECT * FROM test_table limit ? offset ?";
|
||||
Assertions.assertEquals(expectedSql, processedSql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcessSqlWithoutOffset() {
|
||||
OpenGaussDialect dialect = new OpenGaussDialect();
|
||||
LimitHandler limitHandler = dialect.getLimitHandler();
|
||||
String originalSql = "SELECT * FROM test_table";
|
||||
RowSelection selection = new RowSelection();
|
||||
selection.setMaxRows(20); // limit
|
||||
String processedSql = limitHandler.processSql(originalSql, selection);
|
||||
String expectedSql = "SELECT * FROM test_table limit ?";
|
||||
Assertions.assertEquals(expectedSql, processedSql);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSupportsLimit() {
|
||||
OpenGaussDialect dialect = new OpenGaussDialect();
|
||||
LimitHandler limitHandler = dialect.getLimitHandler();
|
||||
Assertions.assertTrue(limitHandler.supportsLimit());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue