123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740 |
- #include <QtTest>
- #include <QMetaObject>
- #include "EasyQtSql.h"
- #include "../Shared/Shared.h"
- using namespace EasyQtSql;
- class TestSelect : public QObject
- {
- Q_OBJECT
- private:
- //====================================================
- // Test data
- const QList<Row> &testData() const
- {
- const static QList<Row> rows =
- { {1, 2, 3, "a"}
- , {4, 5, 6, "b"}
- , {7, 8, 9, "c"}
- };
- return rows;
- }
- //====================================================
- public:
- TestSelect(){}
- ~TestSelect(){}
- private slots:
- void initTestCase();
- void cleanupTestCase();
- void test_case1();
- void test_case2();
- void test_case3();
- void test_case4();
- void test_case5();
- void test_case6();
- void test_case7();
- void test_case8();
- void test_case9();
- void test_case10();
- void test_case11();
- void test_case12();
- void test_case13();
- void test_case14();
- void test_case14_1();
- void test_case15();
- void test_case16();
- void test_case17();
- private:
- int colCount() const
- {
- static Row sampleRow;
- return sampleRow.staticMetaObject.propertyCount();
- }
- int rowCount() const
- {
- return testData().count();
- }
- const char *selectABCDQuery = "SELECT a, b, c, d FROM testTable";
- };
- void TestSelect::initTestCase()
- {
- QLatin1Literal driverName("QSQLITE");
- if (!QSqlDatabase::drivers().contains(driverName))
- QFAIL("This test requires the SQLITE database driver");
- QSqlDatabase sdb = QSqlDatabase::addDatabase(driverName);
- sdb.setDatabaseName(":memory:");
- if (!sdb.open())
- {
- QFAIL(sdb.lastError().text().toStdString().c_str());
- }
- Transaction t(sdb);
- t.execNonQuery("CREATE TABLE testTable (a int, b int, c int, d text)");
- const auto &rows = testData();
- //multi insert
- NonQueryResult res1 = t.insertInto("testTable (a, b, c, d)")
- .values(rows[0].a, rows[0].b, rows[0].c, rows[0].d)
- .values(rows[1].a, rows[1].b, rows[1].c, rows[1].d)
- .values(rows[2].a, rows[2].b, rows[2].c, rows[2].d)
- .exec();
- t.commit();
- }
- void TestSelect::cleanupTestCase()
- {
- {
- QSqlDatabase sdb = QSqlDatabase::database(QSqlDatabase::defaultConnection);
- if (sdb.isOpen())
- {
- sdb.close();
- }
- }
- QSqlDatabase::removeDatabase(QSqlDatabase::defaultConnection);
- }
- void TestSelect::test_case1() //row count
- {
- Transaction t;
- QueryResult res = t.execQuery("SELECT COUNT(*) FROM testTable");
- if (res.next())
- {
- int count = -1;
- res.fetchVars(count);
- QCOMPARE(count, rowCount());
- }
- else
- {
- QFAIL("No query result");
- }
- }
- void TestSelect::test_case2() //col count
- {
- Transaction t;
- QueryResult res = t.execQuery("SELECT * FROM testTable");
- if (res.next())
- {
- QVariantList list;
- res.fetchList(list);
- QCOMPARE(list.count(), colCount());
- }
- else
- {
- QFAIL("No query result");
- }
- }
- void TestSelect::test_case3() //select specified columns
- {
- Transaction t;
- QueryResult res = t.execQuery("SELECT a, c FROM testTable");
- if (res.next())
- {
- QVariantList list;
- res.fetchList(list);
- QCOMPARE(list.count(), 2); //two columns selected (a, c)
- }
- else
- {
- QFAIL("No query result");
- }
- }
- void TestSelect::test_case4() //select rows with condition
- {
- Transaction t;
- QueryResult res = t.execQuery("SELECT COUNT(*) FROM testTable WHERE a = 4 OR b = 8");
- if (res.next())
- {
- int count = -1;
- res.fetchVars(count);
- QCOMPARE(count, 2); //2 rows expected (a = 4 OR b = 8)
- }
- else
- {
- QFAIL("No query result");
- }
- }
- void TestSelect::test_case5() //select rows with condition (prepare stmt)
- {
- Transaction t;
- PreparedQuery query = t.prepare("SELECT COUNT(*) FROM testTable WHERE a = ? OR b = ?");
- QueryResult res = query.exec(4, 8);
- if (res.next())
- {
- int count = -1;
- res.fetchVars(count);
- QCOMPARE(count, 2); //2 rows expected (a = 4 OR b = 8)
- }
- else
- {
- QFAIL("No query result");
- }
- }
- void TestSelect::test_case6() //fetch rows
- {
- Transaction t;
- QueryResult res = t.execQuery(selectABCDQuery);
- const auto &rows = testData();
- int i = 0;
- while (res.next())
- {
- int a, b, c;
- QString d;
- res.fetchVars(a, b, c, d);
- const Row &row = rows[i];
- QCOMPARE(a, row.a);
- QCOMPARE(b, row.b);
- QCOMPARE(c, row.c);
- QCOMPARE(d, row.d);
- i++;
- }
- QCOMPARE(i, rows.count());
- }
- void TestSelect::test_case7() //fetch rows
- {
- Transaction t;
- QueryResult res = t.execQuery(selectABCDQuery);
- const auto &rows = testData();
- int i = 0;
- while (res.next())
- {
- QVariantList list;
- res.fetchList(list);
- const Row &row = rows[i];
- QCOMPARE(list.value(0).toInt() , row.a);
- QCOMPARE(list.value(1).toInt() , row.b);
- QCOMPARE(list.value(2).toInt() , row.c);
- QCOMPARE(list.value(3).toString(), row.d);
- i++;
- }
- QCOMPARE(i, rows.count());
- }
- void TestSelect::test_case8() //fetch rows
- {
- Transaction t;
- QueryResult res = t.execQuery(selectABCDQuery);
- const auto &rows = testData();
- int i = 0;
- while (res.next())
- {
- QVariantMap map;
- res.fetchMap(map);
- const Row &row = rows[i];
- QCOMPARE(map.value("a").toInt() , row.a);
- QCOMPARE(map.value("b").toInt() , row.b);
- QCOMPARE(map.value("c").toInt() , row.c);
- QCOMPARE(map.value("d").toString(), row.d);
- i++;
- }
- QCOMPARE(i, rows.count());
- }
- void TestSelect::test_case9() //fetch rows
- {
- Transaction t;
- QueryResult res = t.execQuery(selectABCDQuery);
- const auto &rows = testData();
- int i = 0;
- while (res.next())
- {
- QStringList list;
- res.fetchStringList(list);
- const Row &row = rows[i];
- QCOMPARE(list.value(0), QString::number(row.a));
- QCOMPARE(list.value(1), QString::number(row.b));
- QCOMPARE(list.value(2), QString::number(row.c));
- QCOMPARE(list.value(3), row.d);
- i++;
- }
- QCOMPARE(i, rows.count());
- }
- void TestSelect::test_case10() //fetch rows
- {
- Transaction t;
- QueryResult res = t.execQuery(selectABCDQuery);
- const auto &rows = testData();
- int i = 0;
- while (res.next())
- {
- QVector<QVariant> vector;
- res.fetchVector(vector);
- const Row &row = rows[i];
- QCOMPARE(vector.value(0).toInt() , row.a);
- QCOMPARE(vector.value(1).toInt() , row.b);
- QCOMPARE(vector.value(2).toInt() , row.c);
- QCOMPARE(vector.value(3).toString(), row.d);
- i++;
- }
- QCOMPARE(i, rows.count());
- }
- void TestSelect::test_case11() //fetch rows
- {
- Transaction t;
- QueryResult res = t.execQuery(selectABCDQuery);
- const auto &rows = testData();
- int i = 0;
- while (res.next())
- {
- TestObject testRow;
- res.fetchObject(testRow);
- const Row &row = rows[i];
- QCOMPARE(testRow.a, row.a);
- QCOMPARE(testRow.b, row.b);
- QCOMPARE(testRow.c, row.c);
- QCOMPARE(testRow.d, row.d);
- i++;
- }
- QCOMPARE(i, rows.count());
- }
- void TestSelect::test_case12() //fetch rows
- {
- Transaction t;
- QueryResult res = t.execQuery(selectABCDQuery);
- const auto &rows = testData();
- int i = 0;
- while (res.next())
- {
- Row testRow;
- res.fetchGadget(testRow);
- const Row &row = rows[i];
- QCOMPARE(testRow.a, row.a);
- QCOMPARE(testRow.b, row.b);
- QCOMPARE(testRow.c, row.c);
- QCOMPARE(testRow.d, row.d);
- i++;
- }
- QCOMPARE(i, rows.count());
- }
- void TestSelect::test_case13() //fetch rows
- {
- Transaction t;
- QueryResult res = t.execQuery(selectABCDQuery);
- const auto &rows = testData();
- int i = 0;
- while (res.next())
- {
- const QVector<int> intVector = res.toVector<int>();
- const Row &row = rows[i];
- const QVector<int> expected = { row.a, row.b, row.c, row.d.toInt() };
- QCOMPARE(intVector, expected);
- i++;
- }
- QCOMPARE(i, rows.count());
- }
- void TestSelect::test_case14() //fetch rows
- {
- Transaction t;
- QueryResult res = t.execQuery(selectABCDQuery);
- const auto &rows = testData();
- int i = 0;
- while (res.next())
- {
- const QVector<int> intVector = res.parseToIntVector();
- const Row &row = rows[i];
- 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)
- qDebug() << intVector << expected;
- QCOMPARE(intVector, expected);
- i++;
- }
- QCOMPARE(i, rows.count());
- }
- void TestSelect::test_case14_1() //fetch rows
- {
- Transaction t;
- QueryResult res = t.execQuery(selectABCDQuery);
- const auto &rows = testData();
- int i = 0;
- while (res.next())
- {
- const QVector<int> intVector = res.parseToIntVector(16);
- const Row &row = rows[i];
- 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")
- qDebug() << intVector << expected;
- QCOMPARE(intVector, expected);
- i++;
- }
- QCOMPARE(i, rows.count());
- }
- void TestSelect::test_case15() //fetch rows
- {
- Transaction t;
- QueryResult res = t.execQuery(selectABCDQuery);
- const auto &rows = testData();
- int i = 0;
- while (res.next())
- {
- const QVector<bool> boolVector = res.toVector<bool>();
- const Row &row = rows[i];
- const QVector<bool> expected = { QVariant(row.a).toBool(), QVariant(row.b).toBool(), QVariant(row.c).toBool(), QVariant(row.d).toBool() };
- QCOMPARE(boolVector, expected);
- i++;
- }
- QCOMPARE(i, rows.count());
- }
- void TestSelect::test_case16() //fetch rows
- {
- Transaction t;
- QueryResult res = t.execQuery(selectABCDQuery);
- const auto &rows = testData();
- int i = 0;
- while (res.next())
- {
- const QVector<QString> strVector = res.toVector<QString>();
- const Row &row = rows[i];
- const QVector<QString> expected = { QVariant(row.a).toString(), QVariant(row.b).toString(), QVariant(row.c).toString(), row.d };
- QCOMPARE(strVector, expected);
- i++;
- }
- QCOMPARE(i, rows.count());
- }
- void TestSelect::test_case17() //fetch rows
- {
- Transaction t;
- const auto &rows = testData();
- {
- int curRow = 0;
- QVector<Row> actualRows;
- const int count = t.each(selectABCDQuery, [&curRow, &rows, &actualRows](const QueryResult &row)
- {
- const auto &rowData = rows.at(curRow);
- Row r;
- r.a = row.value(0).toInt();
- r.b = row.value(1).toInt();
- r.c = row.value(2).toInt();
- r.d = row.value(3).toString();
- actualRows.append(r);
- QCOMPARE(r.a, rowData.a);
- QCOMPARE(r.b, rowData.b);
- QCOMPARE(r.c, rowData.c);
- QCOMPARE(r.d, rowData.d);
- ++curRow;
- });
- QCOMPARE(count, rowCount());
- QCOMPARE(actualRows.count(), rowCount());
- }
- {
- const int count = t.first(selectABCDQuery, [&rows](const QueryResult &row)
- {
- const auto &rowData = rows.at(0);
- QCOMPARE(row.value(0).toInt(), rowData.a);
- QCOMPARE(row.value(1).toInt(), rowData.b);
- QCOMPARE(row.value(2).toInt(), rowData.c);
- QCOMPARE(row.value(3).toString(), rowData.d);
- });
- QCOMPARE(count, 1);
- }
- {
- int curRow = 0;
- const int topCount = 2;
- QVector<Row> actualRows;
- const int count = t.top(selectABCDQuery, topCount, [&curRow, &rows, &actualRows](const QueryResult &row)
- {
- const auto &rowData = rows.at(curRow);
- Row r;
- r.a = row.value(0).toInt();
- r.b = row.value(1).toInt();
- r.c = row.value(2).toInt();
- r.d = row.value(3).toString();
- actualRows.append(r);
- QCOMPARE(r.a, rowData.a);
- QCOMPARE(r.b, rowData.b);
- QCOMPARE(r.c, rowData.c);
- QCOMPARE(r.d, rowData.d);
- ++curRow;
- });
- QCOMPARE(count, topCount);
- QCOMPARE(actualRows.count(), topCount);
- }
- {
- int curRow = 1;
- const int resCount = 1;
- QVector<Row> actualRows;
- const int count = t.range(selectABCDQuery, curRow, resCount, [&curRow, &rows, &actualRows](const QueryResult &row)
- {
- const auto &rowData = rows.at(curRow);
- qDebug() << row.toMap() << curRow;
- Row r;
- r.a = row.value(0).toInt();
- r.b = row.value(1).toInt();
- r.c = row.value(2).toInt();
- r.d = row.value(3).toString();
- actualRows.append(r);
- QCOMPARE(r.a, rowData.a);
- QCOMPARE(r.b, rowData.b);
- QCOMPARE(r.c, rowData.c);
- QCOMPARE(r.d, rowData.d);
- ++curRow;
- });
- QCOMPARE(count, resCount);
- QCOMPARE(actualRows.count(), resCount);
- }
- {
- int curRow = 0;
- const int resCount = 3;
- QVector<Row> actualRows;
- const int count = t.range(selectABCDQuery, curRow, resCount, [&curRow, &rows, &actualRows](const QueryResult &row)
- {
- const auto &rowData = rows.at(curRow);
- qDebug() << row.toMap() << curRow;
- Row r;
- r.a = row.value(0).toInt();
- r.b = row.value(1).toInt();
- r.c = row.value(2).toInt();
- r.d = row.value(3).toString();
- actualRows.append(r);
- QCOMPARE(r.a, rowData.a);
- QCOMPARE(r.b, rowData.b);
- QCOMPARE(r.c, rowData.c);
- QCOMPARE(r.d, rowData.d);
- ++curRow;
- });
- QCOMPARE(count, resCount);
- QCOMPARE(actualRows.count(), resCount);
- }
- {
- int curRow = 0;
- const int resCount = 10;
- QVector<Row> actualRows;
- const int count = t.range(selectABCDQuery, curRow, resCount, [&curRow, &rows, &actualRows](const QueryResult &row)
- {
- const auto &rowData = rows.at(curRow);
- qDebug() << row.toMap() << curRow;
- Row r;
- r.a = row.value(0).toInt();
- r.b = row.value(1).toInt();
- r.c = row.value(2).toInt();
- r.d = row.value(3).toString();
- actualRows.append(r);
- QCOMPARE(r.a, rowData.a);
- QCOMPARE(r.b, rowData.b);
- QCOMPARE(r.c, rowData.c);
- QCOMPARE(r.d, rowData.d);
- ++curRow;
- });
- QCOMPARE(count, rowCount());
- QCOMPARE(actualRows.count(), rowCount());
- }
- {
- const int count = t.range(selectABCDQuery, 3, 10, [](const QueryResult &row)
- {
- Q_UNUSED(row);
- QFAIL("UNSEXPECTED");
- });
- QCOMPARE(count, 0);
- }
- {
- const int count = t.range(selectABCDQuery, 10, 10, [](const QueryResult &row)
- {
- Q_UNUSED(row);
- QFAIL("UNSEXPECTED");
- });
- QCOMPARE(count, 0);
- }
- }
- QTEST_APPLESS_MAIN(TestSelect)
- #include "tst_testselect.moc"
|