Repository: apache/arrow
File: cpp/src/arrow/flight/sql/odbc/flight_sql/utils_test.cc (C++)

Code before:
//
// 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.

#include "arrow/flight/sql/odbc/flight_sql/utils.h"

#include "arrow/flight/sql/odbc/odbcabstraction/include/odbcabstraction/calendar_utils.h"

#include "arrow/compute/initialize.h"
#include "arrow/testing/builder.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/util.h"
#include "gtest/gtest.h"

namespace driver {
namespace flight_sql {

class UtilTestsWithCompute : public ::testing::Test {
 public:
  // This must be done before using the compute kernels in order to
  // register them to the FunctionRegistry.
  void SetUp() override { ASSERT_OK(arrow::compute::Initialize()); }
};

void AssertConvertedArray(const std::shared_ptr<arrow::Array>& expected_array,
                          const std::shared_ptr<arrow::Array>& converted_array,
                          uint64_t size, arrow::Type::type arrow_type) {
  ASSERT_EQ(converted_array->type_id(), arrow_type);
  ASSERT_EQ(converted_array->length(), size);
  ASSERT_EQ(expected_array->ToString(), converted_array->ToString());
}

std::shared_ptr<arrow::Array> convertArray(
    const std::shared_ptr<arrow::Array>& original_array,
    odbcabstraction::CDataType c_type) {
  auto converter = GetConverter(original_array->type_id(), c_type);
  return converter(original_array);
}

void TestArrayConversion(const std::vector<std::string>& input,
                         const std::shared_ptr<arrow::Array>& expected_array,
                         odbcabstraction::CDataType c_type,
                         arrow::Type::type arrow_type) {
  std::shared_ptr<arrow::Array> original_array;
  arrow::ArrayFromVector<arrow::StringType, std::string>(input, &original_array);

  auto converted_array = convertArray(original_array, c_type);

Review comment (suggestion):
`arrow::compute::Initialize()` is called for each test, right?

Can we use `::testing::Environment` to call `arrow::compute::Initialize()` only once?
See also:
* https://google.github.io/googletest/advanced.html#global-set-up-and-tear-down
* https://google.github.io/googletest/advanced.html#global-set-up-and-tear-down

Code after:
//
// 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.

#include "arrow/flight/sql/odbc/flight_sql/utils.h"

#include "arrow/flight/sql/odbc/odbcabstraction/include/odbcabstraction/calendar_utils.h"

#include "arrow/compute/initialize.h"
#include "arrow/testing/builder.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/util.h"
#include "gtest/gtest.h"

namespace driver {
namespace flight_sql {

// A global test "environment", to ensure Arrow compute kernel functions are registered

class ComputeKernelEnvironment : public ::testing::Environment {
 public:
  void SetUp() override { ASSERT_OK(arrow::compute::Initialize()); }
};

::testing::Environment* kernel_env =
    ::testing::AddGlobalTestEnvironment(new ComputeKernelEnvironment);

void AssertConvertedArray(const std::shared_ptr<arrow::Array>& expected_array,
                          const std::shared_ptr<arrow::Array>& converted_array,
                          uint64_t size, arrow::Type::type arrow_type) {
  ASSERT_EQ(converted_array->type_id(), arrow_type);
  ASSERT_EQ(converted_array->length(), size);
  ASSERT_EQ(expected_array->ToString(), converted_array->ToString());
}

std::shared_ptr<arrow::Array> convertArray(
    const std::shared_ptr<arrow::Array>& original_array,
    odbcabstraction::CDataType c_type) {
  auto converter = GetConverter(original_array->type_id(), c_type);
  return converter(original_array);
}

void TestArrayConversion(const std::vector<std::string>& input,
                         const std::shared_ptr<arrow::Array>& expected_array,
                         odbcabstraction::CDataType c_type,
                         arrow::Type::type arrow_type) {
  std::shared_ptr<arrow::Array> original_array;