1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-14 02:52:12 +01:00
falkonOfficial/autotests/sqldatabasetest.cpp

91 lines
2.7 KiB
C++
Raw Normal View History

/* ============================================================
* Falkon - Qt web browser
* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "sqldatabasetest.h"
#include "sqldatabase.h"
#include <QtTest/QTest>
#include <QtTest/QSignalSpy>
#include <QSqlDatabase>
#include <QTemporaryFile>
void SqlDatabaseTest::initTestCase()
{
}
void SqlDatabaseTest::cleanupTestCase()
{
}
static bool waitForFinished(SqlQueryJob *job)
{
QSignalSpy spy(job, &SqlQueryJob::finished);
return spy.wait();
}
void SqlDatabaseTest::sqlQueryJobTest()
{
QTemporaryFile file;
file.open();
2018-07-06 08:20:47 +02:00
QSqlDatabase db = QSqlDatabase::addDatabase(QSL("QSQLITE"));
db.setDatabaseName(file.fileName());
db.open();
SqlDatabase::instance()->setDatabase(db);
QCOMPARE(db.tables().count(), 0);
2022-06-19 09:24:25 +02:00
auto *job = new SqlQueryJob();
2018-07-06 08:20:47 +02:00
job->setQuery(QSL("CREATE TABLE test1 (data TEXT, id INTEGER PRIMARY KEY)"));
job->start();
QVERIFY(waitForFinished(job));
QVERIFY(!job->error().isValid());
2018-07-06 08:20:47 +02:00
QCOMPARE(db.tables(), QStringList{QSL("test1")});
job = new SqlQueryJob();
2018-07-06 08:20:47 +02:00
job->setQuery(QSL("INSERT INTO test1 (data) VALUES (?)"));
job->addBindValue(QSL("test-value"));
job->start();
QVERIFY(waitForFinished(job));
QVERIFY(!job->error().isValid());
QCOMPARE(job->lastInsertId().toInt(), 1);
2018-07-06 08:20:47 +02:00
QSqlQuery query(QSL("SELECT data FROM test1"), db);
query.next();
2018-07-06 08:20:47 +02:00
QCOMPARE(query.value(0).toString(), QSL("test-value"));
QVERIFY(!query.next());
job = new SqlQueryJob();
2018-07-06 08:20:47 +02:00
job->setQuery(QSL("SELECT data FROM test1"));
job->start();
QVERIFY(waitForFinished(job));
QVERIFY(!job->error().isValid());
QCOMPARE(job->records().size(), 1);
2018-07-06 08:20:47 +02:00
QCOMPARE(job->records().at(0).value(0).toString(), QSL("test-value"));
job = new SqlQueryJob();
2018-07-06 08:20:47 +02:00
job->setQuery(QSL("SELECT invalid sql syntax; 1321sdsa from"));
job->start();
QVERIFY(waitForFinished(job));
QVERIFY(job->error().isValid());
}
QTEST_GUILESS_MAIN(SqlDatabaseTest)