You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/07/05 03:38:41 UTC
[shardingsphere] branch master updated: Improve unit test of MySQLInventoryDumper (#18816)
This is an automated email from the ASF dual-hosted git repository.
zhonghongsheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 7d1a160f748 Improve unit test of MySQLInventoryDumper (#18816)
7d1a160f748 is described below
commit 7d1a160f7486e24339c96f1eff797fa1b03149c3
Author: DaneBrown <ta...@163.com>
AuthorDate: Tue Jul 5 11:38:35 2022 +0800
Improve unit test of MySQLInventoryDumper (#18816)
---
...mperTest.java => MySQLInventoryDumperTest.java} | 46 ++++++++++++++--------
1 file changed, 29 insertions(+), 17 deletions(-)
diff --git a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLJdbcDumperTest.java b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLInventoryDumperTest.java
similarity index 78%
rename from shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLJdbcDumperTest.java
rename to shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLInventoryDumperTest.java
index 32c2130dba1..0021588eea6 100644
--- a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLJdbcDumperTest.java
+++ b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-dialect/shardingsphere-data-pipeline-mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLInventoryDumperTest.java
@@ -17,6 +17,15 @@
package org.apache.shardingsphere.data.pipeline.mysql.ingest;
+import java.sql.Connection;
+import java.sql.Date;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.Types;
+import javax.sql.DataSource;
import lombok.SneakyThrows;
import org.apache.shardingsphere.data.pipeline.api.config.ingest.DumperConfiguration;
import org.apache.shardingsphere.data.pipeline.api.config.ingest.InventoryDumperConfiguration;
@@ -30,21 +39,13 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
-import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.sql.Types;
-
+import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
-public final class MySQLJdbcDumperTest {
+public final class MySQLInventoryDumperTest {
private MySQLInventoryDumper mysqlJdbcDumper;
@@ -53,8 +54,7 @@ public final class MySQLJdbcDumperTest {
PipelineDataSourceManager dataSourceManager = new PipelineDataSourceManager();
InventoryDumperConfiguration dumperConfig = mockInventoryDumperConfiguration();
PipelineDataSourceWrapper dataSource = dataSourceManager.getDataSource(dumperConfig.getDataSourceConfig());
- mysqlJdbcDumper = new MySQLInventoryDumper(mockInventoryDumperConfiguration(), new SimpleMemoryPipelineChannel(100),
- dataSource, new PipelineTableMetaDataLoader(dataSource));
+ mysqlJdbcDumper = new MySQLInventoryDumper(mockInventoryDumperConfiguration(), new SimpleMemoryPipelineChannel(100), dataSource, new PipelineTableMetaDataLoader(dataSource));
initTableData(dataSource);
}
@@ -74,9 +74,7 @@ public final class MySQLJdbcDumperTest {
@SneakyThrows(SQLException.class)
private void initTableData(final DataSource dataSource) {
- try (
- Connection connection = dataSource.getConnection();
- Statement statement = connection.createStatement()) {
+ try (Connection connection = dataSource.getConnection(); Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE IF EXISTS t_order");
statement.execute("CREATE TABLE t_order (order_id INT PRIMARY KEY, user_id VARCHAR(12))");
statement.execute("INSERT INTO t_order (order_id, user_id) VALUES (1, 'xxx'), (999, 'yyy')");
@@ -85,15 +83,29 @@ public final class MySQLJdbcDumperTest {
@Test
public void assertReadValue() throws SQLException {
+ String mockDateString = "2022-6-30";
+ Object mockObject = new Object();
+ Date mockDate = Date.valueOf(mockDateString);
ResultSet resultSet = mock(ResultSet.class);
+ String yearDataType = "YEAR";
ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class);
+ when(resultSet.wasNull()).thenReturn(false);
when(resultSet.getMetaData()).thenReturn(resultSetMetaData);
+ when(resultSet.getString(1)).thenReturn(mockDateString);
+ when(resultSet.getObject(2)).thenReturn(mockObject);
+ when(resultSet.getObject(3)).thenReturn(mockDate);
when(resultSetMetaData.getColumnType(1)).thenReturn(Types.TIMESTAMP);
when(resultSetMetaData.getColumnType(2)).thenReturn(Types.VARCHAR);
- mysqlJdbcDumper.readValue(resultSet, 1);
- mysqlJdbcDumper.readValue(resultSet, 2);
+ when(resultSetMetaData.getColumnTypeName(3)).thenReturn(yearDataType);
+ String resultTimeStamp = (String) mysqlJdbcDumper.readValue(resultSet, 1);
+ Object resultObject = mysqlJdbcDumper.readValue(resultSet, 2);
+ Date resultDate = (Date) mysqlJdbcDumper.readValue(resultSet, 3);
+ assertEquals(resultTimeStamp, mockDateString);
+ assertEquals(resultObject, mockObject);
+ assertEquals(resultDate, mockDate);
verify(resultSet).getString(1);
verify(resultSet).getObject(2);
+ verify(resultSet).getObject(3);
}
@Test