tst_testselect.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. #include <QtTest>
  2. #include <QMetaObject>
  3. #include "EasyQtSql.h"
  4. #include "../Shared/Shared.h"
  5. using namespace EasyQtSql;
  6. class TestSelect : public QObject
  7. {
  8. Q_OBJECT
  9. private:
  10. //====================================================
  11. // Test data
  12. const QList<Row> &testData() const
  13. {
  14. const static QList<Row> rows =
  15. { {1, 2, 3, "a"}
  16. , {4, 5, 6, "b"}
  17. , {7, 8, 9, "c"}
  18. };
  19. return rows;
  20. }
  21. //====================================================
  22. public:
  23. TestSelect(){}
  24. ~TestSelect(){}
  25. private slots:
  26. void initTestCase();
  27. void cleanupTestCase();
  28. void test_case1();
  29. void test_case2();
  30. void test_case3();
  31. void test_case4();
  32. void test_case5();
  33. void test_case6();
  34. void test_case7();
  35. void test_case8();
  36. void test_case9();
  37. void test_case10();
  38. void test_case11();
  39. void test_case12();
  40. void test_case13();
  41. void test_case14();
  42. void test_case14_1();
  43. void test_case15();
  44. void test_case16();
  45. void test_case17();
  46. private:
  47. int colCount() const
  48. {
  49. static Row sampleRow;
  50. return sampleRow.staticMetaObject.propertyCount();
  51. }
  52. int rowCount() const
  53. {
  54. return testData().count();
  55. }
  56. const char *selectABCDQuery = "SELECT a, b, c, d FROM testTable";
  57. };
  58. void TestSelect::initTestCase()
  59. {
  60. QLatin1Literal driverName("QSQLITE");
  61. if (!QSqlDatabase::drivers().contains(driverName))
  62. QFAIL("This test requires the SQLITE database driver");
  63. QSqlDatabase sdb = QSqlDatabase::addDatabase(driverName);
  64. sdb.setDatabaseName(":memory:");
  65. if (!sdb.open())
  66. {
  67. QFAIL(sdb.lastError().text().toStdString().c_str());
  68. }
  69. Transaction t(sdb);
  70. t.execNonQuery("CREATE TABLE testTable (a int, b int, c int, d text)");
  71. const auto &rows = testData();
  72. //multi insert
  73. NonQueryResult res1 = t.insertInto("testTable (a, b, c, d)")
  74. .values(rows[0].a, rows[0].b, rows[0].c, rows[0].d)
  75. .values(rows[1].a, rows[1].b, rows[1].c, rows[1].d)
  76. .values(rows[2].a, rows[2].b, rows[2].c, rows[2].d)
  77. .exec();
  78. t.commit();
  79. }
  80. void TestSelect::cleanupTestCase()
  81. {
  82. {
  83. QSqlDatabase sdb = QSqlDatabase::database(QSqlDatabase::defaultConnection);
  84. if (sdb.isOpen())
  85. {
  86. sdb.close();
  87. }
  88. }
  89. QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
  90. }
  91. void TestSelect::test_case1() //row count
  92. {
  93. Transaction t;
  94. QueryResult res = t.execQuery("SELECT COUNT(*) FROM testTable");
  95. if (res.next())
  96. {
  97. int count = -1;
  98. res.fetchVars(count);
  99. QCOMPARE(count, rowCount());
  100. }
  101. else
  102. {
  103. QFAIL("No query result");
  104. }
  105. }
  106. void TestSelect::test_case2() //col count
  107. {
  108. Transaction t;
  109. QueryResult res = t.execQuery("SELECT * FROM testTable");
  110. if (res.next())
  111. {
  112. QVariantList list;
  113. res.fetchList(list);
  114. QCOMPARE(list.count(), colCount());
  115. }
  116. else
  117. {
  118. QFAIL("No query result");
  119. }
  120. }
  121. void TestSelect::test_case3() //select specified columns
  122. {
  123. Transaction t;
  124. QueryResult res = t.execQuery("SELECT a, c FROM testTable");
  125. if (res.next())
  126. {
  127. QVariantList list;
  128. res.fetchList(list);
  129. QCOMPARE(list.count(), 2); //two columns selected (a, c)
  130. }
  131. else
  132. {
  133. QFAIL("No query result");
  134. }
  135. }
  136. void TestSelect::test_case4() //select rows with condition
  137. {
  138. Transaction t;
  139. QueryResult res = t.execQuery("SELECT COUNT(*) FROM testTable WHERE a = 4 OR b = 8");
  140. if (res.next())
  141. {
  142. int count = -1;
  143. res.fetchVars(count);
  144. QCOMPARE(count, 2); //2 rows expected (a = 4 OR b = 8)
  145. }
  146. else
  147. {
  148. QFAIL("No query result");
  149. }
  150. }
  151. void TestSelect::test_case5() //select rows with condition (prepare stmt)
  152. {
  153. Transaction t;
  154. PreparedQuery query = t.prepare("SELECT COUNT(*) FROM testTable WHERE a = ? OR b = ?");
  155. QueryResult res = query.exec(4, 8);
  156. if (res.next())
  157. {
  158. int count = -1;
  159. res.fetchVars(count);
  160. QCOMPARE(count, 2); //2 rows expected (a = 4 OR b = 8)
  161. }
  162. else
  163. {
  164. QFAIL("No query result");
  165. }
  166. }
  167. void TestSelect::test_case6() //fetch rows
  168. {
  169. Transaction t;
  170. QueryResult res = t.execQuery(selectABCDQuery);
  171. const auto &rows = testData();
  172. int i = 0;
  173. while (res.next())
  174. {
  175. int a, b, c;
  176. QString d;
  177. res.fetchVars(a, b, c, d);
  178. const Row &row = rows[i];
  179. QCOMPARE(a, row.a);
  180. QCOMPARE(b, row.b);
  181. QCOMPARE(c, row.c);
  182. QCOMPARE(d, row.d);
  183. i++;
  184. }
  185. QCOMPARE(i, rows.count());
  186. }
  187. void TestSelect::test_case7() //fetch rows
  188. {
  189. Transaction t;
  190. QueryResult res = t.execQuery(selectABCDQuery);
  191. const auto &rows = testData();
  192. int i = 0;
  193. while (res.next())
  194. {
  195. QVariantList list;
  196. res.fetchList(list);
  197. const Row &row = rows[i];
  198. QCOMPARE(list.value(0).toInt() , row.a);
  199. QCOMPARE(list.value(1).toInt() , row.b);
  200. QCOMPARE(list.value(2).toInt() , row.c);
  201. QCOMPARE(list.value(3).toString(), row.d);
  202. i++;
  203. }
  204. QCOMPARE(i, rows.count());
  205. }
  206. void TestSelect::test_case8() //fetch rows
  207. {
  208. Transaction t;
  209. QueryResult res = t.execQuery(selectABCDQuery);
  210. const auto &rows = testData();
  211. int i = 0;
  212. while (res.next())
  213. {
  214. QVariantMap map;
  215. res.fetchMap(map);
  216. const Row &row = rows[i];
  217. QCOMPARE(map.value("a").toInt() , row.a);
  218. QCOMPARE(map.value("b").toInt() , row.b);
  219. QCOMPARE(map.value("c").toInt() , row.c);
  220. QCOMPARE(map.value("d").toString(), row.d);
  221. i++;
  222. }
  223. QCOMPARE(i, rows.count());
  224. }
  225. void TestSelect::test_case9() //fetch rows
  226. {
  227. Transaction t;
  228. QueryResult res = t.execQuery(selectABCDQuery);
  229. const auto &rows = testData();
  230. int i = 0;
  231. while (res.next())
  232. {
  233. QStringList list;
  234. res.fetchStringList(list);
  235. const Row &row = rows[i];
  236. QCOMPARE(list.value(0), QString::number(row.a));
  237. QCOMPARE(list.value(1), QString::number(row.b));
  238. QCOMPARE(list.value(2), QString::number(row.c));
  239. QCOMPARE(list.value(3), row.d);
  240. i++;
  241. }
  242. QCOMPARE(i, rows.count());
  243. }
  244. void TestSelect::test_case10() //fetch rows
  245. {
  246. Transaction t;
  247. QueryResult res = t.execQuery(selectABCDQuery);
  248. const auto &rows = testData();
  249. int i = 0;
  250. while (res.next())
  251. {
  252. QVector<QVariant> vector;
  253. res.fetchVector(vector);
  254. const Row &row = rows[i];
  255. QCOMPARE(vector.value(0).toInt() , row.a);
  256. QCOMPARE(vector.value(1).toInt() , row.b);
  257. QCOMPARE(vector.value(2).toInt() , row.c);
  258. QCOMPARE(vector.value(3).toString(), row.d);
  259. i++;
  260. }
  261. QCOMPARE(i, rows.count());
  262. }
  263. void TestSelect::test_case11() //fetch rows
  264. {
  265. Transaction t;
  266. QueryResult res = t.execQuery(selectABCDQuery);
  267. const auto &rows = testData();
  268. int i = 0;
  269. while (res.next())
  270. {
  271. TestObject testRow;
  272. res.fetchObject(testRow);
  273. const Row &row = rows[i];
  274. QCOMPARE(testRow.a, row.a);
  275. QCOMPARE(testRow.b, row.b);
  276. QCOMPARE(testRow.c, row.c);
  277. QCOMPARE(testRow.d, row.d);
  278. i++;
  279. }
  280. QCOMPARE(i, rows.count());
  281. }
  282. void TestSelect::test_case12() //fetch rows
  283. {
  284. Transaction t;
  285. QueryResult res = t.execQuery(selectABCDQuery);
  286. const auto &rows = testData();
  287. int i = 0;
  288. while (res.next())
  289. {
  290. Row testRow;
  291. res.fetchGadget(testRow);
  292. const Row &row = rows[i];
  293. QCOMPARE(testRow.a, row.a);
  294. QCOMPARE(testRow.b, row.b);
  295. QCOMPARE(testRow.c, row.c);
  296. QCOMPARE(testRow.d, row.d);
  297. i++;
  298. }
  299. QCOMPARE(i, rows.count());
  300. }
  301. void TestSelect::test_case13() //fetch rows
  302. {
  303. Transaction t;
  304. QueryResult res = t.execQuery(selectABCDQuery);
  305. const auto &rows = testData();
  306. int i = 0;
  307. while (res.next())
  308. {
  309. const QVector<int> intVector = res.toVector<int>();
  310. const Row &row = rows[i];
  311. const QVector<int> expected = { row.a, row.b, row.c, row.d.toInt() };
  312. QCOMPARE(intVector, expected);
  313. i++;
  314. }
  315. QCOMPARE(i, rows.count());
  316. }
  317. void TestSelect::test_case14() //fetch rows
  318. {
  319. Transaction t;
  320. QueryResult res = t.execQuery(selectABCDQuery);
  321. const auto &rows = testData();
  322. int i = 0;
  323. while (res.next())
  324. {
  325. const QVector<int> intVector = res.parseToIntVector();
  326. const Row &row = rows[i];
  327. const QVector<int> expected = { row.a, row.b, row.c }; //string value d is not parseable with base 10 in this case (0xA, 0xB, 0xC)
  328. qDebug() << intVector << expected;
  329. QCOMPARE(intVector, expected);
  330. i++;
  331. }
  332. QCOMPARE(i, rows.count());
  333. }
  334. void TestSelect::test_case14_1() //fetch rows
  335. {
  336. Transaction t;
  337. QueryResult res = t.execQuery(selectABCDQuery);
  338. const auto &rows = testData();
  339. int i = 0;
  340. while (res.next())
  341. {
  342. const QVector<int> intVector = res.parseToIntVector(16);
  343. const Row &row = rows[i];
  344. const QVector<int> expected = { row.a, row.b, row.c, row.d.toInt(Q_NULLPTR, 16) }; //string value d is parsible with base 16 ("a", "b", "c")
  345. qDebug() << intVector << expected;
  346. QCOMPARE(intVector, expected);
  347. i++;
  348. }
  349. QCOMPARE(i, rows.count());
  350. }
  351. void TestSelect::test_case15() //fetch rows
  352. {
  353. Transaction t;
  354. QueryResult res = t.execQuery(selectABCDQuery);
  355. const auto &rows = testData();
  356. int i = 0;
  357. while (res.next())
  358. {
  359. const QVector<bool> boolVector = res.toVector<bool>();
  360. const Row &row = rows[i];
  361. const QVector<bool> expected = { QVariant(row.a).toBool(), QVariant(row.b).toBool(), QVariant(row.c).toBool(), QVariant(row.d).toBool() };
  362. QCOMPARE(boolVector, expected);
  363. i++;
  364. }
  365. QCOMPARE(i, rows.count());
  366. }
  367. void TestSelect::test_case16() //fetch rows
  368. {
  369. Transaction t;
  370. QueryResult res = t.execQuery(selectABCDQuery);
  371. const auto &rows = testData();
  372. int i = 0;
  373. while (res.next())
  374. {
  375. const QVector<QString> strVector = res.toVector<QString>();
  376. const Row &row = rows[i];
  377. const QVector<QString> expected = { QVariant(row.a).toString(), QVariant(row.b).toString(), QVariant(row.c).toString(), row.d };
  378. QCOMPARE(strVector, expected);
  379. i++;
  380. }
  381. QCOMPARE(i, rows.count());
  382. }
  383. void TestSelect::test_case17() //fetch rows
  384. {
  385. Transaction t;
  386. const auto &rows = testData();
  387. {
  388. int curRow = 0;
  389. QVector<Row> actualRows;
  390. const int count = t.each(selectABCDQuery, [&curRow, &rows, &actualRows](const QueryResult &row)
  391. {
  392. const auto &rowData = rows.at(curRow);
  393. Row r;
  394. r.a = row.value(0).toInt();
  395. r.b = row.value(1).toInt();
  396. r.c = row.value(2).toInt();
  397. r.d = row.value(3).toString();
  398. actualRows.append(r);
  399. QCOMPARE(r.a, rowData.a);
  400. QCOMPARE(r.b, rowData.b);
  401. QCOMPARE(r.c, rowData.c);
  402. QCOMPARE(r.d, rowData.d);
  403. ++curRow;
  404. });
  405. QCOMPARE(count, rowCount());
  406. QCOMPARE(actualRows.count(), rowCount());
  407. }
  408. {
  409. const int count = t.first(selectABCDQuery, [&rows](const QueryResult &row)
  410. {
  411. const auto &rowData = rows.at(0);
  412. QCOMPARE(row.value(0).toInt(), rowData.a);
  413. QCOMPARE(row.value(1).toInt(), rowData.b);
  414. QCOMPARE(row.value(2).toInt(), rowData.c);
  415. QCOMPARE(row.value(3).toString(), rowData.d);
  416. });
  417. QCOMPARE(count, 1);
  418. }
  419. {
  420. int curRow = 0;
  421. const int topCount = 2;
  422. QVector<Row> actualRows;
  423. const int count = t.top(selectABCDQuery, topCount, [&curRow, &rows, &actualRows](const QueryResult &row)
  424. {
  425. const auto &rowData = rows.at(curRow);
  426. Row r;
  427. r.a = row.value(0).toInt();
  428. r.b = row.value(1).toInt();
  429. r.c = row.value(2).toInt();
  430. r.d = row.value(3).toString();
  431. actualRows.append(r);
  432. QCOMPARE(r.a, rowData.a);
  433. QCOMPARE(r.b, rowData.b);
  434. QCOMPARE(r.c, rowData.c);
  435. QCOMPARE(r.d, rowData.d);
  436. ++curRow;
  437. });
  438. QCOMPARE(count, topCount);
  439. QCOMPARE(actualRows.count(), topCount);
  440. }
  441. {
  442. int curRow = 1;
  443. const int resCount = 1;
  444. QVector<Row> actualRows;
  445. const int count = t.range(selectABCDQuery, curRow, resCount, [&curRow, &rows, &actualRows](const QueryResult &row)
  446. {
  447. const auto &rowData = rows.at(curRow);
  448. qDebug() << row.toMap() << curRow;
  449. Row r;
  450. r.a = row.value(0).toInt();
  451. r.b = row.value(1).toInt();
  452. r.c = row.value(2).toInt();
  453. r.d = row.value(3).toString();
  454. actualRows.append(r);
  455. QCOMPARE(r.a, rowData.a);
  456. QCOMPARE(r.b, rowData.b);
  457. QCOMPARE(r.c, rowData.c);
  458. QCOMPARE(r.d, rowData.d);
  459. ++curRow;
  460. });
  461. QCOMPARE(count, resCount);
  462. QCOMPARE(actualRows.count(), resCount);
  463. }
  464. {
  465. int curRow = 0;
  466. const int resCount = 3;
  467. QVector<Row> actualRows;
  468. const int count = t.range(selectABCDQuery, curRow, resCount, [&curRow, &rows, &actualRows](const QueryResult &row)
  469. {
  470. const auto &rowData = rows.at(curRow);
  471. qDebug() << row.toMap() << curRow;
  472. Row r;
  473. r.a = row.value(0).toInt();
  474. r.b = row.value(1).toInt();
  475. r.c = row.value(2).toInt();
  476. r.d = row.value(3).toString();
  477. actualRows.append(r);
  478. QCOMPARE(r.a, rowData.a);
  479. QCOMPARE(r.b, rowData.b);
  480. QCOMPARE(r.c, rowData.c);
  481. QCOMPARE(r.d, rowData.d);
  482. ++curRow;
  483. });
  484. QCOMPARE(count, resCount);
  485. QCOMPARE(actualRows.count(), resCount);
  486. }
  487. {
  488. int curRow = 0;
  489. const int resCount = 10;
  490. QVector<Row> actualRows;
  491. const int count = t.range(selectABCDQuery, curRow, resCount, [&curRow, &rows, &actualRows](const QueryResult &row)
  492. {
  493. const auto &rowData = rows.at(curRow);
  494. qDebug() << row.toMap() << curRow;
  495. Row r;
  496. r.a = row.value(0).toInt();
  497. r.b = row.value(1).toInt();
  498. r.c = row.value(2).toInt();
  499. r.d = row.value(3).toString();
  500. actualRows.append(r);
  501. QCOMPARE(r.a, rowData.a);
  502. QCOMPARE(r.b, rowData.b);
  503. QCOMPARE(r.c, rowData.c);
  504. QCOMPARE(r.d, rowData.d);
  505. ++curRow;
  506. });
  507. QCOMPARE(count, rowCount());
  508. QCOMPARE(actualRows.count(), rowCount());
  509. }
  510. {
  511. const int count = t.range(selectABCDQuery, 3, 10, [](const QueryResult &row)
  512. {
  513. Q_UNUSED(row);
  514. QFAIL("UNSEXPECTED");
  515. });
  516. QCOMPARE(count, 0);
  517. }
  518. {
  519. const int count = t.range(selectABCDQuery, 10, 10, [](const QueryResult &row)
  520. {
  521. Q_UNUSED(row);
  522. QFAIL("UNSEXPECTED");
  523. });
  524. QCOMPARE(count, 0);
  525. }
  526. }
  527. QTEST_APPLESS_MAIN(TestSelect)
  528. #include "tst_testselect.moc"