Skip to content

Commit ac599f2

Browse files
Add explicit test coverage for trigger script type conversion (#2487)
1 parent 03fa80e commit ac599f2

2 files changed

Lines changed: 52 additions & 2 deletions

File tree

modules/simpletest/resources/queries/lists/People.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,26 @@ if (extraContext)
1111

1212
var LABKEY = require("labkey");
1313

14+
// Issue 52098 - do custom parsing to validate trigger script gets a chance to do type conversion
15+
function stripPrefix(row)
16+
{
17+
if (row.Age && row.Age.toString().indexOf("RemoveMe") === 0)
18+
{
19+
row.Age = row.Age.substring("RemoveMe".length);
20+
}
21+
if (row.FavoriteDateTime && row.FavoriteDateTime.toString().indexOf("RemoveMe") === 0)
22+
{
23+
row.FavoriteDateTime = row.FavoriteDateTime.substring("RemoveMe".length);
24+
}
25+
}
26+
1427
function beforeInsert(row, errors)
1528
{
1629
// Test row map is case-insensitive
1730
if (row.Name != row.nAmE)
1831
throw new Error("beforeInsert row properties must be case-insensitive.");
1932

33+
stripPrefix(row);
2034

2135
// var result = LABKEY.Query.deleteRows({
2236
// schemaName: "lists",
@@ -42,6 +56,8 @@ function beforeUpdate(row, oldRow, errors)
4256
// Test oldRow map is case-insensitive
4357
if (oldRow.Name != oldRow.nAmE)
4458
throw new Error("beforeUpdate oldRow properties must be case-insensitive.");
59+
60+
stripPrefix(row);
4561
}
4662

4763
function afterUpdate(row, oldRow, errors)

src/org/labkey/test/tests/TriggerScriptTest.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public class TriggerScriptTest extends BaseWebDriverTest
8686

8787
private static final String COMMENTS_FIELD = "Comments";
8888
private static final String COUNTRY_FIELD = "Country";
89+
public static final String PEOPLE_LIST_NAME = "People";
8990

9091
protected final PortalHelper _portalHelper = new PortalHelper(this);
9192

@@ -171,6 +172,7 @@ public static void projectSetup()
171172
init.doSetup();
172173
}
173174

175+
174176
protected void doSetup()
175177
{
176178
_containerHelper.createProject(getProjectName(), null);
@@ -188,10 +190,11 @@ protected void doSetup()
188190

189191
_listHelper.createList(getProjectName(), LIST_NAME, "Key", columns);
190192

191-
log("Create list in subfolder to prevent query validation failure");
192-
_listHelper.createList(getProjectName(), "People", "Key",
193+
log("Create the People list");
194+
_listHelper.createList(getProjectName(), PEOPLE_LIST_NAME, "Key",
193195
new FieldDefinition("Name", ColumnType.String).setDescription("Name"),
194196
new FieldDefinition("Age", ColumnType.Integer).setDescription("Age"),
197+
new FieldDefinition("FavoriteDateTime", ColumnType.DateAndTime).setDescription("Favorite date time. Who doesn't have one?"),
195198
new FieldDefinition("Crazy", ColumnType.Boolean).setDescription("Crazy?"));
196199

197200
importFolderFromZip(TestFileUtils.getSampleData("studies/LabkeyDemoStudy.zip"));
@@ -301,7 +304,38 @@ public void testListImportTriggers()
301304
cleanUpListRows();
302305
}
303306

307+
/** Issue 52098 - ensure trigger scripts have a chance to do custom type conversion with the incoming row */
304308
@Test
309+
public void testListAPITriggerTypeConversion() throws Exception
310+
{
311+
Connection cn = WebTestHelper.getRemoteApiConnection();
312+
313+
// Insert a row with a value that can only be handled by the trigger script to make sure it gets a chance
314+
// to do the conversion. People.js should strip the "RemoveMe" prefix from Age and FavoriteDateTime
315+
InsertRowsCommand insCmd = new InsertRowsCommand(LIST_SCHEMA, PEOPLE_LIST_NAME);
316+
insCmd.addRow(Map.of("Name", "Jimbo", "Age", "RemoveMe25", "FavoriteDateTime", "RemoveMe2025-06-11 11:42", "Crazy", "true"));
317+
SaveRowsResponse insResp = insCmd.execute(cn, getProjectName());
318+
List<Map<String, Object>> insertedRows = insResp.getRows();
319+
Assert.assertEquals(1, insertedRows.size());
320+
321+
Map<String, Object> insertedRow = insertedRows.get(0);
322+
Assert.assertEquals("Jimbo", insertedRow.get("Name"));
323+
Assert.assertEquals(25, insertedRow.get("Age"));
324+
Assert.assertEquals("2025-06-11 11:42:00.000", insertedRow.get("FavoriteDateTime"));
325+
326+
// Validate update too
327+
UpdateRowsCommand upCmd = new UpdateRowsCommand(LIST_SCHEMA, PEOPLE_LIST_NAME);
328+
insertedRow.put("Age", "RemoveMe26");
329+
upCmd.addRow(insertedRow);
330+
SaveRowsResponse upResp = upCmd.execute(cn, getProjectName());
331+
List<Map<String, Object>> updatedRows = upResp.getRows();
332+
Assert.assertEquals(1, updatedRows.size());
333+
334+
Map<String, Object> updatedRow = updatedRows.get(0);
335+
Assert.assertEquals(26, updatedRow.get("Age"));
336+
}
337+
338+
@Test
305339
public void testListAPITriggers() throws Exception
306340
{
307341
String ssn1 = "111111112";

0 commit comments

Comments
 (0)