<commit_before>/*
  Copyright (c) 2014 DataStax

  Licensed 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.
*/

#define BOOST_TEST_DYN_LINK
#ifdef STAND_ALONE
#   define BOOST_TEST_MODULE cassandra
#endif

#include <algorithm>

#include <boost/test/unit_test.hpp>
#include <boost/test/debug.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/cstdint.hpp>
#include <boost/format.hpp>
#include <boost/thread.hpp>
#include <boost/chrono.hpp>

#include "cassandra.h"
#include "test_utils.hpp"

struct BasicTests : public test_utils::SingleSessionTest {
  BasicTests() : SingleSessionTest(1, 0) {
    test_utils::execute_query(session, str(boost::format(test_utils::CREATE_KEYSPACE_SIMPLE_FORMAT)
                                           % test_utils::SIMPLE_KEYSPACE % "1"));

    test_utils::execute_query(session, str(boost::format("USE %s") % test_utils::SIMPLE_KEYSPACE));
  }
};

BOOST_FIXTURE_TEST_SUITE(basics, BasicTests)

template <class T>
void insert_single_value(CassSession* session, CassValueType type, T value) {
  std::string table_name = str(boost::format("table_%s") % test_utils::generate_unique_str());
  std::string type_name = test_utils::get_value_type(type);

  test_utils::execute_query(session, str(boost::format("CREATE TABLE %s (tweet_id uuid PRIMARY KEY, test_val %s);")
                                         % table_name % type_name));

  test_utils::Uuid tweet_id = test_utils::generate_random_uuid();

  std::string insert_query = str(boost::format("INSERT INTO %s (tweet_id, test_val) VALUES(?, ?);") % table_name);
  test_utils::CassStatementPtr insert_statement(cass_statement_new(cass_string_init(insert_query.c_str()), 2));
  BOOST_REQUIRE(cass_statement_bind_uuid(insert_statement.get(), 0, tweet_id.uuid) == CASS_OK);
  BOOST_REQUIRE(test_utils::Value<T>::bind(insert_statement.get(), 1, value) == CASS_OK);
  test_utils::CassFuturePtr insert_future(cass_session_execute(session, insert_statement.get()));
  test_utils::wait_and_check_error(insert_future.get());

  std::string select_query = str(boost::format("SELECT * FROM %s WHERE tweet_id = ?;") % table_name);
  test_utils::CassStatementPtr select_statement(cass_statement_new(cass_string_init(select_query.c_str()), 1));
  BOOST_REQUIRE(cass_statement_bind_uuid(select_statement.get(), 0, tweet_id.uuid) == CASS_OK);
  test_utils::CassFuturePtr select_future(cass_session_execute(session, select_statement.get()));
  test_utils::wait_and_check_error(select_future.get());

  test_utils::CassResultPtr result(cass_future_get_result(select_future.get()));
  BOOST_REQUIRE(cass_result_row_count(result.get()) == 1);
  BOOST_REQUIRE(cass_result_column_count(result.get()) == 2);

  const CassValue* column = cass_row_get_column(cass_result_first_row(result.get()), 1);
  T result_value;
  BOOST_REQUIRE(cass_value_type(column) == type);
  BOOST_REQUIRE(test_utils::Value<T>::get(column, &result_value) == CASS_OK);
  BOOST_REQUIRE(test_utils::Value<T>::equal(result_value, value));
}

template <class T>
void insert_min_max_value(CassSession* session, CassValueType type) {
  std::string table_name = str(boost::format("table_%s") % test_utils::generate_unique_str());
  std::string type_name = test_utils::get_value_type(type);

  test_utils::execute_query(session, str(boost::format("CREATE TABLE %s (tweet_id uuid PRIMARY KEY, min_val %s, max_val %s);")
                                         % table_name % type_name % type_name));

  test_utils::Uuid tweet_id = test_utils::generate_random_uuid();

  std::string insert_query = str(boost::format("INSERT INTO %s (tweet_id, min_val, max_val) VALUES(?, ?, ?);") % table_name);
  test_utils::CassStatementPtr insert_statement(cass_statement_new(cass_string_init(insert_query.c_str()), 3));
  BOOST_REQUIRE(cass_statement_bind_uuid(insert_statement.get(), 0, tweet_id.uuid) == CASS_OK);
  BOOST_REQUIRE(test_utils::Value<T>::bind(insert_statement.get(), 1, test_utils::Value<T>::min_value()) == CASS_OK);
  BOOST_REQUIRE(test_utils::Value<T>::bind(insert_statement.get(), 2, test_utils::Value<T>::max_value()) == CASS_OK);
  test_utils::CassFuturePtr result_future(cass_session_execute(session, insert_statement.get()));
  test_utils::wait_and_check_error(result_future.get());

  std::string select_query = str(boost::format("SELECT * FROM %s WHERE tweet_id = ?;") % table_name);
  test_utils::CassStatementPtr select_statement(cass_statement_new(cass_string_init(select_query.c_str()), 1));
  BOOST_REQUIRE(cass_statement_bind_uuid(select_statement.get(), 0, tweet_id.uuid) == CASS_OK);
  test_utils::CassFuturePtr select_future(cass_session_execute(session, select_statement.get()));
  test_utils::wait_and_check_error(select_future.get());

  test_utils::CassResultPtr result(cass_future_get_result(select_future.get()));
  BOOST_REQUIRE(cass_result_row_count(result.get()) == 1);
  BOOST_REQUIRE(cass_result_column_count(result.get()) == 3);

  T min_value;
  BOOST_REQUIRE(test_utils::Value<T>::get(cass_row_get_column(cass_result_first_row(result.get()), 2), &min_value) == CASS_OK);
  BOOST_REQUIRE(test_utils::Value<T>::equal(min_value,  test_utils::Value<T>::min_value()));

  T max_value;
  BOOST_REQUIRE(test_utils::Value<T>::get(cass_row_get_column(cass_result_first_row(result.get()), 1), &max_value) == CASS_OK);
  BOOST_REQUIRE(test_utils::Value<T>::equal(max_value,  test_utils::Value<T>::max_value()));
}

void insert_null_value(CassSession* session, CassValueType type) {
  std::string table_name = str(boost::format("table_%s") % test_utils::generate_unique_str());
  std::string type_name = test_utils::get_value_type(type);

  if(type == CASS_VALUE_TYPE_LIST || type == CASS_VALUE_TYPE_SET) {
    type_name.append("<text>");
  } else if(type == CASS_VALUE_TYPE_MAP) {
    type_name.append("<text, text>");
  }

  test_utils::execute_query(session, str(boost::format("CREATE TABLE %s (tweet_id uuid PRIMARY KEY, test_val %s);")
                                         % table_name % type_name));

  test_utils::Uuid tweet_id = test_utils::generate_random_uuid();

  std::string insert_query = str(boost::format("INSERT INTO %s (tweet_id, test_val) VALUES(?, ?);") % table_name);
  test_utils::CassStatementPtr insert_statement(cass_statement_new(cass_string_init(insert_query.c_str()), 2));
  BOOST_REQUIRE(cass_statement_bind_uuid(insert_statement.get(), 0, tweet_id.uuid) == CASS_OK);
  BOOST_REQUIRE(cass_statement_bind_null(insert_statement.get(), 1) == CASS_OK);
  test_utils::CassFuturePtr insert_future(cass_session_execute(session, insert_statement.get()));
  test_utils::wait_and_check_error(insert_future.get());

  std::string select_query = str(boost::format("SELECT * FROM %s WHERE tweet_id = ?;") % table_name);
  test_utils::CassStatementPtr select_statement(cass_statement_new(cass_string_init(select_query.c_str()), 1));
  BOOST_REQUIRE(cass_statement_bind_uuid(select_statement.get(), 0, tweet_id.uuid) == CASS_OK);
  test_utils::CassFuturePtr select_future(cass_session_execute(session, select_statement.get()));
  test_utils::wait_and_check_error(select_future.get());

  test_utils::CassResultPtr result(cass_future_get_result(select_future.get()));
  BOOST_REQUIRE(cass_result_row_count(result.get()) == 1);
  BOOST_REQUIRE(cass_result_column_count(result.get()) == 2);

  BOOST_REQUIRE(cass_value_is_null(cass_row_get_column(cass_result_first_row(result.get()), 1)));
}

BOOST_AUTO_TEST_CASE(test_basic_types)
{
  insert_single_value<cass_int32_t>(session, CASS_VALUE_TYPE_INT, 123);

  insert_single_value<cass_int64_t>(session, CASS_VALUE_TYPE_BIGINT, 1234567890);<commit_msg>Updating the NULL test case to check for CASS_ERROR_LIB_NULL_VALUE when using the appropiate cass_value_get*() function<commit_after>/*
  Copyright (c) 2014 DataStax

  Licensed 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.
*/

#define BOOST_TEST_DYN_LINK
#ifdef STAND_ALONE
#   define BOOST_TEST_MODULE cassandra
#endif

#include <algorithm>

#include <boost/test/unit_test.hpp>
#include <boost/test/debug.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/cstdint.hpp>
#include <boost/format.hpp>
#include <boost/thread.hpp>
#include <boost/chrono.hpp>

#include "cassandra.h"
#include "test_utils.hpp"

struct BasicTests : public test_utils::SingleSessionTest {
  BasicTests() : SingleSessionTest(1, 0) {
    test_utils::execute_query(session, str(boost::format(test_utils::CREATE_KEYSPACE_SIMPLE_FORMAT)
                                           % test_utils::SIMPLE_KEYSPACE % "1"));

    test_utils::execute_query(session, str(boost::format("USE %s") % test_utils::SIMPLE_KEYSPACE));
  }
};

BOOST_FIXTURE_TEST_SUITE(basics, BasicTests)

template <class T>
void insert_single_value(CassSession* session, CassValueType type, T value) {
  std::string table_name = str(boost::format("table_%s") % test_utils::generate_unique_str());
  std::string type_name = test_utils::get_value_type(type);

  test_utils::execute_query(session, str(boost::format("CREATE TABLE %s (tweet_id uuid PRIMARY KEY, test_val %s);")
                                         % table_name % type_name));

  test_utils::Uuid tweet_id = test_utils::generate_random_uuid();

  std::string insert_query = str(boost::format("INSERT INTO %s (tweet_id, test_val) VALUES(?, ?);") % table_name);
  test_utils::CassStatementPtr insert_statement(cass_statement_new(cass_string_init(insert_query.c_str()), 2));
  BOOST_REQUIRE(cass_statement_bind_uuid(insert_statement.get(), 0, tweet_id.uuid) == CASS_OK);
  BOOST_REQUIRE(test_utils::Value<T>::bind(insert_statement.get(), 1, value) == CASS_OK);
  test_utils::CassFuturePtr insert_future(cass_session_execute(session, insert_statement.get()));
  test_utils::wait_and_check_error(insert_future.get());

  std::string select_query = str(boost::format("SELECT * FROM %s WHERE tweet_id = ?;") % table_name);
  test_utils::CassStatementPtr select_statement(cass_statement_new(cass_string_init(select_query.c_str()), 1));
  BOOST_REQUIRE(cass_statement_bind_uuid(select_statement.get(), 0, tweet_id.uuid) == CASS_OK);
  test_utils::CassFuturePtr select_future(cass_session_execute(session, select_statement.get()));
  test_utils::wait_and_check_error(select_future.get());

  test_utils::CassResultPtr result(cass_future_get_result(select_future.get()));
  BOOST_REQUIRE(cass_result_row_count(result.get()) == 1);
  BOOST_REQUIRE(cass_result_column_count(result.get()) == 2);

  const CassValue* column = cass_row_get_column(cass_result_first_row(result.get()), 1);
  T result_value;
  BOOST_REQUIRE(cass_value_type(column) == type);
  BOOST_REQUIRE(test_utils::Value<T>::get(column, &result_value) == CASS_OK);
  BOOST_REQUIRE(test_utils::Value<T>::equal(result_value, value));
}

template <class T>
void insert_min_max_value(CassSession* session, CassValueType type) {
  std::string table_name = str(boost::format("table_%s") % test_utils::generate_unique_str());
  std::string type_name = test_utils::get_value_type(type);

  test_utils::execute_query(session, str(boost::format("CREATE TABLE %s (tweet_id uuid PRIMARY KEY, min_val %s, max_val %s);")
                                         % table_name % type_name % type_name));

  test_utils::Uuid tweet_id = test_utils::generate_random_uuid();

  std::string insert_query = str(boost::format("INSERT INTO %s (tweet_id, min_val, max_val) VALUES(?, ?, ?);") % table_name);
  test_utils::CassStatementPtr insert_statement(cass_statement_new(cass_string_init(insert_query.c_str()), 3));
  BOOST_REQUIRE(cass_statement_bind_uuid(insert_statement.get(), 0, tweet_id.uuid) == CASS_OK);
  BOOST_REQUIRE(test_utils::Value<T>::bind(insert_statement.get(), 1, test_utils::Value<T>::min_value()) == CASS_OK);
  BOOST_REQUIRE(test_utils::Value<T>::bind(insert_statement.get(), 2, test_utils::Value<T>::max_value()) == CASS_OK);
  test_utils::CassFuturePtr result_future(cass_session_execute(session, insert_statement.get()));
  test_utils::wait_and_check_error(result_future.get());

  std::string select_query = str(boost::format("SELECT * FROM %s WHERE tweet_id = ?;") % table_name);
  test_utils::CassStatementPtr select_statement(cass_statement_new(cass_string_init(select_query.c_str()), 1));
  BOOST_REQUIRE(cass_statement_bind_uuid(select_statement.get(), 0, tweet_id.uuid) == CASS_OK);
  test_utils::CassFuturePtr select_future(cass_session_execute(session, select_statement.get()));
  test_utils::wait_and_check_error(select_future.get());

  test_utils::CassResultPtr result(cass_future_get_result(select_future.get()));
  BOOST_REQUIRE(cass_result_row_count(result.get()) == 1);
  BOOST_REQUIRE(cass_result_column_count(result.get()) == 3);

  T min_value;
  BOOST_REQUIRE(test_utils::Value<T>::get(cass_row_get_column(cass_result_first_row(result.get()), 2), &min_value) == CASS_OK);
  BOOST_REQUIRE(test_utils::Value<T>::equal(min_value,  test_utils::Value<T>::min_value()));

  T max_value;
  BOOST_REQUIRE(test_utils::Value<T>::get(cass_row_get_column(cass_result_first_row(result.get()), 1), &max_value) == CASS_OK);
  BOOST_REQUIRE(test_utils::Value<T>::equal(max_value,  test_utils::Value<T>::max_value()));
}

void insert_null_value(CassSession* session, CassValueType type) {
  std::string table_name = str(boost::format("table_%s") % test_utils::generate_unique_str());
  std::string type_name = test_utils::get_value_type(type);

  if(type == CASS_VALUE_TYPE_LIST || type == CASS_VALUE_TYPE_SET) {
    type_name.append("<text>");
  } else if(type == CASS_VALUE_TYPE_MAP) {
    type_name.append("<text, text>");
  }

  test_utils::execute_query(session, str(boost::format("CREATE TABLE %s (tweet_id uuid PRIMARY KEY, test_val %s);")
                                         % table_name % type_name));

  test_utils::Uuid tweet_id = test_utils::generate_random_uuid();

  std::string insert_query = str(boost::format("INSERT INTO %s (tweet_id, test_val) VALUES(?, ?);") % table_name);
  test_utils::CassStatementPtr insert_statement(cass_statement_new(cass_string_init(insert_query.c_str()), 2));
  BOOST_REQUIRE(cass_statement_bind_uuid(insert_statement.get(), 0, tweet_id.uuid) == CASS_OK);
  BOOST_REQUIRE(cass_statement_bind_null(insert_statement.get(), 1) == CASS_OK);
  test_utils::CassFuturePtr insert_future(cass_session_execute(session, insert_statement.get()));
  test_utils::wait_and_check_error(insert_future.get());

  std::string select_query = str(boost::format("SELECT * FROM %s WHERE tweet_id = ?;") % table_name);
  test_utils::CassStatementPtr select_statement(cass_statement_new(cass_string_init(select_query.c_str()), 1));
  BOOST_REQUIRE(cass_statement_bind_uuid(select_statement.get(), 0, tweet_id.uuid) == CASS_OK);
  test_utils::CassFuturePtr select_future(cass_session_execute(session, select_statement.get()));
  test_utils::wait_and_check_error(select_future.get());

  test_utils::CassResultPtr result(cass_future_get_result(select_future.get()));
  BOOST_REQUIRE(cass_result_row_count(result.get()) == 1);
  BOOST_REQUIRE(cass_result_column_count(result.get()) == 2);

  //Get the test value column from the first row of the result
  const CassValue *testValue = cass_row_get_column(cass_result_first_row(result.get()), 1);

  //Ensure the test value is NULL
  BOOST_REQUIRE(cass_value_is_null(testValue));

  //Verify cass_value_get function returns CASS_ERROR_LIB_NULL_VALUE
  if (type == CASS_VALUE_TYPE_INT) {
    cass_int32_t value = 0;
    BOOST_REQUIRE_EQUAL(cass_value_get_int32(testValue, &value), CASS_ERROR_LIB_NULL_VALUE);
  } else if (type == CASS_VALUE_TYPE_BIGINT || type == CASS_VALUE_TYPE_TIMESTAMP) {
    cass_int64_t value = 0;
    BOOST_REQUIRE_EQUAL(cass_value_get_int64(testValue, &value), CASS_ERROR_LIB_NULL_VALUE);
  } else if (type == CASS_VALUE_TYPE_FLOAT) {
    cass_float_t value = 0;
    BOOST_REQUIRE_EQUAL(cass_value_get_float(testValue, &value), CASS_ERROR_LIB_NULL_VALUE);
  } else if (type == CASS_VALUE_TYPE_DOUBLE) {
    cass_double_t value = 0;
    BOOST_REQUIRE_EQUAL(cass_value_get_double(testValue, &value), CASS_ERROR_LIB_NULL_VALUE);
  } else if (type == CASS_VALUE_TYPE_BOOLEAN) {
    cass_bool_t value = cass_false;
    BOOST_REQUIRE_EQUAL(cass_value_get_bool(testValue, &value), CASS_ERROR_LIB_NULL_VALUE);
  } else if (type == CASS_VALUE_TYPE_UUID || type == CASS_VALUE_TYPE_TIMEUUID) {
    test_utils::Uuid value;
    BOOST_REQUIRE_EQUAL(cass_value_get_uuid(testValue, value), CASS_ERROR_LIB_NULL_VALUE);
  } else if (type == CASS_VALUE_TYPE_INET) {
    CassInet value;
    BOOST_REQUIRE_EQUAL(cass_value_get_inet(testValue, &value), CASS_ERROR_LIB_NULL_VALUE);
  } else if(type == CASS_VALUE_TYPE_ASCII || type == CASS_VALUE_TYPE_TEXT || type == CASS_VALUE_TYPE_VARCHAR) {
    CassString value = cass_string_init("");
    BOOST_REQUIRE_EQUAL(cass_value_get_string(testValue, &value), CASS_ERROR_LIB_NULL_VALUE);
  } else if (type == CASS_VALUE_TYPE_BLOB || type == CASS_VALUE_TYPE_VARINT || type == CASS_VALUE_TYPE_LIST || type == CASS_VALUE_TYPE_MAP || type == CASS_VALUE_TYPE_SET) {
    CassBytes value;
    BOOST_REQUIRE_EQUAL(cass_value_get_bytes(testValue, &value), CASS_ERROR_LIB_NULL_VALUE);
  } else if (type == CASS_VALUE_TYPE_DECIMAL) {
    CassDecimal value;
    BOOST_REQUIRE_EQUAL(cass_value_get_decimal(testValue, &value), CASS_ERROR_LIB_NULL_VALUE);
  } 
}

BOOST_AUTO_TEST_CASE(test_basic_types)
{
  insert_single_value<cass_int32_t>(session, CASS_VALUE_TYPE_INT, 123);

  insert_single_value<cass_int64_t>(session, CASS_VALUE_TYPE_BIGINT, 1234567890);
{'fraction_non_alphanumeric': 0.13075917656028754, 'fraction_numerical': 0.006535235813092255, 'mean_word_length': 7.03984238178634, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6329304', 'n_tokens_mistral': 6520, 'n_tokens_neox': 6297, 'n_words': 990}