OPENJPA-2861 add unit tests for select sum(case..)

This commit is contained in:
Mark Struberg 2021-04-06 20:01:59 +02:00
parent 3647e4e257
commit bb5503b147
2 changed files with 39 additions and 4 deletions

View File

@ -22,6 +22,7 @@ import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
@ -92,6 +93,40 @@ public class TestAggregateFunctions extends SingleEMFTestCase {
em.close();
}
/**
* if a SUM(CASE statement is used, then the effective type might be different
*/
public void testAggregateWithCase() {
EntityManager em = emf.createEntityManager();
// Add a row to the table and re-test
em.getTransaction().begin();
AggEntity ae = new AggEntity();
ae.init();
ae.setStringVal("bare");
em.persist(ae);
AggEntity ae2 = new AggEntity();
ae2.init();
ae2.setStringVal("foot");
em.persist(ae2);
em.getTransaction().commit();
/*X
em.getTransaction().begin();
final TypedQuery<Long> q2 = em.createQuery("select SUM(ae.intVal) from AggEntity AS ae", Long.class);
final Long sum = q2.getSingleResult();
assertEquals(2L, (long) sum);
*/
final TypedQuery<Long> q = em.createQuery("select SUM(CASE ae.stringVal WHEN 'bare' THEN 1 ELSE 0 END) from AggEntity AS ae", Long.class);
final Long sumC = q.getSingleResult();
assertEquals(1L, (long) sumC);
em.getTransaction().commit();
em.close();
}
public void testAggregateCriteria() {
EntityManager em = emf.createEntityManager();
Metamodel mm = emf.getMetamodel();

View File

@ -37,12 +37,12 @@ import org.apache.openjpa.persistence.test.SingleEMFTestCase;
*/
public class TestAggregateQueryWithNoResult extends SingleEMFTestCase {
EntityManager em;
@Override
public void setUp() {
super.setUp(CLEAR_TABLES,
"openjpa.Compatibility", "ReturnNullOnAggregateResult=false", //OPENJPA-1794
Game.class, IndoorGame.class, Scrabble.class,
Chess.class);
Game.class, IndoorGame.class, Scrabble.class, Chess.class);
em = emf.createEntityManager();
assertTrue(em.createQuery("select p from Scrabble p").getResultList().isEmpty());
}