This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 09585ff9548 Add test cases on InventoryColumnValueReader (#33414)
09585ff9548 is described below
commit 09585ff9548aff1e7b6d850849ed865adcd6042c
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Oct 26 19:50:31 2024 +0800
Add test cases on InventoryColumnValueReader (#33414)
* Add test cases on MySQLInventoryColumnValueReader
* Add test cases on OpenGaussInventoryColumnValueReader
* Add test cases on OpenGaussInventoryColumnValueReader
---
.../inventory/MySQLInventoryColumnValueReader.java | 4 +-
.../MySQLInventoryColumnValueReaderTest.java | 56 ++++++++++++
.../OpenGaussInventoryColumnValueReader.java | 16 ++--
.../OpenGaussInventoryColumnValueReaderTest.java | 77 +++++++++++++++++
.../PostgreSQLInventoryColumnValueReader.java | 8 +-
.../PostgreSQLInventoryColumnValueReaderTest.java | 99 ++++++++++++++++++++++
6 files changed, 246 insertions(+), 14 deletions(-)
diff --git
a/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/inventory/MySQLInventoryColumnValueReader.java
b/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/inventory/MySQLInventoryColumnValueReader.java
index 2a13b984974..ecfb42e9d5c 100644
---
a/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/inventory/MySQLInventoryColumnValueReader.java
+++
b/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/inventory/MySQLInventoryColumnValueReader.java
@@ -33,10 +33,10 @@ public final class MySQLInventoryColumnValueReader
implements DialectInventoryCo
@Override
public Optional<Object> read(final ResultSet resultSet, final
ResultSetMetaData metaData, final int columnIndex) throws SQLException {
- return isYearDataType(metaData.getColumnTypeName(columnIndex)) ?
Optional.of(resultSet.getShort(columnIndex)) : Optional.empty();
+ return isYearType(metaData.getColumnTypeName(columnIndex)) ?
Optional.of(resultSet.getShort(columnIndex)) : Optional.empty();
}
- private boolean isYearDataType(final String columnDataTypeName) {
+ private boolean isYearType(final String columnDataTypeName) {
return YEAR_DATA_TYPE.equalsIgnoreCase(columnDataTypeName);
}
diff --git
a/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/inventory/MySQLInventoryColumnValueReaderTest.java
b/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/inventory/MySQLInventoryColumnValueReaderTest.java
new file mode 100644
index 00000000000..41bef84b7c0
--- /dev/null
+++
b/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/inventory/MySQLInventoryColumnValueReaderTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.mysql.ingest.inventory;
+
+import
org.apache.shardingsphere.data.pipeline.core.ingest.dumper.inventory.column.DialectInventoryColumnValueReader;
+import
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.Test;
+
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class MySQLInventoryColumnValueReaderTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "MySQL");
+
+ private final DialectInventoryColumnValueReader reader =
DatabaseTypedSPILoader.getService(DialectInventoryColumnValueReader.class,
databaseType);
+
+ @Test
+ void assertReadWithYearDataTypeValue() throws SQLException {
+ ResultSet resultSet = mock(ResultSet.class);
+ when(resultSet.getShort(1)).thenReturn((short) 1);
+ ResultSetMetaData metaData = mock(ResultSetMetaData.class);
+ when(metaData.getColumnTypeName(1)).thenReturn("YEAR");
+ assertThat(reader.read(resultSet, metaData, 1), is(Optional.of((short)
1)));
+ }
+
+ @Test
+ void assertReadWithOtherType() throws SQLException {
+ assertFalse(reader.read(mock(ResultSet.class),
mock(ResultSetMetaData.class), 1).isPresent());
+ }
+}
diff --git
a/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/inventory/OpenGaussInventoryColumnValueReader.java
b/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/inventory/OpenGaussInventoryColumnValueReader.java
index aa69d62fa75..fbb5e292ebc 100644
---
a/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/inventory/OpenGaussInventoryColumnValueReader.java
+++
b/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/inventory/OpenGaussInventoryColumnValueReader.java
@@ -30,11 +30,11 @@ import java.util.Optional;
*/
public final class OpenGaussInventoryColumnValueReader implements
DialectInventoryColumnValueReader {
- private static final String MONEY_TYPE = "money";
+ private static final String MONEY_DATA_TYPE = "money";
- private static final String BIT_TYPE = "bit";
+ private static final String BIT_DATA_TYPE = "bit";
- private static final String BOOL_TYPE = "bool";
+ private static final String BOOL_DATA_TYPE = "bool";
@Override
public Optional<Object> read(final ResultSet resultSet, final
ResultSetMetaData metaData, final int columnIndex) throws SQLException {
@@ -52,15 +52,15 @@ public final class OpenGaussInventoryColumnValueReader
implements DialectInvento
}
private boolean isMoneyType(final ResultSetMetaData metaData, final int
columnIndex) throws SQLException {
- return
MONEY_TYPE.equalsIgnoreCase(metaData.getColumnTypeName(columnIndex));
+ return
MONEY_DATA_TYPE.equalsIgnoreCase(metaData.getColumnTypeName(columnIndex));
}
- private boolean isBoolType(final ResultSetMetaData metaData, final int
columnIndex) throws SQLException {
- return
BOOL_TYPE.equalsIgnoreCase(metaData.getColumnTypeName(columnIndex));
+ private boolean isBitType(final ResultSetMetaData metaData, final int
columnIndex) throws SQLException {
+ return Types.BIT == metaData.getColumnType(columnIndex) &&
BIT_DATA_TYPE.equalsIgnoreCase(metaData.getColumnTypeName(columnIndex));
}
- private boolean isBitType(final ResultSetMetaData metaData, final int
columnIndex) throws SQLException {
- return Types.BIT == metaData.getColumnType(columnIndex) &&
BIT_TYPE.equalsIgnoreCase(metaData.getColumnTypeName(columnIndex));
+ private boolean isBoolType(final ResultSetMetaData metaData, final int
columnIndex) throws SQLException {
+ return
BOOL_DATA_TYPE.equalsIgnoreCase(metaData.getColumnTypeName(columnIndex));
}
@Override
diff --git
a/kernel/data-pipeline/dialect/opengauss/src/test/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/inventory/OpenGaussInventoryColumnValueReaderTest.java
b/kernel/data-pipeline/dialect/opengauss/src/test/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/inventory/OpenGaussInventoryColumnValueReaderTest.java
new file mode 100644
index 00000000000..9da95ca817c
--- /dev/null
+++
b/kernel/data-pipeline/dialect/opengauss/src/test/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/inventory/OpenGaussInventoryColumnValueReaderTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.opengauss.ingest.inventory;
+
+import
org.apache.shardingsphere.data.pipeline.core.ingest.dumper.inventory.column.DialectInventoryColumnValueReader;
+import
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class OpenGaussInventoryColumnValueReaderTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "openGauss");
+
+ private final DialectInventoryColumnValueReader reader =
DatabaseTypedSPILoader.getService(DialectInventoryColumnValueReader.class,
databaseType);
+
+ @Test
+ void assertReadWithMoneyDataTypeValue() throws SQLException {
+ ResultSet resultSet = mock(ResultSet.class);
+ when(resultSet.getBigDecimal(1)).thenReturn(new BigDecimal("1"));
+ ResultSetMetaData metaData = mock(ResultSetMetaData.class);
+ when(metaData.getColumnTypeName(1)).thenReturn("money");
+ assertThat(reader.read(resultSet, metaData, 1), is(Optional.of(new
BigDecimal("1"))));
+ }
+
+ @Test
+ void assertReadWithBitDataTypeValue() throws SQLException {
+ ResultSet resultSet = mock(ResultSet.class);
+ when(resultSet.getString(1)).thenReturn("1");
+ ResultSetMetaData metaData = mock(ResultSetMetaData.class);
+ when(metaData.getColumnType(1)).thenReturn(Types.BIT);
+ when(metaData.getColumnTypeName(1)).thenReturn("bit");
+ assertThat(reader.read(resultSet, metaData, 1), is(Optional.of("1")));
+ }
+
+ @Test
+ void assertReadWithBoolDataTypeValue() throws SQLException {
+ ResultSet resultSet = mock(ResultSet.class);
+ when(resultSet.getBoolean(1)).thenReturn(true);
+ ResultSetMetaData metaData = mock(ResultSetMetaData.class);
+ when(metaData.getColumnTypeName(1)).thenReturn("bool");
+ assertThat(reader.read(resultSet, metaData, 1), is(Optional.of(true)));
+ }
+
+ @Test
+ void assertReadWithOtherType() throws SQLException {
+ assertFalse(reader.read(mock(ResultSet.class),
mock(ResultSetMetaData.class), 1).isPresent());
+ }
+}
diff --git
a/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/inventory/PostgreSQLInventoryColumnValueReader.java
b/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/inventory/PostgreSQLInventoryColumnValueReader.java
index 1f42bd4bef3..605e4f10cb7 100644
---
a/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/inventory/PostgreSQLInventoryColumnValueReader.java
+++
b/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/inventory/PostgreSQLInventoryColumnValueReader.java
@@ -31,9 +31,9 @@ import java.util.Optional;
*/
public final class PostgreSQLInventoryColumnValueReader implements
DialectInventoryColumnValueReader {
- private static final String PG_MONEY_TYPE = "money";
+ private static final String MONEY_DATA_TYPE = "money";
- private static final String PG_BIT_TYPE = "bit";
+ private static final String BIT_DATA_TYPE = "bit";
@Override
public Optional<Object> read(final ResultSet resultSet, final
ResultSetMetaData metaData, final int columnIndex) throws SQLException {
@@ -47,11 +47,11 @@ public final class PostgreSQLInventoryColumnValueReader
implements DialectInvent
}
private boolean isMoneyType(final ResultSetMetaData metaData, final int
columnIndex) throws SQLException {
- return
PG_MONEY_TYPE.equalsIgnoreCase(metaData.getColumnTypeName(columnIndex));
+ return
MONEY_DATA_TYPE.equalsIgnoreCase(metaData.getColumnTypeName(columnIndex));
}
private boolean isBitType(final ResultSetMetaData metaData, final int
columnIndex) throws SQLException {
- return Types.BIT == metaData.getColumnType(columnIndex) &&
PG_BIT_TYPE.equalsIgnoreCase(metaData.getColumnTypeName(columnIndex));
+ return Types.BIT == metaData.getColumnType(columnIndex) &&
BIT_DATA_TYPE.equalsIgnoreCase(metaData.getColumnTypeName(columnIndex));
}
private static PGobject getBitObject(final ResultSet resultSet, final int
columnIndex) throws SQLException {
diff --git
a/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/inventory/PostgreSQLInventoryColumnValueReaderTest.java
b/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/inventory/PostgreSQLInventoryColumnValueReaderTest.java
new file mode 100644
index 00000000000..846b33384a0
--- /dev/null
+++
b/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/inventory/PostgreSQLInventoryColumnValueReaderTest.java
@@ -0,0 +1,99 @@
+/*
+ * 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.postgresql.ingest.inventory;
+
+import
org.apache.shardingsphere.data.pipeline.core.ingest.dumper.inventory.column.DialectInventoryColumnValueReader;
+import
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.Test;
+import org.postgresql.util.PGobject;
+
+import java.math.BigDecimal;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.Optional;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class PostgreSQLInventoryColumnValueReaderTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
+
+ private final DialectInventoryColumnValueReader reader =
DatabaseTypedSPILoader.getService(DialectInventoryColumnValueReader.class,
databaseType);
+
+ @Test
+ void assertReadWithMoneyDataTypeValue() throws SQLException {
+ ResultSet resultSet = mock(ResultSet.class);
+ when(resultSet.getBigDecimal(1)).thenReturn(new BigDecimal("1"));
+ ResultSetMetaData metaData = mock(ResultSetMetaData.class);
+ when(metaData.getColumnTypeName(1)).thenReturn("money");
+ assertThat(reader.read(resultSet, metaData, 1), is(Optional.of(new
BigDecimal("1"))));
+ }
+
+ @Test
+ void assertReadWithFalseBitDataTypeValue() throws SQLException {
+ ResultSet resultSet = mock(ResultSet.class);
+ when(resultSet.getObject(1)).thenReturn(false);
+ ResultSetMetaData metaData = mock(ResultSetMetaData.class);
+ when(metaData.getColumnType(1)).thenReturn(Types.BIT);
+ when(metaData.getColumnTypeName(1)).thenReturn("bit");
+ Optional<Object> actual = reader.read(resultSet, metaData, 1);
+ assertTrue(actual.isPresent());
+ assertThat(((PGobject) actual.get()).getType(), is("bit"));
+ assertThat(((PGobject) actual.get()).getValue(), is("0"));
+ }
+
+ @Test
+ void assertReadWithTrueBitDataTypeValue() throws SQLException {
+ ResultSet resultSet = mock(ResultSet.class);
+ when(resultSet.getObject(1)).thenReturn(true);
+ ResultSetMetaData metaData = mock(ResultSetMetaData.class);
+ when(metaData.getColumnType(1)).thenReturn(Types.BIT);
+ when(metaData.getColumnTypeName(1)).thenReturn("bit");
+ Optional<Object> actual = reader.read(resultSet, metaData, 1);
+ assertTrue(actual.isPresent());
+ assertThat(((PGobject) actual.get()).getType(), is("bit"));
+ assertThat(((PGobject) actual.get()).getValue(), is("1"));
+ }
+
+ @Test
+ void assertReadWithNullBitDataTypeValue() throws SQLException {
+ ResultSet resultSet = mock(ResultSet.class);
+ ResultSetMetaData metaData = mock(ResultSetMetaData.class);
+ when(metaData.getColumnType(1)).thenReturn(Types.BIT);
+ when(metaData.getColumnTypeName(1)).thenReturn("bit");
+ Optional<Object> actual = reader.read(resultSet, metaData, 1);
+ assertTrue(actual.isPresent());
+ assertThat(((PGobject) actual.get()).getType(), is("bit"));
+ assertNull(((PGobject) actual.get()).getValue());
+ }
+
+ @Test
+ void assertReadWithOtherType() throws SQLException {
+ assertFalse(reader.read(mock(ResultSet.class),
mock(ResultSetMetaData.class), 1).isPresent());
+ }
+}