summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@digia.com>2014-04-02 12:08:29 (GMT)
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-04-16 07:41:03 (GMT)
commit20ff5ac1014d82c1200b3ab0c9e23e149903dc74 (patch)
tree2b79bf0ee0d1db4f6784f1614f2a4da0a64e243c /tests
parent01fd1edbb074b26a054bb545ffed979100f6be12 (diff)
downloadQt-20ff5ac1014d82c1200b3ab0c9e23e149903dc74.zip
Qt-20ff5ac1014d82c1200b3ab0c9e23e149903dc74.tar.gz
Qt-20ff5ac1014d82c1200b3ab0c9e23e149903dc74.tar.bz2
Improve handling of temporary directories in SQL tests.
Initialize directory delayed in shared code and add checks to verify that it is valid. Close attached / cloned databases to prevent locks on files and leaking temporary directories caused by SQLite: QTemporaryDir: Unable to remove "...\Temp\tst_qsqldatabase-P1XkOA" most likely due to the presence of read-only files. QTemporaryDir: Unable to remove "...\Temp\tst_qsqltablemodel-P1XkOA" most likely due to the presence of read-only files. QWARN : tst_QSql::concurrentAccess() QTemporaryDir: Unable to remove "...\Temp\tst_qsql-l0VAKJ" most likely due to the presence of read-only files. In Qt 4, emulate the temporary directory by maintaining a list of files and deleting them in reverse order. Change-Id: If85bbaed04bb1a32e427d642be332996d967f796 Reviewed-by: Andy Shaw <andy.shaw@digia.com> Reviewed-by: Joerg Bornemann <joerg.bornemann@digia.com> (cherry picked from qtbase/9a4beb4d36dc0c613e59aa5d88060d521462f56c)
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qsql/tst_qsql.cpp1
-rw-r--r--tests/auto/qsqldatabase/tst_databases.h27
-rw-r--r--tests/auto/qsqldatabase/tst_qsqldatabase.cpp1
-rw-r--r--tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp3
4 files changed, 29 insertions, 3 deletions
diff --git a/tests/auto/qsql/tst_qsql.cpp b/tests/auto/qsql/tst_qsql.cpp
index 906d8b7..916d124 100644
--- a/tests/auto/qsql/tst_qsql.cpp
+++ b/tests/auto/qsql/tst_qsql.cpp
@@ -254,6 +254,7 @@ void tst_QSql::concurrentAccess()
QVERIFY_SQL( ndb, open() );
QCOMPARE( db.tables(), ndb.tables() );
+ ndb.close();
}
// no database servers installed - don't fail
QVERIFY(1);
diff --git a/tests/auto/qsqldatabase/tst_databases.h b/tests/auto/qsqldatabase/tst_databases.h
index a688f9e..64dcb80 100644
--- a/tests/auto/qsqldatabase/tst_databases.h
+++ b/tests/auto/qsqldatabase/tst_databases.h
@@ -136,13 +136,24 @@ class tst_Databases
{
public:
- tst_Databases(): counter( 0 )
+ tst_Databases(): counter( 0 ), m_sqLitePrefix(QDir::tempPath())
{
+ if (!m_sqLitePrefix.endsWith(QLatin1Char('/')))
+ m_sqLitePrefix += QLatin1Char('/');
+ m_sqLitePrefix += QLatin1String("foo");
+ m_sqLitePrefix += QString::number(QDateTime::currentDateTime().toMSecsSinceEpoch() % quint64(1000));
}
~tst_Databases()
{
close();
+ for (int i = m_sqLiteFiles.size() - 1; i >= 0; --i) {
+ QFile sqLiteFile(m_sqLiteFiles.at(i));
+ if (sqLiteFile.exists() && !sqLiteFile.remove()) {
+ qWarning() << "Cannot remove " << QDir::toNativeSeparators(sqLiteFile.fileName())
+ << ':' << sqLiteFile.errorString();
+ }
+ }
}
// returns a testtable consisting of the names of all database connections if
@@ -279,7 +290,7 @@ public:
// use in-memory database to prevent local files
// addDb("QSQLITE", ":memory:");
- addDb( "QSQLITE", QDir::toNativeSeparators(QDir::tempPath()+"/foo.db") );
+ addDb( "QSQLITE", QDir::toNativeSeparators(sqLiteFileName()));
// addDb( "QSQLITE2", QDir::toNativeSeparators(QDir::tempPath()+"/foo2.db") );
// addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=iceblink.nokia.troll.no\\ICEBLINK", "troll", "trond", "" );
// addDb( "QODBC3", "DRIVER={SQL Native Client};SERVER=silence.nokia.troll.no\\SQLEXPRESS", "troll", "trond", "" );
@@ -569,8 +580,20 @@ public:
return QString();
}
+ QString sqLiteFileName() // Return a temporary file name for SQLite DB
+ {
+ const QString newFileName = m_sqLitePrefix + QLatin1Char('_')
+ + QString::number(m_sqLiteFiles.size()) + QLatin1String(".db");
+ m_sqLiteFiles.append(newFileName);
+ return newFileName;
+ }
+
QStringList dbNames;
int counter;
+
+private:
+ QString m_sqLitePrefix;
+ QStringList m_sqLiteFiles;
};
#endif
diff --git a/tests/auto/qsqldatabase/tst_qsqldatabase.cpp b/tests/auto/qsqldatabase/tst_qsqldatabase.cpp
index bc0eb32..9bdd011 100644
--- a/tests/auto/qsqldatabase/tst_qsqldatabase.cpp
+++ b/tests/auto/qsqldatabase/tst_qsqldatabase.cpp
@@ -2538,6 +2538,7 @@ void tst_QSqlDatabase::sqlite_enable_cache_mode()
QSqlQuery q(db), q2(db2);
QVERIFY_SQL(q, exec("select * from "+qTableName("qtest", __FILE__)));
QVERIFY_SQL(q2, exec("select * from "+qTableName("qtest", __FILE__)));
+ db2.close();
}
QTEST_MAIN(tst_QSqlDatabase)
diff --git a/tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp
index e9bcdaa..078e109 100644
--- a/tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp
+++ b/tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp
@@ -1261,7 +1261,7 @@ void tst_QSqlTableModel::sqlite_attachedDatabase()
QSKIP(":memory: database, skipping test", SkipSingle);
QSqlDatabase attachedDb = QSqlDatabase::cloneDatabase(db, db.driverName() + QLatin1String("attached"));
- attachedDb.setDatabaseName(db.databaseName()+QLatin1String("attached.dat"));
+ attachedDb.setDatabaseName(dbs.sqLiteFileName());
QVERIFY_SQL(attachedDb, open());
QSqlQuery q(attachedDb);
tst_Databases::safeDropTables(attachedDb, QStringList() << "atest" << "atest2");
@@ -1297,6 +1297,7 @@ void tst_QSqlTableModel::sqlite_attachedDatabase()
QCOMPARE(model.rowCount(), 1);
QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 3);
QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QLatin1String("main"));
+ attachedDb.close();
}