Hisoka-X commented on code in PR #9158: URL: https://github.com/apache/seatunnel/pull/9158#discussion_r2043465559
########## seatunnel-connectors-v2/connector-tdengine/src/test/java/org/apache/seatunnel/connectors/seatunnel/tdengine/sink/TDengineSinkWriterTest.java: ########## @@ -0,0 +1,139 @@ +/* + * 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.seatunnel.connectors.seatunnel.tdengine.sink; + + +import lombok.SneakyThrows; +import org.apache.seatunnel.api.table.type.BasicType; +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; +import org.apache.seatunnel.shade.com.typesafe.config.Config; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigFactory; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigValueFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.time.LocalDateTime; + +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.TimeZone; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; + +public class TDengineSinkWriterTest { + private TDengineSinkWriter writer; + + @SneakyThrows + @BeforeEach + public void setup() { + SeaTunnelRowType rowType; + Config config; + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + String[] fieldNames = new String[]{"id", "name", "description", "weight"}; + SeaTunnelDataType<?>[] dataTypes = + new SeaTunnelDataType[]{ + BasicType.LONG_TYPE, + BasicType.STRING_TYPE, + BasicType.STRING_TYPE, + BasicType.STRING_TYPE, + }; + rowType = new SeaTunnelRowType(fieldNames, dataTypes); + config = + ConfigFactory.empty() + .withValue("url", ConfigValueFactory.fromAnyRef("jdbc:TAOS://localhost:6030/")) + .withValue("database", ConfigValueFactory.fromAnyRef("test_db")) + .withValue("stable", ConfigValueFactory.fromAnyRef("test_stable")) + .withValue("username", ConfigValueFactory.fromAnyRef("root")) + .withValue("password", ConfigValueFactory.fromAnyRef("taosdata")) + .withValue("timezone", ConfigValueFactory.fromAnyRef("UTC")); + + // Mock JDBC objects + Connection mockConnection = Mockito.mock(Connection.class); + Statement mockStatement = Mockito.mock(Statement.class); + ResultSet mockResultSet = Mockito.mock(ResultSet.class); + + // Mock ResultSet behavior + when(mockResultSet.next()).thenReturn(true, false); // First call returns true, second call returns false + when(mockResultSet.getString("note")).thenReturn("TAG"); + + // Mock Statement behavior + when(mockStatement.executeQuery("desc test_db.test_stable")).thenReturn(mockResultSet); + + // Mock Connection behavior + when(mockConnection.createStatement()).thenReturn(mockStatement); + + // Mock DriverManager behavior + try (MockedStatic<DriverManager> mockedStatic = mockStatic(DriverManager.class)) { + mockedStatic.when(() -> DriverManager.getConnection(anyString())).thenReturn(mockConnection); + writer = new TDengineSinkWriter(config, rowType); + } + } + + @Test + void testConvertDataType_withNull() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Review Comment: ```suggestion void testConvertDataTypeWithNull() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { ``` ########## seatunnel-connectors-v2/connector-tdengine/src/test/java/org/apache/seatunnel/connectors/seatunnel/tdengine/sink/TDengineSinkWriterTest.java: ########## @@ -0,0 +1,139 @@ +/* + * 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.seatunnel.connectors.seatunnel.tdengine.sink; + + +import lombok.SneakyThrows; +import org.apache.seatunnel.api.table.type.BasicType; +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; +import org.apache.seatunnel.shade.com.typesafe.config.Config; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigFactory; +import org.apache.seatunnel.shade.com.typesafe.config.ConfigValueFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.time.LocalDateTime; + +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.TimeZone; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; + +public class TDengineSinkWriterTest { + private TDengineSinkWriter writer; + + @SneakyThrows + @BeforeEach + public void setup() { + SeaTunnelRowType rowType; + Config config; + TimeZone.setDefault(TimeZone.getTimeZone("UTC")); + String[] fieldNames = new String[]{"id", "name", "description", "weight"}; + SeaTunnelDataType<?>[] dataTypes = + new SeaTunnelDataType[]{ + BasicType.LONG_TYPE, + BasicType.STRING_TYPE, + BasicType.STRING_TYPE, + BasicType.STRING_TYPE, + }; + rowType = new SeaTunnelRowType(fieldNames, dataTypes); + config = + ConfigFactory.empty() + .withValue("url", ConfigValueFactory.fromAnyRef("jdbc:TAOS://localhost:6030/")) + .withValue("database", ConfigValueFactory.fromAnyRef("test_db")) + .withValue("stable", ConfigValueFactory.fromAnyRef("test_stable")) + .withValue("username", ConfigValueFactory.fromAnyRef("root")) + .withValue("password", ConfigValueFactory.fromAnyRef("taosdata")) + .withValue("timezone", ConfigValueFactory.fromAnyRef("UTC")); + + // Mock JDBC objects + Connection mockConnection = Mockito.mock(Connection.class); + Statement mockStatement = Mockito.mock(Statement.class); + ResultSet mockResultSet = Mockito.mock(ResultSet.class); + + // Mock ResultSet behavior + when(mockResultSet.next()).thenReturn(true, false); // First call returns true, second call returns false + when(mockResultSet.getString("note")).thenReturn("TAG"); + + // Mock Statement behavior + when(mockStatement.executeQuery("desc test_db.test_stable")).thenReturn(mockResultSet); + + // Mock Connection behavior + when(mockConnection.createStatement()).thenReturn(mockStatement); + + // Mock DriverManager behavior + try (MockedStatic<DriverManager> mockedStatic = mockStatic(DriverManager.class)) { + mockedStatic.when(() -> DriverManager.getConnection(anyString())).thenReturn(mockConnection); + writer = new TDengineSinkWriter(config, rowType); + } + } + + @Test + void testConvertDataType_withNull() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + // Use reflection to access the private method + Method method = TDengineSinkWriter.class.getDeclaredMethod("convertDataType", Object[].class); + method.setAccessible(true); Review Comment: It's better to not use reflect. Let's change `convertDataType` scope to default. Then add a annotation `@VisibleForTesting`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
