summaryrefslogtreecommitdiffstats
path: root/tests/auto/sql/models/qsqltablemodel
diff options
context:
space:
mode:
authorAndy Shaw <andy.shaw@qt.io>2018-08-17 12:58:01 +0200
committerChristian Ehrlicher <ch.ehrlicher@gmx.de>2018-08-28 16:10:30 +0000
commit28702cb239b0dbc915ee3511768ff8e366e35e88 (patch)
treeac2dee111a44e00d19318cc4fd4344ee0cf91c82 /tests/auto/sql/models/qsqltablemodel
parentfbb4befa33196e04bc25e62bbf43f0d3a1d0846b (diff)
Initialize the QSqlQuery to be invalid when creating a sql model
When QSqlQueryModel or QSqlTableModel is created it will create a QSqlQuery which defaults to using the default QSqlDatabase connection. If this connection belongs to another thread then it will throw a warning as this is not safe to use. Since the QSqlQuery is always recreated when a query is set, the instance which is a member of the class can effectively be invalid until a new one is set. Task-number: QTBUG-69213 Change-Id: I68a5dd59fe62788f531d59a0680da11b118ee383 Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'tests/auto/sql/models/qsqltablemodel')
-rw-r--r--tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp
index 430fa981d5..da31f437d9 100644
--- a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp
+++ b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp
@@ -31,11 +31,29 @@
#include "../../kernel/qsqldatabase/tst_databases.h"
#include <QtSql>
#include <QtSql/private/qsqltablemodel_p.h>
+#include <QThread>
const QString test(qTableName("test", __FILE__, QSqlDatabase())),
test2(qTableName("test2", __FILE__, QSqlDatabase())),
test3(qTableName("test3", __FILE__, QSqlDatabase()));
+// In order to catch when the warning message occurs, indicating that the database belongs to another
+// thread, we have to install our own message handler. To ensure that the test reporting still happens
+// as before, we call the originating one.
+//
+// For now, this is only called inside the modelInAnotherThread() test
+QtMessageHandler oldHandler = nullptr;
+
+void sqlTableModelMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
+{
+ if (type == QtWarningMsg &&
+ msg == "QSqlDatabasePrivate::database: requested database does not "
+ "belong to the calling thread.") {
+ QFAIL("Requested database does not belong to the calling thread.");
+ }
+ if (oldHandler)
+ oldHandler(type, context, msg);
+}
class tst_QSqlTableModel : public QObject
{
@@ -116,6 +134,7 @@ private slots:
void sqlite_bigTable_data() { generic_data("QSQLITE"); }
void sqlite_bigTable();
+ void modelInAnotherThread();
// bug specific tests
void insertRecordBeforeSelect_data() { generic_data(); }
@@ -276,6 +295,10 @@ void tst_QSqlTableModel::init()
void tst_QSqlTableModel::cleanup()
{
recreateTestTables();
+ if (oldHandler) {
+ qInstallMessageHandler(oldHandler);
+ oldHandler = nullptr;
+ }
}
void tst_QSqlTableModel::select()
@@ -2100,5 +2123,29 @@ void tst_QSqlTableModel::invalidFilterAndHeaderData()
QVERIFY(!v.isValid());
}
+class SqlThread : public QThread
+{
+public:
+ SqlThread() : QThread() {}
+ void run()
+ {
+ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "non-default-connection");
+ QSqlTableModel stm(nullptr, db);
+ isDone = true;
+ }
+ bool isDone = false;
+};
+
+void tst_QSqlTableModel::modelInAnotherThread()
+{
+ oldHandler = qInstallMessageHandler(sqlTableModelMessageHandler);
+ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
+ CHECK_DATABASE(db);
+ SqlThread t;
+ t.start();
+ QTRY_VERIFY(t.isDone);
+ QVERIFY(t.isFinished());
+}
+
QTEST_MAIN(tst_QSqlTableModel)
#include "tst_qsqltablemodel.moc"