| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2012 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 |
** GNU Lesser General Public License Usage |
| 11 |
** This file may be used under the terms of the GNU Lesser General Public |
| 12 |
** License version 2.1 as published by the Free Software Foundation and |
| 13 |
** appearing in the file LICENSE.LGPL included in the packaging of this |
| 14 |
** file. Please review the following information to ensure the GNU Lesser |
| 15 |
** General Public License version 2.1 requirements will be met: |
| 16 |
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 17 |
** |
| 18 |
** In addition, as a special exception, Nokia gives you certain additional |
| 19 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
| 20 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 21 |
** |
| 22 |
** GNU General Public License Usage |
| 23 |
** Alternatively, this file may be used under the terms of the GNU General |
| 24 |
** Public License version 3.0 as published by the Free Software Foundation |
| 25 |
** and appearing in the file LICENSE.GPL included in the packaging of this |
| 26 |
** file. Please review the following information to ensure the GNU General |
| 27 |
** Public License version 3.0 requirements will be met: |
| 28 |
** http://www.gnu.org/copyleft/gpl.html. |
| 29 |
** |
| 30 |
** Other Usage |
| 31 |
** Alternatively, this file may be used in accordance with the terms and |
| 32 |
** conditions contained in a signed written agreement between you and Nokia. |
| 33 |
** |
| 34 |
** |
| 35 |
** |
| 36 |
** |
| 37 |
** |
| 38 |
** $QT_END_LICENSE$ |
| 39 |
** |
| 40 |
****************************************************************************/ |
| 41 |
|
| 42 |
|
| 43 |
#include <QtTest/QtTest> |
| 44 |
|
| 45 |
#include <q3sqlselectcursor.h> |
| 46 |
|
| 47 |
#include <qsqldriver.h> |
| 48 |
|
| 49 |
#define NODATABASE_SKIP "No database drivers are available in this Qt configuration" |
| 50 |
|
| 51 |
|
| 52 |
#include "../qsqldatabase/tst_databases.h" |
| 53 |
|
| 54 |
//TESTED_FILES= |
| 55 |
|
| 56 |
QT_FORWARD_DECLARE_CLASS(QSqlDatabase) |
| 57 |
|
| 58 |
class tst_Q3SqlSelectCursor : public QObject |
| 59 |
{ |
| 60 |
Q_OBJECT |
| 61 |
|
| 62 |
public: |
| 63 |
tst_Q3SqlSelectCursor(); |
| 64 |
virtual ~tst_Q3SqlSelectCursor(); |
| 65 |
|
| 66 |
|
| 67 |
public slots: |
| 68 |
void initTestCase(); |
| 69 |
void cleanupTestCase(); |
| 70 |
void init(); |
| 71 |
void cleanup(); |
| 72 |
private slots: |
| 73 |
void value_data() { generic_data(); } |
| 74 |
void value(); |
| 75 |
void _exec_data() { generic_data(); } |
| 76 |
void _exec(); |
| 77 |
|
| 78 |
private: |
| 79 |
void generic_data(const QString &engine=QString()); |
| 80 |
void createTestTables( QSqlDatabase db ); |
| 81 |
void dropTestTables( QSqlDatabase db ); |
| 82 |
void populateTestTables( QSqlDatabase db ); |
| 83 |
|
| 84 |
tst_Databases dbs; |
| 85 |
}; |
| 86 |
|
| 87 |
tst_Q3SqlSelectCursor::tst_Q3SqlSelectCursor() |
| 88 |
{ |
| 89 |
} |
| 90 |
|
| 91 |
tst_Q3SqlSelectCursor::~tst_Q3SqlSelectCursor() |
| 92 |
{ |
| 93 |
} |
| 94 |
|
| 95 |
void tst_Q3SqlSelectCursor::generic_data(const QString& engine) |
| 96 |
{ |
| 97 |
if ( dbs.fillTestTable(engine) == 0 ) { |
| 98 |
if(engine.isEmpty()) |
| 99 |
QSKIP( "No database drivers are available in this Qt configuration", SkipAll ); |
| 100 |
else |
| 101 |
QSKIP( (QString("No database drivers of type %1 are available in this Qt configuration").arg(engine)).toLocal8Bit(), SkipAll ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void tst_Q3SqlSelectCursor::createTestTables( QSqlDatabase db ) |
| 106 |
{ |
| 107 |
if ( !db.isValid() ) |
| 108 |
return; |
| 109 |
QSqlQuery q( db ); |
| 110 |
if(tst_Databases::isPostgreSQL(db)) |
| 111 |
QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
| 112 |
// please never ever change this table; otherwise fix all tests ;) |
| 113 |
if (tst_Databases::isMSAccess(db)) |
| 114 |
QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest", __FILE__ ) + " ( id int not null, t_varchar varchar(40) not null," |
| 115 |
"t_char char(40), t_numeric number, primary key (id, t_varchar) )" )); |
| 116 |
else |
| 117 |
QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest", __FILE__ ) + " ( id int not null, t_varchar varchar(40) not null," |
| 118 |
"t_char char(40), t_numeric numeric(6, 3), primary key (id, t_varchar) )" )); |
| 119 |
} |
| 120 |
|
| 121 |
void tst_Q3SqlSelectCursor::dropTestTables( QSqlDatabase db ) |
| 122 |
{ |
| 123 |
tst_Databases::safeDropTable( db, qTableName( "qtest", __FILE__ ) ); |
| 124 |
} |
| 125 |
|
| 126 |
void tst_Q3SqlSelectCursor::populateTestTables( QSqlDatabase db ) |
| 127 |
{ |
| 128 |
if ( !db.isValid() ) |
| 129 |
return; |
| 130 |
QSqlQuery q( db ); |
| 131 |
|
| 132 |
q.exec( "delete from " + qTableName( "qtest", __FILE__ ) ); //non-fatal |
| 133 |
QVERIFY_SQL(q, exec( "insert into " + qTableName( "qtest", __FILE__ ) + " (id, t_varchar, t_char, t_numeric) values ( 0, 'VarChar0', 'Char0', 1.1 )" )); |
| 134 |
QVERIFY_SQL(q, exec( "insert into " + qTableName( "qtest", __FILE__ ) + " (id, t_varchar, t_char, t_numeric) values ( 1, 'VarChar1', 'Char1', 2.2 )" )); |
| 135 |
QVERIFY_SQL(q, exec( "insert into " + qTableName( "qtest", __FILE__ ) + " (id, t_varchar, t_char, t_numeric) values ( 2, 'VarChar2', 'Char2', 3.3 )" )); |
| 136 |
QVERIFY_SQL(q, exec( "insert into " + qTableName( "qtest", __FILE__ ) + " (id, t_varchar, t_char, t_numeric) values ( 3, 'VarChar3', 'Char3', 4.4 )" )); |
| 137 |
} |
| 138 |
|
| 139 |
void tst_Q3SqlSelectCursor::initTestCase() |
| 140 |
{ |
| 141 |
dbs.open(); |
| 142 |
|
| 143 |
for ( QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it ) { |
| 144 |
QSqlDatabase db = QSqlDatabase::database( (*it) ); |
| 145 |
CHECK_DATABASE( db ); |
| 146 |
|
| 147 |
dropTestTables( db ); //in case of leftovers |
| 148 |
createTestTables( db ); |
| 149 |
populateTestTables( db ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
void tst_Q3SqlSelectCursor::cleanupTestCase() |
| 154 |
{ |
| 155 |
for ( QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it ) { |
| 156 |
QSqlDatabase db = QSqlDatabase::database( (*it) ); |
| 157 |
CHECK_DATABASE( db ); |
| 158 |
dropTestTables( db ); |
| 159 |
} |
| 160 |
|
| 161 |
dbs.close(); |
| 162 |
} |
| 163 |
|
| 164 |
void tst_Q3SqlSelectCursor::init() |
| 165 |
{ |
| 166 |
} |
| 167 |
|
| 168 |
void tst_Q3SqlSelectCursor::cleanup() |
| 169 |
{ |
| 170 |
QFETCH( QString, dbName ); |
| 171 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
| 172 |
CHECK_DATABASE( db ); |
| 173 |
|
| 174 |
if ( QTest::currentTestFailed() ) { |
| 175 |
//since Oracle ODBC totally craps out on error, we init again |
| 176 |
db.close(); |
| 177 |
db.open(); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
void tst_Q3SqlSelectCursor::value() |
| 182 |
{ |
| 183 |
QFETCH( QString, dbName ); |
| 184 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
| 185 |
CHECK_DATABASE( db ); |
| 186 |
|
| 187 |
Q3SqlSelectCursor cur( "select * from " + qTableName( "qtest", __FILE__ ) + " order by id", db ); |
| 188 |
QVERIFY( cur.select() ); |
| 189 |
QVERIFY_SQL(cur, isActive()); |
| 190 |
int i = 0; |
| 191 |
while ( cur.next() ) { |
| 192 |
QVERIFY( cur.value( "id" ).toInt() == i ); |
| 193 |
i++; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
void tst_Q3SqlSelectCursor::_exec() |
| 198 |
{ |
| 199 |
QFETCH( QString, dbName ); |
| 200 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
| 201 |
CHECK_DATABASE( db ); |
| 202 |
|
| 203 |
Q3SqlSelectCursor cur( QString(), db ); |
| 204 |
QVERIFY_SQL(cur, isActive() == false); |
| 205 |
|
| 206 |
cur.exec( "select * from " + qTableName( "qtest", __FILE__ ) ); //nothing should happen |
| 207 |
QVERIFY_SQL(cur, isActive()); |
| 208 |
int i = 0; |
| 209 |
while ( cur.next() ) { |
| 210 |
QVERIFY( cur.value( "id" ).toInt() == i ); |
| 211 |
i++; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
QTEST_MAIN(tst_Q3SqlSelectCursor) |
| 217 |
#include "tst_q3sqlselectcursor.moc" |