Fix QSqlQuery test in relation to PSQL support

Some things needed to be corrected for testing with PSQL, this was
checked against the the PostgreSQL documentation to confirm that the
exepected behaviour is correct.

Change-Id: I45a6b343e9eb920fcae2a62910ecc956abcac0f0
Reviewed-by: Mark Brand <mabrand@mabrand.nl>
This commit is contained in:
Andy Shaw 2012-12-20 19:20:27 +01:00 committed by The Qt Project
parent 2ebb714686
commit 69627730ea
2 changed files with 28 additions and 14 deletions

View File

@ -463,6 +463,13 @@ public:
return "blob";
}
static QString dateTimeTypeName(QSqlDatabase db)
{
if (db.driverName().startsWith("QPSQL"))
return QLatin1String("timestamp");
return QLatin1String("datetime");
}
static QString autoFieldName( QSqlDatabase db )
{
if ( db.driverName().startsWith( "QMYSQL" ) )

View File

@ -3406,7 +3406,7 @@ void tst_QSqlQuery::QTBUG_2192()
tst_Databases::safeDropTable( db, tableName );
QSqlQuery q(db);
QVERIFY_SQL(q, exec("CREATE TABLE " + tableName + " (dt DATETIME)"));
QVERIFY_SQL(q, exec(QString("CREATE TABLE " + tableName + " (dt %1)").arg(tst_Databases::dateTimeTypeName(db))));
QVERIFY_SQL(q, prepare("INSERT INTO " + tableName + " (dt) VALUES (?)"));
q.bindValue(0, QVariant(QDateTime(QDate(2012, 7, 4), QTime(23, 59, 59, 999))));
@ -3578,6 +3578,10 @@ void tst_QSqlQuery::aggregateFunctionTypes()
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
QVariant::Type intType = QVariant::Int;
// QPSQL uses LongLong for manipulation of integers
if (db.driverName().startsWith("QPSQL"))
intType = QVariant::LongLong;
{
const QString tableName(qTableName("numericFunctionsWithIntValues", __FILE__));
tst_Databases::safeDropTable( db, tableName );
@ -3591,7 +3595,7 @@ void tst_QSqlQuery::aggregateFunctionTypes()
if (db.driverName().startsWith("QSQLITE"))
QCOMPARE(q.record().field(0).type(), QVariant::Invalid);
else
QCOMPARE(q.record().field(0).type(), QVariant::Int);
QCOMPARE(q.record().field(0).type(), intType);
QVERIFY_SQL(q, exec("INSERT INTO " + tableName + " (id) VALUES (1)"));
QVERIFY_SQL(q, exec("INSERT INTO " + tableName + " (id) VALUES (2)"));
@ -3599,11 +3603,11 @@ void tst_QSqlQuery::aggregateFunctionTypes()
QVERIFY_SQL(q, exec("SELECT SUM(id) FROM " + tableName));
QVERIFY(q.next());
QCOMPARE(q.value(0).toInt(), 3);
QCOMPARE(q.record().field(0).type(), QVariant::Int);
QCOMPARE(q.record().field(0).type(), intType);
QVERIFY_SQL(q, exec("SELECT AVG(id) FROM " + tableName));
QVERIFY(q.next());
if (db.driverName().startsWith("QSQLITE")) {
if (db.driverName().startsWith("QSQLITE") || db.driverName().startsWith("QPSQL")) {
QCOMPARE(q.value(0).toDouble(), 1.5);
QCOMPARE(q.record().field(0).type(), QVariant::Double);
} else {
@ -3614,7 +3618,7 @@ void tst_QSqlQuery::aggregateFunctionTypes()
QVERIFY_SQL(q, exec("SELECT COUNT(id) FROM " + tableName));
QVERIFY(q.next());
QCOMPARE(q.value(0).toInt(), 2);
QCOMPARE(q.record().field(0).type(), QVariant::Int);
QCOMPARE(q.record().field(0).type(), intType);
QVERIFY_SQL(q, exec("SELECT MIN(id) FROM " + tableName));
QVERIFY(q.next());
@ -3657,7 +3661,7 @@ void tst_QSqlQuery::aggregateFunctionTypes()
QVERIFY_SQL(q, exec("SELECT COUNT(id) FROM " + tableName));
QVERIFY(q.next());
QCOMPARE(q.value(0).toInt(), 2);
QCOMPARE(q.record().field(0).type(), QVariant::Int);
QCOMPARE(q.record().field(0).type(), intType);
QVERIFY_SQL(q, exec("SELECT MIN(id) FROM " + tableName));
QVERIFY(q.next());
@ -3669,15 +3673,18 @@ void tst_QSqlQuery::aggregateFunctionTypes()
QCOMPARE(q.value(0).toDouble(), 2.5);
QCOMPARE(q.record().field(0).type(), QVariant::Double);
QVERIFY_SQL(q, exec("SELECT ROUND(id, 1) FROM " + tableName + " WHERE id=1.5"));
QVERIFY(q.next());
QCOMPARE(q.value(0).toDouble(), 1.5);
QCOMPARE(q.record().field(0).type(), QVariant::Double);
// PSQL does not have support for the round() function
if (!db.driverName().startsWith("QPSQL")) {
QVERIFY_SQL(q, exec("SELECT ROUND(id, 1) FROM " + tableName + " WHERE id=1.5"));
QVERIFY(q.next());
QCOMPARE(q.value(0).toDouble(), 1.5);
QCOMPARE(q.record().field(0).type(), QVariant::Double);
QVERIFY_SQL(q, exec("SELECT ROUND(id, 0) FROM " + tableName + " WHERE id=2.5"));
QVERIFY(q.next());
QCOMPARE(q.value(0).toDouble(), 3.0);
QCOMPARE(q.record().field(0).type(), QVariant::Double);
QVERIFY_SQL(q, exec("SELECT ROUND(id, 0) FROM " + tableName + " WHERE id=2.5"));
QVERIFY(q.next());
QCOMPARE(q.value(0).toDouble(), 3.0);
QCOMPARE(q.record().field(0).type(), QVariant::Double);
}
}
{
const QString tableName(qTableName("stringFunctions", __FILE__));