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.

Change-Id: If85bbaed04bb1a32e427d642be332996d967f796
Reviewed-by: Andy Shaw <andy.shaw@digia.com>
Reviewed-by: Joerg Bornemann <joerg.bornemann@digia.com>
This commit is contained in:
Friedemann Kleint 2014-04-02 14:08:29 +02:00 committed by The Qt Project
parent 19ff7d038f
commit 9a4beb4d36
9 changed files with 50 additions and 25 deletions

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -117,7 +117,7 @@ void tst_QSql::basicDriverTest()
char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
QCoreApplication app(argc, argv, false);
tst_Databases dbs;
dbs.open();
QVERIFY(dbs.open());
foreach (const QString& dbName, dbs.dbNames) {
QSqlDatabase db = QSqlDatabase::database(dbName);
@ -161,7 +161,7 @@ void tst_QSql::open()
QCoreApplication app(argc, argv, false);
tst_Databases dbs;
dbs.open();
QVERIFY(dbs.open());
if (count == -1)
// first iteration: see how many dbs are open
count = (int) dbs.dbNames.count();
@ -188,7 +188,7 @@ void tst_QSql::concurrentAccess()
QCoreApplication app(argc, argv, false);
tst_Databases dbs;
dbs.open();
QVERIFY(dbs.open());
foreach (const QString& dbName, dbs.dbNames) {
QSqlDatabase db = QSqlDatabase::database(dbName);
QVERIFY(db.isValid());
@ -204,6 +204,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);
@ -217,7 +218,7 @@ void tst_QSql::openErrorRecovery()
QCoreApplication app(argc, argv, false);
tst_Databases dbs;
dbs.addDbs();
QVERIFY(dbs.addDbs());
if (dbs.dbNames.isEmpty())
QSKIP("No database drivers installed");
foreach (const QString& dbName, dbs.dbNames) {

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -49,6 +49,7 @@
#include <QSqlQuery>
#include <QRegExp>
#include <QDir>
#include <QScopedPointer>
#include <QVariant>
#include <QDebug>
#include <QSqlTableModel>
@ -248,7 +249,7 @@ public:
dbNames.append( cName );
}
void addDbs()
bool addDbs()
{
//addDb("QOCI", "localhost", "system", "penandy");
// addDb( "QOCI8", "//horsehead.qt-project.org:1521/pony.troll.no", "scott", "tiger" ); // Oracle 9i on horsehead
@ -302,7 +303,10 @@ public:
// use in-memory database to prevent local files
// addDb("QSQLITE", ":memory:");
addDb( "QSQLITE", QDir::toNativeSeparators(dbDir.path() + "/foo.db") );
QTemporaryDir *sqLiteDir = dbDir();
if (!sqLiteDir)
return false;
addDb( QStringLiteral("QSQLITE"), QDir::toNativeSeparators(sqLiteDir->path() + QStringLiteral("/foo.db")) );
// addDb( "QSQLITE2", QDir::toNativeSeparators(dbDir.path() + "/foo2.db") );
// addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=iceblink.qt-project.org\\ICEBLINK", "troll", "trond", "" );
// addDb( "QODBC3", "DRIVER={SQL Native Client};SERVER=silence.qt-project.org\\SQLEXPRESS", "troll", "trond", "" );
@ -319,11 +323,14 @@ public:
// addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=bq-winserv2008-x86-01.qt-project.org;DATABASE=testdb;PORT=1433", "testuser", "Ee4Gabf6_", "" );
// addDb( "QODBC", "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\\dbs\\access\\testdb.mdb", "", "", "" );
// addDb( "QODBC", "DRIVER={Postgresql};SERVER=bq-pgsql84.qt-project.org;DATABASE=testdb", "testuser", "Ee4Gabf6_", "" );
return true;
}
void open()
// 'false' return indicates a system error, for example failure to create a temporary directory.
bool open()
{
addDbs();
if (!addDbs())
return false;
QStringList::Iterator it = dbNames.begin();
@ -341,6 +348,7 @@ public:
}
}
}
return true;
}
void close()
@ -589,7 +597,21 @@ public:
QStringList dbNames;
int counter;
QTemporaryDir dbDir;
private:
QTemporaryDir *dbDir()
{
if (m_dbDir.isNull()) {
m_dbDir.reset(new QTemporaryDir);
if (!m_dbDir->isValid()) {
qWarning() << Q_FUNC_INFO << "Unable to create a temporary directory: " << QDir::toNativeSeparators(m_dbDir->path());
m_dbDir.reset();
}
}
return m_dbDir.data();
}
QScopedPointer<QTemporaryDir> m_dbDir;
};
#endif

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -394,7 +394,7 @@ void tst_QSqlDatabase::populateTestTables(QSqlDatabase db)
void tst_QSqlDatabase::initTestCase()
{
qRegisterMetaType<QSqlDriver::NotificationSource>("QSqlDriver::NotificationSource");
dbs.open();
QVERIFY(dbs.open());
for (QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it) {
QSqlDatabase db = QSqlDatabase::database((*it));
@ -2229,6 +2229,7 @@ void tst_QSqlDatabase::sqlite_enable_cache_mode()
QSqlQuery q(db), q2(db2);
QVERIFY_SQL(q, exec("select * from " + qTableName("qtest", __FILE__, db)));
QVERIFY_SQL(q2, exec("select * from " + qTableName("qtest", __FILE__, db)));
db2.close();
}
QTEST_MAIN(tst_QSqlDatabase)

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -69,7 +69,7 @@ private slots:
void tst_QSqlDriver::initTestCase_data()
{
dbs.open();
QVERIFY(dbs.open());
if (dbs.fillTestTable() == 0)
QSKIP("No database drivers are available in this Qt configuration");
}

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -257,7 +257,7 @@ tst_QSqlQuery::~tst_QSqlQuery()
void tst_QSqlQuery::initTestCase()
{
dbs.open();
QVERIFY(dbs.open());
for ( QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it ) {
QSqlDatabase db = QSqlDatabase::database(( *it ) );

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -350,7 +350,7 @@ void tst_QSqlThread::recreateTestTables()
void tst_QSqlThread::initTestCase()
{
dbs.open();
QVERIFY(dbs.open());
recreateTestTables();
}

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -124,7 +124,7 @@ tst_QSqlQueryModel::~tst_QSqlQueryModel()
void tst_QSqlQueryModel::initTestCase()
{
dbs.open();
QVERIFY(dbs.open());
for (QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it) {
QSqlDatabase db = QSqlDatabase::database((*it));
CHECK_DATABASE(db);

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -98,7 +98,7 @@ private:
void tst_QSqlRelationalTableModel::initTestCase_data()
{
dbs.open();
QVERIFY(dbs.open());
if (dbs.fillTestTable() == 0)
QSKIP("No database drivers are available in this Qt configuration");
}

View File

@ -1,6 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
@ -157,7 +157,7 @@ private:
tst_QSqlTableModel::tst_QSqlTableModel()
{
dbs.open();
QVERIFY(dbs.open());
}
tst_QSqlTableModel::~tst_QSqlTableModel()
@ -1968,6 +1968,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();
}