File indexing completed on 2024-05-05 13:20:09
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2018 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "sqldatabasetest.h" 0019 #include "sqldatabase.h" 0020 0021 #include <QtTest/QTest> 0022 #include <QtTest/QSignalSpy> 0023 #include <QSqlDatabase> 0024 #include <QTemporaryFile> 0025 0026 void SqlDatabaseTest::initTestCase() 0027 { 0028 } 0029 0030 void SqlDatabaseTest::cleanupTestCase() 0031 { 0032 } 0033 0034 static bool waitForFinished(SqlQueryJob *job) 0035 { 0036 QSignalSpy spy(job, &SqlQueryJob::finished); 0037 return spy.wait(); 0038 } 0039 0040 void SqlDatabaseTest::sqlQueryJobTest() 0041 { 0042 QTemporaryFile file; 0043 file.open(); 0044 0045 QSqlDatabase db = QSqlDatabase::addDatabase(QSL("QSQLITE")); 0046 db.setDatabaseName(file.fileName()); 0047 db.open(); 0048 0049 SqlDatabase::instance()->setDatabase(db); 0050 0051 QCOMPARE(db.tables().count(), 0); 0052 0053 auto *job = new SqlQueryJob(); 0054 job->setQuery(QSL("CREATE TABLE test1 (data TEXT, id INTEGER PRIMARY KEY)")); 0055 job->start(); 0056 QVERIFY(waitForFinished(job)); 0057 QVERIFY(!job->error().isValid()); 0058 0059 QCOMPARE(db.tables(), QStringList{QSL("test1")}); 0060 0061 job = new SqlQueryJob(); 0062 job->setQuery(QSL("INSERT INTO test1 (data) VALUES (?)")); 0063 job->addBindValue(QSL("test-value")); 0064 job->start(); 0065 QVERIFY(waitForFinished(job)); 0066 QVERIFY(!job->error().isValid()); 0067 0068 QCOMPARE(job->lastInsertId().toInt(), 1); 0069 QSqlQuery query(QSL("SELECT data FROM test1"), db); 0070 query.next(); 0071 QCOMPARE(query.value(0).toString(), QSL("test-value")); 0072 QVERIFY(!query.next()); 0073 0074 job = new SqlQueryJob(); 0075 job->setQuery(QSL("SELECT data FROM test1")); 0076 job->start(); 0077 QVERIFY(waitForFinished(job)); 0078 QVERIFY(!job->error().isValid()); 0079 0080 QCOMPARE(job->records().size(), 1); 0081 QCOMPARE(job->records().at(0).value(0).toString(), QSL("test-value")); 0082 0083 job = new SqlQueryJob(); 0084 job->setQuery(QSL("SELECT invalid sql syntax; 1321sdsa from")); 0085 job->start(); 0086 QVERIFY(waitForFinished(job)); 0087 QVERIFY(job->error().isValid()); 0088 } 0089 0090 QTEST_GUILESS_MAIN(SqlDatabaseTest)