util/tests/auto/qsqlquerymodel/tst_qsqlquerymodel.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include <QtGui>
       
    45 
       
    46 #include <qsqldriver.h>
       
    47 #include <qsqldatabase.h>
       
    48 #include <qsqlerror.h>
       
    49 #include <qsqlfield.h>
       
    50 #include <qsqlquery.h>
       
    51 #include <qsqlrecord.h>
       
    52 
       
    53 #include <qsqlquerymodel.h>
       
    54 #include <qsortfilterproxymodel.h>
       
    55 
       
    56 #include "../qsqldatabase/tst_databases.h"
       
    57 
       
    58 //TESTED_CLASS=
       
    59 //TESTED_FILES=
       
    60 
       
    61 Q_DECLARE_METATYPE(Qt::Orientation)
       
    62 
       
    63 class tst_QSqlQueryModel : public QObject
       
    64 {
       
    65     Q_OBJECT
       
    66 
       
    67 public:
       
    68     tst_QSqlQueryModel();
       
    69     virtual ~tst_QSqlQueryModel();
       
    70 
       
    71 public slots:
       
    72     void initTestCase();
       
    73     void cleanupTestCase();
       
    74     void init();
       
    75     void cleanup();
       
    76 
       
    77 private slots:
       
    78     void insertColumn_data() { generic_data(); }
       
    79     void insertColumn();
       
    80     void removeColumn_data() { generic_data(); }
       
    81     void removeColumn();
       
    82     void record_data() { generic_data(); }
       
    83     void record();
       
    84     void setHeaderData_data() { generic_data(); }
       
    85     void setHeaderData();
       
    86     void fetchMore_data() { generic_data(); }
       
    87     void fetchMore();
       
    88 
       
    89     //problem specific tests
       
    90     void withSortFilterProxyModel_data() { generic_data(); }
       
    91     void withSortFilterProxyModel();
       
    92     void setQuerySignalEmission_data() { generic_data(); }
       
    93     void setQuerySignalEmission();
       
    94     void setQueryWithNoRowsInResultSet_data() { generic_data(); }
       
    95     void setQueryWithNoRowsInResultSet();
       
    96 
       
    97     void task_180617();
       
    98     void task_180617_data() { generic_data(); }
       
    99     void task_QTBUG_4963_setHeaderDataWithProxyModel();
       
   100 
       
   101 private:
       
   102     void generic_data(const QString &engine=QString());
       
   103     void dropTestTables(QSqlDatabase db);
       
   104     void createTestTables(QSqlDatabase db);
       
   105     void populateTestTables(QSqlDatabase db);
       
   106     tst_Databases dbs;
       
   107 };
       
   108 
       
   109 /* Stupid class that makes protected members public for testing */
       
   110 class DBTestModel: public QSqlQueryModel
       
   111 {
       
   112 public:
       
   113     DBTestModel(QObject *parent = 0): QSqlQueryModel(parent) {}
       
   114     QModelIndex indexInQuery(const QModelIndex &item) const { return QSqlQueryModel::indexInQuery(item); }
       
   115 };
       
   116 
       
   117 tst_QSqlQueryModel::tst_QSqlQueryModel()
       
   118 {
       
   119 }
       
   120 
       
   121 tst_QSqlQueryModel::~tst_QSqlQueryModel()
       
   122 {
       
   123 }
       
   124 
       
   125 void tst_QSqlQueryModel::initTestCase()
       
   126 {
       
   127     qRegisterMetaType<QModelIndex>("QModelIndex");
       
   128     dbs.open();
       
   129     for (QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it) {
       
   130 	QSqlDatabase db = QSqlDatabase::database((*it));
       
   131 	CHECK_DATABASE(db);
       
   132 	dropTestTables(db); //in case of leftovers
       
   133 	createTestTables(db);
       
   134 	populateTestTables(db);
       
   135     }
       
   136 }
       
   137 
       
   138 void tst_QSqlQueryModel::cleanupTestCase()
       
   139 {
       
   140     for (QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it) {
       
   141 	QSqlDatabase db = QSqlDatabase::database((*it));
       
   142 	CHECK_DATABASE(db);
       
   143 	dropTestTables(db);
       
   144     }
       
   145     dbs.close();
       
   146 }
       
   147 
       
   148 void tst_QSqlQueryModel::dropTestTables(QSqlDatabase db)
       
   149 {
       
   150     QStringList tableNames;
       
   151     tableNames << qTableName("test", __FILE__)
       
   152             << qTableName("test2", __FILE__)
       
   153             << qTableName("test3", __FILE__)
       
   154             << qTableName("many", __FILE__);
       
   155     tst_Databases::safeDropTables(db, tableNames);
       
   156 }
       
   157 
       
   158 void tst_QSqlQueryModel::createTestTables(QSqlDatabase db)
       
   159 {
       
   160     dropTestTables(db);
       
   161     QSqlQuery q(db);
       
   162     if(tst_Databases::isPostgreSQL(db))
       
   163         QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
       
   164     QVERIFY_SQL( q, exec("create table " + qTableName("test", __FILE__) + "(id integer not null, name varchar(20), title integer, primary key (id))"));
       
   165     QVERIFY_SQL( q, exec("create table " + qTableName("test2", __FILE__) + "(id integer not null, title varchar(20), primary key (id))"));
       
   166     QVERIFY_SQL( q, exec("create table " + qTableName("test3", __FILE__) + "(id integer not null, primary key (id))"));
       
   167     QVERIFY_SQL( q, exec("create table " + qTableName("many", __FILE__) + "(id integer not null, name varchar(20), primary key (id))"));
       
   168 }
       
   169 
       
   170 void tst_QSqlQueryModel::populateTestTables(QSqlDatabase db)
       
   171 {
       
   172     qWarning() << "Populating test tables, this can take quite a while... ZZZzzz...";
       
   173     bool hasTransactions = db.driver()->hasFeature(QSqlDriver::Transactions);
       
   174 
       
   175     QSqlQuery q(db), q2(db);
       
   176 
       
   177     tst_Databases::safeDropTables(db, QStringList() << qTableName("manytmp", __FILE__) << qTableName("test3tmp", __FILE__));
       
   178     QVERIFY_SQL(q, exec("create table " + qTableName("manytmp", __FILE__) + "(id integer not null, name varchar(20), primary key (id))"));
       
   179     QVERIFY_SQL(q, exec("create table " + qTableName("test3tmp", __FILE__) + "(id integer not null, primary key (id))"));
       
   180 
       
   181     if (hasTransactions) QVERIFY_SQL(db, transaction());
       
   182 
       
   183     QVERIFY_SQL(q, exec("insert into " + qTableName("test", __FILE__) + " values(1, 'harry', 1)"));
       
   184     QVERIFY_SQL(q, exec("insert into " + qTableName("test", __FILE__) + " values(2, 'trond', 2)"));
       
   185     QVERIFY_SQL(q, exec("insert into " + qTableName("test2", __FILE__) + " values(1, 'herr')"));
       
   186     QVERIFY_SQL(q, exec("insert into " + qTableName("test2", __FILE__) + " values(2, 'mister')"));
       
   187 
       
   188     QVERIFY_SQL(q, exec(QString("insert into " + qTableName("test3", __FILE__) + " values(0)")));
       
   189     QVERIFY_SQL(q, prepare("insert into "+qTableName("test3", __FILE__)+"(id) select id + ? from "+qTableName("test3tmp", __FILE__)));
       
   190     for (int i=1; i<260; i*=2) {
       
   191         q2.exec("delete from "+qTableName("test3tmp", __FILE__));
       
   192         QVERIFY_SQL(q2, exec("insert into "+qTableName("test3tmp", __FILE__)+"(id) select id from "+qTableName("test3", __FILE__)));
       
   193         q.bindValue(0, i);
       
   194         QVERIFY_SQL(q, exec());
       
   195     }
       
   196 
       
   197     QVERIFY_SQL(q, exec(QString("insert into " + qTableName("many", __FILE__) + "(id, name) values (0, \'harry\')")));
       
   198     QVERIFY_SQL(q, prepare("insert into "+qTableName("many", __FILE__)+"(id, name) select id + ?, name from "+qTableName("manytmp", __FILE__)));
       
   199     for (int i=1; i < 2048; i*=2) {
       
   200         q2.exec("delete from "+qTableName("manytmp", __FILE__));
       
   201         QVERIFY_SQL(q2, exec("insert into "+qTableName("manytmp", __FILE__)+"(id, name) select id, name from "+qTableName("many", __FILE__)));
       
   202         q.bindValue(0, i);
       
   203         QVERIFY_SQL(q, exec());
       
   204     }
       
   205 
       
   206     if (hasTransactions) QVERIFY_SQL(db, commit());
       
   207 
       
   208     tst_Databases::safeDropTables(db, QStringList() << qTableName("manytmp", __FILE__) << qTableName("test3tmp", __FILE__));
       
   209 }
       
   210 
       
   211 void tst_QSqlQueryModel::generic_data(const QString& engine)
       
   212 {
       
   213     if ( dbs.fillTestTable(engine) == 0 ) {
       
   214         if(engine.isEmpty())
       
   215            QSKIP( "No database drivers are available in this Qt configuration", SkipAll );
       
   216         else
       
   217            QSKIP( (QString("No database drivers of type %1 are available in this Qt configuration").arg(engine)).toLocal8Bit(), SkipAll );
       
   218     }
       
   219 }
       
   220 
       
   221 void tst_QSqlQueryModel::init()
       
   222 {
       
   223 }
       
   224 
       
   225 void tst_QSqlQueryModel::cleanup()
       
   226 {
       
   227 }
       
   228 
       
   229 void tst_QSqlQueryModel::removeColumn()
       
   230 {
       
   231     QFETCH(QString, dbName);
       
   232     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   233     CHECK_DATABASE(db);
       
   234 
       
   235     DBTestModel model;
       
   236     model.setQuery(QSqlQuery("select * from " + qTableName("test", __FILE__), db));
       
   237     model.fetchMore();
       
   238     QSignalSpy spy(&model, SIGNAL(columnsAboutToBeRemoved(QModelIndex, int, int)));
       
   239 
       
   240     QCOMPARE(model.columnCount(), 3);
       
   241     QVERIFY(model.removeColumn(0));
       
   242     QCOMPARE(spy.count(), 1);
       
   243     QVERIFY(*(QModelIndex *)spy.at(0).at(0).constData() == QModelIndex());
       
   244     QCOMPARE(spy.at(0).at(1).toInt(), 0);
       
   245     QCOMPARE(spy.at(0).at(2).toInt(), 0);
       
   246 
       
   247     QCOMPARE(model.columnCount(), 2);
       
   248     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), 1);
       
   249     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), 2);
       
   250     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), -1);
       
   251     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), -1);
       
   252 
       
   253     QVERIFY(model.insertColumn(1));
       
   254     QCOMPARE(model.columnCount(), 3);
       
   255     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), 1);
       
   256     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), -1);
       
   257     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), 2);
       
   258     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), -1);
       
   259 
       
   260     QCOMPARE(model.data(model.index(0, 0)).toString(), QString("harry"));
       
   261     QCOMPARE(model.data(model.index(0, 1)), QVariant());
       
   262     QCOMPARE(model.data(model.index(0, 2)).toInt(), 1);
       
   263     QCOMPARE(model.data(model.index(0, 3)), QVariant());
       
   264 
       
   265     QVERIFY(!model.removeColumn(42));
       
   266     QVERIFY(!model.removeColumn(3));
       
   267     QVERIFY(!model.removeColumn(1, model.index(1, 2)));
       
   268     QCOMPARE(model.columnCount(), 3);
       
   269 
       
   270     QVERIFY(model.removeColumn(2));
       
   271 
       
   272     QCOMPARE(spy.count(), 2);
       
   273     QVERIFY(*(QModelIndex *)spy.at(1).at(0).constData() == QModelIndex());
       
   274     QCOMPARE(spy.at(1).at(1).toInt(), 2);
       
   275     QCOMPARE(spy.at(1).at(2).toInt(), 2);
       
   276 
       
   277     QCOMPARE(model.columnCount(), 2);
       
   278     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), 1);
       
   279     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), -1);
       
   280     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), -1);
       
   281     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), -1);
       
   282 
       
   283     QVERIFY(model.removeColumn(1));
       
   284 
       
   285     QCOMPARE(spy.count(), 3);
       
   286     QVERIFY(*(QModelIndex *)spy.at(2).at(0).constData() == QModelIndex());
       
   287     QCOMPARE(spy.at(2).at(1).toInt(), 1);
       
   288     QCOMPARE(spy.at(2).at(2).toInt(), 1);
       
   289 
       
   290     QCOMPARE(model.columnCount(), 1);
       
   291     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), 1);
       
   292     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), -1);
       
   293     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), -1);
       
   294     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), -1);
       
   295     QCOMPARE(model.data(model.index(0, 0)).toString(), QString("harry"));
       
   296 
       
   297     QVERIFY(model.removeColumn(0));
       
   298 
       
   299     QCOMPARE(spy.count(), 4);
       
   300     QVERIFY(*(QModelIndex *)spy.at(3).at(0).constData() == QModelIndex());
       
   301     QCOMPARE(spy.at(3).at(1).toInt(), 0);
       
   302     QCOMPARE(spy.at(3).at(2).toInt(), 0);
       
   303 
       
   304     QCOMPARE(model.columnCount(), 0);
       
   305     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), -1);
       
   306     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), -1);
       
   307     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), -1);
       
   308     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), -1);
       
   309 }
       
   310 
       
   311 void tst_QSqlQueryModel::insertColumn()
       
   312 {
       
   313     QFETCH(QString, dbName);
       
   314     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   315     CHECK_DATABASE(db);
       
   316 
       
   317     DBTestModel model;
       
   318     model.setQuery(QSqlQuery("select * from " + qTableName("test", __FILE__), db));
       
   319     model.fetchMore(); // necessary???
       
   320 
       
   321     QSignalSpy spy(&model, SIGNAL(columnsInserted(QModelIndex, int, int)));
       
   322 
       
   323     QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
       
   324     QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
       
   325     QCOMPARE(model.data(model.index(0, 2)).toInt(), 1);
       
   326     QCOMPARE(model.data(model.index(0, 3)), QVariant());
       
   327 
       
   328     QVERIFY(model.insertColumn(1));
       
   329 
       
   330     QCOMPARE(spy.count(), 1);
       
   331     QVERIFY(*(QModelIndex *)spy.at(0).at(0).constData() == QModelIndex());
       
   332     QCOMPARE(spy.at(0).at(1).toInt(), 1);
       
   333     QCOMPARE(spy.at(0).at(2).toInt(), 1);
       
   334 
       
   335     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), 0);
       
   336     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), -1);
       
   337     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), 1);
       
   338     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), 2);
       
   339     QCOMPARE(model.indexInQuery(model.index(0, 4)).column(), -1);
       
   340 
       
   341     QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
       
   342     QCOMPARE(model.data(model.index(0, 1)), QVariant());
       
   343     QCOMPARE(model.data(model.index(0, 2)).toString(), QString("harry"));
       
   344     QCOMPARE(model.data(model.index(0, 3)).toInt(), 1);
       
   345     QCOMPARE(model.data(model.index(0, 4)), QVariant());
       
   346 
       
   347     QVERIFY(!model.insertColumn(-1));
       
   348     QVERIFY(!model.insertColumn(100));
       
   349     QVERIFY(!model.insertColumn(1, model.index(1, 1)));
       
   350 
       
   351     QVERIFY(model.insertColumn(0));
       
   352 
       
   353     QCOMPARE(spy.count(), 2);
       
   354     QVERIFY(*(QModelIndex *)spy.at(1).at(0).constData() == QModelIndex());
       
   355     QCOMPARE(spy.at(1).at(1).toInt(), 0);
       
   356     QCOMPARE(spy.at(1).at(2).toInt(), 0);
       
   357 
       
   358     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), -1);
       
   359     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), 0);
       
   360     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), -1);
       
   361     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), 1);
       
   362     QCOMPARE(model.indexInQuery(model.index(0, 4)).column(), 2);
       
   363     QCOMPARE(model.indexInQuery(model.index(0, 5)).column(), -1);
       
   364 
       
   365     QVERIFY(!model.insertColumn(6));
       
   366     QVERIFY(model.insertColumn(5));
       
   367 
       
   368     QCOMPARE(spy.count(), 3);
       
   369     QVERIFY(*(QModelIndex *)spy.at(2).at(0).constData() == QModelIndex());
       
   370     QCOMPARE(spy.at(2).at(1).toInt(), 5);
       
   371     QCOMPARE(spy.at(2).at(2).toInt(), 5);
       
   372 
       
   373     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), -1);
       
   374     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), 0);
       
   375     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), -1);
       
   376     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), 1);
       
   377     QCOMPARE(model.indexInQuery(model.index(0, 4)).column(), 2);
       
   378     QCOMPARE(model.indexInQuery(model.index(0, 5)).column(), -1);
       
   379     QCOMPARE(model.indexInQuery(model.index(0, 6)).column(), -1);
       
   380 
       
   381     bool isToUpper = db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2");
       
   382     QCOMPARE(model.record().field(0).name(), QString());
       
   383     QCOMPARE(model.record().field(1).name(), isToUpper ? QString("ID") : QString("id"));
       
   384     QCOMPARE(model.record().field(2).name(), QString());
       
   385     QCOMPARE(model.record().field(3).name(), isToUpper ? QString("NAME") : QString("name"));
       
   386     QCOMPARE(model.record().field(4).name(), isToUpper ? QString("TITLE") : QString("title"));
       
   387     QCOMPARE(model.record().field(5).name(), QString());
       
   388     QCOMPARE(model.record().field(6).name(), QString());
       
   389 }
       
   390 
       
   391 void tst_QSqlQueryModel::record()
       
   392 {
       
   393     QFETCH(QString, dbName);
       
   394     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   395     CHECK_DATABASE(db);
       
   396 
       
   397     QSqlQueryModel model;
       
   398     model.setQuery(QSqlQuery("select * from " + qTableName("test", __FILE__), db));
       
   399 
       
   400     QSqlRecord rec = model.record();
       
   401 
       
   402     bool isToUpper = db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2");
       
   403 
       
   404     QCOMPARE(rec.count(), 3);
       
   405     QCOMPARE(rec.fieldName(0), isToUpper ? QString("ID") : QString("id"));
       
   406     QCOMPARE(rec.fieldName(1), isToUpper ? QString("NAME") : QString("name"));
       
   407     QCOMPARE(rec.fieldName(2), isToUpper ? QString("TITLE") : QString("title"));
       
   408     QCOMPARE(rec.value(0), QVariant());
       
   409     QCOMPARE(rec.value(1), QVariant());
       
   410     QCOMPARE(rec.value(2), QVariant());
       
   411 
       
   412     rec = model.record(0);
       
   413     QCOMPARE(rec.fieldName(0), isToUpper ? QString("ID") : QString("id"));
       
   414     QCOMPARE(rec.fieldName(1), isToUpper ? QString("NAME") : QString("name"));
       
   415     QCOMPARE(rec.fieldName(2), isToUpper ? QString("TITLE") : QString("title"));
       
   416     QCOMPARE(rec.value(0).toString(), QString("1"));
       
   417     QCOMPARE(rec.value(1), QVariant("harry"));
       
   418     QCOMPARE(rec.value(2), QVariant(1));
       
   419 }
       
   420 
       
   421 void tst_QSqlQueryModel::setHeaderData()
       
   422 {
       
   423     QFETCH(QString, dbName);
       
   424     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   425     CHECK_DATABASE(db);
       
   426 
       
   427     QSqlQueryModel model;
       
   428 
       
   429     QVERIFY(!model.setHeaderData(5, Qt::Vertical, "foo"));
       
   430     QVERIFY(model.headerData(5, Qt::Vertical).isValid());
       
   431 
       
   432     model.setQuery(QSqlQuery("select * from " + qTableName("test", __FILE__), db));
       
   433 
       
   434     qRegisterMetaType<Qt::Orientation>("Qt::Orientation");
       
   435     QSignalSpy spy(&model, SIGNAL(headerDataChanged(Qt::Orientation, int, int)));
       
   436     QVERIFY(model.setHeaderData(2, Qt::Horizontal, "bar"));
       
   437     QCOMPARE(model.headerData(2, Qt::Horizontal).toString(), QString("bar"));
       
   438     QCOMPARE(spy.count(), 1);
       
   439     QCOMPARE(qvariant_cast<Qt::Orientation>(spy.value(0).value(0)), Qt::Horizontal);
       
   440     QCOMPARE(spy.value(0).value(1).toInt(), 2);
       
   441     QCOMPARE(spy.value(0).value(2).toInt(), 2);
       
   442 
       
   443     QVERIFY(!model.setHeaderData(7, Qt::Horizontal, "foo", Qt::ToolTipRole));
       
   444     QVERIFY(!model.headerData(7, Qt::Horizontal, Qt::ToolTipRole).isValid());
       
   445 
       
   446     bool isToUpper = db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2");
       
   447     QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), isToUpper ? QString("ID") : QString("id"));
       
   448     QCOMPARE(model.headerData(1, Qt::Horizontal).toString(), isToUpper ? QString("NAME") : QString("name"));
       
   449     QCOMPARE(model.headerData(2, Qt::Horizontal).toString(), QString("bar"));
       
   450     QVERIFY(model.headerData(3, Qt::Horizontal).isValid());
       
   451 }
       
   452 
       
   453 void tst_QSqlQueryModel::fetchMore()
       
   454 {
       
   455     QFETCH(QString, dbName);
       
   456     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   457     CHECK_DATABASE(db);
       
   458 
       
   459     QSqlQueryModel model;
       
   460     QSignalSpy spy(&model, SIGNAL(rowsInserted(QModelIndex, int, int)));
       
   461 
       
   462     model.setQuery(QSqlQuery("select * from " + qTableName("many", __FILE__), db));
       
   463     int rowCount = model.rowCount();
       
   464 
       
   465     QCOMPARE(spy.value(0).value(1).toInt(), 0);
       
   466     QCOMPARE(spy.value(0).value(2).toInt(), rowCount - 1);
       
   467 
       
   468     // If the driver doesn't return the query size fetchMore() causes the
       
   469     // model to grow and new signals are emitted
       
   470     if (!db.driver()->hasFeature(QSqlDriver::QuerySize)) {
       
   471         spy.clear();
       
   472         model.fetchMore();
       
   473         int newRowCount = model.rowCount();
       
   474         QCOMPARE(spy.value(0).value(1).toInt(), rowCount);
       
   475         QCOMPARE(spy.value(0).value(2).toInt(), newRowCount - 1);
       
   476     }
       
   477 }
       
   478 
       
   479 // For task 149491: When used with QSortFilterProxyModel, a view and a
       
   480 // database that doesn't support the QuerySize feature, blank rows was
       
   481 // appended if the query returned more than 256 rows and setQuery()
       
   482 // was called more than once. This because an insertion of rows was
       
   483 // triggered at the same time as the model was being cleared.
       
   484 void tst_QSqlQueryModel::withSortFilterProxyModel()
       
   485 {
       
   486     QFETCH(QString, dbName);
       
   487     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   488     CHECK_DATABASE(db);
       
   489 
       
   490     if (db.driver()->hasFeature(QSqlDriver::QuerySize))
       
   491         QSKIP("Test applies only for drivers not reporting the query size.", SkipSingle);
       
   492 
       
   493     QSqlQueryModel model;
       
   494     model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test3", __FILE__), db));
       
   495     QSortFilterProxyModel proxy;
       
   496     proxy.setSourceModel(&model);
       
   497 
       
   498     QTableView view;
       
   499     view.setModel(&proxy);
       
   500 
       
   501     QSignalSpy modelRowsRemovedSpy(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)));
       
   502     QSignalSpy modelRowsInsertedSpy(&model, SIGNAL(rowsInserted(const QModelIndex &, int, int)));
       
   503     model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test3", __FILE__), db));
       
   504     view.scrollToBottom();
       
   505 
       
   506     QTestEventLoop::instance().enterLoop(1);
       
   507 
       
   508     QCOMPARE(proxy.rowCount(), 511);
       
   509 
       
   510     // The second call to setQuery() clears the model by removing it's rows.
       
   511     // Only 256 rows because that is what was cached.
       
   512     QCOMPARE(modelRowsRemovedSpy.count(), 1);
       
   513     QCOMPARE(modelRowsRemovedSpy.value(0).value(1).toInt(), 0);
       
   514     QCOMPARE(modelRowsRemovedSpy.value(0).value(2).toInt(), 255);
       
   515 
       
   516     // The call to scrollToBottom() forces the model to fetch all rows,
       
   517     // which will be done in two steps.
       
   518     QCOMPARE(modelRowsInsertedSpy.count(), 2);
       
   519     QCOMPARE(modelRowsInsertedSpy.value(0).value(1).toInt(), 0);
       
   520     QCOMPARE(modelRowsInsertedSpy.value(0).value(2).toInt(), 255);
       
   521     QCOMPARE(modelRowsInsertedSpy.value(1).value(1).toInt(), 256);
       
   522     QCOMPARE(modelRowsInsertedSpy.value(1).value(2).toInt(), 510);
       
   523 }
       
   524 
       
   525 // For task 155402: When the model is already empty when setQuery() is called
       
   526 // no rows have to be removed and rowsAboutToBeRemoved and rowsRemoved should
       
   527 // not be emitted.
       
   528 void tst_QSqlQueryModel::setQuerySignalEmission()
       
   529 {
       
   530     QFETCH(QString, dbName);
       
   531     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   532     CHECK_DATABASE(db);
       
   533 
       
   534     QSqlQueryModel model;
       
   535     QSignalSpy modelRowsAboutToBeRemovedSpy(&model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)));
       
   536     QSignalSpy modelRowsRemovedSpy(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)));
       
   537 
       
   538     // First select, the model was empty and no rows had to be removed!
       
   539     model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test", __FILE__), db));
       
   540     QCOMPARE(modelRowsAboutToBeRemovedSpy.count(), 0);
       
   541     QCOMPARE(modelRowsRemovedSpy.count(), 0);
       
   542 
       
   543     // Second select, the model wasn't empty and two rows had to be removed!
       
   544     model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test", __FILE__), db));
       
   545     QCOMPARE(modelRowsAboutToBeRemovedSpy.count(), 1);
       
   546     QCOMPARE(modelRowsAboutToBeRemovedSpy.value(0).value(1).toInt(), 0);
       
   547     QCOMPARE(modelRowsAboutToBeRemovedSpy.value(0).value(2).toInt(), 1);
       
   548     QCOMPARE(modelRowsRemovedSpy.count(), 1);
       
   549     QCOMPARE(modelRowsRemovedSpy.value(0).value(1).toInt(), 0);
       
   550     QCOMPARE(modelRowsRemovedSpy.value(0).value(2).toInt(), 1);
       
   551 }
       
   552 
       
   553 // For task 170783: When the query's result set is empty no rows should be inserted,
       
   554 // i.e. no rowsAboutToBeInserted or rowsInserted signals should be emitted.
       
   555 void tst_QSqlQueryModel::setQueryWithNoRowsInResultSet()
       
   556 {
       
   557     QFETCH(QString, dbName);
       
   558     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   559     CHECK_DATABASE(db);
       
   560 
       
   561     QSqlQueryModel model;
       
   562     QSignalSpy modelRowsAboutToBeInsertedSpy(&model, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)));
       
   563     QSignalSpy modelRowsInsertedSpy(&model, SIGNAL(rowsInserted(const QModelIndex &, int, int)));
       
   564 
       
   565     // The query's result set will be empty so no signals should be emitted!
       
   566     QSqlQuery query(db);
       
   567     QVERIFY_SQL(query, exec("SELECT * FROM " + qTableName("test", __FILE__) + " where 0 = 1"));
       
   568     model.setQuery(query);
       
   569     QCOMPARE(modelRowsAboutToBeInsertedSpy.count(), 0);
       
   570     QCOMPARE(modelRowsInsertedSpy.count(), 0);
       
   571 }
       
   572 
       
   573 // For task 180617
       
   574 // According to the task, several specific duplicate SQL queries would cause
       
   575 // multiple empty grid lines to be visible in the view
       
   576 void tst_QSqlQueryModel::task_180617()
       
   577 {
       
   578     QFETCH(QString, dbName);
       
   579     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   580     CHECK_DATABASE(db);
       
   581     const QString test3(qTableName("test3", __FILE__));
       
   582 
       
   583     QTableView view;
       
   584     QCOMPARE(view.columnAt(0), -1);
       
   585     QCOMPARE(view.rowAt(0), -1);
       
   586 
       
   587     QSqlQueryModel model;
       
   588     model.setQuery( "SELECT TOP 0 * FROM " + test3, db );
       
   589     view.setModel(&model);
       
   590 
       
   591     bool error = false;
       
   592     // Usually a syntax error
       
   593     if (model.lastError().isValid())    // usually a syntax error
       
   594         error = true;
       
   595 
       
   596     QCOMPARE(view.columnAt(0), (error)?-1:0 );
       
   597     QCOMPARE(view.rowAt(0), -1);
       
   598 
       
   599     model.setQuery( "SELECT TOP 0 * FROM " + test3, db );
       
   600     model.setQuery( "SELECT TOP 0 * FROM " + test3, db );
       
   601     model.setQuery( "SELECT TOP 0 * FROM " + test3, db );
       
   602     model.setQuery( "SELECT TOP 0 * FROM " + test3, db );
       
   603 
       
   604     QCOMPARE(view.columnAt(0),  (error)?-1:0 );
       
   605     QCOMPARE(view.rowAt(0), -1);
       
   606 }
       
   607 
       
   608 void tst_QSqlQueryModel::task_QTBUG_4963_setHeaderDataWithProxyModel()
       
   609 {
       
   610     QSqlQueryModel plainModel;
       
   611     QSortFilterProxyModel proxyModel;
       
   612     proxyModel.setSourceModel(&plainModel);
       
   613     QVERIFY(!plainModel.setHeaderData(0, Qt::Horizontal, QObject::tr("ID")));
       
   614     // And it should not crash.
       
   615 }
       
   616 
       
   617 QTEST_MAIN(tst_QSqlQueryModel)
       
   618 #include "tst_qsqlquerymodel.moc"