OmniSciDB  5ade3759e0
TokenCompletionHintsTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "../ThriftHandler/TokenCompletionHints.h"
18 #include "TestHelpers.h"
19 
20 #include <gtest/gtest.h>
21 
22 TEST(FindLastWord, SimpleId) {
23  std::string partial_query{"SELECT test"};
24  ASSERT_EQ("test", find_last_word_from_cursor(partial_query, partial_query.size()));
25 }
26 
27 TEST(FindLastWord, QualifiedId) {
28  std::string partial_query{"SELECT test.x"};
29  ASSERT_EQ("test.x", find_last_word_from_cursor(partial_query, partial_query.size()));
30 }
31 
32 TEST(FindLastWord, CursorPastEnd) {
33  std::string partial_query{"SELECT test"};
34  ASSERT_EQ("", find_last_word_from_cursor(partial_query, partial_query.size() + 1));
35 }
36 
37 TEST(FindLastWord, CursorInside) {
38  std::string partial_query{"SELECT str FROM te LIMIT 10"};
39  ASSERT_EQ("te", find_last_word_from_cursor(partial_query, 18));
40 }
41 
42 TEST(FindLastWord, EmptyString) {
43  ASSERT_EQ("", find_last_word_from_cursor("", 0));
44  ASSERT_EQ("", find_last_word_from_cursor("", 1));
45  ASSERT_EQ("", find_last_word_from_cursor("", -1));
46 }
47 
48 namespace {
49 
50 void assert_set_equals(const std::vector<std::string>& expected,
51  const std::vector<std::string>& actual) {
52  auto actual_sorted = actual;
53  std::sort(actual_sorted.begin(), actual_sorted.end());
54  auto expected_sorted = expected;
55  std::sort(expected_sorted.begin(), expected_sorted.end());
56  ASSERT_EQ(expected_sorted, actual_sorted);
57 }
58 
59 } // namespace
60 
61 TEST(Completion, QualifiedColumnName) {
62  std::unordered_map<std::string, std::unordered_set<std::string>> column_names_by_table;
63  column_names_by_table["test"] = {"x", "ss", "str"};
64  column_names_by_table["test_inner"] = {"x"};
65  {
66  std::vector<TCompletionHint> completion_hints;
67  std::string last_word{"test.x"};
68  ASSERT_TRUE(
69  get_qualified_column_hints(completion_hints, last_word, column_names_by_table));
70  ASSERT_EQ(size_t(1), completion_hints.size());
71  ASSERT_TRUE(TCompletionHintType::COLUMN == completion_hints.front().type);
72  ASSERT_EQ(last_word, completion_hints.front().replaced);
73  assert_set_equals({"test.x"}, completion_hints.front().hints);
74  }
75  {
76  std::vector<TCompletionHint> completion_hints;
77  std::string last_word{"test.s"};
78  ASSERT_TRUE(
79  get_qualified_column_hints(completion_hints, last_word, column_names_by_table));
80  ASSERT_EQ(size_t(1), completion_hints.size());
81  ASSERT_TRUE(TCompletionHintType::COLUMN == completion_hints.front().type);
82  ASSERT_EQ(last_word, completion_hints.front().replaced);
83  assert_set_equals({"test.ss", "test.str"}, completion_hints.front().hints);
84  }
85  {
86  std::vector<TCompletionHint> completion_hints;
87  std::string last_word{"test."};
88  ASSERT_TRUE(
89  get_qualified_column_hints(completion_hints, last_word, column_names_by_table));
90  ASSERT_EQ(size_t(1), completion_hints.size());
91  ASSERT_TRUE(TCompletionHintType::COLUMN == completion_hints.front().type);
92  ASSERT_EQ(last_word, completion_hints.front().replaced);
93  assert_set_equals({"test.x", "test.ss", "test.str"}, completion_hints.front().hints);
94  }
95  {
96  std::vector<TCompletionHint> completion_hints;
97  ASSERT_TRUE(
98  get_qualified_column_hints(completion_hints, "test.y", column_names_by_table));
99  ASSERT_TRUE(completion_hints.empty());
100  }
101 }
102 
103 TEST(Completion, ColumnName) {
104  std::unordered_map<std::string, std::unordered_set<std::string>> column_names_by_table;
105  column_names_by_table["test"] = {"x", "ss", "str"};
106  column_names_by_table["test_inner"] = {"x"};
107  {
108  std::vector<TCompletionHint> completion_hints;
109  std::string last_word{"s"};
110  get_column_hints(completion_hints, last_word, column_names_by_table);
111  ASSERT_EQ(size_t(1), completion_hints.size());
112  ASSERT_TRUE(TCompletionHintType::COLUMN == completion_hints.front().type);
113  ASSERT_EQ(last_word, completion_hints.front().replaced);
114  assert_set_equals({"ss", "str"}, completion_hints.front().hints);
115  }
116  {
117  std::vector<TCompletionHint> completion_hints;
118  std::string last_word{"x"};
119  get_column_hints(completion_hints, last_word, column_names_by_table);
120  ASSERT_EQ(size_t(1), completion_hints.size());
121  ASSERT_TRUE(TCompletionHintType::COLUMN == completion_hints.front().type);
122  ASSERT_EQ(last_word, completion_hints.front().replaced);
123  assert_set_equals({"x"}, completion_hints.front().hints);
124  }
125  {
126  std::vector<TCompletionHint> completion_hints;
127  get_column_hints(completion_hints, "y", column_names_by_table);
128  ASSERT_TRUE(completion_hints.empty());
129  }
130 }
131 
132 TEST(Completion, FilterKeywords) {
133  std::vector<TCompletionHint> original_hints;
134  std::vector<TCompletionHint> expected_filtered_hints;
135  {
136  TCompletionHint hint;
137  hint.type = TCompletionHintType::COLUMN;
138  hint.hints.emplace_back("foo");
139  original_hints.push_back(hint);
140  expected_filtered_hints.push_back(hint);
141  }
142  {
143  TCompletionHint hint;
144  hint.type = TCompletionHintType::KEYWORD;
145  hint.hints.emplace_back("SUBSTR");
146  original_hints.push_back(hint);
147  }
148  {
149  TCompletionHint hint;
150  hint.type = TCompletionHintType::KEYWORD;
151  hint.hints.emplace_back("GROUP");
152  original_hints.push_back(hint);
153  expected_filtered_hints.push_back(hint);
154  }
155  {
156  TCompletionHint hint;
157  hint.type = TCompletionHintType::KEYWORD;
158  hint.hints.emplace_back("ON");
159  original_hints.push_back(hint);
160  expected_filtered_hints.push_back(hint);
161  }
162  {
163  TCompletionHint hint;
164  hint.type = TCompletionHintType::KEYWORD;
165  hint.hints.emplace_back("OUTER");
166  original_hints.push_back(hint);
167  expected_filtered_hints.push_back(hint);
168  }
169  const auto filtered_hints = just_whitelisted_keyword_hints(original_hints);
170  ASSERT_EQ(expected_filtered_hints, filtered_hints);
171 }
172 
173 TEST(Completion, ShouldSuggestColumnHints) {
174  ASSERT_TRUE(should_suggest_column_hints("SELECT x"));
175  ASSERT_FALSE(should_suggest_column_hints("SELECT x "));
176  ASSERT_TRUE(should_suggest_column_hints("SELECT x,"));
177  ASSERT_TRUE(should_suggest_column_hints("SELECT x , "));
178  ASSERT_TRUE(should_suggest_column_hints("SELECT x, y"));
179  ASSERT_FALSE(should_suggest_column_hints("SELECT x, y "));
180  ASSERT_TRUE(should_suggest_column_hints("SELECT x, y,"));
181  ASSERT_TRUE(should_suggest_column_hints("SELECT x, y , "));
182  ASSERT_TRUE(should_suggest_column_hints("SELECT "));
183 }
184 
185 int main(int argc, char** argv) {
187  testing::InitGoogleTest(&argc, argv);
188 
189  int err{0};
190  try {
191  err = RUN_ALL_TESTS();
192  } catch (const std::exception& e) {
193  LOG(ERROR) << e.what();
194  }
195  return err;
196 }
int main(int argc, char **argv)
#define LOG(tag)
Definition: Logger.h:182
std::vector< TCompletionHint > just_whitelisted_keyword_hints(const std::vector< TCompletionHint > &hints)
void get_column_hints(std::vector< TCompletionHint > &hints, const std::string &last_word, const std::unordered_map< std::string, std::unordered_set< std::string >> &column_names_by_table)
bool get_qualified_column_hints(std::vector< TCompletionHint > &hints, const std::string &last_word, const std::unordered_map< std::string, std::unordered_set< std::string >> &column_names_by_table)
bool should_suggest_column_hints(const std::string &partial_query)
std::string find_last_word_from_cursor(const std::string &sql, const ssize_t cursor)
TEST(FindLastWord, SimpleId)
void assert_set_equals(const std::vector< std::string > &expected, const std::vector< std::string > &actual)
void init_logger_stderr_only(int argc, char const *const *argv)
Definition: TestHelpers.h:194