|
1 | 1 | #include <clickhouse/client.h> |
| 2 | +#include <clickhouse/columns/tuple.h> |
| 3 | +#include <clickhouse/types/types.h> |
| 4 | + |
| 5 | +#include "clickhouse/columns/column.h" |
| 6 | +#include "gtest/gtest-message.h" |
2 | 7 | #include "readonly_client_test.h" |
3 | 8 | #include "connection_failed_client_test.h" |
4 | 9 | #include "utils.h" |
@@ -83,3 +88,43 @@ TEST(BlockTest, Iterators) { |
83 | 88 | ASSERT_NE(block.cbegin(), block.cend()); |
84 | 89 | } |
85 | 90 |
|
| 91 | +TEST(BlockTest, Clear) { |
| 92 | + // Test that Block::Clear removes all rows from all of the columns, |
| 93 | + // without changing column instances, types, names, etc. |
| 94 | + |
| 95 | + auto block = MakeBlock({ |
| 96 | + {"foo", std::make_shared<ColumnUInt8>(std::vector<uint8_t>{1, 2, 3, 4, 5})}, |
| 97 | + {"bar", std::make_shared<ColumnString>(std::vector<std::string>{"1", "2", "3", "4", "5"})}, |
| 98 | + }); |
| 99 | + |
| 100 | + std::vector<std::tuple<std::string, Column*>> expected_columns_description; |
| 101 | + for (const auto & c : block) { |
| 102 | + expected_columns_description.emplace_back(c.Name(), c.Column().get()); |
| 103 | + } |
| 104 | + |
| 105 | + block.Clear(); |
| 106 | + |
| 107 | + // Block must report empty after being cleared |
| 108 | + EXPECT_EQ(0u, block.GetRowCount()); |
| 109 | + |
| 110 | + size_t i = 0; |
| 111 | + for (const auto & c : block) { |
| 112 | + const auto & [expected_name, expected_column] = expected_columns_description[i]; |
| 113 | + SCOPED_TRACE(testing::Message("col #") << c.ColumnIndex() << " \"" << c.Name() << "\""); |
| 114 | + |
| 115 | + // MUST be same column object |
| 116 | + EXPECT_EQ(expected_column, c.Column().get()); |
| 117 | + |
| 118 | + // MUST have same column name |
| 119 | + EXPECT_EQ(expected_name, c.Name()); |
| 120 | + |
| 121 | + // column MUST be empty |
| 122 | + EXPECT_EQ(0u, c.Column()->Size()) |
| 123 | + << c.ColumnIndex() << " : " << c.Name(); |
| 124 | + |
| 125 | + ++i; |
| 126 | + EXPECT_FALSE(true); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | + |
0 commit comments