mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-19 05:30:43 +00:00
Add aggregateAction; Add renderTemplateAction
This commit is contained in:
@ -26,9 +26,9 @@ import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QAuthenticationType;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import com.kingsrook.qqq.backend.core.modules.authentication.metadata.QAuthenticationMetaData;
|
||||
import com.kingsrook.qqq.backend.module.rdbms.actions.RDBMSActionTest;
|
||||
@ -131,7 +131,10 @@ public class TestUtils
|
||||
.withField(new QFieldMetaData("firstName", QFieldType.STRING).withBackendName("first_name"))
|
||||
.withField(new QFieldMetaData("lastName", QFieldType.STRING).withBackendName("last_name"))
|
||||
.withField(new QFieldMetaData("birthDate", QFieldType.DATE).withBackendName("birth_date"))
|
||||
.withField(new QFieldMetaData("email", QFieldType.STRING))
|
||||
.withField(new QFieldMetaData("email", QFieldType.STRING).withBackendName("email"))
|
||||
.withField(new QFieldMetaData("isEmployed", QFieldType.BOOLEAN).withBackendName("is_employed"))
|
||||
.withField(new QFieldMetaData("annualSalary", QFieldType.DECIMAL).withBackendName("annual_salary"))
|
||||
.withField(new QFieldMetaData("daysWorked", QFieldType.INTEGER).withBackendName("days_worked"))
|
||||
.withBackendDetails(new RDBMSTableBackendDetails()
|
||||
.withTableName("person"));
|
||||
}
|
||||
|
@ -0,0 +1,327 @@
|
||||
/*
|
||||
* QQQ - Low-code Application Framework for Engineers.
|
||||
* Copyright (C) 2021-2022. Kingsrook, LLC
|
||||
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
|
||||
* contact@kingsrook.com
|
||||
* https://github.com/Kingsrook/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qqq.backend.module.rdbms.actions;
|
||||
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.actions.tables.InsertAction;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.Aggregate;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateOperator;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateOutput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateResult;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.QFilterOrderByAggregate;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.session.QSession;
|
||||
import com.kingsrook.qqq.backend.module.rdbms.TestUtils;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class RDBMSAggregateActionTest extends RDBMSActionTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@BeforeEach
|
||||
public void beforeEach() throws Exception
|
||||
{
|
||||
super.primeTestDatabase();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void testUnfilteredNoGroupBy() throws QException
|
||||
{
|
||||
AggregateInput aggregateInput = initAggregateRequest();
|
||||
Aggregate countOfId = new Aggregate("id", AggregateOperator.COUNT);
|
||||
Aggregate sumOfId = new Aggregate("id", AggregateOperator.SUM);
|
||||
Aggregate averageOfDaysWorked = new Aggregate("daysWorked", AggregateOperator.AVG);
|
||||
Aggregate maxAnnualSalary = new Aggregate("annualSalary", AggregateOperator.MAX);
|
||||
Aggregate minFirstName = new Aggregate("firstName", AggregateOperator.MIN);
|
||||
aggregateInput.withAggregate(countOfId);
|
||||
aggregateInput.withAggregate(sumOfId);
|
||||
aggregateInput.withAggregate(averageOfDaysWorked);
|
||||
aggregateInput.withAggregate(maxAnnualSalary);
|
||||
aggregateInput.withAggregate(minFirstName);
|
||||
|
||||
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
|
||||
|
||||
AggregateResult aggregateResult = aggregateOutput.getResults().get(0);
|
||||
Assertions.assertEquals(5, aggregateResult.getAggregateValue(countOfId));
|
||||
Assertions.assertEquals(15, aggregateResult.getAggregateValue(sumOfId));
|
||||
Assertions.assertEquals(new BigDecimal("96.4"), aggregateResult.getAggregateValue(averageOfDaysWorked));
|
||||
Assertions.assertEquals(new BigDecimal("1000000.00"), aggregateResult.getAggregateValue(maxAnnualSalary));
|
||||
Assertions.assertEquals("Darin", aggregateResult.getAggregateValue(minFirstName));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void testFilteredNoGroupBy() throws QException
|
||||
{
|
||||
AggregateInput aggregateInput = initAggregateRequest();
|
||||
Aggregate countOfId = new Aggregate("id", AggregateOperator.COUNT);
|
||||
Aggregate sumOfId = new Aggregate("id", AggregateOperator.SUM);
|
||||
Aggregate averageOfDaysWorked = new Aggregate("daysWorked", AggregateOperator.AVG);
|
||||
Aggregate maxAnnualSalary = new Aggregate("annualSalary", AggregateOperator.MAX);
|
||||
Aggregate minFirstName = new Aggregate("firstName", AggregateOperator.MIN);
|
||||
aggregateInput.withAggregate(countOfId);
|
||||
aggregateInput.withAggregate(sumOfId);
|
||||
aggregateInput.withAggregate(averageOfDaysWorked);
|
||||
aggregateInput.withAggregate(maxAnnualSalary);
|
||||
aggregateInput.withAggregate(minFirstName);
|
||||
|
||||
aggregateInput.setFilter(new QQueryFilter().withCriteria(new QFilterCriteria("firstName", QCriteriaOperator.IN, List.of("Tim", "James"))));
|
||||
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
|
||||
|
||||
AggregateResult aggregateResult = aggregateOutput.getResults().get(0);
|
||||
Assertions.assertEquals(2, aggregateResult.getAggregateValue(countOfId));
|
||||
Assertions.assertEquals(5, aggregateResult.getAggregateValue(sumOfId));
|
||||
Assertions.assertEquals(new BigDecimal("62.0"), aggregateResult.getAggregateValue(averageOfDaysWorked));
|
||||
Assertions.assertEquals(new BigDecimal("26000.00"), aggregateResult.getAggregateValue(maxAnnualSalary));
|
||||
Assertions.assertEquals("James", aggregateResult.getAggregateValue(minFirstName));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void testUnfilteredWithGroupBy() throws QException
|
||||
{
|
||||
////////////////////////////////////////////////////
|
||||
// insert a few extra rows from the core data set //
|
||||
////////////////////////////////////////////////////
|
||||
insertExtraPersonRecords();
|
||||
|
||||
AggregateInput aggregateInput = initAggregateRequest();
|
||||
Aggregate countOfId = new Aggregate("id", AggregateOperator.COUNT);
|
||||
Aggregate sumOfDaysWorked = new Aggregate("daysWorked", AggregateOperator.SUM);
|
||||
aggregateInput.withAggregate(countOfId);
|
||||
aggregateInput.withAggregate(sumOfDaysWorked);
|
||||
|
||||
aggregateInput.withGroupByFieldName("lastName");
|
||||
aggregateInput.setFilter(new QQueryFilter().withOrderBy(new QFilterOrderBy("lastName")));
|
||||
|
||||
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
|
||||
{
|
||||
AggregateResult aggregateResult = aggregateOutput.getResults().get(0);
|
||||
Assertions.assertEquals("Chamberlain", aggregateResult.getGroupByValue("lastName"));
|
||||
Assertions.assertEquals(2, aggregateResult.getAggregateValue(countOfId));
|
||||
Assertions.assertEquals(17, aggregateResult.getAggregateValue(sumOfDaysWorked));
|
||||
}
|
||||
{
|
||||
AggregateResult aggregateResult = aggregateOutput.getResults().get(1);
|
||||
Assertions.assertEquals("Kelkhoff", aggregateResult.getGroupByValue("lastName"));
|
||||
Assertions.assertEquals(4, aggregateResult.getAggregateValue(countOfId));
|
||||
Assertions.assertEquals(11364, aggregateResult.getAggregateValue(sumOfDaysWorked));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void testUnfilteredWithMultiGroupBy() throws QException
|
||||
{
|
||||
////////////////////////////////////////////////////
|
||||
// insert a few extra rows from the core data set //
|
||||
////////////////////////////////////////////////////
|
||||
insertExtraPersonRecords();
|
||||
|
||||
AggregateInput aggregateInput = initAggregateRequest();
|
||||
Aggregate countOfId = new Aggregate("id", AggregateOperator.COUNT);
|
||||
Aggregate sumOfDaysWorked = new Aggregate("daysWorked", AggregateOperator.SUM);
|
||||
aggregateInput.withAggregate(countOfId);
|
||||
aggregateInput.withAggregate(sumOfDaysWorked);
|
||||
|
||||
aggregateInput.withGroupByFieldName("lastName");
|
||||
aggregateInput.withGroupByFieldName("firstName");
|
||||
|
||||
aggregateInput.setFilter(new QQueryFilter()
|
||||
.withOrderBy(new QFilterOrderBy("lastName"))
|
||||
.withOrderBy(new QFilterOrderBy("firstName")));
|
||||
|
||||
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
|
||||
Iterator<AggregateResult> iterator = aggregateOutput.getResults().iterator();
|
||||
AggregateResult aggregateResult;
|
||||
|
||||
aggregateResult = iterator.next();
|
||||
Assertions.assertEquals("Chamberlain", aggregateResult.getGroupByValue("lastName"));
|
||||
Assertions.assertEquals("Donny", aggregateResult.getGroupByValue("firstName"));
|
||||
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
|
||||
|
||||
aggregateResult = iterator.next();
|
||||
Assertions.assertEquals("Chamberlain", aggregateResult.getGroupByValue("lastName"));
|
||||
Assertions.assertEquals("Tim", aggregateResult.getGroupByValue("firstName"));
|
||||
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
|
||||
|
||||
aggregateResult = iterator.next();
|
||||
Assertions.assertEquals("Kelkhoff", aggregateResult.getGroupByValue("lastName"));
|
||||
Assertions.assertEquals("Aaron", aggregateResult.getGroupByValue("firstName"));
|
||||
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
|
||||
|
||||
aggregateResult = iterator.next();
|
||||
Assertions.assertEquals("Kelkhoff", aggregateResult.getGroupByValue("lastName"));
|
||||
Assertions.assertEquals("Darin", aggregateResult.getGroupByValue("firstName"));
|
||||
Assertions.assertEquals(2, aggregateResult.getAggregateValue(countOfId));
|
||||
|
||||
aggregateResult = iterator.next();
|
||||
Assertions.assertEquals("Kelkhoff", aggregateResult.getGroupByValue("lastName"));
|
||||
Assertions.assertEquals("Trevor", aggregateResult.getGroupByValue("firstName"));
|
||||
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void testOrderByAggregate() throws QException
|
||||
{
|
||||
////////////////////////////////////////////////////
|
||||
// insert a few extra rows from the core data set //
|
||||
////////////////////////////////////////////////////
|
||||
insertExtraPersonRecords();
|
||||
|
||||
AggregateInput aggregateInput = initAggregateRequest();
|
||||
Aggregate countOfId = new Aggregate("id", AggregateOperator.COUNT);
|
||||
Aggregate sumOfDaysWorked = new Aggregate("daysWorked", AggregateOperator.SUM);
|
||||
aggregateInput.withAggregate(countOfId);
|
||||
// note - don't query this value - just order by it!! aggregateInput.withAggregate(sumOfDaysWorked);
|
||||
|
||||
aggregateInput.withGroupByFieldName("lastName");
|
||||
|
||||
aggregateInput.setFilter(new QQueryFilter().withOrderBy(new QFilterOrderByAggregate(sumOfDaysWorked, false)));
|
||||
|
||||
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
|
||||
Iterator<AggregateResult> iterator = aggregateOutput.getResults().iterator();
|
||||
AggregateResult aggregateResult;
|
||||
|
||||
aggregateResult = iterator.next();
|
||||
Assertions.assertEquals("Kelkhoff", aggregateResult.getGroupByValue("lastName"));
|
||||
Assertions.assertEquals(4, aggregateResult.getAggregateValue(countOfId));
|
||||
|
||||
aggregateResult = iterator.next();
|
||||
Assertions.assertEquals("Richardson", aggregateResult.getGroupByValue("lastName"));
|
||||
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
|
||||
|
||||
aggregateResult = iterator.next();
|
||||
Assertions.assertEquals("Maes", aggregateResult.getGroupByValue("lastName"));
|
||||
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
|
||||
|
||||
aggregateResult = iterator.next();
|
||||
Assertions.assertEquals("Samples", aggregateResult.getGroupByValue("lastName"));
|
||||
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
|
||||
|
||||
aggregateResult = iterator.next();
|
||||
Assertions.assertEquals("Chamberlain", aggregateResult.getGroupByValue("lastName"));
|
||||
Assertions.assertEquals(2, aggregateResult.getAggregateValue(countOfId));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void testNoRowsFound() throws QException
|
||||
{
|
||||
AggregateInput aggregateInput = initAggregateRequest();
|
||||
Aggregate countOfId = new Aggregate("id", AggregateOperator.COUNT);
|
||||
aggregateInput.withAggregate(countOfId);
|
||||
aggregateInput.withFilter(new QQueryFilter(new QFilterCriteria("id", QCriteriaOperator.EQUALS, -9)));
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// when there's no group-by, we get a row, but w/ 0 count //
|
||||
////////////////////////////////////////////////////////////
|
||||
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
|
||||
AggregateResult aggregateResult = aggregateOutput.getResults().get(0);
|
||||
Assertions.assertEquals(0, aggregateResult.getAggregateValue(countOfId));
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
// but re-run w/ a group-by -- then, if no rows are found, there are 0 result objects. //
|
||||
/////////////////////////////////////////////////////////////////////////////////////////
|
||||
aggregateInput.withGroupByFieldName("lastName");
|
||||
aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
|
||||
assertTrue(aggregateOutput.getResults().isEmpty());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private static void insertExtraPersonRecords() throws QException
|
||||
{
|
||||
InsertInput insertInput = new InsertInput(TestUtils.defineInstance());
|
||||
insertInput.setSession(new QSession());
|
||||
insertInput.setTableName(TestUtils.defineTablePerson().getName());
|
||||
insertInput.setRecords(List.of(
|
||||
new QRecord().withValue("lastName", "Kelkhoff").withValue("firstName", "Trevor").withValue("email", "tk@kr.com").withValue("daysWorked", 1024),
|
||||
new QRecord().withValue("lastName", "Kelkhoff").withValue("firstName", "Darin").withValue("email", "dk2@kr.com").withValue("daysWorked", 314),
|
||||
new QRecord().withValue("lastName", "Kelkhoff").withValue("firstName", "Aaron").withValue("email", "ak@kr.com").withValue("daysWorked", 9999),
|
||||
new QRecord().withValue("lastName", "Chamberlain").withValue("firstName", "Donny").withValue("email", "dc@kr.com").withValue("daysWorked", 17)
|
||||
));
|
||||
new InsertAction().execute(insertInput);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private AggregateInput initAggregateRequest()
|
||||
{
|
||||
AggregateInput aggregateInput = new AggregateInput();
|
||||
aggregateInput.setInstance(TestUtils.defineInstance());
|
||||
aggregateInput.setTableName(TestUtils.defineTablePerson().getName());
|
||||
return aggregateInput;
|
||||
}
|
||||
|
||||
}
|
@ -29,14 +29,17 @@ CREATE TABLE person
|
||||
first_name VARCHAR(80) NOT NULL,
|
||||
last_name VARCHAR(80) NOT NULL,
|
||||
birth_date DATE,
|
||||
email VARCHAR(250) NOT NULL
|
||||
email VARCHAR(250) NOT NULL,
|
||||
is_employed BOOLEAN,
|
||||
annual_salary DECIMAL(12,2),
|
||||
days_worked INTEGER
|
||||
);
|
||||
|
||||
INSERT INTO person (id, first_name, last_name, birth_date, email) VALUES (1, 'Darin', 'Kelkhoff', '1980-05-31', 'darin.kelkhoff@gmail.com');
|
||||
INSERT INTO person (id, first_name, last_name, birth_date, email) VALUES (2, 'James', 'Maes', '1980-05-15', 'jmaes@mmltholdings.com');
|
||||
INSERT INTO person (id, first_name, last_name, birth_date, email) VALUES (3, 'Tim', 'Chamberlain', '1976-05-28', 'tchamberlain@mmltholdings.com');
|
||||
INSERT INTO person (id, first_name, last_name, birth_date, email) VALUES (4, 'Tyler', 'Samples', NULL, 'tsamples@mmltholdings.com');
|
||||
INSERT INTO person (id, first_name, last_name, birth_date, email) VALUES (5, 'Garret', 'Richardson', '1981-01-01', 'grichardson@mmltholdings.com');
|
||||
INSERT INTO person (id, first_name, last_name, birth_date, email, is_employed, annual_salary, days_worked) VALUES (1, 'Darin', 'Kelkhoff', '1980-05-31', 'darin.kelkhoff@gmail.com', 1, 25000, 27);
|
||||
INSERT INTO person (id, first_name, last_name, birth_date, email, is_employed, annual_salary, days_worked) VALUES (2, 'James', 'Maes', '1980-05-15', 'jmaes@mmltholdings.com', 1, 26000, 124);
|
||||
INSERT INTO person (id, first_name, last_name, birth_date, email, is_employed, annual_salary, days_worked) VALUES (3, 'Tim', 'Chamberlain', '1976-05-28', 'tchamberlain@mmltholdings.com', 0, null, 0);
|
||||
INSERT INTO person (id, first_name, last_name, birth_date, email, is_employed, annual_salary, days_worked) VALUES (4, 'Tyler', 'Samples', NULL, 'tsamples@mmltholdings.com', 1, 30000, 99);
|
||||
INSERT INTO person (id, first_name, last_name, birth_date, email, is_employed, annual_salary, days_worked) VALUES (5, 'Garret', 'Richardson', '1981-01-01', 'grichardson@mmltholdings.com', 1, 1000000, 232);
|
||||
|
||||
DROP TABLE IF EXISTS carrier;
|
||||
CREATE TABLE carrier
|
||||
|
Reference in New Issue
Block a user