mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
CE-1955 Add handling for associations w/ some vs. all values coming from defaults instead of columns;
This commit is contained in:
@ -105,6 +105,61 @@ class TallRowsToRecordTest extends BaseTest
|
||||
assertEquals(2, ((List<?>) order.getBackendDetail("fileRows")).size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** test to show that we can do 1 default line item (child record) for each
|
||||
** header record.
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void testOrderAndLinesWithLineValuesFromDefaults() throws QException
|
||||
{
|
||||
CsvFileToRows fileToRows = CsvFileToRows.forString("""
|
||||
orderNo, Ship To, lastName
|
||||
1, Homer, Simpson
|
||||
2, Ned, Flanders
|
||||
""");
|
||||
|
||||
BulkLoadFileRow header = fileToRows.next();
|
||||
|
||||
TallRowsToRecord rowsToRecord = new TallRowsToRecord();
|
||||
|
||||
BulkInsertMapping mapping = new BulkInsertMapping()
|
||||
.withFieldNameToHeaderNameMap(Map.of(
|
||||
"orderNo", "orderNo",
|
||||
"shipToName", "Ship To"
|
||||
))
|
||||
.withFieldNameToDefaultValueMap(Map.of(
|
||||
"orderLine.sku", "NUCLEAR-ROD",
|
||||
"orderLine.quantity", 1
|
||||
))
|
||||
.withMappedAssociations(List.of("orderLine"))
|
||||
.withTableName(TestUtils.TABLE_NAME_ORDER)
|
||||
.withLayout(BulkInsertMapping.Layout.TALL)
|
||||
.withHasHeaderRow(true);
|
||||
|
||||
List<QRecord> records = rowsToRecord.nextPage(fileToRows, header, mapping, Integer.MAX_VALUE);
|
||||
assertEquals(2, records.size());
|
||||
|
||||
QRecord order = records.get(0);
|
||||
assertEquals(1, order.getValueInteger("orderNo"));
|
||||
assertEquals("Homer", order.getValueString("shipToName"));
|
||||
assertEquals(1, order.getAssociatedRecords().get("orderLine").size());
|
||||
assertEquals(List.of("NUCLEAR-ROD"), getValues(order.getAssociatedRecords().get("orderLine"), "sku"));
|
||||
assertEquals(List.of(1), getValues(order.getAssociatedRecords().get("orderLine"), "quantity"));
|
||||
assertEquals("Row 2", order.getBackendDetail("rowNos"));
|
||||
assertEquals(1, ((List<?>) order.getBackendDetail("fileRows")).size());
|
||||
|
||||
order = records.get(1);
|
||||
assertEquals(2, order.getValueInteger("orderNo"));
|
||||
assertEquals("Ned", order.getValueString("shipToName"));
|
||||
assertEquals(1, order.getAssociatedRecords().get("orderLine").size());
|
||||
assertEquals(List.of("NUCLEAR-ROD"), getValues(order.getAssociatedRecords().get("orderLine"), "sku"));
|
||||
assertEquals(List.of(1), getValues(order.getAssociatedRecords().get("orderLine"), "quantity"));
|
||||
assertEquals("Row 3", order.getBackendDetail("rowNos"));
|
||||
assertEquals(1, ((List<?>) order.getBackendDetail("fileRows")).size());
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
@ -98,6 +98,59 @@ class WideRowsToRecordWithExplicitFieldNameSuffixIndexBasedMappingTest extends B
|
||||
assertEquals("Row 3", order.getAssociatedRecords().get("orderLine").get(0).getBackendDetail("rowNos"));
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void testOrderAndLinesWithLineValuesFromDefaults() throws QException
|
||||
{
|
||||
String csv = """
|
||||
orderNo, Ship To, lastName, SKU 1, Quantity 1
|
||||
1, Homer, Simpson, DONUT, 12,
|
||||
2, Ned, Flanders,
|
||||
""";
|
||||
|
||||
CsvFileToRows fileToRows = CsvFileToRows.forString(csv);
|
||||
BulkLoadFileRow header = fileToRows.next();
|
||||
|
||||
WideRowsToRecordWithExplicitFieldNameSuffixIndexBasedMapping rowsToRecord = new WideRowsToRecordWithExplicitFieldNameSuffixIndexBasedMapping();
|
||||
|
||||
BulkInsertMapping mapping = new BulkInsertMapping()
|
||||
.withFieldNameToHeaderNameMap(Map.of(
|
||||
"orderNo", "orderNo",
|
||||
"shipToName", "Ship To",
|
||||
"orderLine.sku,0", "SKU 1",
|
||||
"orderLine.quantity,0", "Quantity 1"
|
||||
))
|
||||
.withFieldNameToDefaultValueMap(Map.of(
|
||||
"orderLine.sku,1", "NUCLEAR-ROD",
|
||||
"orderLine.quantity,1", 1
|
||||
))
|
||||
.withMappedAssociations(List.of("orderLine"))
|
||||
.withTableName(TestUtils.TABLE_NAME_ORDER)
|
||||
.withLayout(BulkInsertMapping.Layout.WIDE)
|
||||
.withHasHeaderRow(true);
|
||||
|
||||
List<QRecord> records = rowsToRecord.nextPage(fileToRows, header, mapping, Integer.MAX_VALUE);
|
||||
assertEquals(2, records.size());
|
||||
|
||||
QRecord order = records.get(0);
|
||||
assertEquals(1, order.getValueInteger("orderNo"));
|
||||
assertEquals("Homer", order.getValueString("shipToName"));
|
||||
assertEquals(List.of("DONUT", "NUCLEAR-ROD"), getValues(order.getAssociatedRecords().get("orderLine"), "sku"));
|
||||
assertEquals(List.of(12, 1), getValues(order.getAssociatedRecords().get("orderLine"), "quantity"));
|
||||
assertEquals(1, ((List<?>) order.getBackendDetail("fileRows")).size());
|
||||
assertEquals("Row 2", order.getAssociatedRecords().get("orderLine").get(0).getBackendDetail("rowNos"));
|
||||
|
||||
order = records.get(1);
|
||||
assertEquals(2, order.getValueInteger("orderNo"));
|
||||
assertEquals("Ned", order.getValueString("shipToName"));
|
||||
assertEquals(List.of("NUCLEAR-ROD"), getValues(order.getAssociatedRecords().get("orderLine"), "sku"));
|
||||
assertEquals(List.of(1), getValues(order.getAssociatedRecords().get("orderLine"), "quantity"));
|
||||
assertEquals(1, ((List<?>) order.getBackendDetail("fileRows")).size());
|
||||
assertEquals("Row 3", order.getAssociatedRecords().get("orderLine").get(0).getBackendDetail("rowNos"));
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
Reference in New Issue
Block a user