1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Copyright (C) 2016 Ivailo Monev
6 ** This file is part of the test suite of the Katie Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser
12 ** General Public License version 2.1 as published by the Free Software
13 ** Foundation and appearing in the file LICENSE.LGPL included in the
14 ** packaging of this file. Please review the following information to
15 ** ensure the GNU Lesser General Public License version 2.1 requirements
16 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
20 ****************************************************************************/
23 #include <QtTest/QtTest>
24 #include <qcoreapplication.h>
25 #include <qsqldatabase.h>
26 #include <qsqlerror.h>
27 #include <qsqlquery.h>
28 #include <qsqlrecord.h>
30 #include <qsqlresult.h>
31 #include <qsqldriver.h>
33 #include <qsqlnulldriver_p.h>
35 #include "../qsqldatabase/tst_databases.h"
39 class tst_QSql : public QObject
50 void cleanupTestCase();
57 // problem specific tests
58 void openErrorRecovery();
59 void concurrentAccess();
60 void basicDriverTest();
63 /****************** General Qt SQL Module tests *****************/
73 void tst_QSql::initTestCase()
77 void tst_QSql::cleanupTestCase()
85 void tst_QSql::cleanup()
90 // this is a very basic test for drivers that cannot create/delete tables
91 // it can be used while developing new drivers,
92 // it's original purpose is to test ODBC Text datasources that are basically
93 // to stupid to do anything more advanced than SELECT/INSERT/UPDATE/DELETE
94 // the datasource has to have a table called "qtest_basictest" consisting
95 // of a field "id"(integer) and "name"(char/varchar).
96 void tst_QSql::basicDriverTest()
99 char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
100 QCoreApplication app( argc, argv );
105 foreach( const QString& dbName, dbs.dbNames )
107 QSqlDatabase db = QSqlDatabase::database( dbName );
108 QVERIFY_SQL( db, isValid() );
110 QStringList tables = db.tables();
113 if ( tables.contains( "qtest_basictest.txt" ) )
114 tableName = "qtest_basictest.txt";
115 else if ( tables.contains( "qtest_basictest" ) )
116 tableName = "qtest_basictest";
117 else if ( tables.contains( "QTEST_BASICTEST" ) )
118 tableName = "QTEST_BASICTEST";
124 qDebug("Testing: %s", qPrintable(tst_Databases::dbToString( db )));
126 QSqlRecord rInf = db.record( tableName );
127 QCOMPARE( rInf.count(), 2 );
128 QCOMPARE( rInf.fieldName( 0 ).toLower(), QString( "id" ) );
129 QCOMPARE( rInf.fieldName( 1 ).toLower(), QString( "name" ) );
133 QVERIFY( 1 ); // make sure the test doesn't fail if no database drivers are there
136 // make sure that the static stuff will be deleted
137 // when using multiple QCoreApplication objects
138 void tst_QSql::open()
142 char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
144 for ( i = 0; i < 10; ++i ) {
146 QCoreApplication app( argc, argv );
151 // first iteration: see how many dbs are open
152 count = (int) dbs.dbNames.count();
154 // next iterations: make sure all are opened again
155 QCOMPARE( count, (int)dbs.dbNames.count() );
160 void tst_QSql::openInvalid()
165 QSqlDatabase db2 = QSqlDatabase::addDatabase("doesnt_exist_will_never_exist", "blah");
166 QFAIL_SQL(db2, open());
169 void tst_QSql::concurrentAccess()
172 char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
173 QCoreApplication app( argc, argv );
177 foreach ( const QString& dbName, dbs.dbNames ) {
178 QSqlDatabase db = QSqlDatabase::database( dbName );
179 QVERIFY( db.isValid() );
180 if (tst_Databases::isMSAccess(db))
183 QSqlDatabase ndb = QSqlDatabase::addDatabase( db.driverName(), "tst_QSql::concurrentAccess" );
184 ndb.setDatabaseName( db.databaseName() );
185 ndb.setHostName( db.hostName() );
186 ndb.setPort( db.port() );
187 ndb.setUserName( db.userName() );
188 ndb.setPassword( db.password() );
189 QVERIFY_SQL( ndb, open() );
191 QCOMPARE( db.tables(), ndb.tables() );
194 // no database servers installed - don't fail
199 void tst_QSql::openErrorRecovery()
202 char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
203 QCoreApplication app( argc, argv );
208 if (dbs.dbNames.isEmpty())
209 QSKIP("No database drivers installed", SkipAll);
210 foreach ( const QString& dbName, dbs.dbNames ) {
211 QSqlDatabase db = QSqlDatabase::database( dbName, false );
212 CHECK_DATABASE( db );
214 QString userName = db.userName();
215 QString password = db.password();
217 // force an open error
218 if ( db.open( "dummy130977", "doesnt_exist" ) ) {
219 qDebug( "Promiscuous database server without access control - test skipped for %s",
220 qPrintable(tst_Databases::dbToString( db )) );
225 QFAIL_SQL( db, isOpen() );
226 QVERIFY_SQL( db, isOpenError() );
229 if ( !db.open( userName, password ) ) {
230 qDebug() << "Could not open Database " << tst_Databases::dbToString( db ) <<
231 ". Assuming DB is down, skipping... (Error: " <<
232 tst_Databases::printError( db.lastError() ) << ")";
235 QVERIFY_SQL( db, open( userName, password ) );
236 QVERIFY_SQL( db, isOpen() );
237 QFAIL_SQL( db, isOpenError() );
239 QFAIL_SQL( db, isOpen() );
241 // force another open error
242 QFAIL_SQL( db, open( "dummy130977", "doesnt_exist" ) );
243 QFAIL_SQL( db, isOpen() );
244 QVERIFY_SQL( db, isOpenError() );
248 QTEST_APPLESS_MAIN(tst_QSql)
250 #include "moc_tst_qsql.cpp"