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 1c7876b2447 Refactor StandardPipelineTableMetaDataLoaderTest use H2 to
do real test (#27983)
1c7876b2447 is described below
commit 1c7876b24472c8e3f4723f489834a95858019096
Author: Xinze Guo <[email protected]>
AuthorDate: Tue Aug 8 13:52:45 2023 +0800
Refactor StandardPipelineTableMetaDataLoaderTest use H2 to do real test
(#27983)
---
.../StandardPipelineTableMetaDataLoader.java | 2 +-
.../StandardPipelineTableMetaDataLoaderTest.java | 160 ------------------
.../StandardPipelineTableMetaDataLoaderTest.java | 178 +++++++++++++++++++++
3 files changed, 179 insertions(+), 161 deletions(-)
diff --git
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/metadata/loader/StandardPipelineTableMetaDataLoader.java
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/metadata/loader/StandardPipelineTableMetaDataLoader.java
index a15781ecf2c..f68c6d312e6 100644
---
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/metadata/loader/StandardPipelineTableMetaDataLoader.java
+++
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/metadata/loader/StandardPipelineTableMetaDataLoader.java
@@ -106,7 +106,7 @@ public final class StandardPipelineTableMetaDataLoader
implements PipelineTableM
String dataTypeName = resultSet.getString("TYPE_NAME");
boolean primaryKey = primaryKeys.contains(columnName);
boolean isNullable =
"YES".equals(resultSet.getString("IS_NULLABLE"));
- boolean isUniqueKey = primaryKey ||
uniqueKeys.values().stream().anyMatch(names -> names.contains(columnName));
+ boolean isUniqueKey =
uniqueKeys.values().stream().anyMatch(names -> names.contains(columnName));
PipelineColumnMetaData columnMetaData = new
PipelineColumnMetaData(ordinalPosition, columnName, dataType, dataTypeName,
isNullable, primaryKey, isUniqueKey);
columnMetaDataMap.put(columnName, columnMetaData);
}
diff --git
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/common/metadata/loader/StandardPipelineTableMetaDataLoaderTest.java
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/common/metadata/loader/StandardPipelineTableMetaDataLoaderTest.java
deleted file mode 100644
index 8a718b8eb7c..00000000000
---
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/common/metadata/loader/StandardPipelineTableMetaDataLoaderTest.java
+++ /dev/null
@@ -1,160 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.data.pipeline.common.metadata.loader;
-
-import
org.apache.shardingsphere.data.pipeline.api.metadata.loader.PipelineTableMetaDataLoader;
-import
org.apache.shardingsphere.data.pipeline.api.metadata.model.PipelineColumnMetaData;
-import
org.apache.shardingsphere.data.pipeline.api.metadata.model.PipelineIndexMetaData;
-import
org.apache.shardingsphere.data.pipeline.api.metadata.model.PipelineTableMetaData;
-import
org.apache.shardingsphere.data.pipeline.common.datasource.PipelineDataSourceWrapper;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.junit.jupiter.MockitoExtension;
-
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.sql.Types;
-import java.util.Collection;
-import java.util.List;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-@ExtendWith(MockitoExtension.class)
-// TODO use H2 to do real test
-class StandardPipelineTableMetaDataLoaderTest {
-
- private static final String TEST_CATALOG = "catalog";
-
- private static final String ORDINAL_POSITION = "ORDINAL_POSITION";
-
- private static final String COLUMN_NAME = "COLUMN_NAME";
-
- private static final String DATA_TYPE = "DATA_TYPE";
-
- private static final String TABLE_NAME = "TABLE_NAME";
-
- private static final String INDEX_NAME = "INDEX_NAME";
-
- private static final String TEST_TABLE = "test";
-
- private static final String TEST_INDEX = "idx_test";
-
- private PipelineDataSourceWrapper dataSource;
-
- @BeforeEach
- void setUp() throws SQLException {
- dataSource = new PipelineDataSourceWrapper(new
MockedDataSource(mockConnection()),
TypedSPILoader.getService(DatabaseType.class, "H2"));
- }
-
- private Connection mockConnection() throws SQLException {
- Connection result = mock(Connection.class);
- when(result.getCatalog()).thenReturn(TEST_CATALOG);
- DatabaseMetaData databaseMetaData = mockDatabaseMetaData();
- when(result.getMetaData()).thenReturn(databaseMetaData);
- return result;
- }
-
- private DatabaseMetaData mockDatabaseMetaData() throws SQLException {
- DatabaseMetaData result = mock(DatabaseMetaData.class);
- ResultSet columnMetaDataResultSet = mockColumnMetaDataResultSet();
- when(result.getColumns(TEST_CATALOG, null, TEST_TABLE,
"%")).thenReturn(columnMetaDataResultSet);
- ResultSet primaryKeyResultSet = mockPrimaryKeyResultSet();
- when(result.getPrimaryKeys(TEST_CATALOG, null,
TEST_TABLE)).thenReturn(primaryKeyResultSet);
- ResultSet indexInfoResultSet = mockIndexInfoResultSet();
- when(result.getIndexInfo(TEST_CATALOG, null, TEST_TABLE, true,
false)).thenReturn(indexInfoResultSet);
- ResultSet mockTableResultSet = mockTableResultSet();
- when(result.getTables(TEST_CATALOG, null, TEST_TABLE,
null)).thenReturn(mockTableResultSet);
- return result;
- }
-
- private ResultSet mockColumnMetaDataResultSet() throws SQLException {
- ResultSet result = mock(ResultSet.class);
- when(result.next()).thenReturn(true, true, true, false);
- when(result.getInt(ORDINAL_POSITION)).thenReturn(1, 2, 3);
- when(result.getString(COLUMN_NAME)).thenReturn("id", "name", "age");
- when(result.getInt(DATA_TYPE)).thenReturn(Types.BIGINT, Types.VARCHAR,
Types.INTEGER);
- return result;
- }
-
- private ResultSet mockPrimaryKeyResultSet() throws SQLException {
- ResultSet result = mock(ResultSet.class);
- when(result.next()).thenReturn(true, false);
- when(result.getString(COLUMN_NAME)).thenReturn("id");
- return result;
- }
-
- private ResultSet mockIndexInfoResultSet() throws SQLException {
- ResultSet result = mock(ResultSet.class);
- when(result.next()).thenReturn(true, true, false);
- when(result.getString(INDEX_NAME)).thenReturn(TEST_INDEX);
- when(result.getString(COLUMN_NAME)).thenReturn("name", "id");
- when(result.getShort(ORDINAL_POSITION)).thenReturn((short) 2, (short)
1);
- return result;
- }
-
- private ResultSet mockTableResultSet() throws SQLException {
- ResultSet result = mock(ResultSet.class);
- when(result.next()).thenReturn(true, false);
- when(result.getString(TABLE_NAME)).thenReturn(TEST_TABLE);
- return result;
- }
-
- @Test
- void assertGetTableMetaData() {
- PipelineTableMetaDataLoader metaDataLoader = new
StandardPipelineTableMetaDataLoader(dataSource);
- PipelineTableMetaData tableMetaData =
metaDataLoader.getTableMetaData(null, TEST_TABLE);
- assertColumnMetaData(tableMetaData);
- assertPrimaryKeys(tableMetaData.getPrimaryKeyColumns());
- assertIndexMetaData(tableMetaData.getUniqueIndexes());
- }
-
- private void assertPrimaryKeys(final List<String> actual) {
- assertThat(actual.size(), is(1));
- assertThat(actual.get(0), is("id"));
- }
-
- private void assertColumnMetaData(final PipelineTableMetaData actual) {
- assertThat(actual.getColumnNames().size(), is(3));
- assertColumnMetaData(actual.getColumnMetaData(1), "id", Types.BIGINT);
- assertColumnMetaData(actual.getColumnMetaData(2), "name",
Types.VARCHAR);
- assertColumnMetaData(actual.getColumnMetaData(3), "age",
Types.INTEGER);
- }
-
- private void assertColumnMetaData(final PipelineColumnMetaData actual,
final String expectedName, final int expectedType) {
- assertThat(actual.getName(), is(expectedName));
- assertThat(actual.getDataType(), is(expectedType));
- }
-
- private void assertIndexMetaData(final Collection<PipelineIndexMetaData>
actualUniqueIndexes) {
- assertThat(actualUniqueIndexes.size(), is(1));
- PipelineIndexMetaData actualIndexMetaData =
actualUniqueIndexes.iterator().next();
- assertThat(actualIndexMetaData.getName(), is(TEST_INDEX));
- assertThat(actualIndexMetaData.getColumns().size(), is(2));
- assertThat(actualIndexMetaData.getColumns().get(0).getName(),
is("id"));
- assertThat(actualIndexMetaData.getColumns().get(1).getName(),
is("name"));
- }
-}
diff --git
a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/common/metadata/loader/StandardPipelineTableMetaDataLoaderTest.java
b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/common/metadata/loader/StandardPipelineTableMetaDataLoaderTest.java
new file mode 100644
index 00000000000..b34ecfdd379
--- /dev/null
+++
b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/common/metadata/loader/StandardPipelineTableMetaDataLoaderTest.java
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.test.it.data.pipeline.common.metadata.loader;
+
+import com.zaxxer.hikari.HikariDataSource;
+import lombok.SneakyThrows;
+import
org.apache.shardingsphere.data.pipeline.api.metadata.model.PipelineColumnMetaData;
+import
org.apache.shardingsphere.data.pipeline.api.metadata.model.PipelineIndexMetaData;
+import
org.apache.shardingsphere.data.pipeline.api.metadata.model.PipelineTableMetaData;
+import
org.apache.shardingsphere.data.pipeline.common.datasource.PipelineDataSourceWrapper;
+import
org.apache.shardingsphere.data.pipeline.common.metadata.loader.StandardPipelineTableMetaDataLoader;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.Collection;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class StandardPipelineTableMetaDataLoaderTest {
+
+ private PipelineDataSourceWrapper dataSource;
+
+ @BeforeEach
+ void setUp() {
+ dataSource = new PipelineDataSourceWrapper(createHikariDataSource(),
TypedSPILoader.getService(DatabaseType.class, "H2"));
+ }
+
+ private HikariDataSource createHikariDataSource() {
+ HikariDataSource result = new HikariDataSource();
+
result.setJdbcUrl("jdbc:h2:mem:standard;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+ result.setUsername("root");
+ result.setPassword("root");
+ result.setMaximumPoolSize(10);
+ result.setMinimumIdle(2);
+ result.setConnectionTimeout(15 * 1000L);
+ result.setIdleTimeout(40 * 1000L);
+ return result;
+ }
+
+ @AfterEach
+ @SneakyThrows(SQLException.class)
+ void cleanUp() {
+ try (Connection connection = dataSource.getConnection()) {
+ connection.createStatement().execute("DROP TABLE IF EXISTS
t_order");
+ }
+ }
+
+ @Test
+ @SneakyThrows(SQLException.class)
+ void assertLoadPrimaryKey() {
+ try (Connection connection = dataSource.getConnection()) {
+ connection.createStatement().execute("CREATE TABLE t_order
(order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY
(order_id))");
+ }
+ StandardPipelineTableMetaDataLoader loader = new
StandardPipelineTableMetaDataLoader(dataSource);
+ PipelineTableMetaData actual = loader.getTableMetaData(null,
"t_order");
+ assertThat(actual.getPrimaryKeyColumns().get(0), is("order_id"));
+ assertThat(actual.getColumnNames().size(), is(3));
+ Collection<PipelineIndexMetaData> uniqueIndexes =
actual.getUniqueIndexes();
+ assertThat(uniqueIndexes.size(), is(1));
+ PipelineIndexMetaData indexMetaData = uniqueIndexes.iterator().next();
+ assertThat(indexMetaData.getColumns().size(), is(1));
+ assertThat(indexMetaData.getColumns().get(0).getName(),
is("order_id"));
+ assertOrderId(actual.getColumnMetaData("order_id"), true);
+ assertUserId(actual.getColumnMetaData("user_id"), false, false);
+ assertStatus(actual.getColumnMetaData("status"));
+ }
+
+ private void assertOrderId(final PipelineColumnMetaData orderIdColumn,
final boolean expectedIsPrimaryKey) {
+ assertThat(orderIdColumn.getOrdinalPosition(), is(1));
+ assertThat(orderIdColumn.getName(), is("order_id"));
+ assertThat(orderIdColumn.getDataType(), is(Types.INTEGER));
+ assertFalse(orderIdColumn.isNullable());
+ assertEquals(orderIdColumn.isPrimaryKey(), expectedIsPrimaryKey);
+ assertTrue(orderIdColumn.isUniqueKey());
+ }
+
+ private void assertUserId(final PipelineColumnMetaData userIdColumn, final
boolean expectedIsPrimaryKey, final boolean expectedIsUniqueKey) {
+ assertThat(userIdColumn.getOrdinalPosition(), is(2));
+ assertThat(userIdColumn.getName(), is("user_id"));
+ assertThat(userIdColumn.getDataType(), is(Types.INTEGER));
+ assertFalse(userIdColumn.isNullable());
+ assertEquals(userIdColumn.isPrimaryKey(), expectedIsPrimaryKey);
+ assertEquals(userIdColumn.isUniqueKey(), expectedIsUniqueKey);
+ }
+
+ private void assertStatus(final PipelineColumnMetaData statusColumn) {
+ assertThat(statusColumn.getOrdinalPosition(), is(3));
+ assertThat(statusColumn.getName(), is("status"));
+ assertThat(statusColumn.getDataType(), is(Types.VARCHAR));
+ assertTrue(statusColumn.isNullable());
+ assertFalse(statusColumn.isPrimaryKey());
+ assertFalse(statusColumn.isUniqueKey());
+ }
+
+ @Test
+ @SneakyThrows(SQLException.class)
+ void assertLoadUniqueKey() {
+ try (Connection connection = dataSource.getConnection()) {
+ connection.createStatement().execute("CREATE TABLE t_order
(order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), UNIQUE KEY
(order_id))");
+ }
+ StandardPipelineTableMetaDataLoader loader = new
StandardPipelineTableMetaDataLoader(dataSource);
+ PipelineTableMetaData actual = loader.getTableMetaData(null,
"t_order");
+ assertTrue(actual.getPrimaryKeyColumns().isEmpty());
+ assertThat(actual.getColumnNames().size(), is(3));
+ Collection<PipelineIndexMetaData> uniqueIndexes =
actual.getUniqueIndexes();
+ PipelineIndexMetaData indexMetaData = uniqueIndexes.iterator().next();
+ assertThat(indexMetaData.getColumns().size(), is(1));
+ assertThat(indexMetaData.getColumns().get(0).getName(),
is("order_id"));
+ assertOrderId(actual.getColumnMetaData("order_id"), false);
+ assertUserId(actual.getColumnMetaData("user_id"), false, false);
+ assertStatus(actual.getColumnMetaData("status"));
+ }
+
+ @Test
+ @SneakyThrows(SQLException.class)
+ void assertLoadCompoundPrimaryKey() {
+ try (Connection connection = dataSource.getConnection()) {
+ connection.createStatement().execute("CREATE TABLE t_order
(order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY
(order_id, user_id))");
+ }
+ StandardPipelineTableMetaDataLoader loader = new
StandardPipelineTableMetaDataLoader(dataSource);
+ PipelineTableMetaData actual = loader.getTableMetaData(null,
"t_order");
+ assertThat(actual.getColumnNames().size(), is(3));
+ assertThat(actual.getPrimaryKeyColumns().size(), is(2));
+ Collection<PipelineIndexMetaData> uniqueIndexes =
actual.getUniqueIndexes();
+ assertThat(uniqueIndexes.size(), is(1));
+ PipelineIndexMetaData indexMetaData = uniqueIndexes.iterator().next();
+ assertThat(indexMetaData.getColumns().get(0).getName(),
is("order_id"));
+ assertThat(indexMetaData.getColumns().get(1).getName(), is("user_id"));
+ assertOrderId(actual.getColumnMetaData("order_id"), true);
+ assertUserId(actual.getColumnMetaData("user_id"), true, true);
+ assertStatus(actual.getColumnMetaData("status"));
+ }
+
+ @Test
+ @SneakyThrows(SQLException.class)
+ void assertLoadCompoundUniqueKey() {
+ try (Connection connection = dataSource.getConnection()) {
+ connection.createStatement().execute("CREATE TABLE t_order
(order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), UNIQUE KEY
(order_id, user_id))");
+ }
+ StandardPipelineTableMetaDataLoader loader = new
StandardPipelineTableMetaDataLoader(dataSource);
+ PipelineTableMetaData actual = loader.getTableMetaData(null,
"t_order");
+ assertTrue(actual.getPrimaryKeyColumns().isEmpty());
+ assertThat(actual.getColumnNames().size(), is(3));
+ Collection<PipelineIndexMetaData> uniqueIndexes =
actual.getUniqueIndexes();
+ assertThat(uniqueIndexes.size(), is(1));
+ PipelineIndexMetaData indexMetaData = uniqueIndexes.iterator().next();
+ assertThat(indexMetaData.getColumns().get(0).getName(),
is("order_id"));
+ assertThat(indexMetaData.getColumns().get(1).getName(), is("user_id"));
+ assertOrderId(actual.getColumnMetaData("order_id"), false);
+ assertUserId(actual.getColumnMetaData("user_id"), false, true);
+ assertStatus(actual.getColumnMetaData("status"));
+ }
+}