Test for APIRecordUtils.jsonQueryStyleQRecordToJSONObject

This commit is contained in:
2023-07-10 11:07:06 -05:00
parent 5cfcb420d0
commit e6816174c3

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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"));
}
}