api association fixes; mostly about propagating ids/fkeys, and having fields (in maps) as expected field types

This commit is contained in:
2023-05-22 16:05:34 -05:00
parent 4b6b60f331
commit 74d003ed3c
6 changed files with 70 additions and 11 deletions

View File

@ -183,7 +183,10 @@ public class QRecordApiAdapter
{
if(subObject instanceof JSONObject subJsonObject)
{
QRecord subRecord = apiJsonObjectToQRecord(subJsonObject, association.getAssociatedTableName(), apiName, apiVersion, includePrimaryKey);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// make sure to always include primary keys (boolean param) on calls for children - to help determine insert/update cases //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
QRecord subRecord = apiJsonObjectToQRecord(subJsonObject, association.getAssociatedTableName(), apiName, apiVersion, true);
qRecord.withAssociatedRecord(association.getName(), subRecord);
}
else

View File

@ -938,6 +938,42 @@ class QJavalinApiHandlerTest extends BaseTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testUpdateAssociations() throws QException
{
insert1Order3Lines4LineExtrinsicsAnd1OrderExtrinsic();
HttpResponse<String> response = Unirest.patch(BASE_URL + "/api/" + VERSION + "/order/1")
.body("""
{"orderLines":
[
{"id": 1, "lineNumber": 1, "sku": "BASIC1", "quantity": 47},
{"id": 2},
{"id": 3}
]
}
""")
.asString();
assertErrorResponse(HttpStatus.NO_CONTENT_204, null, response);
QRecord orderRecord = getRecord(TestUtils.TABLE_NAME_ORDER, 1);
List<QRecord> orderLines = orderRecord.getAssociatedRecords().get("orderLines");
assertThat(orderLines)
.withFailMessage("order lines should be found on order").isNotNull().isNotEmpty()
.withFailMessage("Should have a line with id 1").filteredOn(r -> r.getValueInteger("id").equals(1)).hasSize(1)
.withFailMessage("line with id 1 should have quantity updated to 47").first().matches(r -> r.getValue("quantity").equals(47));
assertThat(orderLines)
.withFailMessage("order lines should be found on order").isNotNull().isNotEmpty()
.withFailMessage("Should have a line with id 2").filteredOn(r -> r.getValueInteger("id").equals(2)).hasSize(1)
.withFailMessage("line with id 2 should have original quantity (42)").first().matches(r -> r.getValue("quantity").equals(42));
}
/*******************************************************************************
**
*******************************************************************************/
@ -1400,6 +1436,7 @@ class QJavalinApiHandlerTest extends BaseTest
GetInput getInput = new GetInput();
getInput.setTableName(tableName);
getInput.setPrimaryKey(id);
getInput.setIncludeAssociations(true);
GetOutput getOutput = new GetAction().execute(getInput);
QRecord record = getOutput.getRecord();
return record;