Skip to content

Commit 888e02e

Browse files
authored
Merge pull request #191 from Enmk/various_test_fixes
Various test fixes
2 parents 3ae6bf2 + 734b48c commit 888e02e

File tree

9 files changed

+126
-67
lines changed

9 files changed

+126
-67
lines changed

ut/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ SET ( clickhouse-cpp-ut-src
1919
array_of_low_cardinality_tests.cpp
2020
CreateColumnByType_ut.cpp
2121
Column_ut.cpp
22+
roundtrip_column.cpp
2223

2324
utils.cpp
2425
value_generators.cpp

ut/Column_ut.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
#include <clickhouse/base/output.h>
1414
#include <clickhouse/base/socket.h> // for ipv4-ipv6 platform-specific stuff
1515

16+
#include <clickhouse/client.h>
17+
1618
#include <gtest/gtest.h>
1719

1820
#include "utils.h"
21+
#include "roundtrip_column.h"
1922
#include "value_generators.h"
2023

2124
namespace {
@@ -262,3 +265,35 @@ TYPED_TEST(GenericColumnTest, LoadAndSave) {
262265

263266
EXPECT_TRUE(CompareRecursive(*column_A, *column_B));
264267
}
268+
269+
const auto LocalHostEndpoint = ClientOptions()
270+
.SetHost( getEnvOrDefault("CLICKHOUSE_HOST", "localhost"))
271+
.SetPort( getEnvOrDefault<size_t>("CLICKHOUSE_PORT", "9000"))
272+
.SetUser( getEnvOrDefault("CLICKHOUSE_USER", "default"))
273+
.SetPassword( getEnvOrDefault("CLICKHOUSE_PASSWORD", ""))
274+
.SetDefaultDatabase(getEnvOrDefault("CLICKHOUSE_DB", "default"));
275+
276+
TYPED_TEST(GenericColumnTest, RoundTrip) {
277+
auto [column, values] = this->MakeColumnWithValues(100);
278+
EXPECT_EQ(values.size(), column->Size());
279+
280+
clickhouse::Client client(LocalHostEndpoint);
281+
282+
if constexpr (std::is_same_v<typename TestFixture::ColumnType, ColumnDate32>) {
283+
// Date32 first appeared in v21.9.2.17-stable
284+
const auto server_info = client.GetServerInfo();
285+
if (versionNumber(server_info) < versionNumber(21, 9)) {
286+
GTEST_SKIP() << "Date32 is availble since v21.9.2.17-stable and can't be tested against server: " << server_info;
287+
}
288+
}
289+
290+
if constexpr (std::is_same_v<typename TestFixture::ColumnType, ColumnInt128>) {
291+
const auto server_info = client.GetServerInfo();
292+
if (versionNumber(server_info) < versionNumber(21, 7)) {
293+
GTEST_SKIP() << "ColumnInt128 is availble since v21.7.2.7-stable and can't be tested against server: " << server_info;
294+
}
295+
}
296+
297+
auto result_typed = RoundtripColumnValues(client, column)->template AsStrict<typename TestFixture::ColumnType>();
298+
EXPECT_TRUE(CompareRecursive(*column, *result_typed));
299+
}

ut/array_of_low_cardinality_tests.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,19 @@ TEST(ArrayOfLowCardinality, InsertAndQuery) {
8787
client.Insert("array_lc", block);
8888

8989
client.Select("SELECT * FROM array_lc", [&](const Block& bl) {
90-
for (size_t c = 0; c < bl.GetRowCount(); ++c) {
91-
auto col = bl[0]->As<ColumnArray>()->GetAsColumn(c);
92-
for (size_t i = 0; i < col->Size(); ++i) {
93-
auto stringColumn = col->As<ColumnString>();
94-
const auto string = stringColumn->At(i);
95-
90+
for (size_t c = 0; c < bl.GetRowCount(); ++c) {
91+
auto col = bl[0]->As<ColumnArray>()->GetAsColumn(c);
92+
for (size_t i = 0; i < col->Size(); ++i) {
93+
if (auto string_column = col->As<ColumnString>()) {
94+
const auto string = string_column->At(i);
95+
ASSERT_EQ(testData[c][i], string);
96+
} else if (auto lc_string_column = col->As<ColumnLowCardinalityT<ColumnString>>()) {
97+
const auto string = lc_string_column->At(i);
9698
ASSERT_EQ(testData[c][i], string);
99+
} else {
100+
FAIL() << "Unexpected column type: " << col->Type()->GetName();
97101
}
98102
}
99-
}
100-
);
101-
}
103+
}
104+
});
105+
}

ut/client_ut.cpp

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,15 @@
33
#include "readonly_client_test.h"
44
#include "connection_failed_client_test.h"
55
#include "utils.h"
6+
#include "roundtrip_column.h"
67

78
#include <gtest/gtest.h>
89

9-
#include <cmath>
1010
#include <thread>
1111
#include <chrono>
1212

1313
using namespace clickhouse;
1414

15-
namespace {
16-
17-
uint64_t versionNumber(
18-
uint64_t version_major,
19-
uint64_t version_minor,
20-
uint64_t version_patch = 0,
21-
uint64_t revision = 0) {
22-
23-
// in this case version_major can be up to 1000
24-
static auto revision_decimal_places = 8;
25-
static auto patch_decimal_places = 4;
26-
static auto minor_decimal_places = 4;
27-
28-
auto const result = version_major * static_cast<uint64_t>(std::pow(10, minor_decimal_places + patch_decimal_places + revision_decimal_places))
29-
+ version_minor * static_cast<uint64_t>(std::pow(10, patch_decimal_places + revision_decimal_places))
30-
+ version_patch * static_cast<uint64_t>(std::pow(10, revision_decimal_places))
31-
+ revision;
32-
33-
return result;
34-
}
35-
36-
uint64_t versionNumber(const ServerInfo & server_info) {
37-
return versionNumber(server_info.version_major, server_info.version_minor, server_info.version_patch, server_info.revision);
38-
}
39-
40-
}
41-
4215
// Use value-parameterized tests to run same tests with different client
4316
// options.
4417
class ClientCase : public testing::TestWithParam<ClientOptions> {
@@ -978,35 +951,6 @@ TEST_P(ClientCase, DISABLED_ArrayArrayUInt64) {
978951
}
979952
}
980953

981-
ColumnRef RoundtripColumnValues(Client& client, ColumnRef expected) {
982-
// Create a temporary table with a single column
983-
// insert values from `expected`
984-
// select and aggregate all values from block into `result` column
985-
auto result = expected->CloneEmpty();
986-
987-
const std::string type_name = result->GetType().GetName();
988-
client.Execute("DROP TEMPORARY TABLE IF EXISTS temporary_roundtrip_table;");
989-
client.Execute("CREATE TEMPORARY TABLE IF NOT EXISTS temporary_roundtrip_table (col " + type_name + ");");
990-
{
991-
Block block;
992-
block.AppendColumn("col", expected);
993-
block.RefreshRowCount();
994-
client.Insert("temporary_roundtrip_table", block);
995-
}
996-
997-
client.Select("SELECT col FROM temporary_roundtrip_table", [&result](const Block& b) {
998-
if (b.GetRowCount() == 0)
999-
return;
1000-
1001-
ASSERT_EQ(1u, b.GetColumnCount());
1002-
result->Append(b[0]);
1003-
});
1004-
1005-
EXPECT_EQ(expected->GetType(), result->GetType());
1006-
EXPECT_EQ(expected->Size(), result->Size());
1007-
return result;
1008-
}
1009-
1010954
TEST_P(ClientCase, RoundtripArrayTUint64) {
1011955
auto array = std::make_shared<ColumnArrayT<ColumnUInt64>>();
1012956
array->Append({0, 1, 2});

ut/roundtrip_column.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "roundtrip_column.h"
2+
3+
#include <clickhouse/client.h>
4+
#include <clickhouse/block.h>
5+
6+
#include <gtest/gtest.h>
7+
8+
namespace {
9+
using namespace clickhouse;
10+
}
11+
12+
ColumnRef RoundtripColumnValues(Client& client, ColumnRef expected) {
13+
// Create a temporary table with a single column
14+
// insert values from `expected`
15+
// select and aggregate all values from block into `result` column
16+
auto result = expected->CloneEmpty();
17+
18+
const std::string type_name = result->GetType().GetName();
19+
client.Execute("DROP TEMPORARY TABLE IF EXISTS temporary_roundtrip_table;");
20+
client.Execute("CREATE TEMPORARY TABLE IF NOT EXISTS temporary_roundtrip_table (col " + type_name + ");");
21+
{
22+
Block block;
23+
block.AppendColumn("col", expected);
24+
block.RefreshRowCount();
25+
client.Insert("temporary_roundtrip_table", block);
26+
}
27+
28+
client.Select("SELECT col FROM temporary_roundtrip_table", [&result](const Block& b) {
29+
if (b.GetRowCount() == 0)
30+
return;
31+
32+
ASSERT_EQ(1u, b.GetColumnCount());
33+
result->Append(b[0]);
34+
});
35+
36+
EXPECT_EQ(expected->GetType(), result->GetType());
37+
EXPECT_EQ(expected->Size(), result->Size());
38+
39+
return result;
40+
}

ut/roundtrip_column.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <clickhouse/columns/column.h>
4+
5+
namespace clickhouse {
6+
class Client;
7+
}
8+
9+
clickhouse::ColumnRef RoundtripColumnValues(clickhouse::Client& client, clickhouse::ColumnRef expected);

ut/utils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <iomanip>
1919
#include <sstream>
2020

21+
2122
namespace {
2223
using namespace clickhouse;
2324
std::ostream & printColumnValue(const ColumnRef& c, const size_t row, std::ostream & ostr);
@@ -266,3 +267,7 @@ std::ostream & operator<<(std::ostream & ostr, const ServerInfo & server_info) {
266267
}
267268

268269
}
270+
271+
uint64_t versionNumber(const ServerInfo & server_info) {
272+
return versionNumber(server_info.version_major, server_info.version_minor, server_info.version_patch, server_info.revision);
273+
}

ut/utils.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22

33
#include <clickhouse/base/platform.h>
4-
#include <clickhouse/columns/uuid.h>
54

65
#include "utils_meta.h"
76
#include "utils_comparison.h"
@@ -12,12 +11,14 @@
1211
#include <system_error>
1312
#include <type_traits>
1413
#include <vector>
14+
#include <cmath>
1515

1616
#include <time.h>
1717

1818
#include <gtest/gtest.h>
1919

2020
namespace clickhouse {
21+
class Client;
2122
class Block;
2223
class Type;
2324
struct ServerInfo;
@@ -136,4 +137,23 @@ std::ostream& operator<<(std::ostream & ostr, const PrintContainer<T>& print_con
136137
return ostr << "]";
137138
}
138139

140+
inline uint64_t versionNumber(
141+
uint64_t version_major,
142+
uint64_t version_minor,
143+
uint64_t version_patch = 0,
144+
uint64_t revision = 0) {
139145

146+
// in this case version_major can be up to 1000
147+
static auto revision_decimal_places = 8;
148+
static auto patch_decimal_places = 4;
149+
static auto minor_decimal_places = 4;
150+
151+
auto const result = version_major * static_cast<uint64_t>(std::pow(10, minor_decimal_places + patch_decimal_places + revision_decimal_places))
152+
+ version_minor * static_cast<uint64_t>(std::pow(10, patch_decimal_places + revision_decimal_places))
153+
+ version_patch * static_cast<uint64_t>(std::pow(10, revision_decimal_places))
154+
+ revision;
155+
156+
return result;
157+
}
158+
159+
uint64_t versionNumber(const clickhouse::ServerInfo & server_info);

ut/value_generators.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

67
#include "utils.h"
78

0 commit comments

Comments
 (0)