Initial backend work to support datetime query expressions from frontend

This commit is contained in:
2023-07-06 18:53:10 -05:00
parent 4299199947
commit 9af1fed422
7 changed files with 542 additions and 7 deletions

View File

@ -0,0 +1,68 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2023. 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.core.model.actions.tables.query.expressions;
import java.time.temporal.ChronoUnit;
import com.kingsrook.qqq.backend.core.BaseTest;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/*******************************************************************************
** Unit test for NowWithOffset
*******************************************************************************/
class NowWithOffsetTest extends BaseTest
{
private static final long DAY_IN_MILLIS = 24 * 60 * 60 * 1000;
/*******************************************************************************
**
*******************************************************************************/
@Test
void test()
{
long now = System.currentTimeMillis();
long oneWeekAgoMillis = NowWithOffset.minus(1, ChronoUnit.WEEKS).evaluate().toEpochMilli();
assertThat(oneWeekAgoMillis).isCloseTo(now - (7 * DAY_IN_MILLIS), Offset.offset(10_000L));
long oneWeekFromNowMillis = NowWithOffset.plus(2, ChronoUnit.WEEKS).evaluate().toEpochMilli();
assertThat(oneWeekFromNowMillis).isCloseTo(now + (14 * DAY_IN_MILLIS), Offset.offset(10_000L));
long oneMonthAgoMillis = NowWithOffset.minus(1, ChronoUnit.MONTHS).evaluate().toEpochMilli();
assertThat(oneMonthAgoMillis).isCloseTo(now - (30 * DAY_IN_MILLIS), Offset.offset(10_000L + 2 * DAY_IN_MILLIS));
long oneMonthFromNowMillis = NowWithOffset.plus(2, ChronoUnit.MONTHS).evaluate().toEpochMilli();
assertThat(oneMonthFromNowMillis).isCloseTo(now + (60 * DAY_IN_MILLIS), Offset.offset(10_000L + 3 * DAY_IN_MILLIS));
long oneYearAgoMillis = NowWithOffset.minus(1, ChronoUnit.YEARS).evaluate().toEpochMilli();
assertThat(oneYearAgoMillis).isCloseTo(now - (365 * DAY_IN_MILLIS), Offset.offset(10_000L + 2 * DAY_IN_MILLIS));
long oneYearFromNowMillis = NowWithOffset.plus(2, ChronoUnit.YEARS).evaluate().toEpochMilli();
assertThat(oneYearFromNowMillis).isCloseTo(now + (730 * DAY_IN_MILLIS), Offset.offset(10_000L + 3 * DAY_IN_MILLIS));
}
}

View File

@ -0,0 +1,93 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2023. 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.core.model.actions.tables.query.serialization;
import java.io.IOException;
import java.time.temporal.ChronoUnit;
import java.util.List;
import com.kingsrook.qqq.backend.core.BaseTest;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.expressions.AbstractFilterExpression;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.expressions.Now;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.expressions.NowWithOffset;
import com.kingsrook.qqq.backend.core.utils.JsonUtils;
import org.junit.jupiter.api.Test;
import static com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator.EQUALS;
import static com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator.GREATER_THAN;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
/*******************************************************************************
** Unit test for QFilterCriteriaDeserializer
*******************************************************************************/
class QFilterCriteriaDeserializerTest extends BaseTest
{
/*******************************************************************************
**
*******************************************************************************/
@Test
void testDeserialize() throws IOException
{
{
QFilterCriteria criteria = JsonUtils.toObject("""
{"fieldName": "id", "operator": "EQUALS", "values": [1]}
""", QFilterCriteria.class);
assertEquals("id", criteria.getFieldName());
assertEquals(EQUALS, criteria.getOperator());
assertEquals(List.of(1), criteria.getValues());
}
{
QFilterCriteria criteria = JsonUtils.toObject("""
{"fieldName": "createDate", "operator": "GREATER_THAN", "expression":
{"type": "NowWithOffset", "operator": "PLUS", "amount": 5, "timeUnit": "MINUTES"}
}
""", QFilterCriteria.class);
assertEquals("createDate", criteria.getFieldName());
assertEquals(GREATER_THAN, criteria.getOperator());
assertNull(criteria.getValues());
AbstractFilterExpression<?> expression = criteria.getExpression();
assertThat(expression).isInstanceOf(NowWithOffset.class);
NowWithOffset nowWithOffset = (NowWithOffset) expression;
assertEquals(5, nowWithOffset.getAmount());
assertEquals(NowWithOffset.Operator.PLUS, nowWithOffset.getOperator());
assertEquals(ChronoUnit.MINUTES, nowWithOffset.getTimeUnit());
}
{
QFilterCriteria criteria = JsonUtils.toObject("""
{"fieldName": "orderDate", "operator": "EQUALS", "expression": {"type": "Now"} }
""", QFilterCriteria.class);
assertEquals("orderDate", criteria.getFieldName());
assertEquals(EQUALS, criteria.getOperator());
assertNull(criteria.getValues());
AbstractFilterExpression<?> expression = criteria.getExpression();
assertThat(expression).isInstanceOf(Now.class);
}
}
}