YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/test/ql-test-base.h
Line
Count
Source (jump to first uncovered line)
1
//--------------------------------------------------------------------------------------------------
2
// Copyright (c) YugaByte, Inc.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
// in compliance with the License.  You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software distributed under the License
10
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
// or implied.  See the License for the specific language governing permissions and limitations
12
// under the License.
13
//
14
//--------------------------------------------------------------------------------------------------
15
16
#ifndef YB_YQL_CQL_QL_TEST_QL_TEST_BASE_H_
17
#define YB_YQL_CQL_QL_TEST_QL_TEST_BASE_H_
18
19
#include "yb/common/ql_rowblock.h"
20
21
#include "yb/gutil/bind.h"
22
23
#include "yb/integration-tests/mini_cluster.h"
24
#include "yb/master/mini_master.h"
25
26
#include "yb/server/server_fwd.h"
27
28
#include "yb/util/async_util.h"
29
#include "yb/util/test_util.h"
30
31
#include "yb/yql/cql/ql/ql_fwd.h"
32
#include "yb/yql/cql/ql/ptree/ptree_fwd.h"
33
#include "yb/yql/cql/ql/util/statement_params.h"
34
#include "yb/yql/cql/ql/util/statement_result.h"
35
#include "yb/yql/cql/ql/util/util_fwd.h"
36
37
namespace yb {
38
namespace ql {
39
40
#define ANALYZE_VALID_STMT(stmt, parse_tree)   \
41
0
  do {                                         \
42
0
    Status s = TestAnalyzer(stmt, parse_tree); \
43
0
    EXPECT_TRUE(s.ok());                       \
44
0
  } while (false)
45
46
#define ANALYZE_INVALID_STMT(stmt, parse_tree) \
47
0
  do {                                         \
48
0
    Status s = TestAnalyzer(stmt, parse_tree); \
49
0
    EXPECT_FALSE(s.ok());                      \
50
0
  } while (false)
51
52
#define PARSE_VALID_STMT(stmt)                 \
53
0
  do {                                         \
54
0
    Status s = TestParser(stmt);               \
55
0
    EXPECT_TRUE(s.ok());                       \
56
0
  } while (false)
57
58
#define PARSE_INVALID_STMT(stmt)               \
59
0
  do {                                         \
60
0
    Status s = TestParser(stmt);               \
61
0
    EXPECT_FALSE(s.ok());                      \
62
0
  } while (false)
63
64
#define PARSE_INVALID_STMT_ERR(stmt, err_msg)  \
65
0
  do {                                         \
66
0
    Status s = TestParser(stmt);               \
67
0
    EXPECT_FALSE(s.ok());                      \
68
0
    EXPECT_FALSE(s.ToString().find(err_msg) == string::npos); \
69
0
  } while (false)
70
71
#define PROCESSOR_RUN(result, stmt)            \
72
0
    LOG(INFO) << "Run: " << stmt;              \
73
0
    Status result = processor->Run(stmt);
74
75
#define EXEC_VALID_STMT(stmt)                  \
76
0
  do {                                         \
77
0
    PROCESSOR_RUN(s, stmt);                    \
78
0
    EXPECT_TRUE(s.ok());                       \
79
0
  } while (false)
80
81
#define EXEC_INVALID_STMT_WITH_ERROR(stmt, err_msg)           \
82
0
  do {                                                        \
83
0
    PROCESSOR_RUN(s, stmt);                                   \
84
0
    EXPECT_FALSE(s.ok());                                     \
85
0
    EXPECT_FALSE(s.ToString().find(err_msg) == string::npos); \
86
0
  } while (false)
87
88
0
#define EXEC_INVALID_STMT(stmt) EXEC_INVALID_STMT_WITH_ERROR(stmt, "")
89
90
#define CHECK_VALID_STMT(stmt)                 \
91
0
  do {                                         \
92
0
    PROCESSOR_RUN(s, stmt);                    \
93
0
    CHECK(s.ok()) << "Failure: " << s;         \
94
0
  } while (false)
95
96
#define CHECK_INVALID_STMT(stmt)               \
97
0
  do {                                         \
98
0
    PROCESSOR_RUN(s, stmt);                    \
99
0
    CHECK(!s.ok()) << "Expect failure";        \
100
0
  } while (false)
101
102
class ClockHolder {
103
 protected:
104
  ClockHolder();
105
106
  server::ClockPtr clock_;
107
};
108
109
class TestQLProcessor : public ClockHolder {
110
 public:
111
  // Public types.
112
  typedef std::unique_ptr<TestQLProcessor> UniPtr;
113
  typedef std::unique_ptr<const TestQLProcessor> UniPtrConst;
114
115
  // Constructors.
116
  TestQLProcessor(client::YBClient* client,
117
                  std::shared_ptr<client::YBMetaDataCache> cache,
118
                  const RoleName& role_name);
119
  virtual ~TestQLProcessor();
120
121
  void RunAsyncDone(
122
      Callback<void(const Status&)> cb, const Status& s,
123
0
      const ExecutedResultPtr& result) {
124
0
    result_ = result;
125
0
    cb.Run(s);
126
0
  }
127
128
  void RunAsync(
129
      const string& stmt, const StatementParameters& params, Callback<void(const Status&)> cb);
130
131
  // Execute a QL statement.
132
  CHECKED_STATUS Run(
133
      const std::string& stmt, const StatementParameters& params = StatementParameters());
134
135
  CHECKED_STATUS Run(const Statement& stmt, const StatementParameters& params);
136
137
  // Construct a row_block and send it back.
138
  std::shared_ptr<QLRowBlock> row_block() const;
139
140
0
  const ExecutedResultPtr& result() const { return result_; }
141
142
  const RowsResult* rows_result() const;
143
144
  std::string CurrentKeyspace() const;
145
146
  CHECKED_STATUS UseKeyspace(const std::string& keyspace_name);
147
148
  void RemoveCachedTableDesc(const client::YBTableName& table_name);
149
150
0
  const ParseTreePtr& GetLastParseTree() const {
151
0
    return parse_tree_;
152
0
  }
153
154
0
  QLProcessor& ql_processor() {
155
0
    return *processor_;
156
0
  }
157
158
  const TreeNodePtr& GetLastParseTreeRoot() const;
159
160
 private:
161
  void RunAsyncInternal(const std::string& stmt, const StatementParameters& params,
162
                        StatementExecutedCallback cb, bool reparsed = false);
163
164
  std::unique_ptr<QLProcessor> processor_;
165
166
  // Execute result.
167
  ExecutedResultPtr result_;
168
169
  ParseTreePtr parse_tree_;
170
};
171
172
// Base class for all QL test cases.
173
class QLTestBase : public YBTest {
174
 public:
175
  //------------------------------------------------------------------------------------------------
176
  // Constructor and destructor.
177
  QLTestBase();
178
  ~QLTestBase();
179
180
  //------------------------------------------------------------------------------------------------
181
  // Test start and cleanup functions.
182
0
  void SetUp() override {
183
0
    YBTest::SetUp();
184
0
  }
185
186
  void TearDown() override;
187
188
  //------------------------------------------------------------------------------------------------
189
  // Test only the parser.
190
  CHECKED_STATUS TestParser(const std::string& stmt);
191
192
  // Tests parser and analyzer
193
  CHECKED_STATUS TestAnalyzer(const string& stmt, ParseTreePtr* parse_tree);
194
195
  //------------------------------------------------------------------------------------------------
196
  // Create simulated cluster.
197
  void CreateSimulatedCluster(int num_tablet_servers = 1);
198
199
  // Create ql processor.
200
  TestQLProcessor* GetQLProcessor(const RoleName& role_name = "");
201
202
203
  //------------------------------------------------------------------------------------------------
204
  // Utility functions for QL tests.
205
206
  void VerifyPaginationSelect(TestQLProcessor* processor,
207
                              const string &select_query,
208
                              int page_size,
209
                              const string expected_rows);
210
211
 protected:
212
  //------------------------------------------------------------------------------------------------
213
214
  // Simulated cluster.
215
  std::shared_ptr<MiniCluster> cluster_;
216
217
  // Simulated YB client.
218
  std::unique_ptr<client::YBClient> client_;
219
  std::shared_ptr<client::YBMetaDataCache> metadata_cache_;
220
221
  // QL Processor.
222
  std::vector<TestQLProcessor::UniPtr> ql_processors_;
223
224
  static const std::string kDefaultKeyspaceName;
225
};
226
227
}  // namespace ql
228
}  // namespace yb
229
230
#endif  // YB_YQL_CQL_QL_TEST_QL_TEST_BASE_H_