Falkon Develop
Cross-platform Qt-based web browser
sqldatabasetest.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "sqldatabasetest.h"
19#include "sqldatabase.h"
20
21#include <QtTest/QTest>
22#include <QtTest/QSignalSpy>
23#include <QSqlDatabase>
24#include <QTemporaryFile>
25
26void SqlDatabaseTest::initTestCase()
27{
28}
29
30void SqlDatabaseTest::cleanupTestCase()
31{
32}
33
34static bool waitForFinished(SqlQueryJob *job)
35{
36 QSignalSpy spy(job, &SqlQueryJob::finished);
37 return spy.wait();
38}
39
40void SqlDatabaseTest::sqlQueryJobTest()
41{
42 QTemporaryFile file;
43 file.open();
44
45 QSqlDatabase db = QSqlDatabase::addDatabase(QSL("QSQLITE"));
46 db.setDatabaseName(file.fileName());
47 db.open();
48
50
51 QCOMPARE(db.tables().count(), 0);
52
53 auto *job = new SqlQueryJob();
54 job->setQuery(QSL("CREATE TABLE test1 (data TEXT, id INTEGER PRIMARY KEY)"));
55 job->start();
56 QVERIFY(waitForFinished(job));
57 QVERIFY(!job->error().isValid());
58
59 QCOMPARE(db.tables(), QStringList{QSL("test1")});
60
61 job = new SqlQueryJob();
62 job->setQuery(QSL("INSERT INTO test1 (data) VALUES (?)"));
63 job->addBindValue(QSL("test-value"));
64 job->start();
65 QVERIFY(waitForFinished(job));
66 QVERIFY(!job->error().isValid());
67
68 QCOMPARE(job->lastInsertId().toInt(), 1);
69 QSqlQuery query(QSL("SELECT data FROM test1"), db);
70 query.next();
71 QCOMPARE(query.value(0).toString(), QSL("test-value"));
72 QVERIFY(!query.next());
73
74 job = new SqlQueryJob();
75 job->setQuery(QSL("SELECT data FROM test1"));
76 job->start();
77 QVERIFY(waitForFinished(job));
78 QVERIFY(!job->error().isValid());
79
80 QCOMPARE(job->records().size(), 1);
81 QCOMPARE(job->records().at(0).value(0).toString(), QSL("test-value"));
82
83 job = new SqlQueryJob();
84 job->setQuery(QSL("SELECT invalid sql syntax; 1321sdsa from"));
85 job->start();
86 QVERIFY(waitForFinished(job));
87 QVERIFY(job->error().isValid());
88}
89
90QTEST_GUILESS_MAIN(SqlDatabaseTest)
static SqlDatabase * instance()
void setDatabase(const QSqlDatabase &database)
QSqlError error() const
Definition: sqldatabase.cpp:51
QVariant lastInsertId() const
Definition: sqldatabase.cpp:56
void start()
Definition: sqldatabase.cpp:71
void addBindValue(const QVariant &value)
Definition: sqldatabase.cpp:46
void setQuery(const QString &query)
Definition: sqldatabase.cpp:41
QVector< QSqlRecord > records() const
Definition: sqldatabase.cpp:66
void finished(SqlQueryJob *job)
#define QSL(x)
Definition: qzcommon.h:40