Skip to content

Commit

Permalink
[#26284] Tests for .project with HQL queries having 'select'
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD committed Jul 4, 2022
1 parent 008a724 commit 364c1b4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class Cat extends PanacheEntity {
@ManyToOne
CatOwner owner;

Double weight;

public Cat(String name, CatOwner owner) {
this.name = name;
this.owner = owner;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.quarkus.it.panache;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection
public class CatProjectionBean {

private final String name;

private final String ownerName;

private final Double weight;

public CatProjectionBean(String name, String ownerName) {
this(name, ownerName, null);
}

public CatProjectionBean(String name, String ownerName, Double weight) {
this.name = name;
this.ownerName = ownerName;
this.weight = weight;
}

public String getName() {
return name;
}

public String getOwnerName() {
return ownerName;
}

public Double getWeight() {
return weight;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1205,11 +1205,40 @@ public String testProjection() {
CatOwner catOwner = new CatOwner("Julie");
catOwner.persist();
Cat bubulle = new Cat("Bubulle", catOwner);
bubulle.weight = 8.5d;
bubulle.persist();

CatDto catDto = Cat.findAll().project(CatDto.class).firstResult();
Assertions.assertEquals("Julie", catDto.ownerName);

CatProjectionBean fieldsProjection = Cat.find("select c.name, c.owner.name as ownerName from Cat c")
.project(CatProjectionBean.class).firstResult();
Assertions.assertEquals("Julie", fieldsProjection.getOwnerName());

PanacheQueryException exception = Assertions.assertThrows(PanacheQueryException.class,
() -> Cat.find("select new FakeClass('fake_cat', 'fake_owner', 12.5 from Cat c)")
.project(CatProjectionBean.class).firstResult());
Assertions.assertTrue(exception.getMessage().startsWith("Unable to perform a projection on a 'select new' query"));

CatProjectionBean constantProjection = Cat.find("select 'fake_cat', 'fake_owner', 12.5 from Cat c")
.project(CatProjectionBean.class).firstResult();
Assertions.assertEquals("fake_cat", constantProjection.getName());
Assertions.assertEquals("fake_owner", constantProjection.getOwnerName());
Assertions.assertEquals(12.5d, constantProjection.getWeight());

PanacheQuery<CatProjectionBean> projectionQuery = Cat
// The spaces at the beginning are intentional
.find(" SELECT c.name, cast(null as string), SUM(c.weight) from Cat c where name = :name group by name ",
Parameters.with("name", bubulle.name))
.project(CatProjectionBean.class);
CatProjectionBean aggregationProjection = projectionQuery.firstResult();
Assertions.assertEquals(bubulle.name, aggregationProjection.getName());
Assertions.assertNull(aggregationProjection.getOwnerName());
Assertions.assertEquals(bubulle.weight, aggregationProjection.getWeight());

long count = projectionQuery.count();
Assertions.assertEquals(1L, count);

Cat.deleteAll();
CatOwner.deleteAll();

Expand Down

0 comments on commit 364c1b4

Please sign in to comment.