tests/auto/qsqlquerymodel/tst_qsqlquerymodel.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 
       
   100 private:
       
   101     void generic_data(const QString &engine=QString());
       
   102     void dropTestTables(QSqlDatabase db);
       
   103     void createTestTables(QSqlDatabase db);
       
   104     void populateTestTables(QSqlDatabase db);
       
   105     tst_Databases dbs;
       
   106 };
       
   107 
       
   108 /* Stupid class that makes protected members public for testing */
       
   109 class DBTestModel: public QSqlQueryModel
       
   110 {
       
   111 public:
       
   112     DBTestModel(QObject *parent = 0): QSqlQueryModel(parent) {}
       
   113     QModelIndex indexInQuery(const QModelIndex &item) const { return QSqlQueryModel::indexInQuery(item); }
       
   114 };
       
   115 
       
   116 tst_QSqlQueryModel::tst_QSqlQueryModel()
       
   117 {
       
   118 }
       
   119 
       
   120 tst_QSqlQueryModel::~tst_QSqlQueryModel()
       
   121 {
       
   122 }
       
   123 
       
   124 void tst_QSqlQueryModel::initTestCase()
       
   125 {
       
   126     qRegisterMetaType<QModelIndex>("QModelIndex");
       
   127     dbs.open();
       
   128     for (QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it) {
       
   129 	QSqlDatabase db = QSqlDatabase::database((*it));
       
   130 	CHECK_DATABASE(db);
       
   131 	dropTestTables(db); //in case of leftovers
       
   132 	createTestTables(db);
       
   133 	populateTestTables(db);
       
   134     }
       
   135 }
       
   136 
       
   137 void tst_QSqlQueryModel::cleanupTestCase()
       
   138 {
       
   139     for (QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it) {
       
   140 	QSqlDatabase db = QSqlDatabase::database((*it));
       
   141 	CHECK_DATABASE(db);
       
   142 	dropTestTables(db);
       
   143     }
       
   144     dbs.close();
       
   145 }
       
   146 
       
   147 void tst_QSqlQueryModel::dropTestTables(QSqlDatabase db)
       
   148 {
       
   149     QStringList tableNames;
       
   150     tableNames << qTableName("test")
       
   151             << qTableName("test2")
       
   152             << qTableName("test3")
       
   153             << qTableName("many");
       
   154     tst_Databases::safeDropTables(db, tableNames);
       
   155 }
       
   156 
       
   157 void tst_QSqlQueryModel::createTestTables(QSqlDatabase db)
       
   158 {
       
   159     dropTestTables(db);
       
   160     QSqlQuery q(db);
       
   161     if(tst_Databases::isPostgreSQL(db))
       
   162         QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
       
   163     QVERIFY_SQL( q, exec("create table " + qTableName("test") + "(id integer not null, name varchar(20), title integer, primary key (id))"));
       
   164     QVERIFY_SQL( q, exec("create table " + qTableName("test2") + "(id integer not null, title varchar(20), primary key (id))"));
       
   165     QVERIFY_SQL( q, exec("create table " + qTableName("test3") + "(id integer not null, primary key (id))"));
       
   166     QVERIFY_SQL( q, exec("create table " + qTableName("many") + "(id integer not null, name varchar(20), primary key (id))"));
       
   167 }
       
   168 
       
   169 void tst_QSqlQueryModel::populateTestTables(QSqlDatabase db)
       
   170 {
       
   171     qWarning() << "Populating test tables, this can take quite a while... ZZZzzz...";
       
   172     bool hasTransactions = db.driver()->hasFeature(QSqlDriver::Transactions);
       
   173 
       
   174     QSqlQuery q(db), q2(db);
       
   175 
       
   176     tst_Databases::safeDropTables(db, QStringList() << qTableName("manytmp") << qTableName("test3tmp"));
       
   177     QVERIFY_SQL(q, exec("create table " + qTableName("manytmp") + "(id integer not null, name varchar(20), primary key (id))"));
       
   178     QVERIFY_SQL(q, exec("create table " + qTableName("test3tmp") + "(id integer not null, primary key (id))"));
       
   179 
       
   180     if (hasTransactions) QVERIFY_SQL(db, transaction());
       
   181 
       
   182     QVERIFY_SQL(q, exec("insert into " + qTableName("test") + " values(1, 'harry', 1)"));
       
   183     QVERIFY_SQL(q, exec("insert into " + qTableName("test") + " values(2, 'trond', 2)"));
       
   184     QVERIFY_SQL(q, exec("insert into " + qTableName("test2") + " values(1, 'herr')"));
       
   185     QVERIFY_SQL(q, exec("insert into " + qTableName("test2") + " values(2, 'mister')"));
       
   186 
       
   187     QVERIFY_SQL(q, exec(QString("insert into " + qTableName("test3") + " values(0)")));
       
   188     QVERIFY_SQL(q, prepare("insert into "+qTableName("test3")+"(id) select id + ? from "+qTableName("test3tmp")));
       
   189     for (int i=1; i<260; i*=2) {
       
   190         q2.exec("delete from "+qTableName("test3tmp"));
       
   191         QVERIFY_SQL(q2, exec("insert into "+qTableName("test3tmp")+"(id) select id from "+qTableName("test3")));
       
   192         q.bindValue(0, i);
       
   193         QVERIFY_SQL(q, exec());
       
   194     }
       
   195 
       
   196     QVERIFY_SQL(q, exec(QString("insert into " + qTableName("many") + "(id, name) values (0, \'harry\')")));
       
   197     QVERIFY_SQL(q, prepare("insert into "+qTableName("many")+"(id, name) select id + ?, name from "+qTableName("manytmp")));
       
   198     for (int i=1; i < 2048; i*=2) {
       
   199         q2.exec("delete from "+qTableName("manytmp"));
       
   200         QVERIFY_SQL(q2, exec("insert into "+qTableName("manytmp")+"(id, name) select id, name from "+qTableName("many")));
       
   201         q.bindValue(0, i);
       
   202         QVERIFY_SQL(q, exec());
       
   203     }
       
   204 
       
   205     if (hasTransactions) QVERIFY_SQL(db, commit());
       
   206 
       
   207     tst_Databases::safeDropTables(db, QStringList() << qTableName("manytmp") << qTableName("test3tmp"));
       
   208 }
       
   209 
       
   210 void tst_QSqlQueryModel::generic_data(const QString& engine)
       
   211 {
       
   212     if ( dbs.fillTestTable(engine) == 0 ) {
       
   213         if(engine.isEmpty())
       
   214            QSKIP( "No database drivers are available in this Qt configuration", SkipAll );
       
   215         else
       
   216            QSKIP( (QString("No database drivers of type %1 are available in this Qt configuration").arg(engine)).toLocal8Bit(), SkipAll );
       
   217     }
       
   218 }
       
   219 
       
   220 void tst_QSqlQueryModel::init()
       
   221 {
       
   222 }
       
   223 
       
   224 void tst_QSqlQueryModel::cleanup()
       
   225 {
       
   226 }
       
   227 
       
   228 void tst_QSqlQueryModel::removeColumn()
       
   229 {
       
   230     QFETCH(QString, dbName);
       
   231     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   232     CHECK_DATABASE(db);
       
   233 
       
   234     DBTestModel model;
       
   235     model.setQuery(QSqlQuery("select * from " + qTableName("test"), db));
       
   236     model.fetchMore();
       
   237     QSignalSpy spy(&model, SIGNAL(columnsAboutToBeRemoved(QModelIndex, int, int)));
       
   238 
       
   239     QCOMPARE(model.columnCount(), 3);
       
   240     QVERIFY(model.removeColumn(0));
       
   241     QCOMPARE(spy.count(), 1);
       
   242     QVERIFY(*(QModelIndex *)spy.at(0).at(0).constData() == QModelIndex());
       
   243     QCOMPARE(spy.at(0).at(1).toInt(), 0);
       
   244     QCOMPARE(spy.at(0).at(2).toInt(), 0);
       
   245 
       
   246     QCOMPARE(model.columnCount(), 2);
       
   247     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), 1);
       
   248     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), 2);
       
   249     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), -1);
       
   250     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), -1);
       
   251 
       
   252     QVERIFY(model.insertColumn(1));
       
   253     QCOMPARE(model.columnCount(), 3);
       
   254     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), 1);
       
   255     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), -1);
       
   256     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), 2);
       
   257     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), -1);
       
   258 
       
   259     QCOMPARE(model.data(model.index(0, 0)).toString(), QString("harry"));
       
   260     QCOMPARE(model.data(model.index(0, 1)), QVariant());
       
   261     QCOMPARE(model.data(model.index(0, 2)).toInt(), 1);
       
   262     QCOMPARE(model.data(model.index(0, 3)), QVariant());
       
   263 
       
   264     QVERIFY(!model.removeColumn(42));
       
   265     QVERIFY(!model.removeColumn(3));
       
   266     QVERIFY(!model.removeColumn(1, model.index(1, 2)));
       
   267     QCOMPARE(model.columnCount(), 3);
       
   268 
       
   269     QVERIFY(model.removeColumn(2));
       
   270 
       
   271     QCOMPARE(spy.count(), 2);
       
   272     QVERIFY(*(QModelIndex *)spy.at(1).at(0).constData() == QModelIndex());
       
   273     QCOMPARE(spy.at(1).at(1).toInt(), 2);
       
   274     QCOMPARE(spy.at(1).at(2).toInt(), 2);
       
   275 
       
   276     QCOMPARE(model.columnCount(), 2);
       
   277     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), 1);
       
   278     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), -1);
       
   279     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), -1);
       
   280     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), -1);
       
   281 
       
   282     QVERIFY(model.removeColumn(1));
       
   283 
       
   284     QCOMPARE(spy.count(), 3);
       
   285     QVERIFY(*(QModelIndex *)spy.at(2).at(0).constData() == QModelIndex());
       
   286     QCOMPARE(spy.at(2).at(1).toInt(), 1);
       
   287     QCOMPARE(spy.at(2).at(2).toInt(), 1);
       
   288 
       
   289     QCOMPARE(model.columnCount(), 1);
       
   290     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), 1);
       
   291     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), -1);
       
   292     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), -1);
       
   293     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), -1);
       
   294     QCOMPARE(model.data(model.index(0, 0)).toString(), QString("harry"));
       
   295 
       
   296     QVERIFY(model.removeColumn(0));
       
   297 
       
   298     QCOMPARE(spy.count(), 4);
       
   299     QVERIFY(*(QModelIndex *)spy.at(3).at(0).constData() == QModelIndex());
       
   300     QCOMPARE(spy.at(3).at(1).toInt(), 0);
       
   301     QCOMPARE(spy.at(3).at(2).toInt(), 0);
       
   302 
       
   303     QCOMPARE(model.columnCount(), 0);
       
   304     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), -1);
       
   305     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), -1);
       
   306     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), -1);
       
   307     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), -1);
       
   308 }
       
   309 
       
   310 void tst_QSqlQueryModel::insertColumn()
       
   311 {
       
   312     QFETCH(QString, dbName);
       
   313     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   314     CHECK_DATABASE(db);
       
   315 
       
   316     DBTestModel model;
       
   317     model.setQuery(QSqlQuery("select * from " + qTableName("test"), db));
       
   318     model.fetchMore(); // necessary???
       
   319 
       
   320     QSignalSpy spy(&model, SIGNAL(columnsInserted(QModelIndex, int, int)));
       
   321 
       
   322     QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
       
   323     QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
       
   324     QCOMPARE(model.data(model.index(0, 2)).toInt(), 1);
       
   325     QCOMPARE(model.data(model.index(0, 3)), QVariant());
       
   326 
       
   327     QVERIFY(model.insertColumn(1));
       
   328 
       
   329     QCOMPARE(spy.count(), 1);
       
   330     QVERIFY(*(QModelIndex *)spy.at(0).at(0).constData() == QModelIndex());
       
   331     QCOMPARE(spy.at(0).at(1).toInt(), 1);
       
   332     QCOMPARE(spy.at(0).at(2).toInt(), 1);
       
   333 
       
   334     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), 0);
       
   335     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), -1);
       
   336     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), 1);
       
   337     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), 2);
       
   338     QCOMPARE(model.indexInQuery(model.index(0, 4)).column(), -1);
       
   339 
       
   340     QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
       
   341     QCOMPARE(model.data(model.index(0, 1)), QVariant());
       
   342     QCOMPARE(model.data(model.index(0, 2)).toString(), QString("harry"));
       
   343     QCOMPARE(model.data(model.index(0, 3)).toInt(), 1);
       
   344     QCOMPARE(model.data(model.index(0, 4)), QVariant());
       
   345 
       
   346     QVERIFY(!model.insertColumn(-1));
       
   347     QVERIFY(!model.insertColumn(100));
       
   348     QVERIFY(!model.insertColumn(1, model.index(1, 1)));
       
   349 
       
   350     QVERIFY(model.insertColumn(0));
       
   351 
       
   352     QCOMPARE(spy.count(), 2);
       
   353     QVERIFY(*(QModelIndex *)spy.at(1).at(0).constData() == QModelIndex());
       
   354     QCOMPARE(spy.at(1).at(1).toInt(), 0);
       
   355     QCOMPARE(spy.at(1).at(2).toInt(), 0);
       
   356 
       
   357     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), -1);
       
   358     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), 0);
       
   359     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), -1);
       
   360     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), 1);
       
   361     QCOMPARE(model.indexInQuery(model.index(0, 4)).column(), 2);
       
   362     QCOMPARE(model.indexInQuery(model.index(0, 5)).column(), -1);
       
   363 
       
   364     QVERIFY(!model.insertColumn(6));
       
   365     QVERIFY(model.insertColumn(5));
       
   366 
       
   367     QCOMPARE(spy.count(), 3);
       
   368     QVERIFY(*(QModelIndex *)spy.at(2).at(0).constData() == QModelIndex());
       
   369     QCOMPARE(spy.at(2).at(1).toInt(), 5);
       
   370     QCOMPARE(spy.at(2).at(2).toInt(), 5);
       
   371 
       
   372     QCOMPARE(model.indexInQuery(model.index(0, 0)).column(), -1);
       
   373     QCOMPARE(model.indexInQuery(model.index(0, 1)).column(), 0);
       
   374     QCOMPARE(model.indexInQuery(model.index(0, 2)).column(), -1);
       
   375     QCOMPARE(model.indexInQuery(model.index(0, 3)).column(), 1);
       
   376     QCOMPARE(model.indexInQuery(model.index(0, 4)).column(), 2);
       
   377     QCOMPARE(model.indexInQuery(model.index(0, 5)).column(), -1);
       
   378     QCOMPARE(model.indexInQuery(model.index(0, 6)).column(), -1);
       
   379 
       
   380     bool isToUpper = db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2");
       
   381     QCOMPARE(model.record().field(0).name(), QString());
       
   382     QCOMPARE(model.record().field(1).name(), isToUpper ? QString("ID") : QString("id"));
       
   383     QCOMPARE(model.record().field(2).name(), QString());
       
   384     QCOMPARE(model.record().field(3).name(), isToUpper ? QString("NAME") : QString("name"));
       
   385     QCOMPARE(model.record().field(4).name(), isToUpper ? QString("TITLE") : QString("title"));
       
   386     QCOMPARE(model.record().field(5).name(), QString());
       
   387     QCOMPARE(model.record().field(6).name(), QString());
       
   388 }
       
   389 
       
   390 void tst_QSqlQueryModel::record()
       
   391 {
       
   392     QFETCH(QString, dbName);
       
   393     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   394     CHECK_DATABASE(db);
       
   395 
       
   396     QSqlQueryModel model;
       
   397     model.setQuery(QSqlQuery("select * from " + qTableName("test"), db));
       
   398 
       
   399     QSqlRecord rec = model.record();
       
   400 
       
   401     bool isToUpper = db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2");
       
   402 
       
   403     QCOMPARE(rec.count(), 3);
       
   404     QCOMPARE(rec.fieldName(0), isToUpper ? QString("ID") : QString("id"));
       
   405     QCOMPARE(rec.fieldName(1), isToUpper ? QString("NAME") : QString("name"));
       
   406     QCOMPARE(rec.fieldName(2), isToUpper ? QString("TITLE") : QString("title"));
       
   407     QCOMPARE(rec.value(0), QVariant());
       
   408     QCOMPARE(rec.value(1), QVariant());
       
   409     QCOMPARE(rec.value(2), QVariant());
       
   410 
       
   411     rec = model.record(0);
       
   412     QCOMPARE(rec.fieldName(0), isToUpper ? QString("ID") : QString("id"));
       
   413     QCOMPARE(rec.fieldName(1), isToUpper ? QString("NAME") : QString("name"));
       
   414     QCOMPARE(rec.fieldName(2), isToUpper ? QString("TITLE") : QString("title"));
       
   415     QCOMPARE(rec.value(0).toString(), QString("1"));
       
   416     QCOMPARE(rec.value(1), QVariant("harry"));
       
   417     QCOMPARE(rec.value(2), QVariant(1));
       
   418 }
       
   419 
       
   420 void tst_QSqlQueryModel::setHeaderData()
       
   421 {
       
   422     QFETCH(QString, dbName);
       
   423     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   424     CHECK_DATABASE(db);
       
   425 
       
   426     QSqlQueryModel model;
       
   427 
       
   428     QVERIFY(!model.setHeaderData(5, Qt::Vertical, "foo"));
       
   429     QVERIFY(model.headerData(5, Qt::Vertical).isValid());
       
   430 
       
   431     qRegisterMetaType<Qt::Orientation>("Qt::Orientation");
       
   432     QSignalSpy spy(&model, SIGNAL(headerDataChanged(Qt::Orientation, int, int)));
       
   433     QVERIFY(model.setHeaderData(2, Qt::Horizontal, "bar"));
       
   434     QCOMPARE(model.headerData(2, Qt::Horizontal).toString(), QString("bar"));
       
   435     QCOMPARE(spy.count(), 1);
       
   436     QCOMPARE(qvariant_cast<Qt::Orientation>(spy.value(0).value(0)), Qt::Horizontal);
       
   437     QCOMPARE(spy.value(0).value(1).toInt(), 2);
       
   438     QCOMPARE(spy.value(0).value(2).toInt(), 2);
       
   439 
       
   440     QVERIFY(model.setHeaderData(7, Qt::Horizontal, "foo", Qt::ToolTipRole));
       
   441     QVERIFY(model.headerData(7, Qt::Horizontal, Qt::ToolTipRole).isValid());
       
   442 
       
   443     model.setQuery(QSqlQuery("select * from " + qTableName("test"), db));
       
   444 
       
   445     bool isToUpper = db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2");
       
   446     QCOMPARE(model.headerData(0, Qt::Horizontal).toString(), isToUpper ? QString("ID") : QString("id"));
       
   447     QCOMPARE(model.headerData(1, Qt::Horizontal).toString(), isToUpper ? QString("NAME") : QString("name"));
       
   448     QCOMPARE(model.headerData(2, Qt::Horizontal).toString(), QString("bar"));
       
   449     QVERIFY(model.headerData(3, Qt::Horizontal).isValid());
       
   450 }
       
   451 
       
   452 void tst_QSqlQueryModel::fetchMore()
       
   453 {
       
   454     QFETCH(QString, dbName);
       
   455     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   456     CHECK_DATABASE(db);
       
   457 
       
   458     QSqlQueryModel model;
       
   459     QSignalSpy spy(&model, SIGNAL(rowsInserted(QModelIndex, int, int)));
       
   460 
       
   461     model.setQuery(QSqlQuery("select * from " + qTableName("many"), db));
       
   462     int rowCount = model.rowCount();
       
   463 
       
   464     QCOMPARE(spy.value(0).value(1).toInt(), 0);
       
   465     QCOMPARE(spy.value(0).value(2).toInt(), rowCount - 1);
       
   466 
       
   467     // If the driver doesn't return the query size fetchMore() causes the
       
   468     // model to grow and new signals are emitted
       
   469     if (!db.driver()->hasFeature(QSqlDriver::QuerySize)) {
       
   470         spy.clear();
       
   471         model.fetchMore();
       
   472         int newRowCount = model.rowCount();
       
   473         QCOMPARE(spy.value(0).value(1).toInt(), rowCount);
       
   474         QCOMPARE(spy.value(0).value(2).toInt(), newRowCount - 1);
       
   475     }
       
   476 }
       
   477 
       
   478 // For task 149491: When used with QSortFilterProxyModel, a view and a
       
   479 // database that doesn't support the QuerySize feature, blank rows was
       
   480 // appended if the query returned more than 256 rows and setQuery()
       
   481 // was called more than once. This because an insertion of rows was
       
   482 // triggered at the same time as the model was being cleared.
       
   483 void tst_QSqlQueryModel::withSortFilterProxyModel()
       
   484 {
       
   485     QFETCH(QString, dbName);
       
   486     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   487     CHECK_DATABASE(db);
       
   488 
       
   489     if (db.driver()->hasFeature(QSqlDriver::QuerySize))
       
   490         QSKIP("Test applies only for drivers not reporting the query size.", SkipSingle);
       
   491 
       
   492     QSqlQueryModel model;
       
   493     model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test3"), db));
       
   494     QSortFilterProxyModel proxy;
       
   495     proxy.setSourceModel(&model);
       
   496 
       
   497     QTableView view;
       
   498     view.setModel(&proxy);
       
   499 
       
   500     QSignalSpy modelRowsRemovedSpy(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)));
       
   501     QSignalSpy modelRowsInsertedSpy(&model, SIGNAL(rowsInserted(const QModelIndex &, int, int)));
       
   502     model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test3"), db));
       
   503     view.scrollToBottom();
       
   504 
       
   505     QTestEventLoop::instance().enterLoop(1);
       
   506 
       
   507     QCOMPARE(proxy.rowCount(), 511);
       
   508 
       
   509     // The second call to setQuery() clears the model by removing it's rows.
       
   510     // Only 256 rows because that is what was cached.
       
   511     QCOMPARE(modelRowsRemovedSpy.count(), 1);
       
   512     QCOMPARE(modelRowsRemovedSpy.value(0).value(1).toInt(), 0);
       
   513     QCOMPARE(modelRowsRemovedSpy.value(0).value(2).toInt(), 255);
       
   514 
       
   515     // The call to scrollToBottom() forces the model to fetch all rows,
       
   516     // which will be done in two steps.
       
   517     QCOMPARE(modelRowsInsertedSpy.count(), 2);
       
   518     QCOMPARE(modelRowsInsertedSpy.value(0).value(1).toInt(), 0);
       
   519     QCOMPARE(modelRowsInsertedSpy.value(0).value(2).toInt(), 255);
       
   520     QCOMPARE(modelRowsInsertedSpy.value(1).value(1).toInt(), 256);
       
   521     QCOMPARE(modelRowsInsertedSpy.value(1).value(2).toInt(), 510);
       
   522 }
       
   523 
       
   524 // For task 155402: When the model is already empty when setQuery() is called
       
   525 // no rows have to be removed and rowsAboutToBeRemoved and rowsRemoved should
       
   526 // not be emitted.
       
   527 void tst_QSqlQueryModel::setQuerySignalEmission()
       
   528 {
       
   529     QFETCH(QString, dbName);
       
   530     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   531     CHECK_DATABASE(db);
       
   532 
       
   533     QSqlQueryModel model;
       
   534     QSignalSpy modelRowsAboutToBeRemovedSpy(&model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)));
       
   535     QSignalSpy modelRowsRemovedSpy(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)));
       
   536 
       
   537     // First select, the model was empty and no rows had to be removed!
       
   538     model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test"), db));
       
   539     QCOMPARE(modelRowsAboutToBeRemovedSpy.count(), 0);
       
   540     QCOMPARE(modelRowsRemovedSpy.count(), 0);
       
   541 
       
   542     // Second select, the model wasn't empty and two rows had to be removed!
       
   543     model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test"), db));
       
   544     QCOMPARE(modelRowsAboutToBeRemovedSpy.count(), 1);
       
   545     QCOMPARE(modelRowsAboutToBeRemovedSpy.value(0).value(1).toInt(), 0);
       
   546     QCOMPARE(modelRowsAboutToBeRemovedSpy.value(0).value(2).toInt(), 1);
       
   547     QCOMPARE(modelRowsRemovedSpy.count(), 1);
       
   548     QCOMPARE(modelRowsRemovedSpy.value(0).value(1).toInt(), 0);
       
   549     QCOMPARE(modelRowsRemovedSpy.value(0).value(2).toInt(), 1);
       
   550 }
       
   551 
       
   552 // For task 170783: When the query's result set is empty no rows should be inserted,
       
   553 // i.e. no rowsAboutToBeInserted or rowsInserted signals should be emitted.
       
   554 void tst_QSqlQueryModel::setQueryWithNoRowsInResultSet()
       
   555 {
       
   556     QFETCH(QString, dbName);
       
   557     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   558     CHECK_DATABASE(db);
       
   559 
       
   560     QSqlQueryModel model;
       
   561     QSignalSpy modelRowsAboutToBeInsertedSpy(&model, SIGNAL(rowsAboutToBeInserted(const QModelIndex &, int, int)));
       
   562     QSignalSpy modelRowsInsertedSpy(&model, SIGNAL(rowsInserted(const QModelIndex &, int, int)));
       
   563 
       
   564     // The query's result set will be empty so no signals should be emitted!
       
   565     QSqlQuery query(db);
       
   566     QVERIFY_SQL(query, exec("SELECT * FROM " + qTableName("test") + " where 0 = 1"));
       
   567     model.setQuery(query);
       
   568     QCOMPARE(modelRowsAboutToBeInsertedSpy.count(), 0);
       
   569     QCOMPARE(modelRowsInsertedSpy.count(), 0);
       
   570 }
       
   571 
       
   572 // For task 180617
       
   573 // According to the task, several specific duplicate SQL queries would cause
       
   574 // multiple empty grid lines to be visible in the view
       
   575 void tst_QSqlQueryModel::task_180617()
       
   576 {
       
   577     QFETCH(QString, dbName);
       
   578     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   579     CHECK_DATABASE(db);
       
   580 
       
   581     QTableView view;
       
   582     QCOMPARE(view.columnAt(0), -1);
       
   583     QCOMPARE(view.rowAt(0), -1);
       
   584 
       
   585     QSqlQueryModel model;
       
   586     model.setQuery( "SELECT TOP 0 * FROM " + qTableName("test3"), db );
       
   587     view.setModel(&model);
       
   588 
       
   589     bool error = false;
       
   590     // Usually a syntax error
       
   591     if (model.lastError().isValid())    // usually a syntax error
       
   592         error = true;
       
   593 
       
   594     QCOMPARE(view.columnAt(0), (error)?-1:0 );
       
   595     QCOMPARE(view.rowAt(0), -1);
       
   596 
       
   597     model.setQuery( "SELECT TOP 0 * FROM " + qTableName("test3"), db );
       
   598     model.setQuery( "SELECT TOP 0 * FROM " + qTableName("test3"), db );
       
   599     model.setQuery( "SELECT TOP 0 * FROM " + qTableName("test3"), db );
       
   600     model.setQuery( "SELECT TOP 0 * FROM " + qTableName("test3"), db );
       
   601 
       
   602     QCOMPARE(view.columnAt(0),  (error)?-1:0 );
       
   603     QCOMPARE(view.rowAt(0), -1);
       
   604 }
       
   605 
       
   606 QTEST_MAIN(tst_QSqlQueryModel)
       
   607 #include "tst_qsqlquerymodel.moc"