|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the test suite of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 #include <qapplication.h> |
|
45 #include <qsqldatabase.h> |
|
46 #include <qsqlerror.h> |
|
47 #include <qsqlquery.h> |
|
48 #ifdef QT3_SUPPORT |
|
49 #include <q3sqlcursor.h> |
|
50 #endif |
|
51 #include <qsqlrecord.h> |
|
52 #include <qsql.h> |
|
53 #include <qsqlresult.h> |
|
54 #include <qsqldriver.h> |
|
55 #include <qdebug.h> |
|
56 #include <private/qsqlnulldriver_p.h> |
|
57 |
|
58 #include "../qsqldatabase/tst_databases.h" |
|
59 |
|
60 //TESTED_FILES= |
|
61 |
|
62 class tst_QSql : public QObject |
|
63 { |
|
64 Q_OBJECT |
|
65 |
|
66 public: |
|
67 tst_QSql(); |
|
68 virtual ~tst_QSql(); |
|
69 |
|
70 |
|
71 public slots: |
|
72 void initTestCase(); |
|
73 void cleanupTestCase(); |
|
74 void init(); |
|
75 void cleanup(); |
|
76 private slots: |
|
77 void open(); |
|
78 void openInvalid(); |
|
79 void registerSqlDriver(); |
|
80 |
|
81 // problem specific tests |
|
82 void openErrorRecovery(); |
|
83 void concurrentAccess(); |
|
84 void basicDriverTest(); |
|
85 }; |
|
86 |
|
87 /****************** General Qt SQL Module tests *****************/ |
|
88 |
|
89 tst_QSql::tst_QSql() |
|
90 { |
|
91 } |
|
92 |
|
93 tst_QSql::~tst_QSql() |
|
94 { |
|
95 } |
|
96 |
|
97 void tst_QSql::initTestCase() |
|
98 { |
|
99 } |
|
100 |
|
101 void tst_QSql::cleanupTestCase() |
|
102 { |
|
103 } |
|
104 |
|
105 void tst_QSql::init() |
|
106 { |
|
107 } |
|
108 |
|
109 void tst_QSql::cleanup() |
|
110 { |
|
111 } |
|
112 |
|
113 |
|
114 // this is a very basic test for drivers that cannot create/delete tables |
|
115 // it can be used while developing new drivers, |
|
116 // it's original purpose is to test ODBC Text datasources that are basically |
|
117 // to stupid to do anything more advanced than SELECT/INSERT/UPDATE/DELETE |
|
118 // the datasource has to have a table called "qtest_basictest" consisting |
|
119 // of a field "id"(integer) and "name"(char/varchar). |
|
120 void tst_QSql::basicDriverTest() |
|
121 { |
|
122 int argc = 0; |
|
123 QApplication app( argc, 0, false ); |
|
124 tst_Databases dbs; |
|
125 dbs.open(); |
|
126 |
|
127 foreach( const QString& dbName, dbs.dbNames ) |
|
128 { |
|
129 QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
130 QVERIFY_SQL( db, isValid() ); |
|
131 |
|
132 QStringList tables = db.tables(); |
|
133 QString tableName; |
|
134 |
|
135 if ( tables.contains( "qtest_basictest.txt" ) ) |
|
136 tableName = "qtest_basictest.txt"; |
|
137 else if ( tables.contains( "qtest_basictest" ) ) |
|
138 tableName = "qtest_basictest"; |
|
139 else if ( tables.contains( "QTEST_BASICTEST" ) ) |
|
140 tableName = "QTEST_BASICTEST"; |
|
141 else { |
|
142 QVERIFY( 1 ); |
|
143 continue; |
|
144 } |
|
145 |
|
146 qDebug( qPrintable( QLatin1String( "Testing: " ) + tst_Databases::dbToString( db ) ) ); |
|
147 |
|
148 QSqlRecord rInf = db.record( tableName ); |
|
149 QCOMPARE( rInf.count(), 2 ); |
|
150 QCOMPARE( rInf.fieldName( 0 ).toLower(), QString( "id" ) ); |
|
151 QCOMPARE( rInf.fieldName( 1 ).toLower(), QString( "name" ) ); |
|
152 |
|
153 #ifdef QT3_SUPPORT |
|
154 QSqlRecord* rec = 0; |
|
155 Q3SqlCursor cur( tableName, true, db ); |
|
156 QVERIFY_SQL( cur, select() ); |
|
157 QCOMPARE( cur.count(), 2 ); |
|
158 QCOMPARE( cur.fieldName( 0 ).lower(), QString( "id" ) ); |
|
159 QCOMPARE( cur.fieldName( 1 ).lower(), QString( "name" ) ); |
|
160 |
|
161 rec = cur.primeDelete(); |
|
162 rec->setGenerated( 0, false ); |
|
163 rec->setGenerated( 1, false ); |
|
164 QVERIFY_SQL( cur, del() ); |
|
165 QVERIFY_SQL( cur, select() ); |
|
166 QCOMPARE( cur.at(), int( QSql::BeforeFirst ) ); |
|
167 QVERIFY( !cur.next() ); |
|
168 rec = cur.primeInsert(); |
|
169 rec->setValue( 0, 1 ); |
|
170 rec->setValue( 1, QString( "Harry" ) ); |
|
171 QVERIFY_SQL( cur, insert( false ) ); |
|
172 rec = cur.primeInsert(); |
|
173 rec->setValue( 0, 2 ); |
|
174 rec->setValue( 1, QString( "Trond" ) ); |
|
175 QVERIFY_SQL( cur, insert( true ) ); |
|
176 QVERIFY_SQL( cur, select( cur.index( QString( "id" ) ) ) ); |
|
177 QVERIFY_SQL( cur, next() ); |
|
178 QCOMPARE( cur.value( 0 ).toInt(), 1 ); |
|
179 QCOMPARE( cur.value( 1 ).toString().stripWhiteSpace(), QString( "Harry" ) ); |
|
180 QVERIFY_SQL( cur, next() ); |
|
181 QCOMPARE( cur.value( 0 ).toInt(), 2 ); |
|
182 QCOMPARE( cur.value( 1 ).toString().stripWhiteSpace(), QString( "Trond" ) ); |
|
183 QVERIFY( !cur.next() ); |
|
184 QVERIFY_SQL( cur, first() ); |
|
185 rec = cur.primeUpdate(); |
|
186 rec->setValue( 1, QString( "Vohi" ) ); |
|
187 QVERIFY_SQL( cur, update( true ) ); |
|
188 QVERIFY_SQL( cur, select( "id = 1" ) ); |
|
189 QVERIFY_SQL( cur, next() ); |
|
190 QCOMPARE( cur.value( 0 ).toInt(), 1 ); |
|
191 QCOMPARE( cur.value( 1 ).toString().stripWhiteSpace(), QString( "Vohi" ) ); |
|
192 #endif |
|
193 } |
|
194 |
|
195 dbs.close(); |
|
196 QVERIFY( 1 ); // make sure the test doesn't fail if no database drivers are there |
|
197 } |
|
198 |
|
199 // make sure that the static stuff will be deleted |
|
200 // when using multiple QApplication objects |
|
201 void tst_QSql::open() |
|
202 { |
|
203 int i; |
|
204 int argc = 0; |
|
205 int count = -1; |
|
206 for ( i = 0; i < 10; ++i ) { |
|
207 |
|
208 QApplication app( argc, 0, false ); |
|
209 tst_Databases dbs; |
|
210 |
|
211 dbs.open(); |
|
212 if ( count == -1 ) |
|
213 // first iteration: see how many dbs are open |
|
214 count = (int) dbs.dbNames.count(); |
|
215 else |
|
216 // next iterations: make sure all are opened again |
|
217 QCOMPARE( count, (int)dbs.dbNames.count() ); |
|
218 dbs.close(); |
|
219 } |
|
220 } |
|
221 |
|
222 void tst_QSql::openInvalid() |
|
223 { |
|
224 QSqlDatabase db; |
|
225 QVERIFY(!db.open()); |
|
226 |
|
227 QSqlDatabase db2 = QSqlDatabase::addDatabase("doesnt_exist_will_never_exist", "blah"); |
|
228 QFAIL_SQL(db2, open()); |
|
229 } |
|
230 |
|
231 void tst_QSql::concurrentAccess() |
|
232 { |
|
233 int argc = 0; |
|
234 QApplication app( argc, 0, false ); |
|
235 tst_Databases dbs; |
|
236 |
|
237 dbs.open(); |
|
238 foreach ( const QString& dbName, dbs.dbNames ) { |
|
239 QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
240 QVERIFY( db.isValid() ); |
|
241 if (tst_Databases::isMSAccess(db)) |
|
242 continue; |
|
243 |
|
244 QSqlDatabase ndb = QSqlDatabase::addDatabase( db.driverName(), "tst_QSql::concurrentAccess" ); |
|
245 ndb.setDatabaseName( db.databaseName() ); |
|
246 ndb.setHostName( db.hostName() ); |
|
247 ndb.setPort( db.port() ); |
|
248 ndb.setUserName( db.userName() ); |
|
249 ndb.setPassword( db.password() ); |
|
250 QVERIFY_SQL( ndb, open() ); |
|
251 |
|
252 QCOMPARE( db.tables(), ndb.tables() ); |
|
253 } |
|
254 // no database servers installed - don't fail |
|
255 QVERIFY(1); |
|
256 dbs.close(); |
|
257 } |
|
258 |
|
259 void tst_QSql::openErrorRecovery() |
|
260 { |
|
261 int argc = 0; |
|
262 QApplication app( argc, 0, false ); |
|
263 tst_Databases dbs; |
|
264 |
|
265 dbs.addDbs(); |
|
266 if (dbs.dbNames.isEmpty()) |
|
267 QSKIP("No database drivers installed", SkipAll); |
|
268 foreach ( const QString& dbName, dbs.dbNames ) { |
|
269 QSqlDatabase db = QSqlDatabase::database( dbName, false ); |
|
270 CHECK_DATABASE( db ); |
|
271 |
|
272 QString userName = db.userName(); |
|
273 QString password = db.password(); |
|
274 |
|
275 // force an open error |
|
276 if ( db.open( "dummy130977", "doesnt_exist" ) ) { |
|
277 qDebug( qPrintable(QLatin1String("Promiscuous database server without access control - test skipped for ") + |
|
278 tst_Databases::dbToString( db )) ); |
|
279 QVERIFY(1); |
|
280 continue; |
|
281 } |
|
282 |
|
283 QFAIL_SQL( db, isOpen() ); |
|
284 QVERIFY_SQL( db, isOpenError() ); |
|
285 |
|
286 // now open it |
|
287 if ( !db.open( userName, password ) ) { |
|
288 qDebug() << "Could not open Database " << tst_Databases::dbToString( db ) << |
|
289 ". Assuming DB is down, skipping... (Error: " << |
|
290 tst_Databases::printError( db.lastError() ) << ")"; |
|
291 continue; |
|
292 } |
|
293 QVERIFY_SQL( db, open( userName, password ) ); |
|
294 QVERIFY_SQL( db, isOpen() ); |
|
295 QFAIL_SQL( db, isOpenError() ); |
|
296 db.close(); |
|
297 QFAIL_SQL( db, isOpen() ); |
|
298 |
|
299 // force another open error |
|
300 QFAIL_SQL( db, open( "dummy130977", "doesnt_exist" ) ); |
|
301 QFAIL_SQL( db, isOpen() ); |
|
302 QVERIFY_SQL( db, isOpenError() ); |
|
303 } |
|
304 } |
|
305 |
|
306 void tst_QSql::registerSqlDriver() |
|
307 { |
|
308 int argc = 0; |
|
309 QApplication app( argc, 0, false ); |
|
310 |
|
311 QSqlDatabase::registerSqlDriver( "QSQLTESTDRIVER", new QSqlDriverCreator<QSqlNullDriver> ); |
|
312 QVERIFY( QSqlDatabase::drivers().contains( "QSQLTESTDRIVER" ) ); |
|
313 |
|
314 QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLTESTDRIVER" ); |
|
315 QVERIFY( db.isValid() ); |
|
316 |
|
317 QCOMPARE( db.tables(), QStringList() ); |
|
318 } |
|
319 |
|
320 QTEST_APPLESS_MAIN(tst_QSql) |
|
321 #include "tst_qsql.moc" |