mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
No effective change - Update tests for aggregation queries
This commit is contained in:
@@ -4,7 +4,9 @@ import io.ebean.Model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Version;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class DMachine extends Model {
|
||||
@@ -17,6 +19,9 @@ public class DMachine extends Model {
|
||||
@Version
|
||||
long version;
|
||||
|
||||
@OneToMany(mappedBy = "machine")
|
||||
List<DMachineStats> machineStats;
|
||||
|
||||
public DMachine(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
@@ -44,4 +49,12 @@ public class DMachine extends Model {
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public List<DMachineStats> getMachineStats() {
|
||||
return machineStats;
|
||||
}
|
||||
|
||||
public void setMachineStats(List<DMachineStats> machineStats) {
|
||||
this.machineStats = machineStats;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ public class DMachineStats {
|
||||
@Id
|
||||
long id;
|
||||
|
||||
@ManyToOne
|
||||
@ManyToOne(optional = false)
|
||||
DMachine machine;
|
||||
|
||||
LocalDate date;
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.tests.model.aggregation;
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import io.ebean.Query;
|
||||
import org.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -74,19 +75,75 @@ public class TestAggregationTopLevel extends BaseTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupBy_date_dynamicFormula() {
|
||||
public void groupBy_machineDate_dynamicFormula() {
|
||||
|
||||
Query<DMachineStats> query = Ebean.find(DMachineStats.class)
|
||||
.select("date, sum(totalKms), sum(hours)")
|
||||
.select("machine, date, sum(totalKms), sum(hours)")
|
||||
.where().gt("date", LocalDate.now().minusDays(10))
|
||||
.having().gt("sum(hours)", 2)
|
||||
.query();
|
||||
|
||||
List<DMachineStats> result = query.findList();
|
||||
assertThat(sqlOf(query)).contains("select t0.date, sum(t0.total_kms), sum(t0.hours) from d_machine_stats t0 where t0.date > ? group by t0.date having sum(t0.hours) > ?");
|
||||
assertThat(sqlOf(query)).contains("select t0.machine_id, t0.date, sum(t0.total_kms), sum(t0.hours) from d_machine_stats t0 where t0.date > ? group by t0.machine_id, t0.date having sum(t0.hours) > ?");
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupBy_machine_dynamicFormula() {
|
||||
|
||||
Query<DMachineStats> query = Ebean.find(DMachineStats.class)
|
||||
.select("machine, sum(totalKms)")
|
||||
.where().gt("date", LocalDate.now().minusDays(10))
|
||||
.having().gt("sum(hours)", 2)
|
||||
.query();
|
||||
|
||||
List<DMachineStats> result = query.findList();
|
||||
assertThat(sqlOf(query)).contains("select t0.machine_id, sum(t0.total_kms) from d_machine_stats t0 where t0.date > ? group by t0.machine_id having sum(t0.hours) > ?");
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupBy_machine_dynamicFormula_withJoin() {
|
||||
|
||||
Query<DMachineStats> query = Ebean.find(DMachineStats.class)
|
||||
.select("machine, sum(totalKms), sum(hours)")
|
||||
.fetch("machine", "name")
|
||||
.where().gt("date", LocalDate.now().minusDays(10))
|
||||
.having().gt("sum(hours)", 2)
|
||||
.query();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<DMachineStats> result = query.findList();
|
||||
assertThat(result).isNotEmpty();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(1);
|
||||
assertThat(sql.get(0)).contains("select sum(t0.total_kms), sum(t0.hours), t1.id, t1.name from d_machine_stats t0 join dmachine t1 on t1.id = t0.machine_id where t0.date > ? group by t1.id, t1.name having sum(t0.hours) > ?");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupBy_machine_dynamicFormula_withQueryJoin() {
|
||||
|
||||
Query<DMachineStats> query = Ebean.find(DMachineStats.class)
|
||||
.select("machine, sum(totalKms), sum(hours)")
|
||||
.fetchQuery("machine", "name")
|
||||
.where().gt("date", LocalDate.now().minusDays(10))
|
||||
.having().gt("sum(hours)", 2)
|
||||
.query();
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
List<DMachineStats> result = query.findList();
|
||||
assertThat(result).isNotEmpty();
|
||||
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
assertThat(sql).hasSize(2);
|
||||
|
||||
assertThat(sql.get(0)).contains("select t0.machine_id, sum(t0.total_kms), sum(t0.hours) from d_machine_stats t0 where t0.date > ? group by t0.machine_id having sum(t0.hours) > ?");
|
||||
assertThat(sql.get(1)).contains("select t0.id, t0.name from dmachine t0 where t0.id");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupBy_MachineAndDate_dynamicFormula() {
|
||||
|
||||
@@ -100,35 +157,22 @@ public class TestAggregationTopLevel extends BaseTestCase {
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupBy_MachineWithJoin_dynamicFormula() {
|
||||
// @Test
|
||||
// public void groupBy_in_fetchClause() {
|
||||
//
|
||||
// Query<DMachine> query = Ebean.find(DMachine.class)
|
||||
// .select("name")
|
||||
// .fetch("machineStats", "date, max(rate), sum(totalKms)")
|
||||
// .where().gt("machineStats.date", LocalDate.now().minusDays(10))
|
||||
// .having().gt("sum(machineStats.totalKms)", 1)
|
||||
// .query();
|
||||
//
|
||||
// List<DMachine> result = query.findList();
|
||||
// assertThat(sqlOf(query)).contains("select distinct t0.id, t0.name, t1.date, max(t1.rate), sum(t1.total_kms) from dmachine t0 left join d_machine_stats t1 on t1.machine_id = t0.id where date > ? group by t0.id, t0.name, t1.date having sum(t1.total_kms) > ? order by t0.id");
|
||||
// assertThat(result).isNotEmpty();
|
||||
// }
|
||||
|
||||
Query<DMachineStats> query = Ebean.find(DMachineStats.class)
|
||||
.select("max(rate), sum(totalKms)")
|
||||
.fetch("machine", "name")
|
||||
.where().gt("date", LocalDate.now().minusDays(10))
|
||||
.query();
|
||||
|
||||
List<DMachineStats> result = query.findList();
|
||||
assertThat(sqlOf(query)).contains("select max(t0.rate), sum(t0.total_kms), t1.id, t1.name from d_machine_stats t0 left join dmachine t1 on t1.id = t0.machine_id where t0.date > ? group by t1.id, t1.name");
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupBy_MachineDateWithJoin_dynamicFormula() {
|
||||
|
||||
Query<DMachineStats> query = Ebean.find(DMachineStats.class)
|
||||
.select("machine, date, max(rate), sum(totalKms)")
|
||||
.fetch("machine", "name")
|
||||
.where().gt("date", LocalDate.now().minusDays(10))
|
||||
.query();
|
||||
|
||||
List<DMachineStats> result = query.findList();
|
||||
assertThat(sqlOf(query)).contains("select t0.date, max(t0.rate), sum(t0.total_kms), t1.id, t1.name from d_machine_stats t0 left join dmachine t1 on t1.id = t0.machine_id where t0.date > ? group by t0.date, t1.id, t1.name");
|
||||
assertThat(result).isNotEmpty();
|
||||
}
|
||||
|
||||
private static void loadData() {
|
||||
public static void loadData() {
|
||||
|
||||
List<DMachine> machines = new ArrayList<>();
|
||||
|
||||
@@ -146,10 +190,11 @@ public class TestAggregationTopLevel extends BaseTestCase {
|
||||
|
||||
DMachineStats stats = new DMachineStats(machine, date);
|
||||
|
||||
stats.setHours(i * 4);
|
||||
stats.setTotalKms(i * 100);
|
||||
stats.setCost(BigDecimal.valueOf(i * 50));
|
||||
stats.setRate(BigDecimal.valueOf(i * 2));
|
||||
long offset = i * machine.getId();
|
||||
stats.setHours(offset * 4);
|
||||
stats.setTotalKms(offset * 100);
|
||||
stats.setCost(BigDecimal.valueOf(offset * 50));
|
||||
stats.setRate(BigDecimal.valueOf(offset * 2));
|
||||
|
||||
allStats.add(stats);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user