CE-1107: added ability to use replaceAction specifying that null key values be treated as equal

This commit is contained in:
Tim Chamberlain
2024-04-17 22:26:30 -05:00
parent 0641ac41d6
commit dc9a2f8698
5 changed files with 248 additions and 12 deletions

View File

@ -43,7 +43,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
/*******************************************************************************
** Unit test for ReplaceAction
** Unit test for ReplaceAction
*******************************************************************************/
class ReplaceActionTest extends BaseTest
{
@ -157,6 +157,134 @@ class ReplaceActionTest extends BaseTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testTwoKeysWithNullsNotMatchingAllowingDelete() throws QException
{
String tableName = TestUtils.TABLE_NAME_TWO_KEYS;
////////////////////////////////
// start with these 2 records //
////////////////////////////////
new InsertAction().execute(new InsertInput(tableName).withRecords(List.of(
new QRecord().withValue("key1", 1).withValue("key2", 2),
new QRecord().withValue("key1", 3)
)));
////////////////////////////////////////////////////
// now do a replace action that just updates them //
////////////////////////////////////////////////////
List<QRecord> newThings = List.of(
new QRecord().withValue("key1", 1).withValue("key2", 2),
new QRecord().withValue("key1", 3)
);
//////////////////////////////
// replace allowing deletes //
//////////////////////////////
ReplaceInput replaceInput = new ReplaceInput();
replaceInput.setTableName(tableName);
replaceInput.setKey(new UniqueKey("key1", "key2"));
replaceInput.setOmitDmlAudit(true);
replaceInput.setRecords(newThings);
replaceInput.setFilter(null);
ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput);
assertEquals(1, replaceOutput.getInsertOutput().getRecords().size());
assertEquals(1, replaceOutput.getUpdateOutput().getRecords().size());
assertEquals(1, replaceOutput.getDeleteOutput().getDeletedRecordCount());
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testTwoKeysWithNullsNotMatchingNotAllowingDelete() throws QException
{
String tableName = TestUtils.TABLE_NAME_TWO_KEYS;
////////////////////////////////
// start with these 2 records //
////////////////////////////////
new InsertAction().execute(new InsertInput(tableName).withRecords(List.of(
new QRecord().withValue("key1", 1).withValue("key2", 2),
new QRecord().withValue("key1", 3)
)));
////////////////////////////////////////////////////
// now do a replace action that just updates them //
////////////////////////////////////////////////////
List<QRecord> newThings = List.of(
new QRecord().withValue("key1", 1).withValue("key2", 2),
new QRecord().withValue("key1", 3)
);
/////////////////////////////////
// replace disallowing deletes //
/////////////////////////////////
ReplaceInput replaceInput = new ReplaceInput();
replaceInput.setTableName(tableName);
replaceInput.setKey(new UniqueKey("key1", "key2"));
replaceInput.setOmitDmlAudit(true);
replaceInput.setRecords(newThings);
replaceInput.setFilter(null);
replaceInput.setPerformDeletes(false);
ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput);
assertEquals(1, replaceOutput.getInsertOutput().getRecords().size());
assertEquals(1, replaceOutput.getUpdateOutput().getRecords().size());
assertNull(replaceOutput.getDeleteOutput());
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testTwoKeysWithNullMatching() throws QException
{
String tableName = TestUtils.TABLE_NAME_TWO_KEYS;
////////////////////////////////
// start with these 2 records //
////////////////////////////////
new InsertAction().execute(new InsertInput(tableName).withRecords(List.of(
new QRecord().withValue("key1", 1).withValue("key2", 2),
new QRecord().withValue("key1", 3)
)));
////////////////////////////////////////////////////
// now do a replace action that just updates them //
////////////////////////////////////////////////////
List<QRecord> newThings = List.of(
new QRecord().withValue("key1", 1).withValue("key2", 2),
new QRecord().withValue("key1", 3)
);
///////////////////////////////////////////////
// replace treating null key values as equal //
///////////////////////////////////////////////
ReplaceInput replaceInput = new ReplaceInput();
replaceInput.setTableName(tableName);
replaceInput.setKey(new UniqueKey("key1", "key2"));
replaceInput.setOmitDmlAudit(true);
replaceInput.setRecords(newThings);
replaceInput.setFilter(null);
replaceInput.setAllowNullKeyValuesToEqual(true);
ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput);
assertEquals(0, replaceOutput.getInsertOutput().getRecords().size());
assertEquals(2, replaceOutput.getUpdateOutput().getRecords().size());
assertEquals(0, replaceOutput.getDeleteOutput().getDeletedRecordCount());
}
/*******************************************************************************
**
*******************************************************************************/
@ -297,4 +425,4 @@ class ReplaceActionTest extends BaseTest
return new GetAction().executeForRecord(new GetInput(TestUtils.TABLE_NAME_PERSON_MEMORY).withUniqueKey(Map.of("firstName", firstName, "lastName", lastName))).getValueInteger("noOfShoes");
}
}
}

View File

@ -138,6 +138,7 @@ public class TestUtils
public static final String APP_NAME_PEOPLE = "peopleApp";
public static final String APP_NAME_MISCELLANEOUS = "miscellaneous";
public static final String TABLE_NAME_TWO_KEYS = "twoKeys";
public static final String TABLE_NAME_PERSON = "person";
public static final String TABLE_NAME_SHAPE = "shape";
public static final String TABLE_NAME_SHAPE_CACHE = "shapeCache";
@ -196,6 +197,7 @@ public class TestUtils
qInstance.addBackend(defineMemoryBackend());
qInstance.addTable(defineTablePerson());
qInstance.addTable(defineTableTwoKeys());
qInstance.addTable(definePersonFileTable());
qInstance.addTable(definePersonMemoryTable());
qInstance.addTable(definePersonMemoryCacheTable());
@ -545,6 +547,24 @@ public class TestUtils
/*******************************************************************************
** Define the 'two key' table used in standard tests.
*******************************************************************************/
public static QTableMetaData defineTableTwoKeys()
{
return new QTableMetaData()
.withName(TABLE_NAME_TWO_KEYS)
.withLabel("Two Keys")
.withBackendName(MEMORY_BACKEND_NAME)
.withPrimaryKeyField("id")
.withField(new QFieldMetaData("id", QFieldType.INTEGER).withIsEditable(false))
.withUniqueKey(new UniqueKey("key1", "key2"))
.withField(new QFieldMetaData("key1", QFieldType.INTEGER))
.withField(new QFieldMetaData("key2", QFieldType.INTEGER));
}
/*******************************************************************************
** Define the 'person' table used in standard tests.
*******************************************************************************/
@ -791,6 +811,26 @@ public class TestUtils
/*******************************************************************************
** Define a table with unique key where one is nullable
*******************************************************************************/
public static QTableMetaData defineTwoKeyTable()
{
return (new QTableMetaData()
.withName(TABLE_NAME_BASEPULL)
.withLabel("Basepull Test")
.withPrimaryKeyField("id")
.withBackendName(MEMORY_BACKEND_NAME)
.withFields(TestUtils.defineTablePerson().getFields()))
.withField(new QFieldMetaData("id", QFieldType.INTEGER).withIsEditable(false))
.withField(new QFieldMetaData("createDate", QFieldType.DATE_TIME).withBackendName("create_date").withIsEditable(false))
.withField(new QFieldMetaData("modifyDate", QFieldType.DATE_TIME).withBackendName("modify_date").withIsEditable(false))
.withField(new QFieldMetaData(BASEPULL_KEY_FIELD_NAME, QFieldType.STRING).withBackendName("process_name").withIsRequired(true))
.withField(new QFieldMetaData(BASEPULL_LAST_RUN_TIME_FIELD_NAME, QFieldType.DATE_TIME).withBackendName("last_run_time").withIsRequired(true));
}
/*******************************************************************************
** Define a basepullTable
*******************************************************************************/