From 7d72487a75abbda46b4230b7bd028644dd5850d5 Mon Sep 17 00:00:00 2001 From: Darin Kelkhoff Date: Tue, 15 Nov 2022 15:41:35 -0600 Subject: [PATCH] Initial checkin --- .../widgets/ChildRecordListRendererTest.java | 155 ++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/actions/dashboard/widgets/ChildRecordListRendererTest.java diff --git a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/actions/dashboard/widgets/ChildRecordListRendererTest.java b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/actions/dashboard/widgets/ChildRecordListRendererTest.java new file mode 100644 index 00000000..60ec3cb0 --- /dev/null +++ b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/actions/dashboard/widgets/ChildRecordListRendererTest.java @@ -0,0 +1,155 @@ +/* + * 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 . + */ + +package com.kingsrook.qqq.backend.core.actions.dashboard.widgets; + + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import com.kingsrook.qqq.backend.core.actions.dashboard.RenderWidgetAction; +import com.kingsrook.qqq.backend.core.exceptions.QException; +import com.kingsrook.qqq.backend.core.exceptions.QNotFoundException; +import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetInput; +import com.kingsrook.qqq.backend.core.model.actions.widgets.RenderWidgetOutput; +import com.kingsrook.qqq.backend.core.model.dashboard.widgets.ChildRecordListData; +import com.kingsrook.qqq.backend.core.model.data.QRecord; +import com.kingsrook.qqq.backend.core.model.metadata.QInstance; +import com.kingsrook.qqq.backend.core.model.metadata.dashboard.QWidgetMetaData; +import com.kingsrook.qqq.backend.core.model.session.QSession; +import com.kingsrook.qqq.backend.core.modules.backend.implementations.memory.MemoryRecordStore; +import com.kingsrook.qqq.backend.core.utils.TestUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + + +/******************************************************************************* + ** Unit test for ChildRecordListRenderer + *******************************************************************************/ +class ChildRecordListRendererTest +{ + + /******************************************************************************* + ** + *******************************************************************************/ + @BeforeEach + @AfterEach + void beforeAndAfterEach() + { + MemoryRecordStore.getInstance().reset(); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testParentRecordNotFound() throws QException + { + QInstance qInstance = TestUtils.defineInstance(); + QWidgetMetaData widget = ChildRecordListRenderer.defineWidgetFromJoin(qInstance.getJoin("orderLineItem")) + .withLabel("Line Items"); + qInstance.addWidget(widget); + + RenderWidgetInput input = new RenderWidgetInput(qInstance); + input.setSession(new QSession()); + input.setWidgetMetaData(widget); + input.setQueryParams(new HashMap<>(Map.of("id", "1"))); + + RenderWidgetAction renderWidgetAction = new RenderWidgetAction(); + assertThatThrownBy(() -> renderWidgetAction.execute(input)) + .isInstanceOf(QNotFoundException.class); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testNoChildRecordsFound() throws QException + { + QInstance qInstance = TestUtils.defineInstance(); + QWidgetMetaData widget = ChildRecordListRenderer.defineWidgetFromJoin(qInstance.getJoin("orderLineItem")) + .withLabel("Line Items"); + qInstance.addWidget(widget); + + TestUtils.insertRecords(qInstance, qInstance.getTable(TestUtils.TABLE_NAME_ORDER), List.of( + new QRecord().withValue("id", 1) + )); + + RenderWidgetInput input = new RenderWidgetInput(qInstance); + input.setSession(new QSession()); + input.setWidgetMetaData(widget); + input.setQueryParams(new HashMap<>(Map.of("id", "1"))); + + RenderWidgetAction renderWidgetAction = new RenderWidgetAction(); + RenderWidgetOutput output = renderWidgetAction.execute(input); + + ChildRecordListData childRecordListData = (ChildRecordListData) output.getWidgetData(); + assertThat(childRecordListData.getChildTableMetaData()).hasFieldOrPropertyWithValue("name", TestUtils.TABLE_NAME_LINE_ITEM); + assertThat(childRecordListData.getQueryOutput().getRecords()).isEmpty(); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testChildRecordsFound() throws QException + { + QInstance qInstance = TestUtils.defineInstance(); + QWidgetMetaData widget = ChildRecordListRenderer.defineWidgetFromJoin(qInstance.getJoin("orderLineItem")) + .withLabel("Line Items"); + qInstance.addWidget(widget); + + TestUtils.insertRecords(qInstance, qInstance.getTable(TestUtils.TABLE_NAME_ORDER), List.of( + new QRecord().withValue("id", 1), + new QRecord().withValue("id", 2) + )); + + TestUtils.insertRecords(qInstance, qInstance.getTable(TestUtils.TABLE_NAME_LINE_ITEM), List.of( + new QRecord().withValue("orderId", 1).withValue("sku", "ABC").withValue("lineNumber", 2), + new QRecord().withValue("orderId", 1).withValue("sku", "BCD").withValue("lineNumber", 1), + new QRecord().withValue("orderId", 2).withValue("sku", "XYZ") // should not be found. + )); + + RenderWidgetInput input = new RenderWidgetInput(qInstance); + input.setSession(new QSession()); + input.setWidgetMetaData(widget); + input.setQueryParams(new HashMap<>(Map.of("id", "1"))); + + RenderWidgetAction renderWidgetAction = new RenderWidgetAction(); + RenderWidgetOutput output = renderWidgetAction.execute(input); + + ChildRecordListData childRecordListData = (ChildRecordListData) output.getWidgetData(); + assertThat(childRecordListData.getChildTableMetaData()).hasFieldOrPropertyWithValue("name", TestUtils.TABLE_NAME_LINE_ITEM); + assertThat(childRecordListData.getQueryOutput().getRecords()).hasSize(2); + assertThat(childRecordListData.getQueryOutput().getRecords().get(0).getValueString("sku")).isEqualTo("BCD"); + assertThat(childRecordListData.getQueryOutput().getRecords().get(1).getValueString("sku")).isEqualTo("ABC"); + } + +} \ No newline at end of file