diff options
author | Matt Newell <newellm@blur.com> | 2012-03-05 23:24:54 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-03-31 08:39:53 +0200 |
commit | 9b423b6e6920250f97b74aeb86f609cf55a5ee22 (patch) | |
tree | 9140e6f248a552dbe26eea180a228ba2864b45c0 | |
parent | f524470a95487fad7b82cb4c2b613a50126841c3 (diff) |
Expand QtSql tests covering boundValues and boundValueName
Tests added that cover boundValues with positional binding,
and boundValueName.
Change-Id: I2962d76607b716d19d3e0be958109be2f032f2d9
Reviewed-by: Mark Brand <mabrand@mabrand.nl>
-rw-r--r-- | tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp | 87 |
1 files changed, 75 insertions, 12 deletions
diff --git a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp index dab34a89ed..3de65d00ad 100644 --- a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp +++ b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp @@ -1651,6 +1651,24 @@ void tst_QSqlQuery::synonyms() QCOMPARE( rec.field( 2 ).name().toLower(), QString( "t_varchar" ) ); } +// This class is used to test protected QSqlResult methods +class ResultHelper: public QSqlResult +{ + +public: + ResultHelper(): QSqlResult( 0 ) {} // don't call, it's only for stupid compilers + + bool execBatch( bool bindArray = false ) + { + return QSqlResult::execBatch( bindArray ); + } + + QString boundValueName( int pos ) const + { + return QSqlResult::boundValueName( pos ); + } +}; + // It doesn't make sense to split this into several tests void tst_QSqlQuery::prepare_bind_exec() { @@ -1764,33 +1782,90 @@ void tst_QSqlQuery::prepare_bind_exec() QVERIFY_SQL( q, exec( "DELETE FROM " + qtest_prepare ) ); + /*** Below we test QSqlQuery::boundValues() with position arguments. + * Due to the fact that the name of a positional argument is not + * specified by the Qt docs, we only test that the QMap contains + * the correct values and that QSqlResult::boundValueName returns + * the key that corrosponds to the correct value. ***/ QVERIFY( q.prepare( "insert into " + qtest_prepare + " (id, name) values (?, ?)" ) ); q.bindValue( 0, 0 ); q.bindValue( 1, values[ 0 ] ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 0 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), values[0] ); QVERIFY_SQL( q, exec() ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 0 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), values[0] ); + q.addBindValue( 1 ); q.addBindValue( values[ 1 ] ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 1 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), values[1] ); QVERIFY_SQL( q, exec() ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 1 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), values[1] ); + q.addBindValue( 2 ); q.addBindValue( values[ 2 ] ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), values[2] ); QVERIFY_SQL( q, exec() ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), values[2] ); + q.addBindValue( 3 ); q.addBindValue( values[ 3 ] ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 3 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), values[3] ); QVERIFY_SQL( q, exec() ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 3 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), values[3] ); + q.addBindValue( 4 ); q.addBindValue( values[ 4 ] ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 4 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), values[4] ); QVERIFY_SQL( q, exec() ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 4 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), values[4] ); + q.bindValue( 1, values[ 5 ] ); q.bindValue( 0, 5 ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 5 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), values[5] ); QVERIFY_SQL( q, exec() ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 5 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), values[5] ); + q.bindValue( 0, 6 ); q.bindValue( 1, QString() ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 6 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), QString() ); QVERIFY_SQL( q, exec() ); + QCOMPARE( q.boundValues().size(), 2 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 6 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), QString() ); if ( db.driver()->hasFeature( QSqlDriver::Unicode ) ) { q.bindValue( 0, 7 ); q.bindValue( 1, utf8str ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 7 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), utf8str ); QVERIFY_SQL( q, exec() ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(0) ].toInt(), 7 ); + QCOMPARE( q.boundValues()[ ((ResultHelper*)q.result())->boundValueName(1) ].toString(), utf8str ); } QVERIFY_SQL( q, exec( "SELECT * FROM " + qtest_prepare + " order by id" ) ); @@ -1961,18 +2036,6 @@ void tst_QSqlQuery::invalidQuery() QVERIFY( !q.next() ); } -class ResultHelper: public QSqlResult -{ - -public: - ResultHelper(): QSqlResult( 0 ) {} // don't call, it's only for stupid compilers - - bool execBatch( bool bindArray = false ) - { - return QSqlResult::execBatch( bindArray ); - } -}; - void tst_QSqlQuery::batchExec() { QFETCH( QString, dbName ); |