OmniSciDB  5ade3759e0
TokenCompletionHintsTest.cpp File Reference
#include "../ThriftHandler/TokenCompletionHints.h"
#include "TestHelpers.h"
#include <gtest/gtest.h>
+ Include dependency graph for TokenCompletionHintsTest.cpp:

Go to the source code of this file.

Namespaces

 anonymous_namespace{TokenCompletionHintsTest.cpp}
 

Functions

 TEST (FindLastWord, SimpleId)
 
 TEST (FindLastWord, QualifiedId)
 
 TEST (FindLastWord, CursorPastEnd)
 
 TEST (FindLastWord, CursorInside)
 
 TEST (FindLastWord, EmptyString)
 
void anonymous_namespace{TokenCompletionHintsTest.cpp}::assert_set_equals (const std::vector< std::string > &expected, const std::vector< std::string > &actual)
 
 TEST (Completion, QualifiedColumnName)
 
 TEST (Completion, ColumnName)
 
 TEST (Completion, FilterKeywords)
 
 TEST (Completion, ShouldSuggestColumnHints)
 
int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 185 of file TokenCompletionHintsTest.cpp.

References logger::ERROR, TestHelpers::init_logger_stderr_only(), and LOG.

185  {
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 }
#define LOG(tag)
Definition: Logger.h:182
void init_logger_stderr_only(int argc, char const *const *argv)
Definition: TestHelpers.h:194
+ Here is the call graph for this function:

◆ TEST() [1/9]

TEST ( FindLastWord  ,
SimpleId   
)

Definition at line 22 of file TokenCompletionHintsTest.cpp.

References find_last_word_from_cursor().

22  {
23  std::string partial_query{"SELECT test"};
24  ASSERT_EQ("test", find_last_word_from_cursor(partial_query, partial_query.size()));
25 }
std::string find_last_word_from_cursor(const std::string &sql, const ssize_t cursor)
+ Here is the call graph for this function:

◆ TEST() [2/9]

TEST ( FindLastWord  ,
QualifiedId   
)

Definition at line 27 of file TokenCompletionHintsTest.cpp.

References find_last_word_from_cursor().

27  {
28  std::string partial_query{"SELECT test.x"};
29  ASSERT_EQ("test.x", find_last_word_from_cursor(partial_query, partial_query.size()));
30 }
std::string find_last_word_from_cursor(const std::string &sql, const ssize_t cursor)
+ Here is the call graph for this function:

◆ TEST() [3/9]

TEST ( FindLastWord  ,
CursorPastEnd   
)

Definition at line 32 of file TokenCompletionHintsTest.cpp.

References find_last_word_from_cursor().

32  {
33  std::string partial_query{"SELECT test"};
34  ASSERT_EQ("", find_last_word_from_cursor(partial_query, partial_query.size() + 1));
35 }
std::string find_last_word_from_cursor(const std::string &sql, const ssize_t cursor)
+ Here is the call graph for this function:

◆ TEST() [4/9]

TEST ( FindLastWord  ,
CursorInside   
)

Definition at line 37 of file TokenCompletionHintsTest.cpp.

References find_last_word_from_cursor().

37  {
38  std::string partial_query{"SELECT str FROM te LIMIT 10"};
39  ASSERT_EQ("te", find_last_word_from_cursor(partial_query, 18));
40 }
std::string find_last_word_from_cursor(const std::string &sql, const ssize_t cursor)
+ Here is the call graph for this function:

◆ TEST() [5/9]

TEST ( FindLastWord  ,
EmptyString   
)

Definition at line 42 of file TokenCompletionHintsTest.cpp.

References find_last_word_from_cursor().

42  {
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 }
std::string find_last_word_from_cursor(const std::string &sql, const ssize_t cursor)
+ Here is the call graph for this function:

◆ TEST() [6/9]

TEST ( Completion  ,
QualifiedColumnName   
)

Definition at line 61 of file TokenCompletionHintsTest.cpp.

References anonymous_namespace{TokenCompletionHintsTest.cpp}::assert_set_equals(), and get_qualified_column_hints().

61  {
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 }
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)
void assert_set_equals(const std::vector< std::string > &expected, const std::vector< std::string > &actual)
+ Here is the call graph for this function:

◆ TEST() [7/9]

TEST ( Completion  ,
ColumnName   
)

Definition at line 103 of file TokenCompletionHintsTest.cpp.

References anonymous_namespace{TokenCompletionHintsTest.cpp}::assert_set_equals(), and get_column_hints().

103  {
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 }
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)
void assert_set_equals(const std::vector< std::string > &expected, const std::vector< std::string > &actual)
+ Here is the call graph for this function:

◆ TEST() [8/9]

TEST ( Completion  ,
FilterKeywords   
)

Definition at line 132 of file TokenCompletionHintsTest.cpp.

References just_whitelisted_keyword_hints().

132  {
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 }
std::vector< TCompletionHint > just_whitelisted_keyword_hints(const std::vector< TCompletionHint > &hints)
+ Here is the call graph for this function:

◆ TEST() [9/9]

TEST ( Completion  ,
ShouldSuggestColumnHints   
)

Definition at line 173 of file TokenCompletionHintsTest.cpp.

References should_suggest_column_hints().

173  {
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 }
bool should_suggest_column_hints(const std::string &partial_query)
+ Here is the call graph for this function: