OSDN Git Service

use proper type for written bytes count in QFileSystemEngine::copyFile()
[kde/Katie.git] / tests / auto / qsql / tst_qsql.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Copyright (C) 2016 Ivailo Monev
5 **
6 ** This file is part of the test suite of the Katie Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 **
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.
17 **
18 ** $QT_END_LICENSE$
19 **
20 ****************************************************************************/
21
22
23 #include <QtTest/QtTest>
24 #include <qcoreapplication.h>
25 #include <qsqldatabase.h>
26 #include <qsqlerror.h>
27 #include <qsqlquery.h>
28 #include <qsqlrecord.h>
29 #include <qsql.h>
30 #include <qsqlresult.h>
31 #include <qsqldriver.h>
32 #include <qdebug.h>
33 #include <qsqlnulldriver_p.h>
34
35 #include "../qsqldatabase/tst_databases.h"
36
37 //TESTED_FILES=
38
39 class tst_QSql : public QObject
40 {
41 Q_OBJECT
42
43 public:
44     tst_QSql();
45     virtual ~tst_QSql();
46
47
48 public slots:
49     void initTestCase();
50     void cleanupTestCase();
51     void init();
52     void cleanup();
53 private slots:
54     void open();
55     void openInvalid();
56
57     // problem specific tests
58     void openErrorRecovery();
59     void concurrentAccess();
60     void basicDriverTest();
61 };
62
63 /****************** General Qt SQL Module tests *****************/
64
65 tst_QSql::tst_QSql()
66 {
67 }
68
69 tst_QSql::~tst_QSql()
70 {
71 }
72
73 void tst_QSql::initTestCase()
74 {
75 }
76
77 void tst_QSql::cleanupTestCase()
78 {
79 }
80
81 void tst_QSql::init()
82 {
83 }
84
85 void tst_QSql::cleanup()
86 {
87 }
88
89
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()
97 {
98     int argc = 1;
99     char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
100     QCoreApplication app( argc, argv );
101
102     tst_Databases dbs;
103     dbs.open();
104
105     foreach( const QString& dbName, dbs.dbNames )
106     {
107         QSqlDatabase db = QSqlDatabase::database( dbName );
108         QVERIFY_SQL( db, isValid() );
109
110         QStringList tables = db.tables();
111         QString tableName;
112
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";
119         else {
120             QVERIFY( 1 );
121             continue;
122         }
123
124         qDebug("Testing: %s", qPrintable(tst_Databases::dbToString( db )));
125
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" ) );
130     }
131
132     dbs.close();
133     QVERIFY( 1 ); // make sure the test doesn't fail if no database drivers are there
134 }
135
136 // make sure that the static stuff will be deleted
137 // when using multiple QCoreApplication objects
138 void tst_QSql::open()
139 {
140     int i;
141     int argc = 1;
142     char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
143     int count = -1;
144     for ( i = 0; i < 10; ++i ) {
145
146        QCoreApplication app( argc, argv );
147        tst_Databases dbs;
148
149        dbs.open();
150        if ( count == -1 )
151            // first iteration: see how many dbs are open
152            count = (int) dbs.dbNames.count();
153        else
154            // next iterations: make sure all are opened again
155            QCOMPARE( count, (int)dbs.dbNames.count() );
156        dbs.close();
157     }
158 }
159
160 void tst_QSql::openInvalid()
161 {
162     QSqlDatabase db;
163     QVERIFY(!db.open());
164
165     QSqlDatabase db2 = QSqlDatabase::addDatabase("doesnt_exist_will_never_exist", "blah");
166     QFAIL_SQL(db2, open());
167 }
168
169 void tst_QSql::concurrentAccess()
170 {
171     int argc = 1;
172     char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
173     QCoreApplication app( argc, argv );
174     tst_Databases dbs;
175
176     dbs.open();
177     foreach ( const QString& dbName, dbs.dbNames ) {
178         QSqlDatabase db = QSqlDatabase::database( dbName );
179         QVERIFY( db.isValid() );
180         if (tst_Databases::isMSAccess(db))
181             continue;
182
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() );
190
191         QCOMPARE( db.tables(), ndb.tables() );
192         ndb.close();
193     }
194     // no database servers installed - don't fail
195     QVERIFY(1);
196     dbs.close();
197 }
198
199 void tst_QSql::openErrorRecovery()
200 {
201     int argc = 1;
202     char *argv[] = { const_cast<char*>(QTest::currentAppName()) };
203     QCoreApplication app( argc, argv );
204
205     tst_Databases dbs;
206
207     dbs.addDbs();
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 );
213
214         QString userName = db.userName();
215         QString password = db.password();
216
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 )) );
221             QVERIFY(1);
222             continue;
223         }
224
225         QFAIL_SQL( db, isOpen() );
226         QVERIFY_SQL( db, isOpenError() );
227
228         // now open it
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() ) << ")";
233             continue;
234         }
235         QVERIFY_SQL( db, open( userName, password ) );
236         QVERIFY_SQL( db, isOpen() );
237         QFAIL_SQL( db, isOpenError() );
238         db.close();
239         QFAIL_SQL( db, isOpen() );
240
241         // force another open error
242         QFAIL_SQL( db, open( "dummy130977", "doesnt_exist" ) );
243         QFAIL_SQL( db, isOpen() );
244         QVERIFY_SQL( db, isOpenError() );
245     }
246 }
247
248 QTEST_APPLESS_MAIN(tst_QSql)
249
250 #include "moc_tst_qsql.cpp"