Skip to content

Commit ef0e515

Browse files
committed
Fixed tests
1 parent 945d755 commit ef0e515

File tree

2 files changed

+102
-15
lines changed

2 files changed

+102
-15
lines changed

ut/client_ut.cpp

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,29 @@ class ClientCase : public testing::TestWithParam<ClientOptions> {
3030
client_->Execute("DROP DATABASE test_clickhouse_cpp");
3131
}
3232

33+
template <typename T>
34+
std::shared_ptr<T> createTableWithOneColumn(Block & block)
35+
{
36+
auto col = std::make_shared<T>();
37+
const auto type_name = col->GetType().GetName();
38+
39+
client_->Execute("DROP TABLE IF EXISTS " + table_name + ";");
40+
client_->Execute("CREATE TABLE IF NOT EXISTS " + table_name + "( " + column_name + " " + type_name + " )"
41+
"ENGINE = Memory");
42+
43+
block.AppendColumn("test_column", col);
44+
45+
return col;
46+
}
47+
48+
std::string getOneColumnSelectQuery() const
49+
{
50+
return "SELECT " + column_name + " FROM " + table_name;
51+
}
52+
3353
std::unique_ptr<Client> client_;
54+
const std::string table_name = "test_clickhouse_cpp.test_ut_table";
55+
const std::string column_name = "test_column";
3456
};
3557

3658
TEST_P(ClientCase, Array) {
@@ -117,22 +139,57 @@ TEST_P(ClientCase, Date) {
117139

118140
TEST_P(ClientCase, LowCardinality) {
119141
Block block;
120-
client_->Execute("DROP TABLE IF EXISTS test_clickhouse_cpp.low_cardinality;");
142+
auto lc = createTableWithOneColumn<ColumnLowCardinalityT<ColumnString>>(block);
121143

122-
client_->Execute("CREATE TABLE IF NOT EXISTS "
123-
"test_clickhouse_cpp.low_cardinality (lc LowCardinality(String)) "
124-
"ENGINE = Memory");
144+
const std::vector<std::string> data{{"FooBar", "1", "2", "Foo", "4", "Bar", "Foo", "7", "8", "Foo"}};
145+
lc->AppendMany(data);
146+
147+
block.RefreshRowCount();
148+
client_->Insert(table_name, block);
149+
150+
size_t total_rows = 0;
151+
client_->Select(getOneColumnSelectQuery(),
152+
[&total_rows, &data](const Block& block) {
153+
total_rows += block.GetRowCount();
154+
if (block.GetRowCount() == 0) {
155+
return;
156+
}
157+
158+
ASSERT_EQ(1U, block.GetColumnCount());
159+
if (auto col = block[0]->As<ColumnLowCardinalityT<ColumnString>>()) {
160+
ASSERT_EQ(data.size(), col->Size());
161+
for (size_t i = 0; i < col->Size(); ++i) {
162+
EXPECT_EQ(data[i], (*col)[i]) << " at index: " << i;
163+
}
164+
}
165+
}
166+
);
167+
168+
ASSERT_EQ(total_rows, data.size());
169+
}
170+
171+
TEST_P(ClientCase, LowCardinality_InsertAfterClear) {
172+
// User can successfully insert values after invoking Clear() on LC column.
173+
Block block;
174+
auto lc = createTableWithOneColumn<ColumnLowCardinalityT<ColumnString>>(block);
175+
176+
// Add some data, but don't care about it much.
177+
lc->AppendMany(std::vector<std::string_view>{"abc", "def", "123", "abc", "123", "def", "ghi"});
178+
EXPECT_GT(lc->Size(), 0u);
179+
EXPECT_GT(lc->GetDictionarySize(), 0u);
125180

126-
auto lc = std::make_shared<ColumnLowCardinalityT<ColumnString>>();
181+
lc->Clear();
127182

183+
// Now ensure that all data appended after Clear() is inserted properly
128184
const std::vector<std::string> data{{"FooBar", "1", "2", "Foo", "4", "Bar", "Foo", "7", "8", "Foo"}};
129185
lc->AppendMany(data);
130186

131-
block.AppendColumn("lc", lc);
132-
client_->Insert("test_clickhouse_cpp.low_cardinality", block);
187+
block.RefreshRowCount();
188+
client_->Insert(table_name, block);
133189

190+
// Now validate that data was properly inserted
134191
size_t total_rows = 0;
135-
client_->Select("SELECT lc FROM test_clickhouse_cpp.low_cardinality",
192+
client_->Select(getOneColumnSelectQuery(),
136193
[&total_rows, &data](const Block& block) {
137194
total_rows += block.GetRowCount();
138195
if (block.GetRowCount() == 0) {

ut/columns_ut.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
#include <contrib/gtest/gtest.h>
1212
#include "utils.h"
1313

14+
#include <string_view>
15+
16+
17+
namespace
18+
{
19+
1420
using namespace clickhouse;
21+
using namespace std::literals::string_view_literals;
1522

1623
static std::vector<uint32_t> MakeNumbers() {
1724
return std::vector<uint32_t>
@@ -40,6 +47,17 @@ static std::vector<uint64_t> MakeUUIDs() {
4047
0x3507213c178649f9llu, 0x9faf035d662f60aellu};
4148
}
4249

50+
#define BINARY_STRING(x) std::string_view(x, sizeof(x) - 1)
51+
52+
static const auto LOWCARDINALITY_STRING_FOOBAR_10_ITEMS_BINARY =
53+
"\x01\x00\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00"
54+
"\x09\x00\x00\x00\x00\x00\x00\x00\x00\x06\x46\x6f\x6f\x42\x61\x72"
55+
"\x01\x31\x01\x32\x03\x46\x6f\x6f\x01\x34\x03\x42\x61\x72\x01\x37"
56+
"\x01\x38\x0a\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06"
57+
"\x04\x07\x08\x04"sv;
58+
59+
}
60+
4361
// TODO: add tests for ColumnDecimal.
4462

4563
TEST(ColumnsCase, NumericInit) {
@@ -217,14 +235,26 @@ TEST(ColumnsCase, LowCardinalityWrapperString_Append_and_Read) {
217235
}
218236
}
219237

220-
#define BINARY_STRING(x) std::string_view(x, sizeof(x) - 1)
238+
TEST(ColumnsCase, ColumnLowCardinalityT_String_Clear_and_Append) {
239+
const size_t items_count = 11;
240+
ColumnLowCardinalityT<ColumnString> col;
241+
for (const auto & item : build_vector(&foobar, items_count))
242+
{
243+
col.Append(item);
244+
}
221245

222-
static const auto LOWCARDINALITY_STRING_FOOBAR_10_ITEMS_BINARY =
223-
BINARY_STRING("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00"
224-
"\x09\x00\x00\x00\x00\x00\x00\x00\x00\x06\x46\x6f\x6f\x42\x61\x72"
225-
"\x01\x31\x01\x32\x03\x46\x6f\x6f\x01\x34\x03\x42\x61\x72\x01\x37"
226-
"\x01\x38\x0a\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06"
227-
"\x04\x07\x08\x04");
246+
col.Clear();
247+
ASSERT_EQ(col.Size(), 0u);
248+
ASSERT_EQ(col.GetDictionarySize(), 1u); // null-item
249+
250+
for (const auto & item : build_vector(&foobar, items_count))
251+
{
252+
col.Append(item);
253+
}
254+
255+
ASSERT_EQ(col.Size(), items_count);
256+
ASSERT_EQ(col.GetDictionarySize(), 8u + 1); // 8 unique items from sequence + 1 null-item
257+
}
228258

229259
TEST(ColumnsCase, LowCardinalityString_Load) {
230260
const size_t items_count = 10;

0 commit comments

Comments
 (0)