summaryrefslogtreecommitdiffstats
path: root/tests/auto/sql
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/sql')
-rw-r--r--tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp118
-rw-r--r--tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp11
-rw-r--r--tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp87
3 files changed, 198 insertions, 18 deletions
diff --git a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp
index b22e876c3c..584fcb045a 100644
--- a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp
+++ b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp
@@ -222,6 +222,8 @@ private slots:
void sqlite_real_data() { generic_data("QSQLITE"); }
void sqlite_real();
+ void aggregateFunctionTypes_data() { generic_data(); }
+ void aggregateFunctionTypes();
private:
// returns all database connections
void generic_data(const QString &engine=QString());
@@ -3339,5 +3341,121 @@ void tst_QSqlQuery::sqlite_real()
QCOMPARE(q.value(0).toDouble(), 5.6);
}
+void tst_QSqlQuery::aggregateFunctionTypes()
+{
+ QFETCH(QString, dbName);
+ QSqlDatabase db = QSqlDatabase::database(dbName);
+ CHECK_DATABASE(db);
+ {
+ const QString tableName(qTableName("numericFunctionsWithIntValues", __FILE__));
+ tst_Databases::safeDropTable( db, tableName );
+
+ QSqlQuery q(db);
+ QVERIFY_SQL(q, exec("CREATE TABLE " + tableName + " (id INTEGER)"));
+
+ // First test without any entries
+ QVERIFY_SQL(q, exec("SELECT SUM(id) FROM " + tableName));
+ QVERIFY(q.next());
+ QCOMPARE(q.record().field(0).type(), QVariant::Invalid);
+
+ QVERIFY_SQL(q, exec("INSERT INTO " + tableName + " (id) VALUES (1)"));
+ QVERIFY_SQL(q, exec("INSERT INTO " + tableName + " (id) VALUES (2)"));
+
+ 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);
+
+ QVERIFY_SQL(q, exec("SELECT AVG(id) FROM " + tableName));
+ QVERIFY(q.next());
+ QCOMPARE(q.value(0).toDouble(), 1.5);
+ QCOMPARE(q.record().field(0).type(), QVariant::Double);
+
+ 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);
+
+ QVERIFY_SQL(q, exec("SELECT MIN(id) FROM " + tableName));
+ QVERIFY(q.next());
+ QCOMPARE(q.value(0).toInt(), 1);
+ QCOMPARE(q.record().field(0).type(), QVariant::Int);
+
+ QVERIFY_SQL(q, exec("SELECT MAX(id) FROM " + tableName));
+ QVERIFY(q.next());
+ QCOMPARE(q.value(0).toInt(), 2);
+ QCOMPARE(q.record().field(0).type(), QVariant::Int);
+ }
+ {
+ const QString tableName(qTableName("numericFunctionsWithDoubleValues", __FILE__));
+ tst_Databases::safeDropTable( db, tableName );
+
+ QSqlQuery q(db);
+ QVERIFY_SQL(q, exec("CREATE TABLE " + tableName + " (id DOUBLE)"));
+
+ // First test without any entries
+ QVERIFY_SQL(q, exec("SELECT SUM(id) FROM " + tableName));
+ QVERIFY(q.next());
+ QCOMPARE(q.record().field(0).type(), QVariant::Invalid);
+
+ QVERIFY_SQL(q, exec("INSERT INTO " + tableName + " (id) VALUES (1.5)"));
+ QVERIFY_SQL(q, exec("INSERT INTO " + tableName + " (id) VALUES (2.5)"));
+
+ QVERIFY_SQL(q, exec("SELECT SUM(id) FROM " + tableName));
+ QVERIFY(q.next());
+ QCOMPARE(q.value(0).toDouble(), 4.0);
+ QCOMPARE(q.record().field(0).type(), QVariant::Double);
+
+ QVERIFY_SQL(q, exec("SELECT AVG(id) FROM " + tableName));
+ QVERIFY(q.next());
+ QCOMPARE(q.value(0).toDouble(), 2.0);
+ QCOMPARE(q.record().field(0).type(), QVariant::Double);
+
+ 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);
+
+ QVERIFY_SQL(q, exec("SELECT MIN(id) FROM " + tableName));
+ QVERIFY(q.next());
+ QCOMPARE(q.value(0).toDouble(), 1.5);
+ QCOMPARE(q.record().field(0).type(), QVariant::Double);
+
+ QVERIFY_SQL(q, exec("SELECT MAX(id) FROM " + tableName));
+ QVERIFY(q.next());
+ 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);
+
+ 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__));
+ tst_Databases::safeDropTable( db, tableName );
+
+ QSqlQuery q(db);
+ QVERIFY_SQL(q, exec("CREATE TABLE " + tableName + " (id INTEGER, txt VARCHAR(50))"));
+
+ QVERIFY_SQL(q, exec("SELECT MAX(txt) FROM " + tableName));
+ QVERIFY(q.next());
+ QCOMPARE(q.record().field(0).type(), QVariant::Invalid);
+
+ QVERIFY_SQL(q, exec("INSERT INTO " + tableName + " (id, txt) VALUES (1, 'lower')"));
+ QVERIFY_SQL(q, exec("INSERT INTO " + tableName + " (id, txt) VALUES (2, 'upper')"));
+
+ QVERIFY_SQL(q, exec("SELECT MAX(txt) FROM " + tableName));
+ QVERIFY(q.next());
+ QCOMPARE(q.value(0).toString(), QLatin1String("upper"));
+ QCOMPARE(q.record().field(0).type(), QVariant::String);
+ }
+}
+
QTEST_MAIN( tst_QSqlQuery )
#include "tst_qsqlquery.moc"
diff --git a/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp b/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp
index ddafeea427..ce0d8db1fd 100644
--- a/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp
+++ b/tests/auto/sql/models/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp
@@ -542,6 +542,13 @@ void tst_QSqlRelationalTableModel::setRecord()
model.setSort(0, Qt::AscendingOrder);
QVERIFY_SQL(model, submit());
+ if (model.editStrategy() != QSqlTableModel::OnManualSubmit) {
+ QCOMPARE(model.data(model.index(1, 0)).toInt(), 7);
+ QCOMPARE(model.data(model.index(1, 1)).toString(), QString("tester"));
+ QCOMPARE(model.data(model.index(1, 2)).toString(), QString("herr"));
+ QVERIFY_SQL(model, select());
+ }
+
QCOMPARE(model.data(model.index(3, 0)).toInt(), 7);
QCOMPARE(model.data(model.index(3, 1)).toString(), QString("tester"));
QCOMPARE(model.data(model.index(3, 2)).toString(), QString("herr"));
@@ -599,6 +606,8 @@ void tst_QSqlRelationalTableModel::insertWithStrategies()
QVERIFY_SQL(model, submitAll());
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
+ // The changes were submitted, but there was no automatic select to resort
+ QVERIFY_SQL(model, select());
QCOMPARE(model.data(model.index(0,0)).toInt(), 1);
QCOMPARE(model.data(model.index(0,1)).toString(), QString("harry"));
@@ -1401,6 +1410,8 @@ void tst_QSqlRelationalTableModel::whiteSpaceInIdentifiers()
QVERIFY_SQL(model, insertRecord(-1, rec));
model.submitAll();
+ if (model.editStrategy() != QSqlTableModel::OnManualSubmit)
+ QVERIFY_SQL(model, select());
QCOMPARE(model.data(model.index(0, 0)).toInt(), 3);
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("Washington"));
diff --git a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp
index 270de8292b..448111cd9f 100644
--- a/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp
+++ b/tests/auto/sql/models/qsqltablemodel/tst_qsqltablemodel.cpp
@@ -411,8 +411,11 @@ void tst_QSqlTableModel::setRecord()
} else if ((QSqlTableModel::EditStrategy)submitpolicy == QSqlTableModel::OnRowChange && i == model.rowCount() -1)
model.submit();
else {
- // dataChanged() is not emitted when submitAll() is called
- QCOMPARE(spy.count(), 1);
+ // dataChanged() emitted by selectRow() as well as setRecord()
+ if ((QSqlTableModel::EditStrategy)submitpolicy == QSqlTableModel::OnFieldChange)
+ QCOMPARE(spy.count(), 2);
+ else
+ QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).count(), 2);
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(0)), model.index(i, 0));
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(1)), model.index(i, rec.count() - 1));
@@ -471,8 +474,7 @@ void tst_QSqlTableModel::insertRow()
rec.setValue(0, 42);
rec.setValue(1, QString("francis"));
- // FieldChange updates immediately and resorts
- // Row/Manual submit does not resort
+ // Setting record does not cause resort
QVERIFY(model.setRecord(2, rec));
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
@@ -482,8 +484,23 @@ void tst_QSqlTableModel::insertRow()
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond"));
QCOMPARE(model.data(model.index(1, 2)).toInt(), 2);
- // See comment above setRecord
- if (submitpolicy == QSqlTableModel::OnFieldChange) {
+ QCOMPARE(model.data(model.index(2, 0)).toInt(), 42);
+ QCOMPARE(model.data(model.index(2, 1)).toString(), QString("francis"));
+ QCOMPARE(model.data(model.index(2, 2)).toInt(), 2);
+ QCOMPARE(model.data(model.index(3, 0)).toInt(), 3);
+ QCOMPARE(model.data(model.index(3, 1)).toString(), QString("vohi"));
+ QCOMPARE(model.data(model.index(3, 2)).toInt(), 3);
+
+ QVERIFY(model.submitAll());
+
+ if (submitpolicy == QSqlTableModel::OnManualSubmit) {
+ // After the submit we should have the resorted view
+ QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
+ QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
+ QCOMPARE(model.data(model.index(0, 2)).toInt(), 1);
+ QCOMPARE(model.data(model.index(1, 0)).toInt(), 2);
+ QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond"));
+ QCOMPARE(model.data(model.index(1, 2)).toInt(), 2);
QCOMPARE(model.data(model.index(2, 0)).toInt(), 3);
QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi"));
QCOMPARE(model.data(model.index(2, 2)).toInt(), 3);
@@ -491,6 +508,13 @@ void tst_QSqlTableModel::insertRow()
QCOMPARE(model.data(model.index(3, 1)).toString(), QString("francis"));
QCOMPARE(model.data(model.index(3, 2)).toInt(), 2);
} else {
+ // Submit does not select, therefore not resorted
+ QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
+ QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
+ QCOMPARE(model.data(model.index(0, 2)).toInt(), 1);
+ QCOMPARE(model.data(model.index(1, 0)).toInt(), 2);
+ QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond"));
+ QCOMPARE(model.data(model.index(1, 2)).toInt(), 2);
QCOMPARE(model.data(model.index(2, 0)).toInt(), 42);
QCOMPARE(model.data(model.index(2, 1)).toString(), QString("francis"));
QCOMPARE(model.data(model.index(2, 2)).toInt(), 2);
@@ -499,9 +523,8 @@ void tst_QSqlTableModel::insertRow()
QCOMPARE(model.data(model.index(3, 2)).toInt(), 3);
}
- QVERIFY(model.submitAll());
-
- // After the submit we should have the resorted view
+ QVERIFY(model.select());
+ // After the select we should have the resorted view in all strategies
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1);
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
QCOMPARE(model.data(model.index(0, 2)).toInt(), 1);
@@ -514,7 +537,6 @@ void tst_QSqlTableModel::insertRow()
QCOMPARE(model.data(model.index(3, 0)).toInt(), 42);
QCOMPARE(model.data(model.index(3, 1)).toString(), QString("francis"));
QCOMPARE(model.data(model.index(3, 2)).toInt(), 2);
-
}
void tst_QSqlTableModel::insertRecord()
@@ -647,8 +669,7 @@ void tst_QSqlTableModel::removeRow()
QVERIFY_SQL(model, select());
QCOMPARE(model.rowCount(), 3);
- // headerDataChanged must be emitted by the model when the edit strategy is OnManualSubmit,
- // when OnFieldChange or OnRowChange it's not needed because the model will re-select.
+ // headerDataChanged must be emitted by the model since the row won't vanish until select
qRegisterMetaType<Qt::Orientation>("Qt::Orientation");
QSignalSpy headerDataChangedSpy(&model, SIGNAL(headerDataChanged(Qt::Orientation, int, int)));
@@ -673,7 +694,10 @@ void tst_QSqlTableModel::removeRow()
headerDataChangedSpy.clear();
QVERIFY(model.removeRow(1));
- QCOMPARE(headerDataChangedSpy.count(), 0);
+ QCOMPARE(headerDataChangedSpy.count(), 1);
+ QCOMPARE(model.rowCount(), 3);
+
+ QVERIFY_SQL(model, select());
QCOMPARE(model.rowCount(), 2);
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
@@ -706,6 +730,11 @@ void tst_QSqlTableModel::removeRows()
QCOMPARE(beforeDeleteSpy.count(), 2);
QVERIFY(beforeDeleteSpy.at(0).at(0).toInt() == 0);
QVERIFY(beforeDeleteSpy.at(1).at(0).toInt() == 1);
+ // deleted rows shown as empty until select
+ QCOMPARE(model.rowCount(), 3);
+ QCOMPARE(model.data(model.index(0, 1)).toString(), QString(""));
+ QVERIFY(model.select());
+ // deleted rows are gone
QCOMPARE(model.rowCount(), 1);
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("vohi"));
model.clear();
@@ -730,10 +759,10 @@ void tst_QSqlTableModel::removeRows()
QSignalSpy headerDataChangedSpy(&model, SIGNAL(headerDataChanged(Qt::Orientation, int, int)));
QVERIFY(model.removeRows(0, 2, QModelIndex()));
QCOMPARE(headerDataChangedSpy.count(), 2);
- QCOMPARE(headerDataChangedSpy.at(0).at(1).toInt(), 0);
- QCOMPARE(headerDataChangedSpy.at(0).at(2).toInt(), 0);
- QCOMPARE(headerDataChangedSpy.at(1).at(1).toInt(), 1);
- QCOMPARE(headerDataChangedSpy.at(1).at(2).toInt(), 1);
+ QCOMPARE(headerDataChangedSpy.at(0).at(1).toInt(), 1);
+ QCOMPARE(headerDataChangedSpy.at(0).at(2).toInt(), 1);
+ QCOMPARE(headerDataChangedSpy.at(1).at(1).toInt(), 0);
+ QCOMPARE(headerDataChangedSpy.at(1).at(2).toInt(), 0);
QCOMPARE(model.rowCount(), 3);
QVERIFY(beforeDeleteSpy.count() == 0);
QVERIFY(model.submitAll());
@@ -778,6 +807,14 @@ void tst_QSqlTableModel::removeInsertedRow()
model.submitAll();
+ if (model.editStrategy() != QSqlTableModel::OnManualSubmit) {
+ QCOMPARE(model.rowCount(), 4);
+ QCOMPARE(model.data(model.index(1, 0)).toInt(), 55);
+ QCOMPARE(model.data(model.index(1, 1)).toString(), QString("null columns"));
+ QCOMPARE(model.data(model.index(1, 2)).isNull(), true);
+ QVERIFY(model.select());
+ }
+
QCOMPARE(model.rowCount(), 4);
QCOMPARE(model.data(model.index(3, 0)).toInt(), 55);
QCOMPARE(model.data(model.index(3, 1)).toString(), QString("null columns"));
@@ -785,8 +822,17 @@ void tst_QSqlTableModel::removeInsertedRow()
QVERIFY(model.removeRow(3));
model.submitAll();
- QCOMPARE(model.rowCount(), 3);
+ if (model.editStrategy() != QSqlTableModel::OnManualSubmit) {
+ QCOMPARE(model.rowCount(), 4);
+ QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
+ QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond"));
+ QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi"));
+ QCOMPARE(model.data(model.index(3, 1)).toString(), QString(""));
+ QVERIFY(model.select());
+ }
+
+ QCOMPARE(model.rowCount(), 3);
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry"));
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond"));
QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi"));
@@ -1129,6 +1175,11 @@ void tst_QSqlTableModel::insertRecordBeforeSelect()
buffer.setValue("title", 0);
QVERIFY_SQL(model, insertRecord(1, buffer));
+ if (model.editStrategy() != QSqlTableModel::OnManualSubmit) {
+ QCOMPARE(model.rowCount(), 2);
+ QVERIFY_SQL(model, select());
+ }
+
int rowCount = model.rowCount();
model.clear();
QCOMPARE(model.rowCount(), 0);