tests/auto/q3sqlselectcursor/tst_q3sqlselectcursor.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 
       
    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" ) + " ( 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" ) + " ( 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" ) );
       
   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" ) ); //non-fatal
       
   133     QVERIFY_SQL(q, exec( "insert into " + qTableName( "qtest" ) + " (id, t_varchar, t_char, t_numeric) values ( 0, 'VarChar0', 'Char0', 1.1 )" ));
       
   134     QVERIFY_SQL(q, exec( "insert into " + qTableName( "qtest" ) + " (id, t_varchar, t_char, t_numeric) values ( 1, 'VarChar1', 'Char1', 2.2 )" ));
       
   135     QVERIFY_SQL(q, exec( "insert into " + qTableName( "qtest" ) + " (id, t_varchar, t_char, t_numeric) values ( 2, 'VarChar2', 'Char2', 3.3 )" ));
       
   136     QVERIFY_SQL(q, exec( "insert into " + qTableName( "qtest" ) + " (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" ) + " 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" ) ); //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"