From e6816174c3fc668416193e0bd3fd137af5e26e97 Mon Sep 17 00:00:00 2001 From: Darin Kelkhoff Date: Mon, 10 Jul 2023 11:07:06 -0500 Subject: [PATCH] Test for APIRecordUtils.jsonQueryStyleQRecordToJSONObject --- .../api/actions/APIRecordUtilsTest.java | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 qqq-backend-module-api/src/test/java/com/kingsrook/qqq/backend/module/api/actions/APIRecordUtilsTest.java diff --git a/qqq-backend-module-api/src/test/java/com/kingsrook/qqq/backend/module/api/actions/APIRecordUtilsTest.java b/qqq-backend-module-api/src/test/java/com/kingsrook/qqq/backend/module/api/actions/APIRecordUtilsTest.java new file mode 100644 index 00000000..90eb8e8f --- /dev/null +++ b/qqq-backend-module-api/src/test/java/com/kingsrook/qqq/backend/module/api/actions/APIRecordUtilsTest.java @@ -0,0 +1,79 @@ +/* + * 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 . + */ + +package com.kingsrook.qqq.backend.module.api.actions; + + +import com.kingsrook.qqq.backend.core.context.QContext; +import com.kingsrook.qqq.backend.core.model.data.QRecord; +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.tables.QTableMetaData; +import com.kingsrook.qqq.backend.module.api.BaseTest; +import com.kingsrook.qqq.backend.module.api.TestUtils; +import org.json.JSONObject; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + + +/******************************************************************************* + ** Unit test for APIRecordUtils + *******************************************************************************/ +class APIRecordUtilsTest extends BaseTest +{ + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test() + { + QTableMetaData table = QContext.getQInstance().getTable(TestUtils.MOCK_TABLE_NAME); + table.withField(new QFieldMetaData("myField", QFieldType.INTEGER).withBackendName("myBackendName")); + + QRecord record = new QRecord() + .withValue("foo", 1) + .withValue("bar.baz", 2) + .withValue("list[0].a", 3) + .withValue("list[1].a", 4) + .withValue("list[1].b", 5) + .withValue("myField", 6); + + JSONObject jsonObject = APIRecordUtils.jsonQueryStyleQRecordToJSONObject(table, record, true); + assertEquals(1, jsonObject.getInt("foo")); + assertEquals(2, jsonObject.getJSONObject("bar").getInt("baz")); + assertEquals(3, jsonObject.getJSONArray("list").getJSONObject(0).getInt("a")); + assertEquals(4, jsonObject.getJSONArray("list").getJSONObject(1).getInt("a")); + assertEquals(5, jsonObject.getJSONArray("list").getJSONObject(1).getInt("b")); + assertEquals(6, jsonObject.getInt("myBackendName")); + assertFalse(jsonObject.has("myField")); + + /////////////////////////////////////////////////////// + // if we say "false" for includeNonTableFields, then // + // we should only get the myField field // + /////////////////////////////////////////////////////// + jsonObject = APIRecordUtils.jsonQueryStyleQRecordToJSONObject(table, record, false); + assertFalse(jsonObject.has("foo")); + assertEquals(6, jsonObject.getInt("myBackendName")); + } + +} \ No newline at end of file