Skip to content

Commit 8d08d90

Browse files
author
苏义超
committed
add unit test
1 parent d274074 commit 8d08d90

File tree

1 file changed

+52
-1
lines changed
  • dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/test/java/org/apache/dolphinscheduler/plugin/task/sql

1 file changed

+52
-1
lines changed

dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/test/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTaskTest.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,30 @@ void testGenerateEmptyRow_WithNullResultSet_ReturnsErrorObject() throws Exceptio
250250
Assertions.assertEquals("resultSet is null", row.get("error").asText());
251251
}
252252

253+
@Test
254+
void testGenerateEmptyRow_WithDuplicateColumns_DeduplicatesLabels() throws Exception {
255+
ResultSet mockResultSet = mock(ResultSet.class);
256+
ResultSetMetaData mockMetaData = mock(ResultSetMetaData.class);
257+
258+
when(mockResultSet.getMetaData()).thenReturn(mockMetaData);
259+
when(mockMetaData.getColumnCount()).thenReturn(3);
260+
when(mockMetaData.getColumnLabel(1)).thenReturn("id");
261+
when(mockMetaData.getColumnLabel(2)).thenReturn("id"); // duplicate
262+
when(mockMetaData.getColumnLabel(3)).thenReturn("name");
263+
264+
Method method = SqlTask.class.getDeclaredMethod("generateEmptyRow", ResultSet.class);
265+
method.setAccessible(true);
266+
267+
ArrayNode result = (ArrayNode) method.invoke(sqlTask, mockResultSet);
268+
269+
Assertions.assertNotNull(result);
270+
Assertions.assertEquals(1, result.size());
271+
272+
ObjectNode row = (ObjectNode) result.get(0);
273+
Assertions.assertTrue(row.has("id"));
274+
Assertions.assertTrue(row.has("name"));
275+
}
276+
253277
@Test
254278
void testResultProcess_NullResultSet_ReturnsEmptyResult() throws Exception {
255279
Method resultProcessMethod = SqlTask.class.getDeclaredMethod("resultProcess", ResultSet.class);
@@ -329,6 +353,33 @@ void testResultProcess_ColumnLabelIsEmpty_UsesGenericName() throws Exception {
329353
Assertions.assertTrue(result.contains("\"col_1\":\"value1\""));
330354
}
331355

356+
@Test
357+
void testResultProcess_MultipleNullColumnLabels_GeneratesUniqueGenericNames() throws Exception {
358+
ResultSet mockRs = mock(ResultSet.class);
359+
ResultSetMetaData mockMd = mock(ResultSetMetaData.class);
360+
361+
when(mockRs.getMetaData()).thenReturn(mockMd);
362+
when(mockMd.getColumnCount()).thenReturn(3);
363+
when(mockMd.getColumnLabel(1)).thenReturn(null);
364+
when(mockMd.getColumnLabel(2)).thenReturn("");
365+
when(mockMd.getColumnLabel(3)).thenReturn(null);
366+
367+
when(mockRs.next()).thenReturn(true, false);
368+
when(mockRs.getObject(1)).thenReturn("val1");
369+
when(mockRs.getObject(2)).thenReturn("val2");
370+
when(mockRs.getObject(3)).thenReturn("val3");
371+
372+
Method method = SqlTask.class.getDeclaredMethod("resultProcess", ResultSet.class);
373+
method.setAccessible(true);
374+
375+
String result = (String) method.invoke(sqlTask, mockRs);
376+
377+
Assertions.assertNotNull(result);
378+
Assertions.assertTrue(result.contains("\"col_1\":\"val1\""));
379+
Assertions.assertTrue(result.contains("\"col_2\":\"val2\""));
380+
Assertions.assertTrue(result.contains("\"col_3\":\"val3\""));
381+
}
382+
332383
@Test
333384
void testResultProcess_SingleRowWithDuplicateColumns_GeneratesUniqueKeys() throws Exception {
334385
ResultSet mockResultSet = mock(ResultSet.class);
@@ -390,7 +441,7 @@ void testResultProcess_DuplicateColumnLabels_AreDeduplicated() throws Exception
390441
}
391442

392443
@Test
393-
void testResultProcess_ColumnNames_IdIdId2Id2Id3_HandlesSuffixCollisionCorrectly() throws Exception {
444+
void testResultProcess_HandlesSuffixCollisionWithDuplicateColumns() throws Exception {
394445
// Arrange: ResultSet with column labels: id, id, id_2, id_2, id_3
395446
ResultSet mockRs = mock(ResultSet.class);
396447
ResultSetMetaData mockMd = mock(ResultSetMetaData.class);

0 commit comments

Comments
 (0)