Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various test fixes #191

Merged
merged 3 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ut/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ SET ( clickhouse-cpp-ut-src
array_of_low_cardinality_tests.cpp
CreateColumnByType_ut.cpp
Column_ut.cpp
roundtrip_column.cpp

utils.cpp
value_generators.cpp
Expand Down
35 changes: 35 additions & 0 deletions ut/Column_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
#include <clickhouse/base/output.h>
#include <clickhouse/base/socket.h> // for ipv4-ipv6 platform-specific stuff

#include <clickhouse/client.h>

#include <gtest/gtest.h>

#include "utils.h"
#include "roundtrip_column.h"
#include "value_generators.h"

namespace {
Expand Down Expand Up @@ -262,3 +265,35 @@ TYPED_TEST(GenericColumnTest, LoadAndSave) {

EXPECT_TRUE(CompareRecursive(*column_A, *column_B));
}

const auto LocalHostEndpoint = ClientOptions()
.SetHost( getEnvOrDefault("CLICKHOUSE_HOST", "localhost"))
.SetPort( getEnvOrDefault<size_t>("CLICKHOUSE_PORT", "9000"))
.SetUser( getEnvOrDefault("CLICKHOUSE_USER", "default"))
.SetPassword( getEnvOrDefault("CLICKHOUSE_PASSWORD", ""))
.SetDefaultDatabase(getEnvOrDefault("CLICKHOUSE_DB", "default"));

TYPED_TEST(GenericColumnTest, RoundTrip) {
auto [column, values] = this->MakeColumnWithValues(100);
EXPECT_EQ(values.size(), column->Size());

clickhouse::Client client(LocalHostEndpoint);

if constexpr (std::is_same_v<typename TestFixture::ColumnType, ColumnDate32>) {
// Date32 first appeared in v21.9.2.17-stable
const auto server_info = client.GetServerInfo();
if (versionNumber(server_info) < versionNumber(21, 9)) {
GTEST_SKIP() << "Date32 is availble since v21.9.2.17-stable and can't be tested against server: " << server_info;
}
}

if constexpr (std::is_same_v<typename TestFixture::ColumnType, ColumnInt128>) {
const auto server_info = client.GetServerInfo();
if (versionNumber(server_info) < versionNumber(21, 7)) {
GTEST_SKIP() << "ColumnInt128 is availble since v21.7.2.7-stable and can't be tested against server: " << server_info;
}
}

auto result_typed = RoundtripColumnValues(client, column)->template AsStrict<typename TestFixture::ColumnType>();
EXPECT_TRUE(CompareRecursive(*column, *result_typed));
}
22 changes: 13 additions & 9 deletions ut/array_of_low_cardinality_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,19 @@ TEST(ArrayOfLowCardinality, InsertAndQuery) {
client.Insert("array_lc", block);

client.Select("SELECT * FROM array_lc", [&](const Block& bl) {
for (size_t c = 0; c < bl.GetRowCount(); ++c) {
auto col = bl[0]->As<ColumnArray>()->GetAsColumn(c);
for (size_t i = 0; i < col->Size(); ++i) {
auto stringColumn = col->As<ColumnString>();
const auto string = stringColumn->At(i);

for (size_t c = 0; c < bl.GetRowCount(); ++c) {
auto col = bl[0]->As<ColumnArray>()->GetAsColumn(c);
for (size_t i = 0; i < col->Size(); ++i) {
if (auto string_column = col->As<ColumnString>()) {
const auto string = string_column->At(i);
ASSERT_EQ(testData[c][i], string);
} else if (auto lc_string_column = col->As<ColumnLowCardinalityT<ColumnString>>()) {
const auto string = lc_string_column->At(i);
ASSERT_EQ(testData[c][i], string);
} else {
FAIL() << "Unexpected column type: " << col->Type()->GetName();
}
}
}
);
}
}
});
}
58 changes: 1 addition & 57 deletions ut/client_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,15 @@
#include "readonly_client_test.h"
#include "connection_failed_client_test.h"
#include "utils.h"
#include "roundtrip_column.h"

#include <gtest/gtest.h>

#include <cmath>
#include <thread>
#include <chrono>

using namespace clickhouse;

namespace {

uint64_t versionNumber(
uint64_t version_major,
uint64_t version_minor,
uint64_t version_patch = 0,
uint64_t revision = 0) {

// in this case version_major can be up to 1000
static auto revision_decimal_places = 8;
static auto patch_decimal_places = 4;
static auto minor_decimal_places = 4;

auto const result = version_major * static_cast<uint64_t>(std::pow(10, minor_decimal_places + patch_decimal_places + revision_decimal_places))
+ version_minor * static_cast<uint64_t>(std::pow(10, patch_decimal_places + revision_decimal_places))
+ version_patch * static_cast<uint64_t>(std::pow(10, revision_decimal_places))
+ revision;

return result;
}

uint64_t versionNumber(const ServerInfo & server_info) {
return versionNumber(server_info.version_major, server_info.version_minor, server_info.version_patch, server_info.revision);
}

}

// Use value-parameterized tests to run same tests with different client
// options.
class ClientCase : public testing::TestWithParam<ClientOptions> {
Expand Down Expand Up @@ -978,35 +951,6 @@ TEST_P(ClientCase, DISABLED_ArrayArrayUInt64) {
}
}

ColumnRef RoundtripColumnValues(Client& client, ColumnRef expected) {
// Create a temporary table with a single column
// insert values from `expected`
// select and aggregate all values from block into `result` column
auto result = expected->CloneEmpty();

const std::string type_name = result->GetType().GetName();
client.Execute("DROP TEMPORARY TABLE IF EXISTS temporary_roundtrip_table;");
client.Execute("CREATE TEMPORARY TABLE IF NOT EXISTS temporary_roundtrip_table (col " + type_name + ");");
{
Block block;
block.AppendColumn("col", expected);
block.RefreshRowCount();
client.Insert("temporary_roundtrip_table", block);
}

client.Select("SELECT col FROM temporary_roundtrip_table", [&result](const Block& b) {
if (b.GetRowCount() == 0)
return;

ASSERT_EQ(1u, b.GetColumnCount());
result->Append(b[0]);
});

EXPECT_EQ(expected->GetType(), result->GetType());
EXPECT_EQ(expected->Size(), result->Size());
return result;
}

TEST_P(ClientCase, RoundtripArrayTUint64) {
auto array = std::make_shared<ColumnArrayT<ColumnUInt64>>();
array->Append({0, 1, 2});
Expand Down
40 changes: 40 additions & 0 deletions ut/roundtrip_column.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "roundtrip_column.h"

#include <clickhouse/client.h>
#include <clickhouse/block.h>

#include <gtest/gtest.h>

namespace {
using namespace clickhouse;
}

ColumnRef RoundtripColumnValues(Client& client, ColumnRef expected) {
// Create a temporary table with a single column
// insert values from `expected`
// select and aggregate all values from block into `result` column
auto result = expected->CloneEmpty();

const std::string type_name = result->GetType().GetName();
client.Execute("DROP TEMPORARY TABLE IF EXISTS temporary_roundtrip_table;");
client.Execute("CREATE TEMPORARY TABLE IF NOT EXISTS temporary_roundtrip_table (col " + type_name + ");");
{
Block block;
block.AppendColumn("col", expected);
block.RefreshRowCount();
client.Insert("temporary_roundtrip_table", block);
}

client.Select("SELECT col FROM temporary_roundtrip_table", [&result](const Block& b) {
if (b.GetRowCount() == 0)
return;

ASSERT_EQ(1u, b.GetColumnCount());
result->Append(b[0]);
});

EXPECT_EQ(expected->GetType(), result->GetType());
EXPECT_EQ(expected->Size(), result->Size());

return result;
}
9 changes: 9 additions & 0 deletions ut/roundtrip_column.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <clickhouse/columns/column.h>

namespace clickhouse {
class Client;
}

clickhouse::ColumnRef RoundtripColumnValues(clickhouse::Client& client, clickhouse::ColumnRef expected);
5 changes: 5 additions & 0 deletions ut/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <iomanip>
#include <sstream>


namespace {
using namespace clickhouse;
std::ostream & printColumnValue(const ColumnRef& c, const size_t row, std::ostream & ostr);
Expand Down Expand Up @@ -266,3 +267,7 @@ std::ostream & operator<<(std::ostream & ostr, const ServerInfo & server_info) {
}

}

uint64_t versionNumber(const ServerInfo & server_info) {
return versionNumber(server_info.version_major, server_info.version_minor, server_info.version_patch, server_info.revision);
}
22 changes: 21 additions & 1 deletion ut/utils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <clickhouse/base/platform.h>
#include <clickhouse/columns/uuid.h>

#include "utils_meta.h"
#include "utils_comparison.h"
Expand All @@ -12,12 +11,14 @@
#include <system_error>
#include <type_traits>
#include <vector>
#include <cmath>

#include <time.h>

#include <gtest/gtest.h>

namespace clickhouse {
class Client;
class Block;
class Type;
struct ServerInfo;
Expand Down Expand Up @@ -136,4 +137,23 @@ std::ostream& operator<<(std::ostream & ostr, const PrintContainer<T>& print_con
return ostr << "]";
}

inline uint64_t versionNumber(
uint64_t version_major,
uint64_t version_minor,
uint64_t version_patch = 0,
uint64_t revision = 0) {

// in this case version_major can be up to 1000
static auto revision_decimal_places = 8;
static auto patch_decimal_places = 4;
static auto minor_decimal_places = 4;

auto const result = version_major * static_cast<uint64_t>(std::pow(10, minor_decimal_places + patch_decimal_places + revision_decimal_places))
+ version_minor * static_cast<uint64_t>(std::pow(10, patch_decimal_places + revision_decimal_places))
+ version_patch * static_cast<uint64_t>(std::pow(10, revision_decimal_places))
+ revision;

return result;
}

uint64_t versionNumber(const clickhouse::ServerInfo & server_info);
1 change: 1 addition & 0 deletions ut/value_generators.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <clickhouse/base/socket.h> // for ipv4-ipv6 platform-specific stuff
#include <clickhouse/columns/numeric.h>
#include <clickhouse/columns/uuid.h>

#include "utils.h"

Expand Down