QSqlTableModelTest: misc cleanup
Cleanup QSqlTableModelTest: - use pmf QSignalSpy ctor - use QCOMPARE/QCOMPARE_GE instead QVERIFY where possible - properly close additional databases so temporary folders created for sqlite can be deleted on exit Pick-to: 6.8 Change-Id: I84e8960b81128c141a0e2cd6d03837c9b5ac6652 Reviewed-by: Axel Spoerl <axel.spoerl@qt.io> (cherry picked from commit a47dc990793a1e299e9ba869e1d6c76fa0bf9d65) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
3c178b40e7
commit
3cd991037d
@ -520,7 +520,7 @@ void tst_QSqlTableModel::setData()
|
|||||||
// initial state
|
// initial state
|
||||||
QModelIndex idx = model.index(0, 0);
|
QModelIndex idx = model.index(0, 0);
|
||||||
QVariant val = model.data(idx);
|
QVariant val = model.data(idx);
|
||||||
QVERIFY(val == int(1));
|
QCOMPARE(val, 1);
|
||||||
QVERIFY(!val.isNull());
|
QVERIFY(!val.isNull());
|
||||||
QFAIL_SQL(model, isDirty());
|
QFAIL_SQL(model, isDirty());
|
||||||
|
|
||||||
@ -528,7 +528,7 @@ void tst_QSqlTableModel::setData()
|
|||||||
idx = model.index(0, 0);
|
idx = model.index(0, 0);
|
||||||
QVERIFY_SQL(model, setData(idx, int(0)));
|
QVERIFY_SQL(model, setData(idx, int(0)));
|
||||||
val = model.data(idx);
|
val = model.data(idx);
|
||||||
QVERIFY(val == int(0));
|
QCOMPARE(val, 0);
|
||||||
QVERIFY(!val.isNull());
|
QVERIFY(!val.isNull());
|
||||||
QVERIFY_SQL(model, isDirty(idx));
|
QVERIFY_SQL(model, isDirty(idx));
|
||||||
QVERIFY_SQL(model, submitAll());
|
QVERIFY_SQL(model, submitAll());
|
||||||
@ -546,7 +546,7 @@ void tst_QSqlTableModel::setData()
|
|||||||
idx = model.index(0, 0);
|
idx = model.index(0, 0);
|
||||||
QVERIFY_SQL(model, setData(idx, int(0)));
|
QVERIFY_SQL(model, setData(idx, int(0)));
|
||||||
val = model.data(idx);
|
val = model.data(idx);
|
||||||
QVERIFY(val == int(0));
|
QCOMPARE(val, 0);
|
||||||
QVERIFY(!val.isNull());
|
QVERIFY(!val.isNull());
|
||||||
QVERIFY_SQL(model, isDirty(idx));
|
QVERIFY_SQL(model, isDirty(idx));
|
||||||
QVERIFY_SQL(model, submitAll());
|
QVERIFY_SQL(model, submitAll());
|
||||||
@ -555,7 +555,7 @@ void tst_QSqlTableModel::setData()
|
|||||||
idx = model.index(0, 0);
|
idx = model.index(0, 0);
|
||||||
QVERIFY_SQL(model, setData(idx, int(0)));
|
QVERIFY_SQL(model, setData(idx, int(0)));
|
||||||
val = model.data(idx);
|
val = model.data(idx);
|
||||||
QVERIFY(val == int(0));
|
QCOMPARE(val, 0);
|
||||||
QVERIFY(!val.isNull());
|
QVERIFY(!val.isNull());
|
||||||
QFAIL_SQL(model, isDirty(idx));
|
QFAIL_SQL(model, isDirty(idx));
|
||||||
|
|
||||||
@ -595,7 +595,7 @@ void tst_QSqlTableModel::setRecord()
|
|||||||
QVERIFY_SQL(model, select());
|
QVERIFY_SQL(model, select());
|
||||||
|
|
||||||
for (int i = 0; i < model.rowCount(); ++i) {
|
for (int i = 0; i < model.rowCount(); ++i) {
|
||||||
QSignalSpy spy(&model, SIGNAL(dataChanged(QModelIndex,QModelIndex)));
|
QSignalSpy spy(&model, &QSqlTableModel::dataChanged);
|
||||||
|
|
||||||
QSqlRecord rec = model.record(i);
|
QSqlRecord rec = model.record(i);
|
||||||
rec.setValue(1, rec.value(1).toString() + 'X');
|
rec.setValue(1, rec.value(1).toString() + 'X');
|
||||||
@ -605,7 +605,6 @@ void tst_QSqlTableModel::setRecord()
|
|||||||
// dataChanged() emitted by setData() for each *changed* column
|
// dataChanged() emitted by setData() for each *changed* column
|
||||||
if (submitpolicy == QSqlTableModel::OnManualSubmit) {
|
if (submitpolicy == QSqlTableModel::OnManualSubmit) {
|
||||||
QCOMPARE(spy.size(), 2);
|
QCOMPARE(spy.size(), 2);
|
||||||
QCOMPARE(spy.at(0).size(), 2);
|
|
||||||
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(0)), model.index(i, 1));
|
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(0)), model.index(i, 1));
|
||||||
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(1)), model.index(i, 1));
|
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(1)), model.index(i, 1));
|
||||||
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(1).at(0)), model.index(i, 2));
|
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(1).at(0)), model.index(i, 2));
|
||||||
@ -614,12 +613,8 @@ void tst_QSqlTableModel::setRecord()
|
|||||||
} else if (submitpolicy == QSqlTableModel::OnRowChange && i == model.rowCount() -1)
|
} else if (submitpolicy == QSqlTableModel::OnRowChange && i == model.rowCount() -1)
|
||||||
model.submit();
|
model.submit();
|
||||||
else {
|
else {
|
||||||
if (submitpolicy != QSqlTableModel::OnManualSubmit)
|
|
||||||
// dataChanged() also emitted by selectRow()
|
// dataChanged() also emitted by selectRow()
|
||||||
QCOMPARE(spy.size(), 3);
|
QCOMPARE(spy.size(), 3);
|
||||||
else
|
|
||||||
QCOMPARE(spy.size(), 2);
|
|
||||||
QCOMPARE(spy.at(0).size(), 2);
|
|
||||||
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(0)), model.index(i, 1));
|
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(0)), model.index(i, 1));
|
||||||
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(1)), model.index(i, 1));
|
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(1)), model.index(i, 1));
|
||||||
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(1).at(0)), model.index(i, 2));
|
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(1).at(0)), model.index(i, 2));
|
||||||
@ -1088,7 +1083,7 @@ void tst_QSqlTableModel::removeRow()
|
|||||||
|
|
||||||
// headerDataChanged must be emitted by the model since the row won't vanish until select
|
// headerDataChanged must be emitted by the model since the row won't vanish until select
|
||||||
qRegisterMetaType<Qt::Orientation>("Qt::Orientation");
|
qRegisterMetaType<Qt::Orientation>("Qt::Orientation");
|
||||||
QSignalSpy headerDataChangedSpy(&model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)));
|
QSignalSpy headerDataChangedSpy(&model, &QSqlTableModel::headerDataChanged);
|
||||||
|
|
||||||
QVERIFY(model.removeRow(1));
|
QVERIFY(model.removeRow(1));
|
||||||
QCOMPARE(headerDataChangedSpy.size(), 1);
|
QCOMPARE(headerDataChangedSpy.size(), 1);
|
||||||
@ -1135,7 +1130,7 @@ void tst_QSqlTableModel::removeRows()
|
|||||||
QVERIFY_SQL(model, select());
|
QVERIFY_SQL(model, select());
|
||||||
QCOMPARE(model.rowCount(), 3);
|
QCOMPARE(model.rowCount(), 3);
|
||||||
|
|
||||||
QSignalSpy beforeDeleteSpy(&model, SIGNAL(beforeDelete(int)));
|
QSignalSpy beforeDeleteSpy(&model, &QSqlTableModel::beforeDelete);
|
||||||
|
|
||||||
// Make sure wrong stuff is ok
|
// Make sure wrong stuff is ok
|
||||||
QVERIFY(!model.removeRows(-1,1)); // negative start
|
QVERIFY(!model.removeRows(-1,1)); // negative start
|
||||||
@ -1176,7 +1171,7 @@ void tst_QSqlTableModel::removeRows()
|
|||||||
QVERIFY(!model.removeRows(1, 0, model.index(2, 0))); // can't pass a valid modelindex
|
QVERIFY(!model.removeRows(1, 0, model.index(2, 0))); // can't pass a valid modelindex
|
||||||
|
|
||||||
qRegisterMetaType<Qt::Orientation>("Qt::Orientation");
|
qRegisterMetaType<Qt::Orientation>("Qt::Orientation");
|
||||||
QSignalSpy headerDataChangedSpy(&model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)));
|
QSignalSpy headerDataChangedSpy(&model, &QSqlTableModel::headerDataChanged);
|
||||||
QVERIFY(model.removeRows(0, 2, QModelIndex()));
|
QVERIFY(model.removeRows(0, 2, QModelIndex()));
|
||||||
QCOMPARE(headerDataChangedSpy.size(), 2);
|
QCOMPARE(headerDataChangedSpy.size(), 2);
|
||||||
QCOMPARE(headerDataChangedSpy.at(0).at(1).toInt(), 1);
|
QCOMPARE(headerDataChangedSpy.at(0).at(1).toInt(), 1);
|
||||||
@ -1505,7 +1500,7 @@ void tst_QSqlTableModel::isDirty()
|
|||||||
if (submitpolicy == QSqlTableModel::OnRowChange) {
|
if (submitpolicy == QSqlTableModel::OnRowChange) {
|
||||||
// dirty row must block change on other rows
|
// dirty row must block change on other rows
|
||||||
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
|
||||||
QVERIFY(model.rowCount() > 1);
|
QCOMPARE_GE(model.rowCount(), 1);
|
||||||
QVERIFY_SQL(model, setData(model.index(0, 1), QString("sam i am")));
|
QVERIFY_SQL(model, setData(model.index(0, 1), QString("sam i am")));
|
||||||
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("sam i am"));
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("sam i am"));
|
||||||
QVERIFY_SQL(model, isDirty());
|
QVERIFY_SQL(model, isDirty());
|
||||||
@ -1772,8 +1767,8 @@ void tst_QSqlTableModel::setFilter()
|
|||||||
QCOMPARE(model.rowCount(), 1);
|
QCOMPARE(model.rowCount(), 1);
|
||||||
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
||||||
|
|
||||||
QSignalSpy modelAboutToBeResetSpy(&model, SIGNAL(modelAboutToBeReset()));
|
QSignalSpy modelAboutToBeResetSpy(&model, &QSqlTableModel::modelAboutToBeReset);
|
||||||
QSignalSpy modelResetSpy(&model, SIGNAL(modelReset()));
|
QSignalSpy modelResetSpy(&model, &QSqlTableModel::modelReset);
|
||||||
model.setFilter("id = 2");
|
model.setFilter("id = 2");
|
||||||
|
|
||||||
// check the signals
|
// check the signals
|
||||||
@ -1910,9 +1905,9 @@ void tst_QSqlTableModel::insertRecordsInLoop()
|
|||||||
record.setValue(1, "Testman");
|
record.setValue(1, "Testman");
|
||||||
record.setValue(2, 1);
|
record.setValue(2, 1);
|
||||||
|
|
||||||
QSignalSpy modelAboutToBeResetSpy(&model, SIGNAL(modelAboutToBeReset()));
|
QSignalSpy modelAboutToBeResetSpy(&model, &QSqlTableModel::modelAboutToBeReset);
|
||||||
QSignalSpy modelResetSpy(&model, SIGNAL(modelReset()));
|
QSignalSpy modelResetSpy(&model, &QSqlTableModel::modelReset);
|
||||||
QSignalSpy spyRowsInserted(&model, SIGNAL(rowsInserted(QModelIndex,int,int)));
|
QSignalSpy spyRowsInserted(&model, &QSqlTableModel::rowsInserted);
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
QVERIFY(model.insertRecord(model.rowCount(), record));
|
QVERIFY(model.insertRecord(model.rowCount(), record));
|
||||||
QCOMPARE(spyRowsInserted.at(i).at(1).toInt(), i+3); // The table already contains three rows
|
QCOMPARE(spyRowsInserted.at(i).at(1).toInt(), i+3); // The table already contains three rows
|
||||||
@ -1936,7 +1931,10 @@ void tst_QSqlTableModel::sqlite_escaped_delimiters()
|
|||||||
if (db.databaseName() == ":memory:")
|
if (db.databaseName() == ":memory:")
|
||||||
QSKIP(":memory: database, skipping test");
|
QSKIP(":memory: database, skipping test");
|
||||||
|
|
||||||
auto attachedDb = QSqlDatabase::cloneDatabase(db, db.driverName() + QLatin1String("attached"));
|
const auto attachedDbName = db.driverName() + QLatin1String("-attached");
|
||||||
|
auto closeDb = qScopeGuard([&]() { QSqlDatabase::removeDatabase(attachedDbName); });
|
||||||
|
{
|
||||||
|
auto attachedDb = QSqlDatabase::cloneDatabase(db, attachedDbName);
|
||||||
attachedDb.setDatabaseName(db.databaseName() + QLatin1String("attached.dat"));
|
attachedDb.setDatabaseName(db.databaseName() + QLatin1String("attached.dat"));
|
||||||
QVERIFY_SQL(attachedDb, open());
|
QVERIFY_SQL(attachedDb, open());
|
||||||
QSqlQuery q(attachedDb);
|
QSqlQuery q(attachedDb);
|
||||||
@ -1993,9 +1991,9 @@ void tst_QSqlTableModel::sqlite_escaped_delimiters()
|
|||||||
QVERIFY_SQL(model, select());
|
QVERIFY_SQL(model, select());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
attachedDb.close();
|
attachedDb.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void tst_QSqlTableModel::sqlite_attachedDatabase()
|
void tst_QSqlTableModel::sqlite_attachedDatabase()
|
||||||
{
|
{
|
||||||
@ -2005,6 +2003,9 @@ void tst_QSqlTableModel::sqlite_attachedDatabase()
|
|||||||
if(db.databaseName() == ":memory:")
|
if(db.databaseName() == ":memory:")
|
||||||
QSKIP(":memory: database, skipping test");
|
QSKIP(":memory: database, skipping test");
|
||||||
|
|
||||||
|
const auto attachedDbName = db.driverName() + QLatin1String("-attached");
|
||||||
|
auto closeDb = qScopeGuard([&]() { QSqlDatabase::removeDatabase(attachedDbName); });
|
||||||
|
{
|
||||||
QSqlDatabase attachedDb = QSqlDatabase::cloneDatabase(db, db.driverName() + QLatin1String("attached"));
|
QSqlDatabase attachedDb = QSqlDatabase::cloneDatabase(db, db.driverName() + QLatin1String("attached"));
|
||||||
attachedDb.setDatabaseName(db.databaseName()+QLatin1String("attached.dat"));
|
attachedDb.setDatabaseName(db.databaseName()+QLatin1String("attached.dat"));
|
||||||
QVERIFY_SQL(attachedDb, open());
|
QVERIFY_SQL(attachedDb, open());
|
||||||
@ -2046,6 +2047,7 @@ void tst_QSqlTableModel::sqlite_attachedDatabase()
|
|||||||
QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QLatin1String("main"));
|
QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QLatin1String("main"));
|
||||||
attachedDb.close();
|
attachedDb.close();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void tst_QSqlTableModel::tableModifyWithBlank()
|
void tst_QSqlTableModel::tableModifyWithBlank()
|
||||||
@ -2234,6 +2236,7 @@ void tst_QSqlTableModel::sqlite_selectFromIdentifierWithDot()
|
|||||||
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
|
||||||
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("Andy"));
|
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("Andy"));
|
||||||
}
|
}
|
||||||
|
auto closeDb = qScopeGuard([&]() { QSqlDatabase::removeDatabase("attachedDb"); });
|
||||||
{
|
{
|
||||||
QSqlDatabase attachedDb = QSqlDatabase::addDatabase("QSQLITE", "attachedDb");
|
QSqlDatabase attachedDb = QSqlDatabase::addDatabase("QSQLITE", "attachedDb");
|
||||||
attachedDb.setDatabaseName(db.databaseName().replace("foo.db", "attached.db"));
|
attachedDb.setDatabaseName(db.databaseName().replace("foo.db", "attached.db"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user