1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
#include "completionhelper.h"
#include "expectedtoken.h"
#include "dbsqlite3mock.h"
#include "parser/lexer.h"
#include "parser/token.h"
#include "parser/keywords.h"
#include "sqlitestudio.h"
#include "mocks.h"
#include <QString>
#include <QtTest>
#include <QDebug>
class CompletionHelperTest : public QObject
{
Q_OBJECT
public:
CompletionHelperTest();
private:
QList<ExpectedToken*> getEntryList(QList<ExpectedTokenPtr> tokens);
QSet<ExpectedToken::Type> getTypeList(QList<ExpectedTokenPtr> tokens);
Db* db = nullptr;
bool contains(const QList<ExpectedTokenPtr>& tokens, ExpectedToken::Type type);
bool contains(const QList<ExpectedTokenPtr>& tokens, ExpectedToken::Type type,
const QString& value);
bool contains(const QList<ExpectedTokenPtr>& tokens, ExpectedToken::Type type,
const QString& value, const QString& prefix);
bool contains(const QList<ExpectedTokenPtr> &tokens, ExpectedToken::Type type,
const QString &value, const QString &prefix, const QString &contextInfo);
int find(const QList<ExpectedTokenPtr>& tokens, ExpectedToken::Type type);
int find(const QList<ExpectedTokenPtr>& tokens, ExpectedToken::Type type,
const QString& value);
int find(const QList<ExpectedTokenPtr>& tokens, ExpectedToken::Type type,
const QString& value, const QString& prefix);
int find(const QList<ExpectedTokenPtr> &tokens, ExpectedToken::Type type,
const QString &value, const QString &prefix, const QString &contextInfo);
private Q_SLOTS:
void testResCol1();
void testFrom1();
void testFrom2();
void testResCol2();
void testResCol3();
void testResCol4();
void testResCol5();
void testResCol6();
void testFromKw();
void testUpdateTable();
void testUpdateCols1();
void initTestCase();
void cleanupTestCase();
};
CompletionHelperTest::CompletionHelperTest()
{
}
QList<ExpectedToken *> CompletionHelperTest::getEntryList(QList<ExpectedTokenPtr> tokens)
{
QList<ExpectedToken*> entries;
for (ExpectedTokenPtr expectedToken : tokens)
entries += expectedToken.data();
return entries;
}
QSet<ExpectedToken::Type> CompletionHelperTest::getTypeList(QList<ExpectedTokenPtr> tokens)
{
QSet<ExpectedToken::Type> entries;
for (ExpectedTokenPtr expectedToken : tokens)
entries += expectedToken->type;
return entries;
}
bool CompletionHelperTest::contains(const QList<ExpectedTokenPtr> &tokens, ExpectedToken::Type type)
{
return find(tokens, type) > -1;
}
bool CompletionHelperTest::contains(const QList<ExpectedTokenPtr> &tokens, ExpectedToken::Type type, const QString &value)
{
return find(tokens, type, value) > -1;
}
bool CompletionHelperTest::contains(const QList<ExpectedTokenPtr> &tokens, ExpectedToken::Type type, const QString &value, const QString &prefix)
{
return find(tokens, type, value, prefix) > -1;
}
bool CompletionHelperTest::contains(const QList<ExpectedTokenPtr> &tokens, ExpectedToken::Type type, const QString &value, const QString &prefix, const QString &contextInfo)
{
return find(tokens, type, value, prefix, contextInfo) > -1;
}
int CompletionHelperTest::find(const QList<ExpectedTokenPtr> &tokens, ExpectedToken::Type type)
{
int i = 0;
for (ExpectedTokenPtr token : tokens)
{
if (token->type == type)
return i;
i++;
}
return -1;
}
int CompletionHelperTest::find(const QList<ExpectedTokenPtr> &tokens, ExpectedToken::Type type, const QString &value)
{
int i = -1;
for (ExpectedTokenPtr token : tokens)
{
i++;
if (token->type != type)
continue;
if (token->value == value)
return i;
}
return -1;
}
int CompletionHelperTest::find(const QList<ExpectedTokenPtr> &tokens, ExpectedToken::Type type, const QString &value, const QString &prefix)
{
int i = -1;
for (ExpectedTokenPtr token : tokens)
{
i++;
if (token->type != type)
continue;
if (token->value != value)
continue;
if (token->prefix == prefix)
return i;
}
return -1;
}
int CompletionHelperTest::find(const QList<ExpectedTokenPtr> &tokens, ExpectedToken::Type type, const QString &value, const QString &prefix, const QString &contextInfo)
{
int i = -1;
for (ExpectedTokenPtr token : tokens)
{
i++;
if (token->type != type)
continue;
if (token->value != value)
continue;
if (token->prefix != prefix)
continue;
if (token->contextInfo == contextInfo)
return i;
}
return -1;
}
void CompletionHelperTest::testFrom1()
{
QString sql = "select * FROM ";
CompletionHelper helper(sql, db);
QList<ExpectedTokenPtr> tokens = helper.getExpectedTokens().filtered();
QVERIFY(contains(tokens, ExpectedToken::TABLE, "test"));
QVERIFY(contains(tokens, ExpectedToken::TABLE, "sqlite_master"));
QVERIFY(contains(tokens, ExpectedToken::TABLE, "sqlite_temp_master"));
QVERIFY(contains(tokens, ExpectedToken::DATABASE, "main"));
}
void CompletionHelperTest::testFrom2()
{
QString sql = "select id from abc, ";
CompletionHelper helper(sql, db);
QList<ExpectedTokenPtr> tokens = helper.getExpectedTokens().filtered();
// QList<ExpectedToken*> entries = getEntryList(tokens);
QVERIFY(contains(tokens, ExpectedToken::TABLE));
QVERIFY(contains(tokens, ExpectedToken::DATABASE));
QVERIFY(!contains(tokens, ExpectedToken::FUNCTION));
QVERIFY(!contains(tokens, ExpectedToken::COLUMN));
// Because abc was already used, the order should be:
// test
// sqlite_master
// sqlite_temp_master
// abc
QVERIFY(find(tokens, ExpectedToken::TABLE, "test") == 0);
QVERIFY(find(tokens, ExpectedToken::TABLE, "sqlite_master") == 1);
QVERIFY(find(tokens, ExpectedToken::TABLE, "sqlite_temp_master") == 2);
QVERIFY(find(tokens, ExpectedToken::TABLE, "abc") == 3);
}
void CompletionHelperTest::testResCol1()
{
QString sql = "select main.test.";
CompletionHelper helper(sql, db);
QList<ExpectedTokenPtr> tokens = helper.getExpectedTokens().filtered();
QVERIFY(!contains(tokens, ExpectedToken::COLUMN, "name"));
QVERIFY(contains(tokens, ExpectedToken::COLUMN, "id"));
QVERIFY(contains(tokens, ExpectedToken::COLUMN, "val"));
}
void CompletionHelperTest::testResCol2()
{
QString sql = "select ";
CompletionHelper helper(sql, db);
QList<ExpectedTokenPtr> tokens = helper.getExpectedTokens().filtered();
//QList<ExpectedToken*> entries = getEntryList(tokens);
// Exclude JOIN keywords and FK MATCH keywords
QVERIFY(!contains(tokens, ExpectedToken::KEYWORD, "NATURAL"));
QVERIFY(!contains(tokens, ExpectedToken::KEYWORD, "CROSS"));
QVERIFY(!contains(tokens, ExpectedToken::KEYWORD, "SIMPLE"));
QVERIFY(!contains(tokens, ExpectedToken::KEYWORD, "FULL"));
// Exclude OTHER as there is at least one CTX token available
QVERIFY(!contains(tokens, ExpectedToken::OTHER));
QVERIFY(contains(tokens, ExpectedToken::DATABASE));
QVERIFY(contains(tokens, ExpectedToken::FUNCTION));
QVERIFY(contains(tokens, ExpectedToken::TABLE, "sqlite_master"));
QVERIFY(contains(tokens, ExpectedToken::COLUMN, "name", QString()));
QVERIFY(contains(tokens, ExpectedToken::COLUMN, "id", QString()));
}
void CompletionHelperTest::testResCol3()
{
QString sql = "select from test";
CompletionHelper helper(sql, 7, db);
QList<ExpectedTokenPtr> tokens = helper.getExpectedTokens().filtered();
//QList<ExpectedToken*> entries = getEntryList(tokens);
QVERIFY(contains(tokens, ExpectedToken::TABLE));
QVERIFY(contains(tokens, ExpectedToken::DATABASE));
QVERIFY(contains(tokens, ExpectedToken::FUNCTION));
QVERIFY(contains(tokens, ExpectedToken::COLUMN, "id", QString()));
QVERIFY(contains(tokens, ExpectedToken::COLUMN, "val", QString()));
// Order should be:
// id - test (no prefix value)
// val - test (default, no prefix value)
// val2 - test (default, no prefix value)
// id - abc (no prefix, we didn't mention other table in from clause)
QVERIFY(find(tokens, ExpectedToken::COLUMN, "id", QString(), "test") == 0);
QVERIFY(find(tokens, ExpectedToken::COLUMN, "val", QString(), "test") == 1);
QVERIFY(find(tokens, ExpectedToken::COLUMN, "val2", QString(), "test") == 2);
QVERIFY(find(tokens, ExpectedToken::COLUMN, "id", QString(), "abc") == 3);
}
void CompletionHelperTest::testResCol4()
{
QString sql = "select test.id, from test";
CompletionHelper helper(sql, 15, db);
QList<ExpectedTokenPtr> tokens = helper.getExpectedTokens().filtered();
//QList<ExpectedToken*> entries = getEntryList(tokens);
QVERIFY(contains(tokens, ExpectedToken::TABLE));
QVERIFY(contains(tokens, ExpectedToken::DATABASE));
QVERIFY(contains(tokens, ExpectedToken::FUNCTION));
// Because test.id was already used, the order should be:
// val - test (default, no prefix value)
// val2 - test (default, no prefix value)
// id - test (no prefix, only one table in FROM)
// id - abc (no prefix, only one table in FROM)
QVERIFY(find(tokens, ExpectedToken::COLUMN, "val", QString(), "test") == 0);
QVERIFY(find(tokens, ExpectedToken::COLUMN, "val2", QString(), "test") == 1);
QVERIFY(find(tokens, ExpectedToken::COLUMN, "id", QString(), "test") == 2);
QVERIFY(find(tokens, ExpectedToken::COLUMN, "id", QString(), "abc") == 3);
}
void CompletionHelperTest::testResCol5()
{
QString sql = "select test.id, val from test";
CompletionHelper helper(sql, 15, db);
QList<ExpectedTokenPtr> tokens = helper.getExpectedTokens().filtered();
//QList<ExpectedToken*> entries = getEntryList(tokens);
QVERIFY(contains(tokens, ExpectedToken::TABLE));
QVERIFY(contains(tokens, ExpectedToken::DATABASE));
QVERIFY(contains(tokens, ExpectedToken::FUNCTION));
// Because test.id and val were already used, the order should be:
// val2 - test (default, no prefix value)
// id - test
// val - test (default, no prefix value)
// id - abc (no prefix, only one table in FROM)
QVERIFY(find(tokens, ExpectedToken::COLUMN, "val2") == 0);
QVERIFY(find(tokens, ExpectedToken::COLUMN, "id", QString(), "test") == 1);
QVERIFY(find(tokens, ExpectedToken::COLUMN, "val") == 2);
QVERIFY(find(tokens, ExpectedToken::COLUMN, "id", QString(), "abc") == 3);
}
void CompletionHelperTest::testResCol6()
{
QString sql = "select (select from sqlite_master as S, sqlite_master as S2) from test as A";
CompletionHelper helper(sql, 15, db);
QList<ExpectedTokenPtr> tokens = helper.getExpectedTokens().filtered();
//QList<ExpectedToken*> entries = getEntryList(tokens);
QVERIFY(contains(tokens, ExpectedToken::TABLE));
QVERIFY(contains(tokens, ExpectedToken::DATABASE));
QVERIFY(contains(tokens, ExpectedToken::FUNCTION));
// Desired order:
// S.name - sqlite_master S (can be second or first)
// S.name - sqlite_master S2 (can be second or first)
// ...
// id - test (default, no prefix)
// ...
// id - abc (no prefix, only one table with this column in FROM)
// ...
int s_sqlite_master_name = find(tokens, ExpectedToken::COLUMN, "name", "S", "sqlite_master");
int s2_sqlite_master_name = find(tokens, ExpectedToken::COLUMN, "name", "S2", "sqlite_master");
int test_id = find(tokens, ExpectedToken::COLUMN, "id", "A", "test");
int abc_id = find(tokens, ExpectedToken::COLUMN, "id", QString(), "abc");
QVERIFY(s_sqlite_master_name <= 1);
QVERIFY(s2_sqlite_master_name <= 1);
QVERIFY(test_id > s2_sqlite_master_name);
QVERIFY(abc_id > test_id);
}
void CompletionHelperTest::testFromKw()
{
QString sql = "select * FR";
CompletionHelper helper(sql, db);
QList<ExpectedTokenPtr> tokens = helper.getExpectedTokens().filtered();
QVERIFY(contains(tokens, ExpectedToken::KEYWORD, "FROM"));
QVERIFY(!contains(tokens, ExpectedToken::COLUMN));
QVERIFY(tokens.size() == 1);
}
void CompletionHelperTest::testUpdateTable()
{
QString sql = "update ";
CompletionHelper helper(sql, db);
QList<ExpectedTokenPtr> tokens = helper.getExpectedTokens().filtered();
QVERIFY(contains(tokens, ExpectedToken::TABLE));
}
void CompletionHelperTest::testUpdateCols1()
{
QString sql = "update test set id = 5, ";
CompletionHelper helper(sql, db);
QList<ExpectedTokenPtr> tokens = helper.getExpectedTokens().filtered();
// TODO if table is provided and there is at least one column in proposal for it, then skip columns from other tables.
// TODO Make the context more precise - distinguish between left side column from right side expression
QVERIFY(find(tokens, ExpectedToken::COLUMN, "id", QString(), "test") == 0);
QVERIFY(find(tokens, ExpectedToken::COLUMN, "val", QString(), "test") == 1);
QVERIFY(find(tokens, ExpectedToken::COLUMN, "val2", QString(), "test") == 2);
//QVERIFY(!contains(tokens, ExpectedToken::COLUMN, "id", QString(), "abc")); // TODO
}
void CompletionHelperTest::initTestCase()
{
initKeywords();
Lexer::staticInit();
CompletionHelper::init();
initMocks();
db = new DbSqlite3Mock("testdb");
db->open();
db->exec("CREATE TABLE test (id int, val text, val2 text);");
db->exec("CREATE TABLE abc (id int, xyz text);");
}
void CompletionHelperTest::cleanupTestCase()
{
db->close();
delete db;
db = nullptr;
deleteMockRepo();
}
QTEST_APPLESS_MAIN(CompletionHelperTest)
#include "tst_completionhelpertest.moc"
|