lidavidm commented on code in PR #47788: URL: https://github.com/apache/arrow/pull/47788#discussion_r2434276097
########## cpp/src/arrow/flight/sql/odbc/tests/odbc_test_suite.cc: ########## @@ -0,0 +1,475 @@ +// 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. + +// For DSN registration. flight_sql_connection.h needs to included first due to conflicts +// with windows.h +#include "arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h" + +#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h" + +// For DSN registration +#include "arrow/flight/sql/odbc/odbc_impl/config/configuration.h" +#include "arrow/flight/sql/odbc/odbc_impl/encoding_utils.h" +#include "arrow/flight/sql/odbc/odbc_impl/odbc_connection.h" + +namespace arrow::flight::sql::odbc { +namespace tests { + +void FlightSQLODBCRemoteTestBase::AllocEnvConnHandles(SQLINTEGER odbc_ver) { + // Allocate an environment handle + EXPECT_EQ(SQL_SUCCESS, SQLAllocEnv(&env)); Review Comment: You probably want ASSERT_EQ. EXPECT_EQ won't abort the test. ########## cpp/src/arrow/flight/sql/odbc/odbc_impl/encoding_utils.h: ########## @@ -80,4 +81,24 @@ inline size_t ConvertToSqlWChar(const std::string& str, SQLWCHAR* buffer, } } +/// \brief Convert buffer of SqlWchar to standard string +/// \param[in] wchar_msg SqlWchar to convert +/// \param[in] msg_len Number of characters in wchar_msg +/// \return wchar_msg in std::string format +inline std::string SqlWcharToString(SQLWCHAR* wchar_msg, SQLINTEGER msg_len = SQL_NTS) { + if (!wchar_msg || wchar_msg[0] == 0 || msg_len == 0) { Review Comment: Shouldn't the length be checked before the contents? ########## cpp/src/arrow/flight/sql/odbc/tests/odbc_test_suite.cc: ########## @@ -0,0 +1,475 @@ +// 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. + +// For DSN registration. flight_sql_connection.h needs to included first due to conflicts +// with windows.h +#include "arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h" + +#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h" + +// For DSN registration +#include "arrow/flight/sql/odbc/odbc_impl/config/configuration.h" +#include "arrow/flight/sql/odbc/odbc_impl/encoding_utils.h" +#include "arrow/flight/sql/odbc/odbc_impl/odbc_connection.h" + +namespace arrow::flight::sql::odbc { +namespace tests { + +void FlightSQLODBCRemoteTestBase::AllocEnvConnHandles(SQLINTEGER odbc_ver) { + // Allocate an environment handle + EXPECT_EQ(SQL_SUCCESS, SQLAllocEnv(&env)); + + EXPECT_EQ( + SQL_SUCCESS, + SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, + reinterpret_cast<SQLPOINTER>(static_cast<intptr_t>(odbc_ver)), 0)); + + // Allocate a connection using alloc handle + EXPECT_EQ(SQL_SUCCESS, SQLAllocHandle(SQL_HANDLE_DBC, env, &conn)); +} + +void FlightSQLODBCRemoteTestBase::Connect(SQLINTEGER odbc_ver) { + AllocEnvConnHandles(odbc_ver); + std::string connect_str = GetConnectionString(); + ConnectWithString(connect_str); +} + +void FlightSQLODBCRemoteTestBase::ConnectWithString(std::string connect_str) { + // Connect string + std::vector<SQLWCHAR> connect_str0(connect_str.begin(), connect_str.end()); + + SQLWCHAR out_str[ODBC_BUFFER_SIZE]; + SQLSMALLINT out_str_len; + + // Connecting to ODBC server. + SQLRETURN ret = SQLDriverConnect(conn, NULL, &connect_str0[0], + static_cast<SQLSMALLINT>(connect_str0.size()), out_str, + ODBC_BUFFER_SIZE, &out_str_len, SQL_DRIVER_NOPROMPT); + if (ret != SQL_SUCCESS) { + std::cerr << GetOdbcErrorMessage(SQL_HANDLE_DBC, conn) << std::endl; + } + + // Assert connection is successful before we continue + ASSERT_EQ(SQL_SUCCESS, ret); + + // Allocate a statement using alloc handle + ASSERT_EQ(SQL_SUCCESS, SQLAllocHandle(SQL_HANDLE_STMT, conn, &stmt)); +} + +void FlightSQLODBCRemoteTestBase::Disconnect() { + // Close statement + EXPECT_EQ(SQL_SUCCESS, SQLFreeHandle(SQL_HANDLE_STMT, stmt)); + + // Disconnect from ODBC + SQLRETURN ret = SQLDisconnect(conn); + if (ret != SQL_SUCCESS) { + std::cerr << GetOdbcErrorMessage(SQL_HANDLE_DBC, conn) << std::endl; + } Review Comment: Ditto ########## cpp/src/arrow/flight/sql/odbc/tests/odbc_test_suite.cc: ########## @@ -0,0 +1,475 @@ +// 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. + +// For DSN registration. flight_sql_connection.h needs to included first due to conflicts +// with windows.h +#include "arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h" + +#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h" + +// For DSN registration +#include "arrow/flight/sql/odbc/odbc_impl/config/configuration.h" +#include "arrow/flight/sql/odbc/odbc_impl/encoding_utils.h" +#include "arrow/flight/sql/odbc/odbc_impl/odbc_connection.h" + +namespace arrow::flight::sql::odbc { +namespace tests { + +void FlightSQLODBCRemoteTestBase::AllocEnvConnHandles(SQLINTEGER odbc_ver) { + // Allocate an environment handle + EXPECT_EQ(SQL_SUCCESS, SQLAllocEnv(&env)); + + EXPECT_EQ( + SQL_SUCCESS, + SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, + reinterpret_cast<SQLPOINTER>(static_cast<intptr_t>(odbc_ver)), 0)); + + // Allocate a connection using alloc handle + EXPECT_EQ(SQL_SUCCESS, SQLAllocHandle(SQL_HANDLE_DBC, env, &conn)); +} + +void FlightSQLODBCRemoteTestBase::Connect(SQLINTEGER odbc_ver) { + AllocEnvConnHandles(odbc_ver); + std::string connect_str = GetConnectionString(); + ConnectWithString(connect_str); Review Comment: Ditto ########## cpp/src/arrow/flight/sql/odbc/odbc_impl/system_dsn.h: ########## Review Comment: Do these have to be in the global namespace? ########## cpp/src/arrow/flight/sql/odbc/tests/connection_test.cc: ########## @@ -0,0 +1,42 @@ +// 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. + +#ifdef _WIN32 +# include <windows.h> +#endif + +#include <sql.h> +#include <sqltypes.h> +#include <sqlucode.h> + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace arrow::flight::sql::odbc { +namespace tests { + +TEST(SQLAllocHandle, SQLAllocHandleEnv) { + SQLHENV env; + + // Allocate an environment handle + SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); + + EXPECT_THAT(env, ::testing::NotNull()); Review Comment: Don't you need to free the handle? ########## cpp/src/arrow/flight/sql/odbc/tests/odbc_test_suite.cc: ########## @@ -0,0 +1,475 @@ +// 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. + +// For DSN registration. flight_sql_connection.h needs to included first due to conflicts +// with windows.h +#include "arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h" + +#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h" + +// For DSN registration +#include "arrow/flight/sql/odbc/odbc_impl/config/configuration.h" +#include "arrow/flight/sql/odbc/odbc_impl/encoding_utils.h" +#include "arrow/flight/sql/odbc/odbc_impl/odbc_connection.h" + +namespace arrow::flight::sql::odbc { +namespace tests { + +void FlightSQLODBCRemoteTestBase::AllocEnvConnHandles(SQLINTEGER odbc_ver) { + // Allocate an environment handle + EXPECT_EQ(SQL_SUCCESS, SQLAllocEnv(&env)); + + EXPECT_EQ( + SQL_SUCCESS, + SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, + reinterpret_cast<SQLPOINTER>(static_cast<intptr_t>(odbc_ver)), 0)); + + // Allocate a connection using alloc handle + EXPECT_EQ(SQL_SUCCESS, SQLAllocHandle(SQL_HANDLE_DBC, env, &conn)); +} + +void FlightSQLODBCRemoteTestBase::Connect(SQLINTEGER odbc_ver) { + AllocEnvConnHandles(odbc_ver); + std::string connect_str = GetConnectionString(); + ConnectWithString(connect_str); +} + +void FlightSQLODBCRemoteTestBase::ConnectWithString(std::string connect_str) { + // Connect string + std::vector<SQLWCHAR> connect_str0(connect_str.begin(), connect_str.end()); + + SQLWCHAR out_str[ODBC_BUFFER_SIZE]; + SQLSMALLINT out_str_len; + + // Connecting to ODBC server. + SQLRETURN ret = SQLDriverConnect(conn, NULL, &connect_str0[0], + static_cast<SQLSMALLINT>(connect_str0.size()), out_str, + ODBC_BUFFER_SIZE, &out_str_len, SQL_DRIVER_NOPROMPT); + if (ret != SQL_SUCCESS) { + std::cerr << GetOdbcErrorMessage(SQL_HANDLE_DBC, conn) << std::endl; + } Review Comment: ```suggestion ASSERT_EQ(SQL_SUCCESS, ret) << GetOdbcErrorMessage(SQL_HANDLE_DBC, conn); ``` ########## cpp/src/arrow/flight/sql/odbc/tests/odbc_test_suite.cc: ########## @@ -0,0 +1,475 @@ +// 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. + +// For DSN registration. flight_sql_connection.h needs to included first due to conflicts +// with windows.h +#include "arrow/flight/sql/odbc/odbc_impl/flight_sql_connection.h" + +#include "arrow/flight/sql/odbc/tests/odbc_test_suite.h" + +// For DSN registration +#include "arrow/flight/sql/odbc/odbc_impl/config/configuration.h" +#include "arrow/flight/sql/odbc/odbc_impl/encoding_utils.h" +#include "arrow/flight/sql/odbc/odbc_impl/odbc_connection.h" + +namespace arrow::flight::sql::odbc { +namespace tests { + +void FlightSQLODBCRemoteTestBase::AllocEnvConnHandles(SQLINTEGER odbc_ver) { + // Allocate an environment handle + EXPECT_EQ(SQL_SUCCESS, SQLAllocEnv(&env)); + + EXPECT_EQ( + SQL_SUCCESS, + SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, + reinterpret_cast<SQLPOINTER>(static_cast<intptr_t>(odbc_ver)), 0)); + + // Allocate a connection using alloc handle + EXPECT_EQ(SQL_SUCCESS, SQLAllocHandle(SQL_HANDLE_DBC, env, &conn)); +} + +void FlightSQLODBCRemoteTestBase::Connect(SQLINTEGER odbc_ver) { + AllocEnvConnHandles(odbc_ver); Review Comment: Needs to be `ASSERT_NO_FATAL_FAILURE(AllocEnvConnHandles(...))` ########## cpp/src/arrow/flight/sql/odbc/tests/connection_test.cc: ########## @@ -0,0 +1,42 @@ +// 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. + +#ifdef _WIN32 +# include <windows.h> +#endif + +#include <sql.h> +#include <sqltypes.h> +#include <sqlucode.h> + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace arrow::flight::sql::odbc { +namespace tests { + +TEST(SQLAllocHandle, SQLAllocHandleEnv) { + SQLHENV env; + + // Allocate an environment handle + SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env); + + EXPECT_THAT(env, ::testing::NotNull()); Review Comment: You can also just use `ASSERT_NE(nullptr, env)` instead of reaching for GMock for simple assertions -- 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]
