/* * 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. */ #ifndef _DOCUMENTDB_ODBC_TEST_TEST_UTILS #define _DOCUMENTDB_ODBC_TEST_TEST_UTILS #ifdef _WIN32 #include #endif #include #include #include #include #include "documentdb/odbc/common/utils.h" #define ODBC_THROW_ON_ERROR(ret, type, handle) \ if (!SQL_SUCCEEDED(ret)) { \ throw documentdb_test::GetOdbcError(type, handle); \ } #define ODBC_FAIL_ON_ERROR(ret, type, handle) \ if (!SQL_SUCCEEDED(ret)) { \ BOOST_FAIL(documentdb_test::GetOdbcErrorMessage(type, handle)); \ } #define ODBC_FAIL_ON_ERROR1(ret, type, handle, msg) \ if (!SQL_SUCCEEDED(ret)) { \ BOOST_FAIL(documentdb_test::GetOdbcErrorMessage(type, handle) \ + ", msg = " + msg); \ } #define MUTE_TEST_FOR_TEAMCITY \ if (jetbrains::teamcity::underTeamcity()) { \ BOOST_TEST_MESSAGE( \ "Muted on TeamCity because of periodical non-critical failures"); \ BOOST_CHECK(jetbrains::teamcity::underTeamcity()); \ return; \ } /** * Copy std::string to buffer. * * @param dst Destination buffer. * @param src Source std::string. * @param n Copy at most n bytes of src. */ inline void CopyStringToBuffer(char* dst, const std::string& src, size_t n) { if (n == 0) { return; } using std::min; size_t size = min(src.size(), n - 1); memset(dst + size, '\0', n - size); memcpy(dst, src.c_str(), size); } /** * Client ODBC erorr. */ class OdbcClientError : public std::exception { public: /** * Constructor * * @param sqlstate SQL state. * @param message Error message. */ OdbcClientError(const std::string& sqlstate, const std::string& message) : sqlstate(sqlstate), message(message) { // No-op. } /** * Destructor. */ virtual ~OdbcClientError() DOCUMENTDB_NO_THROW { // No-op. } /** * Implementation of the standard std::exception::what() method. * Synonym for GetText() method. * * @return Error message string. */ virtual const char* what() const DOCUMENTDB_NO_THROW { return message.c_str(); } /** SQL state. */ std::string sqlstate; /** Error message. */ std::string message; }; namespace documentdb_test { /** Read buffer size. */ enum { ODBC_BUFFER_SIZE = 1024 }; /** * Extract error. * * @param handleType Type of the handle. * @param handle Handle. * @return Error. */ OdbcClientError GetOdbcError(SQLSMALLINT handleType, SQLHANDLE handle); /** * Extract error state. * * @param handleType Type of the handle. * @param handle Handle. * @param idx Error record index. * @return Error state. */ std::string GetOdbcErrorState(SQLSMALLINT handleType, SQLHANDLE handle, int idx = 1); /** * Extract error message. * * @param handleType Type of the handle. * @param handle Handle. * @param idx Error record index. * @return Error message. */ std::string GetOdbcErrorMessage(SQLSMALLINT handleType, SQLHANDLE handle, int idx = 1); /** * @return Test config directory path. */ std::string GetTestConfigDir(); /** * Remove all the LFS artifacts. */ void ClearLfs(); } // namespace documentdb_test #endif // _DOCUMENTDB_ODBC_TEST_TEST_UTILS