tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 "../qsqldatabase/tst_databases.h"
       
    45 #include <QtSql>
       
    46 
       
    47 //TESTED_CLASS=
       
    48 //TESTED_FILES=
       
    49 
       
    50 Q_DECLARE_METATYPE(QModelIndex)
       
    51 
       
    52 class tst_QSqlTableModel : public QObject
       
    53 {
       
    54     Q_OBJECT
       
    55 
       
    56 public:
       
    57     tst_QSqlTableModel();
       
    58     virtual ~tst_QSqlTableModel();
       
    59 
       
    60 
       
    61     void dropTestTables();
       
    62     void createTestTables();
       
    63     void recreateTestTables();
       
    64     void repopulateTestTables();
       
    65 
       
    66     tst_Databases dbs;
       
    67 
       
    68 public slots:
       
    69     void initTestCase();
       
    70     void cleanupTestCase();
       
    71     void init();
       
    72     void cleanup();
       
    73 private slots:
       
    74 
       
    75     void select_data() { generic_data(); }
       
    76     void select();
       
    77     void submitAll_data() { generic_data(); }
       
    78     void submitAll();
       
    79     void setRecord_data()  { generic_data(); }
       
    80     void setRecord();
       
    81     void insertRow_data() { generic_data(); }
       
    82     void insertRow();
       
    83     void insertRecord_data() { generic_data(); }
       
    84     void insertRecord();
       
    85     void insertMultiRecords_data() { generic_data(); }
       
    86     void insertMultiRecords();
       
    87     void removeRow_data() { generic_data(); }
       
    88     void removeRow();
       
    89     void removeRows_data() { generic_data(); }
       
    90     void removeRows();
       
    91     void removeInsertedRow_data() { generic_data(); }
       
    92     void removeInsertedRow();
       
    93     void setFilter_data() { generic_data(); }
       
    94     void setFilter();
       
    95     void setInvalidFilter_data() { generic_data(); }
       
    96     void setInvalidFilter();
       
    97 
       
    98     void emptyTable_data() { generic_data(); }
       
    99     void emptyTable();
       
   100     void tablesAndSchemas_data() { generic_data("QPSQL"); }
       
   101     void tablesAndSchemas();
       
   102     void whitespaceInIdentifiers_data() { generic_data(); }
       
   103     void whitespaceInIdentifiers();
       
   104     void primaryKeyOrder_data() { generic_data("QSQLITE"); }
       
   105     void primaryKeyOrder();
       
   106 
       
   107     void sqlite_bigTable_data() { generic_data("QSQLITE"); }
       
   108     void sqlite_bigTable();
       
   109 
       
   110     // bug specific tests
       
   111     void insertRecordBeforeSelect_data() { generic_data(); }
       
   112     void insertRecordBeforeSelect();
       
   113     void submitAllOnInvalidTable_data() { generic_data(); }
       
   114     void submitAllOnInvalidTable();
       
   115     void insertRecordsInLoop_data() { generic_data(); }
       
   116     void insertRecordsInLoop();
       
   117     void sqlite_attachedDatabase_data() { generic_data("QSQLITE"); }
       
   118     void sqlite_attachedDatabase(); // For task 130799
       
   119     void tableModifyWithBlank_data() { generic_data(); }
       
   120     void tableModifyWithBlank(); // For mail task
       
   121 
       
   122     void removeColumnAndRow_data() { generic_data(); }
       
   123     void removeColumnAndRow(); // task 256032
       
   124 
       
   125     void insertBeforeDelete_data() { generic_data(); }
       
   126     void insertBeforeDelete();
       
   127 private:
       
   128     void generic_data(const QString& engine=QString());
       
   129 };
       
   130 
       
   131 tst_QSqlTableModel::tst_QSqlTableModel()
       
   132 {
       
   133     qRegisterMetaType<QModelIndex>("QModelIndex");
       
   134     dbs.open();
       
   135 }
       
   136 
       
   137 tst_QSqlTableModel::~tst_QSqlTableModel()
       
   138 {
       
   139 }
       
   140 
       
   141 void tst_QSqlTableModel::dropTestTables()
       
   142 {
       
   143     for (int i = 0; i < dbs.dbNames.count(); ++i) {
       
   144         QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i));
       
   145         QSqlQuery q(db);
       
   146         if(tst_Databases::isPostgreSQL(db))
       
   147             QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
       
   148 
       
   149         QStringList tableNames;
       
   150         tableNames << qTableName("test")
       
   151                    << qTableName("test2")
       
   152                    << qTableName("test3")
       
   153                    << qTableName("test4")
       
   154                    << qTableName("emptytable")
       
   155                    << qTableName("bigtable")
       
   156                    << qTableName("foo");
       
   157         if (testWhiteSpaceNames(db.driverName()))
       
   158             tableNames << qTableName("qtestw hitespace", db.driver());
       
   159 
       
   160         tst_Databases::safeDropTables(db, tableNames);
       
   161 
       
   162         if (db.driverName().startsWith("QPSQL")) {
       
   163             q.exec("DROP SCHEMA " + qTableName("testschema") + " CASCADE");
       
   164         }
       
   165     }
       
   166 }
       
   167 
       
   168 void tst_QSqlTableModel::createTestTables()
       
   169 {
       
   170     for (int i = 0; i < dbs.dbNames.count(); ++i) {
       
   171         QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i));
       
   172         QSqlQuery q(db);
       
   173 
       
   174         QVERIFY_SQL( q, exec("create table " + qTableName("test") + "(id int, name varchar(20), title int)"));
       
   175 
       
   176         QVERIFY_SQL( q, exec("create table " + qTableName("test2") + "(id int, title varchar(20))"));
       
   177 
       
   178         QVERIFY_SQL( q, exec("create table " + qTableName("test3") + "(id int, random varchar(20), randomtwo varchar(20))"));
       
   179 
       
   180         QVERIFY_SQL( q, exec("create table " + qTableName("test4") + "(column1 varchar(50), column2 varchar(50), column3 varchar(50))"));
       
   181 
       
   182         QVERIFY_SQL( q, exec("create table " + qTableName("emptytable") + "(id int)"));
       
   183 
       
   184         if (testWhiteSpaceNames(db.driverName())) {
       
   185             QString qry = "create table " + qTableName("qtestw hitespace", db.driver()) + " ("+ db.driver()->escapeIdentifier("a field", QSqlDriver::FieldName) + " int)";
       
   186             QVERIFY_SQL( q, exec(qry));
       
   187         }
       
   188     }
       
   189 }
       
   190 
       
   191 void tst_QSqlTableModel::repopulateTestTables()
       
   192 {
       
   193     for (int i = 0; i < dbs.dbNames.count(); ++i) {
       
   194         QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i));
       
   195         QSqlQuery q(db);
       
   196 
       
   197         q.exec("delete from " + qTableName("test"));
       
   198         QVERIFY_SQL( q, exec("insert into " + qTableName("test") + " values(1, 'harry', 1)"));
       
   199         QVERIFY_SQL( q, exec("insert into " + qTableName("test") + " values(2, 'trond', 2)"));
       
   200         QVERIFY_SQL( q, exec("insert into " + qTableName("test") + " values(3, 'vohi', 3)"));
       
   201 
       
   202         q.exec("delete from " + qTableName("test2"));
       
   203         QVERIFY_SQL( q, exec("insert into " + qTableName("test2") + " values(1, 'herr')"));
       
   204         QVERIFY_SQL( q, exec("insert into " + qTableName("test2") + " values(2, 'mister')"));
       
   205 
       
   206         q.exec("delete from " + qTableName("test3"));
       
   207         QVERIFY_SQL( q, exec("insert into " + qTableName("test3") + " values(1, 'foo', 'bar')"));
       
   208         QVERIFY_SQL( q, exec("insert into " + qTableName("test3") + " values(2, 'baz', 'joe')"));
       
   209     }
       
   210 }
       
   211 
       
   212 void tst_QSqlTableModel::recreateTestTables()
       
   213 {
       
   214     dropTestTables();
       
   215     createTestTables();
       
   216     repopulateTestTables();
       
   217 }
       
   218 
       
   219 void tst_QSqlTableModel::generic_data(const QString &engine)
       
   220 {
       
   221     if ( dbs.fillTestTable(engine) == 0 ) {
       
   222         if(engine.isEmpty())
       
   223            QSKIP( "No database drivers are available in this Qt configuration", SkipAll );
       
   224         else
       
   225            QSKIP( (QString("No database drivers of type %1 are available in this Qt configuration").arg(engine)).toLocal8Bit(), SkipAll );
       
   226     }
       
   227 }
       
   228 
       
   229 void tst_QSqlTableModel::initTestCase()
       
   230 {
       
   231     recreateTestTables();
       
   232 }
       
   233 
       
   234 void tst_QSqlTableModel::cleanupTestCase()
       
   235 {
       
   236     dropTestTables();
       
   237     dbs.close();
       
   238 }
       
   239 
       
   240 void tst_QSqlTableModel::init()
       
   241 {
       
   242 }
       
   243 
       
   244 void tst_QSqlTableModel::cleanup()
       
   245 {
       
   246     repopulateTestTables();
       
   247 }
       
   248 
       
   249 void tst_QSqlTableModel::select()
       
   250 {
       
   251     QFETCH(QString, dbName);
       
   252     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   253     CHECK_DATABASE(db);
       
   254 
       
   255     QSqlTableModel model(0, db);
       
   256     model.setTable(qTableName("test"));
       
   257     model.setSort(0, Qt::AscendingOrder);
       
   258     QVERIFY_SQL(model, select());
       
   259 
       
   260     QCOMPARE(model.rowCount(), 3);
       
   261     QCOMPARE(model.columnCount(), 3);
       
   262 
       
   263     QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
       
   264     QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
       
   265     QCOMPARE(model.data(model.index(0, 2)).toInt(), 1);
       
   266     QCOMPARE(model.data(model.index(0, 3)), QVariant());
       
   267 
       
   268     QCOMPARE(model.data(model.index(1, 0)).toInt(), 2);
       
   269     QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond"));
       
   270     QCOMPARE(model.data(model.index(1, 2)).toInt(), 2);
       
   271     QCOMPARE(model.data(model.index(1, 3)), QVariant());
       
   272 
       
   273     QCOMPARE(model.data(model.index(2, 0)).toInt(), 3);
       
   274     QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi"));
       
   275     QCOMPARE(model.data(model.index(2, 2)).toInt(), 3);
       
   276     QCOMPARE(model.data(model.index(2, 3)), QVariant());
       
   277 
       
   278     QCOMPARE(model.data(model.index(3, 0)), QVariant());
       
   279     QCOMPARE(model.data(model.index(3, 1)), QVariant());
       
   280     QCOMPARE(model.data(model.index(3, 2)), QVariant());
       
   281     QCOMPARE(model.data(model.index(3, 3)), QVariant());
       
   282 }
       
   283 
       
   284 void tst_QSqlTableModel::setRecord()
       
   285 {
       
   286     QFETCH(QString, dbName);
       
   287     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   288     CHECK_DATABASE(db);
       
   289 
       
   290     QList<QSqlTableModel::EditStrategy> policies = QList<QSqlTableModel::EditStrategy>() << QSqlTableModel::OnFieldChange << QSqlTableModel::OnRowChange << QSqlTableModel::OnManualSubmit;
       
   291 
       
   292     QString Xsuffix;
       
   293     foreach( QSqlTableModel::EditStrategy submitpolicy, policies) {
       
   294 
       
   295         QSqlTableModel model(0, db);
       
   296         model.setEditStrategy((QSqlTableModel::EditStrategy)submitpolicy);
       
   297         model.setTable(qTableName("test3"));
       
   298         model.setSort(0, Qt::AscendingOrder);
       
   299         QVERIFY_SQL(model, select());
       
   300 
       
   301         for (int i = 0; i < model.rowCount(); ++i) {
       
   302             QSignalSpy spy(&model, SIGNAL(dataChanged(QModelIndex,QModelIndex)));
       
   303 
       
   304             QSqlRecord rec = model.record(i);
       
   305             rec.setValue(1, rec.value(1).toString() + 'X');
       
   306             rec.setValue(2, rec.value(2).toString() + 'X');
       
   307             QVERIFY(model.setRecord(i, rec));
       
   308 
       
   309             if ((QSqlTableModel::EditStrategy)submitpolicy == QSqlTableModel::OnManualSubmit)
       
   310                 QVERIFY(model.submitAll());
       
   311             else if ((QSqlTableModel::EditStrategy)submitpolicy == QSqlTableModel::OnRowChange && i == model.rowCount() -1)
       
   312                 model.submit();
       
   313             else {
       
   314                 // dataChanged() is not emitted when submitAll() is called
       
   315                 QCOMPARE(spy.count(), 2);
       
   316                 QCOMPARE(spy.at(0).count(), 2);
       
   317                 QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(0)), model.index(i, 1));
       
   318                 QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(1)), model.index(i, 1));
       
   319             }
       
   320         }
       
   321 
       
   322         Xsuffix.append('X');
       
   323 
       
   324         QCOMPARE(model.data(model.index(0, 1)).toString(), QString("foo").append(Xsuffix));
       
   325         QCOMPARE(model.data(model.index(0, 2)).toString(), QString("bar").append(Xsuffix));
       
   326         QCOMPARE(model.data(model.index(1, 1)).toString(), QString("baz").append(Xsuffix));
       
   327         QCOMPARE(model.data(model.index(1, 2)).toString(), QString("joe").append(Xsuffix));
       
   328     }
       
   329 }
       
   330 
       
   331 void tst_QSqlTableModel::insertRow()
       
   332 {
       
   333     QFETCH(QString, dbName);
       
   334     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   335     CHECK_DATABASE(db);
       
   336 
       
   337     QSqlTableModel model(0, db);
       
   338     model.setEditStrategy(QSqlTableModel::OnRowChange);
       
   339     model.setTable(qTableName("test"));
       
   340     model.setSort(0, Qt::AscendingOrder);
       
   341     QVERIFY_SQL(model, select());
       
   342 
       
   343     QVERIFY(model.insertRow(2));
       
   344     QSqlRecord rec = model.record(1);
       
   345     rec.setValue(0, 42);
       
   346     rec.setValue(1, QString("vohi"));
       
   347     QVERIFY(model.setRecord(2, rec));
       
   348 
       
   349     QCOMPARE(model.data(model.index(2, 0)).toInt(), 42);
       
   350     QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi"));
       
   351     QCOMPARE(model.data(model.index(2, 2)).toInt(), 2);
       
   352 
       
   353     QVERIFY(model.submitAll());
       
   354 }
       
   355 
       
   356 void tst_QSqlTableModel::insertRecord()
       
   357 {
       
   358     QFETCH(QString, dbName);
       
   359     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   360     CHECK_DATABASE(db);
       
   361 
       
   362     QSqlTableModel model(0, db);
       
   363     model.setEditStrategy(QSqlTableModel::OnManualSubmit);
       
   364     model.setTable(qTableName("test"));
       
   365     model.setSort(0, Qt::AscendingOrder);
       
   366     QVERIFY_SQL(model, select());
       
   367 
       
   368     QSqlRecord rec = model.record();
       
   369     rec.setValue(0, 42);
       
   370     rec.setValue(1, QString("vohi"));
       
   371     rec.setValue(2, 1);
       
   372     QVERIFY(model.insertRecord(1, rec));
       
   373     QCOMPARE(model.rowCount(), 4);
       
   374 
       
   375     QCOMPARE(model.data(model.index(1, 0)).toInt(), 42);
       
   376     QCOMPARE(model.data(model.index(1, 1)).toString(), QString("vohi"));
       
   377     QCOMPARE(model.data(model.index(1, 2)).toInt(), 1);
       
   378 
       
   379     model.revertAll();
       
   380     model.setEditStrategy(QSqlTableModel::OnRowChange);
       
   381 
       
   382     QVERIFY(model.insertRecord(-1, rec));
       
   383 
       
   384     QCOMPARE(model.data(model.index(3, 0)).toInt(), 42);
       
   385     QCOMPARE(model.data(model.index(3, 1)).toString(), QString("vohi"));
       
   386     QCOMPARE(model.data(model.index(3, 2)).toInt(), 1);
       
   387 }
       
   388 
       
   389 void tst_QSqlTableModel::insertMultiRecords()
       
   390 {
       
   391     QFETCH(QString, dbName);
       
   392     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   393     CHECK_DATABASE(db);
       
   394 
       
   395     QSqlTableModel model(0, db);
       
   396     model.setEditStrategy(QSqlTableModel::OnManualSubmit);
       
   397     model.setTable(qTableName("test"));
       
   398     model.setSort(0, Qt::AscendingOrder);
       
   399     QVERIFY_SQL(model, select());
       
   400 
       
   401     QCOMPARE(model.rowCount(), 3);
       
   402 
       
   403     QVERIFY(model.insertRow(2));
       
   404 
       
   405     QCOMPARE(model.data(model.index(2, 0)), QVariant());
       
   406     QCOMPARE(model.data(model.index(2, 1)), QVariant());
       
   407     QCOMPARE(model.data(model.index(2, 2)), QVariant());
       
   408 
       
   409     QVERIFY(model.insertRow(3));
       
   410     QVERIFY(model.insertRow(0));
       
   411 
       
   412     QCOMPARE(model.data(model.index(5, 0)).toInt(), 3);
       
   413     QCOMPARE(model.data(model.index(5, 1)).toString(), QString("vohi"));
       
   414     QCOMPARE(model.data(model.index(5, 2)).toInt(), 3);
       
   415 
       
   416     QVERIFY(model.setData(model.index(0, 0), QVariant(42)));
       
   417     QVERIFY(model.setData(model.index(3, 0), QVariant(43)));
       
   418     QVERIFY(model.setData(model.index(4, 0), QVariant(44)));
       
   419     QVERIFY(model.setData(model.index(4, 1), QVariant(QLatin1String("gunnar"))));
       
   420     QVERIFY(model.setData(model.index(4, 2), QVariant(1)));
       
   421 
       
   422     QVERIFY(model.submitAll());
       
   423     model.clear();
       
   424     model.setTable(qTableName("test"));
       
   425     model.setSort(0, Qt::AscendingOrder);
       
   426     QVERIFY_SQL(model, select());
       
   427 
       
   428     QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
       
   429     QCOMPARE(model.data(model.index(1, 0)).toInt(), 2);
       
   430     QCOMPARE(model.data(model.index(2, 0)).toInt(), 3);
       
   431     QCOMPARE(model.data(model.index(3, 0)).toInt(), 42);
       
   432     QCOMPARE(model.data(model.index(4, 0)).toInt(), 43);
       
   433     QCOMPARE(model.data(model.index(5, 0)).toInt(), 44);
       
   434     QCOMPARE(model.data(model.index(5, 1)).toString(), QString("gunnar"));
       
   435     QCOMPARE(model.data(model.index(5, 2)).toInt(), 1);
       
   436 }
       
   437 
       
   438 void tst_QSqlTableModel::submitAll()
       
   439 {
       
   440     QFETCH(QString, dbName);
       
   441     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   442     CHECK_DATABASE(db);
       
   443 
       
   444     QSqlTableModel model(0, db);
       
   445     model.setTable(qTableName("test"));
       
   446     model.setSort(0, Qt::AscendingOrder);
       
   447     model.setEditStrategy(QSqlTableModel::OnManualSubmit);
       
   448     QVERIFY_SQL(model, select());
       
   449 
       
   450     QVERIFY(model.setData(model.index(0, 1), "harry2", Qt::EditRole));
       
   451     QVERIFY(model.setData(model.index(1, 1), "trond2", Qt::EditRole));
       
   452 
       
   453     QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2"));
       
   454     QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond2"));
       
   455 
       
   456     QVERIFY_SQL(model, submitAll());
       
   457 
       
   458     QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2"));
       
   459     QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond2"));
       
   460 
       
   461     QVERIFY(model.setData(model.index(0, 1), "harry", Qt::EditRole));
       
   462     QVERIFY(model.setData(model.index(1, 1), "trond", Qt::EditRole));
       
   463 
       
   464     QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
       
   465     QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond"));
       
   466 
       
   467     QVERIFY_SQL(model, submitAll());
       
   468 
       
   469     QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
       
   470     QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond"));
       
   471 }
       
   472 
       
   473 void tst_QSqlTableModel::removeRow()
       
   474 {
       
   475     QFETCH(QString, dbName);
       
   476     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   477     CHECK_DATABASE(db);
       
   478 
       
   479     QSqlTableModel model(0, db);
       
   480     model.setTable(qTableName("test"));
       
   481     model.setSort(0, Qt::AscendingOrder);
       
   482     model.setEditStrategy(QSqlTableModel::OnManualSubmit);
       
   483     QVERIFY_SQL(model, select());
       
   484     QCOMPARE(model.rowCount(), 3);
       
   485 
       
   486     // headerDataChanged must be emitted by the model when the edit strategy is OnManualSubmit,
       
   487     // when OnFieldChange or OnRowChange it's not needed because the model will re-select.
       
   488     qRegisterMetaType<Qt::Orientation>("Qt::Orientation");
       
   489     QSignalSpy headerDataChangedSpy(&model, SIGNAL(headerDataChanged(Qt::Orientation, int, int)));
       
   490 
       
   491     QVERIFY(model.removeRow(1));
       
   492     QCOMPARE(headerDataChangedSpy.count(), 1);
       
   493     QCOMPARE(*static_cast<const Qt::Orientation *>(headerDataChangedSpy.at(0).value(0).constData()), Qt::Vertical);
       
   494     QCOMPARE(headerDataChangedSpy.at(0).at(1).toInt(), 1);
       
   495     QCOMPARE(headerDataChangedSpy.at(0).at(2).toInt(), 1);
       
   496     QVERIFY(model.submitAll());
       
   497     QCOMPARE(model.rowCount(), 2);
       
   498 
       
   499     QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
       
   500     QCOMPARE(model.data(model.index(1, 0)).toInt(), 3);
       
   501     model.clear();
       
   502 
       
   503     recreateTestTables();
       
   504 
       
   505     model.setTable(qTableName("test"));
       
   506     model.setEditStrategy(QSqlTableModel::OnRowChange);
       
   507     QVERIFY_SQL(model, select());
       
   508     QCOMPARE(model.rowCount(), 3);
       
   509 
       
   510     headerDataChangedSpy.clear();
       
   511     QVERIFY(model.removeRow(1));
       
   512     QCOMPARE(headerDataChangedSpy.count(), 0);
       
   513     QCOMPARE(model.rowCount(), 2);
       
   514 
       
   515     QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
       
   516     QCOMPARE(model.data(model.index(1, 1)).toString(), QString("vohi"));
       
   517 }
       
   518 
       
   519 void tst_QSqlTableModel::removeRows()
       
   520 {
       
   521     QFETCH(QString, dbName);
       
   522     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   523     CHECK_DATABASE(db);
       
   524 
       
   525     QSqlTableModel model(0, db);
       
   526     model.setTable(qTableName("test"));
       
   527     model.setSort(0, Qt::AscendingOrder);
       
   528     model.setEditStrategy(QSqlTableModel::OnFieldChange);
       
   529     QVERIFY_SQL(model, select());
       
   530     QCOMPARE(model.rowCount(), 3);
       
   531 
       
   532     QSignalSpy beforeDeleteSpy(&model, SIGNAL(beforeDelete(int)));
       
   533     QVERIFY_SQL(model, removeRows(0, 2));
       
   534     QVERIFY(beforeDeleteSpy.count() == 2);
       
   535     QVERIFY(beforeDeleteSpy.at(0).at(0).toInt() == 0);
       
   536     QVERIFY(beforeDeleteSpy.at(1).at(0).toInt() == 1);
       
   537     QCOMPARE(model.rowCount(), 1);
       
   538     QCOMPARE(model.data(model.index(0, 1)).toString(), QString("vohi"));
       
   539     model.clear();
       
   540 
       
   541     recreateTestTables();
       
   542     model.setTable(qTableName("test"));
       
   543     model.setEditStrategy(QSqlTableModel::OnManualSubmit);
       
   544     QVERIFY_SQL(model, select());
       
   545     QCOMPARE(model.rowCount(), 3);
       
   546     beforeDeleteSpy.clear();
       
   547 
       
   548     // When the edit strategy is OnManualSubmit the beforeDelete() signal
       
   549     // isn't emitted until submitAll() is called.
       
   550     qRegisterMetaType<Qt::Orientation>("Qt::Orientation");
       
   551     QSignalSpy headerDataChangedSpy(&model, SIGNAL(headerDataChanged(Qt::Orientation, int, int)));
       
   552     QVERIFY(model.removeRows(0, 2, QModelIndex()));
       
   553     QCOMPARE(headerDataChangedSpy.count(), 2);
       
   554     QCOMPARE(headerDataChangedSpy.at(0).at(1).toInt(), 0);
       
   555     QCOMPARE(headerDataChangedSpy.at(0).at(2).toInt(), 0);
       
   556     QCOMPARE(headerDataChangedSpy.at(1).at(1).toInt(), 1);
       
   557     QCOMPARE(headerDataChangedSpy.at(1).at(2).toInt(), 1);
       
   558     QCOMPARE(model.rowCount(), 3);
       
   559     QVERIFY(beforeDeleteSpy.count() == 0);
       
   560     QVERIFY(model.submitAll());
       
   561     QVERIFY(beforeDeleteSpy.count() == 2);
       
   562     QVERIFY(beforeDeleteSpy.at(0).at(0).toInt() == 0);
       
   563     QVERIFY(beforeDeleteSpy.at(1).at(0).toInt() == 1);
       
   564     QCOMPARE(model.rowCount(), 1);
       
   565     QCOMPARE(model.data(model.index(0, 1)).toString(), QString("vohi"));
       
   566 }
       
   567 
       
   568 void tst_QSqlTableModel::removeInsertedRow()
       
   569 {
       
   570     QFETCH(QString, dbName);
       
   571     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   572     CHECK_DATABASE(db);
       
   573 
       
   574     for (int i = 0; i <= 1; ++i) {
       
   575 
       
   576         QSqlTableModel model(0, db);
       
   577         model.setTable(qTableName("test"));
       
   578         model.setSort(0, Qt::AscendingOrder);
       
   579 
       
   580         model.setEditStrategy(i == 0
       
   581                 ? QSqlTableModel::OnRowChange : QSqlTableModel::OnManualSubmit);
       
   582         QVERIFY_SQL(model, select());
       
   583         QCOMPARE(model.rowCount(), 3);
       
   584 
       
   585         QVERIFY(model.insertRow(1));
       
   586         QCOMPARE(model.rowCount(), 4);
       
   587 
       
   588         QVERIFY(model.removeRow(1));
       
   589         QCOMPARE(model.rowCount(), 3);
       
   590 
       
   591         QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
       
   592         QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond"));
       
   593         QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi"));
       
   594         model.clear();
       
   595 
       
   596         recreateTestTables();
       
   597     }
       
   598 }
       
   599 
       
   600 void tst_QSqlTableModel::emptyTable()
       
   601 {
       
   602     QFETCH(QString, dbName);
       
   603     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   604     CHECK_DATABASE(db);
       
   605 
       
   606     QSqlTableModel model(0, db);
       
   607     QCOMPARE(model.rowCount(), 0);
       
   608     QCOMPARE(model.columnCount(), 0);
       
   609 
       
   610     model.setTable(qTableName("emptytable"));
       
   611     QCOMPARE(model.rowCount(), 0);
       
   612     QCOMPARE(model.columnCount(), 1);
       
   613 
       
   614     QVERIFY_SQL(model, select());
       
   615     QCOMPARE(model.rowCount(), 0);
       
   616     QCOMPARE(model.columnCount(), 1);
       
   617 }
       
   618 
       
   619 void tst_QSqlTableModel::tablesAndSchemas()
       
   620 {
       
   621     QFETCH(QString, dbName);
       
   622     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   623     CHECK_DATABASE(db);
       
   624 
       
   625     QSqlQuery q(db);
       
   626     q.exec("DROP SCHEMA " + qTableName("testschema") + " CASCADE");
       
   627     QVERIFY_SQL( q, exec("create schema " + qTableName("testschema")));
       
   628     QString tableName = qTableName("testschema") + '.' + qTableName("testtable");
       
   629     QVERIFY_SQL( q, exec("create table " + tableName + "(id int)"));
       
   630     QVERIFY_SQL( q, exec("insert into " + tableName + " values(1)"));
       
   631     QVERIFY_SQL( q, exec("insert into " + tableName + " values(2)"));
       
   632 
       
   633     QSqlTableModel model(0, db);
       
   634     model.setTable(tableName);
       
   635     QVERIFY_SQL(model, select());
       
   636     QCOMPARE(model.rowCount(), 2);
       
   637     QCOMPARE(model.columnCount(), 1);
       
   638 }
       
   639 
       
   640 void tst_QSqlTableModel::whitespaceInIdentifiers()
       
   641 {
       
   642     QFETCH(QString, dbName);
       
   643     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   644     CHECK_DATABASE(db);
       
   645 
       
   646     if (!testWhiteSpaceNames(db.driverName()))
       
   647         QSKIP("DBMS doesn't support whitespaces in identifiers", SkipSingle);
       
   648 
       
   649     QString tableName = qTableName("qtestw hitespace", db.driver());
       
   650 
       
   651     QSqlTableModel model(0, db);
       
   652     model.setTable(tableName);
       
   653     QVERIFY_SQL(model, select());
       
   654 }
       
   655 
       
   656 void tst_QSqlTableModel::primaryKeyOrder()
       
   657 {
       
   658     QFETCH(QString, dbName);
       
   659     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   660     CHECK_DATABASE(db);
       
   661 
       
   662     QSqlQuery q(db);
       
   663 
       
   664     if(tst_Databases::isPostgreSQL(db))
       
   665         QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
       
   666 
       
   667     QVERIFY_SQL( q, exec("create table "+qTableName("foo")+"(a varchar(20), id int not null primary key, b varchar(20))"));
       
   668 
       
   669     QSqlTableModel model(0, db);
       
   670     model.setTable(qTableName("foo"));
       
   671 
       
   672     QSqlIndex pk = model.primaryKey();
       
   673     QCOMPARE(pk.count(), 1);
       
   674     QCOMPARE(pk.fieldName(0), QLatin1String("id"));
       
   675 
       
   676     QVERIFY(model.insertRow(0));
       
   677     QVERIFY(model.setData(model.index(0, 0), "hello"));
       
   678     QVERIFY(model.setData(model.index(0, 1), 42));
       
   679     QVERIFY(model.setData(model.index(0, 2), "blah"));
       
   680     QVERIFY_SQL(model, submitAll());
       
   681 
       
   682     QVERIFY(model.setData(model.index(0, 1), 43));
       
   683     QVERIFY_SQL(model, submitAll());
       
   684 
       
   685     QCOMPARE(model.data(model.index(0, 1)).toInt(), 43);
       
   686 }
       
   687 
       
   688 void tst_QSqlTableModel::setInvalidFilter()
       
   689 {
       
   690     QFETCH(QString, dbName);
       
   691     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   692     CHECK_DATABASE(db);
       
   693 
       
   694     // set an invalid filter, make sure it fails
       
   695     QSqlTableModel model(0, db);
       
   696     model.setTable(qTableName("test"));
       
   697     model.setFilter("blahfahsel");
       
   698 
       
   699     QCOMPARE(model.filter(), QString("blahfahsel"));
       
   700     QVERIFY(!model.select());
       
   701 
       
   702     // set a valid filter later, make sure if passes
       
   703     model.setFilter("id = 1");
       
   704     QVERIFY_SQL(model, select());
       
   705 }
       
   706 
       
   707 void tst_QSqlTableModel::setFilter()
       
   708 {
       
   709     QFETCH(QString, dbName);
       
   710     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   711     CHECK_DATABASE(db);
       
   712 
       
   713     QSqlTableModel model(0, db);
       
   714     model.setTable(qTableName("test"));
       
   715     model.setFilter("id = 1");
       
   716     QCOMPARE(model.filter(), QString("id = 1"));
       
   717     QVERIFY_SQL(model, select());
       
   718 
       
   719     QCOMPARE(model.rowCount(), 1);
       
   720     QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
       
   721 
       
   722     QSignalSpy rowsRemovedSpy(&model, SIGNAL(rowsRemoved(QModelIndex,int,int)));
       
   723     QSignalSpy rowsAboutToBeRemovedSpy(&model,
       
   724             SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)));
       
   725     QSignalSpy rowsInsertedSpy(&model, SIGNAL(rowsInserted(QModelIndex,int,int)));
       
   726     QSignalSpy rowsAboutToBeInsertedSpy(&model,
       
   727             SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)));
       
   728     model.setFilter("id = 2");
       
   729 
       
   730     // check the signals
       
   731     QCOMPARE(rowsAboutToBeRemovedSpy.count(), 1);
       
   732     QCOMPARE(rowsRemovedSpy.count(), 1);
       
   733     QCOMPARE(rowsAboutToBeInsertedSpy.count(), 1);
       
   734     QCOMPARE(rowsInsertedSpy.count(), 1);
       
   735     QList<QVariant> args = rowsAboutToBeRemovedSpy.takeFirst();
       
   736     QCOMPARE(args.count(), 3);
       
   737     QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), QModelIndex());
       
   738     QCOMPARE(args.at(1).toInt(), 0);
       
   739     QCOMPARE(args.at(2).toInt(), 0);
       
   740     args = rowsRemovedSpy.takeFirst();
       
   741     QCOMPARE(args.count(), 3);
       
   742     QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), QModelIndex());
       
   743     QCOMPARE(args.at(1).toInt(), 0);
       
   744     QCOMPARE(args.at(2).toInt(), 0);
       
   745     args = rowsInsertedSpy.takeFirst();
       
   746     QCOMPARE(args.count(), 3);
       
   747     QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), QModelIndex());
       
   748     QCOMPARE(args.at(1).toInt(), 0);
       
   749     QCOMPARE(args.at(2).toInt(), 0);
       
   750     args = rowsAboutToBeInsertedSpy.takeFirst();
       
   751     QCOMPARE(args.count(), 3);
       
   752     QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), QModelIndex());
       
   753     QCOMPARE(args.at(1).toInt(), 0);
       
   754     QCOMPARE(args.at(2).toInt(), 0);
       
   755 
       
   756     QCOMPARE(model.rowCount(), 1);
       
   757     QCOMPARE(model.data(model.index(0, 0)).toInt(), 2);
       
   758 }
       
   759 
       
   760 void tst_QSqlTableModel::sqlite_bigTable()
       
   761 {
       
   762     QFETCH(QString, dbName);
       
   763     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   764     CHECK_DATABASE(db);
       
   765 
       
   766     bool hasTransactions = db.driver()->hasFeature(QSqlDriver::Transactions);
       
   767     if (hasTransactions) QVERIFY(db.transaction());
       
   768     QSqlQuery q(db);
       
   769     QVERIFY_SQL( q, exec("create table "+qTableName("bigtable")+"(id int primary key, name varchar)"));
       
   770     QVERIFY_SQL( q, prepare("insert into "+qTableName("bigtable")+"(id, name) values (?, ?)"));
       
   771     QTime startTime;
       
   772     startTime.start();
       
   773     for (int i = 0; i < 10000; ++i) {
       
   774         q.addBindValue(i);
       
   775         q.addBindValue(QString::number(i));
       
   776         if(i%1000 == 0 && startTime.elapsed() > 5000)
       
   777             qDebug() << i << "records written";
       
   778         QVERIFY_SQL( q, exec());
       
   779     }
       
   780     q.clear();
       
   781     if (hasTransactions) QVERIFY(db.commit());
       
   782 
       
   783     QSqlTableModel model(0, db);
       
   784     model.setTable(qTableName("bigtable"));
       
   785     QVERIFY_SQL(model, select());
       
   786 
       
   787     QSqlRecord rec = model.record();
       
   788     rec.setValue("id", 424242);
       
   789     rec.setValue("name", "Guillaume");
       
   790     QVERIFY_SQL(model, insertRecord(-1, rec));
       
   791 
       
   792     model.clear();
       
   793 }
       
   794 
       
   795 // For task 118547: couldn't insert records unless select()
       
   796 // had first been called.
       
   797 void tst_QSqlTableModel::insertRecordBeforeSelect()
       
   798 {
       
   799     QFETCH(QString, dbName);
       
   800     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   801     CHECK_DATABASE(db);
       
   802 
       
   803     QSqlTableModel model(0, db);
       
   804     model.setTable(qTableName("test"));
       
   805     QCOMPARE(model.lastError().type(), QSqlError::NoError);
       
   806 
       
   807     QSqlRecord buffer = model.record();
       
   808     buffer.setValue("id", 13);
       
   809     buffer.setValue("name", QString("The Lion King"));
       
   810     buffer.setValue("title", 0);
       
   811     QVERIFY_SQL(model, insertRecord(-1, buffer));
       
   812 
       
   813     buffer.setValue("id", 26);
       
   814     buffer.setValue("name", QString("T. Leary"));
       
   815     buffer.setValue("title", 0);
       
   816     QVERIFY_SQL(model, insertRecord(1, buffer));
       
   817 
       
   818     int rowCount = model.rowCount();
       
   819     model.clear();
       
   820     QCOMPARE(model.rowCount(), 0);
       
   821 
       
   822     QSqlTableModel model2(0, db);
       
   823     model2.setTable(qTableName("test"));
       
   824     QVERIFY_SQL(model2, select());
       
   825     QCOMPARE(model2.rowCount(), rowCount);
       
   826 }
       
   827 
       
   828 // For task 118547: set errors if table doesn't exist and if records
       
   829 // are inserted and submitted on a non-existing table.
       
   830 void tst_QSqlTableModel::submitAllOnInvalidTable()
       
   831 {
       
   832     QFETCH(QString, dbName);
       
   833     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   834     CHECK_DATABASE(db);
       
   835 
       
   836     QSqlTableModel model(0, db);
       
   837     model.setEditStrategy(QSqlTableModel::OnManualSubmit);
       
   838 
       
   839     // setTable returns a void, so the error can only be caught by
       
   840     // manually checking lastError(). ### Qt5: This should be changed!
       
   841     model.setTable(qTableName("invalidTable"));
       
   842     QCOMPARE(model.lastError().type(), QSqlError::StatementError);
       
   843 
       
   844     // This will give us an empty record which is expected behavior
       
   845     QSqlRecord buffer = model.record();
       
   846     buffer.setValue("bogus", 1000);
       
   847     buffer.setValue("bogus2", QString("I will go nowhere!"));
       
   848 
       
   849     // Inserting the record into the *model* will work (OnManualSubmit)
       
   850     QVERIFY_SQL(model, insertRecord(-1, buffer));
       
   851 
       
   852     // The submit and select shall fail because the table doesn't exist
       
   853     QEXPECT_FAIL("", "The table doesn't exist: submitAll() shall fail",
       
   854         Continue);
       
   855     QVERIFY_SQL(model, submitAll());
       
   856     QEXPECT_FAIL("", "The table doesn't exist: select() shall fail",
       
   857         Continue);
       
   858     QVERIFY_SQL(model, select());
       
   859 }
       
   860 
       
   861 // For task 147575: the rowsRemoved signal emitted from the model was lying
       
   862 void tst_QSqlTableModel::insertRecordsInLoop()
       
   863 {
       
   864     QFETCH(QString, dbName);
       
   865     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   866     CHECK_DATABASE(db);
       
   867 
       
   868     QSqlTableModel model(0, db);
       
   869     model.setTable(qTableName("test"));
       
   870     model.setEditStrategy(QSqlTableModel::OnManualSubmit);
       
   871     model.select();
       
   872 
       
   873     QSqlRecord record = model.record();
       
   874     record.setValue(0, 10);
       
   875     record.setValue(1, "Testman");
       
   876     record.setValue(2, 1);
       
   877 
       
   878     QSignalSpy spyRowsRemoved(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)));
       
   879     QSignalSpy spyRowsInserted(&model, SIGNAL(rowsInserted(const QModelIndex &, int, int)));
       
   880     for (int i = 0; i < 10; i++) {
       
   881         QVERIFY(model.insertRecord(model.rowCount(), record));
       
   882         QCOMPARE(spyRowsInserted.at(i).at(1).toInt(), i+3); // The table already contains three rows
       
   883         QCOMPARE(spyRowsInserted.at(i).at(2).toInt(), i+3);
       
   884     }
       
   885     model.submitAll(); // submitAll() calls select() which clears and repopulates the table
       
   886 
       
   887     int firstRowIndex = 0, lastRowIndex = 12;
       
   888     QCOMPARE(spyRowsRemoved.count(), 1);
       
   889     QCOMPARE(spyRowsRemoved.at(0).at(1).toInt(), firstRowIndex);
       
   890     QCOMPARE(spyRowsRemoved.at(0).at(2).toInt(), lastRowIndex);
       
   891 
       
   892     QCOMPARE(spyRowsInserted.at(10).at(1).toInt(), firstRowIndex);
       
   893     QCOMPARE(spyRowsInserted.at(10).at(2).toInt(), lastRowIndex);
       
   894     QCOMPARE(spyRowsInserted.count(), 11);
       
   895 
       
   896     QCOMPARE(model.rowCount(), 13);
       
   897     QCOMPARE(model.columnCount(), 3);
       
   898 }
       
   899 
       
   900 void tst_QSqlTableModel::sqlite_attachedDatabase()
       
   901 {
       
   902     QFETCH(QString, dbName);
       
   903     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   904     CHECK_DATABASE(db);
       
   905     if(db.databaseName() == ":memory:")
       
   906         QSKIP(":memory: database, skipping test", SkipSingle);
       
   907 
       
   908     QSqlDatabase attachedDb = QSqlDatabase::cloneDatabase(db, db.driverName() + QLatin1String("attached"));
       
   909     attachedDb.setDatabaseName(db.databaseName()+QLatin1String("attached.dat"));
       
   910     QVERIFY_SQL(attachedDb, open());
       
   911     QSqlQuery q(attachedDb);
       
   912     tst_Databases::safeDropTables(attachedDb, QStringList() << "atest" << "atest2");
       
   913     QVERIFY_SQL( q, exec("CREATE TABLE atest(id int, text varchar(20))"));
       
   914     QVERIFY_SQL( q, exec("CREATE TABLE atest2(id int, text varchar(20))"));
       
   915     QVERIFY_SQL( q, exec("INSERT INTO atest VALUES(1, 'attached-atest')"));
       
   916     QVERIFY_SQL( q, exec("INSERT INTO atest2 VALUES(2, 'attached-atest2')"));
       
   917 
       
   918     QSqlQuery q2(db);
       
   919     tst_Databases::safeDropTable(db, "atest");
       
   920     QVERIFY_SQL(q2, exec("CREATE TABLE atest(id int, text varchar(20))"));
       
   921     QVERIFY_SQL(q2, exec("INSERT INTO atest VALUES(3, 'main')"));
       
   922     QVERIFY_SQL(q2, exec("ATTACH DATABASE \""+attachedDb.databaseName()+"\" as adb"));
       
   923 
       
   924     // This should query the table in the attached database (schema supplied)
       
   925     QSqlTableModel model(0, db);
       
   926     model.setTable("adb.atest");
       
   927     QVERIFY_SQL(model, select());
       
   928     QCOMPARE(model.rowCount(), 1);
       
   929     QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 1);
       
   930     QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QLatin1String("attached-atest"));
       
   931 
       
   932     // This should query the table in the attached database (unique tablename)
       
   933     model.setTable("atest2");
       
   934     QVERIFY_SQL(model, select());
       
   935     QCOMPARE(model.rowCount(), 1);
       
   936     QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 2);
       
   937     QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QLatin1String("attached-atest2"));
       
   938 
       
   939     // This should query the table in the main database (tables in main db has 1st priority)
       
   940     model.setTable("atest");
       
   941     QVERIFY_SQL(model, select());
       
   942     QCOMPARE(model.rowCount(), 1);
       
   943     QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 3);
       
   944     QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QLatin1String("main"));
       
   945 }
       
   946 
       
   947 
       
   948 void tst_QSqlTableModel::tableModifyWithBlank()
       
   949 {
       
   950     QFETCH(QString, dbName);
       
   951     QSqlDatabase db = QSqlDatabase::database(dbName);
       
   952     CHECK_DATABASE(db);
       
   953 
       
   954     QSqlTableModel model(0, db);
       
   955     model.setTable(qTableName("test4"));
       
   956     model.select();
       
   957 
       
   958     //generate a time stamp for the test. Add one second to the current time to make sure
       
   959     //it is different than the QSqlQuery test.
       
   960     QString timeString=QDateTime::currentDateTime().addSecs(1).toString(Qt::ISODate);
       
   961 
       
   962     //insert a new row, with column0 being the timestamp.
       
   963     //Should be equivalent to QSqlQuery INSERT INTO... command)
       
   964     QVERIFY_SQL(model, insertRow(0));
       
   965     QVERIFY_SQL(model, setData(model.index(0,0),timeString));
       
   966     QVERIFY_SQL(model, submitAll());
       
   967 
       
   968     //set a filter on the table so the only record we get is the one we just made
       
   969     //I could just do another setData command, but I want to make sure the TableModel
       
   970     //matches exactly what is stored in the database
       
   971     model.setFilter("column1='"+timeString+"'"); //filter to get just the newly entered row
       
   972     QVERIFY_SQL(model, select());
       
   973 
       
   974     //Make sure we only get one record, and that it is the one we just made
       
   975     QCOMPARE(model.rowCount(), 1); //verify only one entry
       
   976     QCOMPARE(model.record(0).value(0).toString(), timeString); //verify correct record
       
   977 
       
   978     //At this point we know that the intial value (timestamp) was succsefully stored in the database
       
   979     //Attempt to modify the data in the new record
       
   980     //equivalent to query.exec("update test set column3="... command in direct test
       
   981     //set the data in the first column to "col1ModelData"
       
   982     QVERIFY_SQL(model, setData(model.index(0,1), "col1ModelData"));
       
   983 
       
   984     //do a quick check to make sure that the setData command properly set the value in the model
       
   985     QCOMPARE(model.record(0).value(1).toString(), QLatin1String("col1ModelData"));
       
   986 
       
   987     //submit the changed data to the database
       
   988     //This is where I have been getting errors.
       
   989     QVERIFY_SQL(model, submitAll());
       
   990 
       
   991     //make sure the model has the most current data for our record
       
   992     QVERIFY_SQL(model, select());
       
   993 
       
   994     //verify that our new record was the only record returned
       
   995     QCOMPARE(model.rowCount(), 1);
       
   996 
       
   997     //And that the record returned is, in fact, our test record.
       
   998     QCOMPARE(model.record(0).value(0).toString(), timeString);
       
   999 
       
  1000     //Make sure the value of the first column matches what we set it to previously.
       
  1001     QCOMPARE(model.record(0).value(1).toString(), QLatin1String("col1ModelData"));
       
  1002 }
       
  1003 
       
  1004 void tst_QSqlTableModel::removeColumnAndRow()
       
  1005 {
       
  1006     QFETCH(QString, dbName);
       
  1007     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1008     CHECK_DATABASE(db);
       
  1009 
       
  1010     QSqlTableModel model(0, db);
       
  1011     model.setTable(qTableName("test"));
       
  1012     model.setEditStrategy(QSqlTableModel::OnManualSubmit);
       
  1013     QVERIFY_SQL(model, select());
       
  1014     QCOMPARE(model.rowCount(), 3);
       
  1015     QCOMPARE(model.columnCount(), 3);
       
  1016 
       
  1017     QVERIFY(model.removeColumn(0));
       
  1018     QVERIFY(model.removeRow(0));
       
  1019     QVERIFY(model.submitAll());
       
  1020     QCOMPARE(model.rowCount(), 2);
       
  1021     QCOMPARE(model.columnCount(), 2);
       
  1022 
       
  1023     // check with another table because the model has been modified
       
  1024     // but not the sql table
       
  1025     QSqlTableModel model2(0, db);
       
  1026     model2.setTable(qTableName("test"));
       
  1027     QVERIFY_SQL(model2, select());
       
  1028     QCOMPARE(model2.rowCount(), 2);
       
  1029     QCOMPARE(model2.columnCount(), 3);
       
  1030 }
       
  1031 
       
  1032 void tst_QSqlTableModel::insertBeforeDelete()
       
  1033 {
       
  1034     QFETCH(QString, dbName);
       
  1035     QSqlDatabase db = QSqlDatabase::database(dbName);
       
  1036     CHECK_DATABASE(db);
       
  1037 
       
  1038     QSqlQuery q(db);
       
  1039     QVERIFY_SQL( q, exec("insert into " + qTableName("test") + " values(9, 'andrew', 9)"));
       
  1040     QVERIFY_SQL( q, exec("insert into " + qTableName("test") + " values(10, 'justin', 10)"));
       
  1041 
       
  1042     QSqlTableModel model(0, db);
       
  1043     model.setTable(qTableName("test"));
       
  1044     model.setEditStrategy(QSqlTableModel::OnManualSubmit);
       
  1045     QVERIFY_SQL(model, select());
       
  1046 
       
  1047     QSqlRecord rec = model.record();
       
  1048     rec.setValue(0, 4);
       
  1049     rec.setValue(1, QString("bill"));
       
  1050     rec.setValue(2, 4);
       
  1051     QVERIFY_SQL(model, insertRecord(4, rec));
       
  1052 
       
  1053     QVERIFY_SQL(model, removeRow(5));
       
  1054     QVERIFY_SQL(model, submitAll());
       
  1055     QCOMPARE(model.rowCount(), 5);
       
  1056 }
       
  1057 
       
  1058 QTEST_MAIN(tst_QSqlTableModel)
       
  1059 #include "tst_qsqltablemodel.moc"