author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 20:15:53 +0300 | |
branch | RCL_3 |
changeset 13 | c0432d11811c |
parent 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
#include "../qsqldatabase/tst_databases.h" |
|
45 |
#include <QtSql> |
|
46 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
47 |
const QString test(qTableName("test", __FILE__)), |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
48 |
test2(qTableName("test2", __FILE__)), |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
49 |
test3(qTableName("test3", __FILE__)); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
50 |
|
0 | 51 |
//TESTED_CLASS= |
52 |
//TESTED_FILES= |
|
53 |
||
54 |
Q_DECLARE_METATYPE(QModelIndex) |
|
55 |
||
56 |
class tst_QSqlTableModel : public QObject |
|
57 |
{ |
|
58 |
Q_OBJECT |
|
59 |
||
60 |
public: |
|
61 |
tst_QSqlTableModel(); |
|
62 |
virtual ~tst_QSqlTableModel(); |
|
63 |
||
64 |
||
65 |
void dropTestTables(); |
|
66 |
void createTestTables(); |
|
67 |
void recreateTestTables(); |
|
68 |
void repopulateTestTables(); |
|
69 |
||
70 |
tst_Databases dbs; |
|
71 |
||
72 |
public slots: |
|
73 |
void initTestCase(); |
|
74 |
void cleanupTestCase(); |
|
75 |
void init(); |
|
76 |
void cleanup(); |
|
77 |
private slots: |
|
78 |
||
79 |
void select_data() { generic_data(); } |
|
80 |
void select(); |
|
81 |
void submitAll_data() { generic_data(); } |
|
82 |
void submitAll(); |
|
83 |
void setRecord_data() { generic_data(); } |
|
84 |
void setRecord(); |
|
85 |
void insertRow_data() { generic_data(); } |
|
86 |
void insertRow(); |
|
87 |
void insertRecord_data() { generic_data(); } |
|
88 |
void insertRecord(); |
|
89 |
void insertMultiRecords_data() { generic_data(); } |
|
90 |
void insertMultiRecords(); |
|
91 |
void removeRow_data() { generic_data(); } |
|
92 |
void removeRow(); |
|
93 |
void removeRows_data() { generic_data(); } |
|
94 |
void removeRows(); |
|
95 |
void removeInsertedRow_data() { generic_data(); } |
|
96 |
void removeInsertedRow(); |
|
97 |
void setFilter_data() { generic_data(); } |
|
98 |
void setFilter(); |
|
99 |
void setInvalidFilter_data() { generic_data(); } |
|
100 |
void setInvalidFilter(); |
|
101 |
||
102 |
void emptyTable_data() { generic_data(); } |
|
103 |
void emptyTable(); |
|
104 |
void tablesAndSchemas_data() { generic_data("QPSQL"); } |
|
105 |
void tablesAndSchemas(); |
|
106 |
void whitespaceInIdentifiers_data() { generic_data(); } |
|
107 |
void whitespaceInIdentifiers(); |
|
108 |
void primaryKeyOrder_data() { generic_data("QSQLITE"); } |
|
109 |
void primaryKeyOrder(); |
|
110 |
||
111 |
void sqlite_bigTable_data() { generic_data("QSQLITE"); } |
|
112 |
void sqlite_bigTable(); |
|
113 |
||
114 |
// bug specific tests |
|
115 |
void insertRecordBeforeSelect_data() { generic_data(); } |
|
116 |
void insertRecordBeforeSelect(); |
|
117 |
void submitAllOnInvalidTable_data() { generic_data(); } |
|
118 |
void submitAllOnInvalidTable(); |
|
119 |
void insertRecordsInLoop_data() { generic_data(); } |
|
120 |
void insertRecordsInLoop(); |
|
121 |
void sqlite_attachedDatabase_data() { generic_data("QSQLITE"); } |
|
122 |
void sqlite_attachedDatabase(); // For task 130799 |
|
123 |
void tableModifyWithBlank_data() { generic_data(); } |
|
124 |
void tableModifyWithBlank(); // For mail task |
|
125 |
||
126 |
void removeColumnAndRow_data() { generic_data(); } |
|
127 |
void removeColumnAndRow(); // task 256032 |
|
128 |
||
129 |
void insertBeforeDelete_data() { generic_data(); } |
|
130 |
void insertBeforeDelete(); |
|
131 |
private: |
|
132 |
void generic_data(const QString& engine=QString()); |
|
133 |
}; |
|
134 |
||
135 |
tst_QSqlTableModel::tst_QSqlTableModel() |
|
136 |
{ |
|
137 |
qRegisterMetaType<QModelIndex>("QModelIndex"); |
|
138 |
dbs.open(); |
|
139 |
} |
|
140 |
||
141 |
tst_QSqlTableModel::~tst_QSqlTableModel() |
|
142 |
{ |
|
143 |
} |
|
144 |
||
145 |
void tst_QSqlTableModel::dropTestTables() |
|
146 |
{ |
|
147 |
for (int i = 0; i < dbs.dbNames.count(); ++i) { |
|
148 |
QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i)); |
|
149 |
QSqlQuery q(db); |
|
150 |
if(tst_Databases::isPostgreSQL(db)) |
|
151 |
QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
|
152 |
||
153 |
QStringList tableNames; |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
154 |
tableNames << test |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
155 |
<< test2 |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
156 |
<< test3 |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
157 |
<< qTableName("test4", __FILE__) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
158 |
<< qTableName("emptytable", __FILE__) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
159 |
<< qTableName("bigtable", __FILE__) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
160 |
<< qTableName("foo", __FILE__); |
0 | 161 |
if (testWhiteSpaceNames(db.driverName())) |
162 |
tableNames << qTableName("qtestw hitespace", db.driver()); |
|
163 |
||
164 |
tst_Databases::safeDropTables(db, tableNames); |
|
165 |
||
166 |
if (db.driverName().startsWith("QPSQL")) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
167 |
q.exec("DROP SCHEMA " + qTableName("testschema", __FILE__) + " CASCADE"); |
0 | 168 |
} |
169 |
} |
|
170 |
} |
|
171 |
||
172 |
void tst_QSqlTableModel::createTestTables() |
|
173 |
{ |
|
174 |
for (int i = 0; i < dbs.dbNames.count(); ++i) { |
|
175 |
QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i)); |
|
176 |
QSqlQuery q(db); |
|
177 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
178 |
QVERIFY_SQL( q, exec("create table " + test + "(id int, name varchar(20), title int)")); |
0 | 179 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
180 |
QVERIFY_SQL( q, exec("create table " + test2 + "(id int, title varchar(20))")); |
0 | 181 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
182 |
QVERIFY_SQL( q, exec("create table " + test3 + "(id int, random varchar(20), randomtwo varchar(20))")); |
0 | 183 |
|
13
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
184 |
if(!tst_Databases::isSqlServer(db)) |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
185 |
QVERIFY_SQL( q, exec("create table " + qTableName("test4", __FILE__) + "(column1 varchar(50), column2 varchar(50), column3 varchar(50))")); |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
186 |
else |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
187 |
QVERIFY_SQL( q, exec("create table " + qTableName("test4", __FILE__) + "(column1 varchar(50), column2 varchar(50) NULL, column3 varchar(50))")); |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
7
diff
changeset
|
188 |
|
0 | 189 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
190 |
QVERIFY_SQL( q, exec("create table " + qTableName("emptytable", __FILE__) + "(id int)")); |
0 | 191 |
|
192 |
if (testWhiteSpaceNames(db.driverName())) { |
|
193 |
QString qry = "create table " + qTableName("qtestw hitespace", db.driver()) + " ("+ db.driver()->escapeIdentifier("a field", QSqlDriver::FieldName) + " int)"; |
|
194 |
QVERIFY_SQL( q, exec(qry)); |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
void tst_QSqlTableModel::repopulateTestTables() |
|
200 |
{ |
|
201 |
for (int i = 0; i < dbs.dbNames.count(); ++i) { |
|
202 |
QSqlDatabase db = QSqlDatabase::database(dbs.dbNames.at(i)); |
|
203 |
QSqlQuery q(db); |
|
204 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
205 |
q.exec("delete from " + test); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
206 |
QVERIFY_SQL( q, exec("insert into " + test + " values(1, 'harry', 1)")); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
207 |
QVERIFY_SQL( q, exec("insert into " + test + " values(2, 'trond', 2)")); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
208 |
QVERIFY_SQL( q, exec("insert into " + test + " values(3, 'vohi', 3)")); |
0 | 209 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
210 |
q.exec("delete from " + test2); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
211 |
QVERIFY_SQL( q, exec("insert into " + test2 + " values(1, 'herr')")); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
212 |
QVERIFY_SQL( q, exec("insert into " + test2 + " values(2, 'mister')")); |
0 | 213 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
214 |
q.exec("delete from " + test3); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
215 |
QVERIFY_SQL( q, exec("insert into " + test3 + " values(1, 'foo', 'bar')")); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
216 |
QVERIFY_SQL( q, exec("insert into " + test3 + " values(2, 'baz', 'joe')")); |
0 | 217 |
} |
218 |
} |
|
219 |
||
220 |
void tst_QSqlTableModel::recreateTestTables() |
|
221 |
{ |
|
222 |
dropTestTables(); |
|
223 |
createTestTables(); |
|
224 |
repopulateTestTables(); |
|
225 |
} |
|
226 |
||
227 |
void tst_QSqlTableModel::generic_data(const QString &engine) |
|
228 |
{ |
|
229 |
if ( dbs.fillTestTable(engine) == 0 ) { |
|
230 |
if(engine.isEmpty()) |
|
231 |
QSKIP( "No database drivers are available in this Qt configuration", SkipAll ); |
|
232 |
else |
|
233 |
QSKIP( (QString("No database drivers of type %1 are available in this Qt configuration").arg(engine)).toLocal8Bit(), SkipAll ); |
|
234 |
} |
|
235 |
} |
|
236 |
||
237 |
void tst_QSqlTableModel::initTestCase() |
|
238 |
{ |
|
239 |
recreateTestTables(); |
|
240 |
} |
|
241 |
||
242 |
void tst_QSqlTableModel::cleanupTestCase() |
|
243 |
{ |
|
244 |
dropTestTables(); |
|
245 |
dbs.close(); |
|
246 |
} |
|
247 |
||
248 |
void tst_QSqlTableModel::init() |
|
249 |
{ |
|
250 |
} |
|
251 |
||
252 |
void tst_QSqlTableModel::cleanup() |
|
253 |
{ |
|
254 |
repopulateTestTables(); |
|
255 |
} |
|
256 |
||
257 |
void tst_QSqlTableModel::select() |
|
258 |
{ |
|
259 |
QFETCH(QString, dbName); |
|
260 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
261 |
CHECK_DATABASE(db); |
|
262 |
||
263 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
264 |
model.setTable(test); |
0 | 265 |
model.setSort(0, Qt::AscendingOrder); |
266 |
QVERIFY_SQL(model, select()); |
|
267 |
||
268 |
QCOMPARE(model.rowCount(), 3); |
|
269 |
QCOMPARE(model.columnCount(), 3); |
|
270 |
||
271 |
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); |
|
272 |
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); |
|
273 |
QCOMPARE(model.data(model.index(0, 2)).toInt(), 1); |
|
274 |
QCOMPARE(model.data(model.index(0, 3)), QVariant()); |
|
275 |
||
276 |
QCOMPARE(model.data(model.index(1, 0)).toInt(), 2); |
|
277 |
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond")); |
|
278 |
QCOMPARE(model.data(model.index(1, 2)).toInt(), 2); |
|
279 |
QCOMPARE(model.data(model.index(1, 3)), QVariant()); |
|
280 |
||
281 |
QCOMPARE(model.data(model.index(2, 0)).toInt(), 3); |
|
282 |
QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi")); |
|
283 |
QCOMPARE(model.data(model.index(2, 2)).toInt(), 3); |
|
284 |
QCOMPARE(model.data(model.index(2, 3)), QVariant()); |
|
285 |
||
286 |
QCOMPARE(model.data(model.index(3, 0)), QVariant()); |
|
287 |
QCOMPARE(model.data(model.index(3, 1)), QVariant()); |
|
288 |
QCOMPARE(model.data(model.index(3, 2)), QVariant()); |
|
289 |
QCOMPARE(model.data(model.index(3, 3)), QVariant()); |
|
290 |
} |
|
291 |
||
292 |
void tst_QSqlTableModel::setRecord() |
|
293 |
{ |
|
294 |
QFETCH(QString, dbName); |
|
295 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
296 |
CHECK_DATABASE(db); |
|
297 |
||
298 |
QList<QSqlTableModel::EditStrategy> policies = QList<QSqlTableModel::EditStrategy>() << QSqlTableModel::OnFieldChange << QSqlTableModel::OnRowChange << QSqlTableModel::OnManualSubmit; |
|
299 |
||
300 |
QString Xsuffix; |
|
301 |
foreach( QSqlTableModel::EditStrategy submitpolicy, policies) { |
|
302 |
||
303 |
QSqlTableModel model(0, db); |
|
304 |
model.setEditStrategy((QSqlTableModel::EditStrategy)submitpolicy); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
305 |
model.setTable(test3); |
0 | 306 |
model.setSort(0, Qt::AscendingOrder); |
307 |
QVERIFY_SQL(model, select()); |
|
308 |
||
309 |
for (int i = 0; i < model.rowCount(); ++i) { |
|
310 |
QSignalSpy spy(&model, SIGNAL(dataChanged(QModelIndex,QModelIndex))); |
|
311 |
||
312 |
QSqlRecord rec = model.record(i); |
|
313 |
rec.setValue(1, rec.value(1).toString() + 'X'); |
|
314 |
rec.setValue(2, rec.value(2).toString() + 'X'); |
|
315 |
QVERIFY(model.setRecord(i, rec)); |
|
316 |
||
317 |
if ((QSqlTableModel::EditStrategy)submitpolicy == QSqlTableModel::OnManualSubmit) |
|
318 |
QVERIFY(model.submitAll()); |
|
319 |
else if ((QSqlTableModel::EditStrategy)submitpolicy == QSqlTableModel::OnRowChange && i == model.rowCount() -1) |
|
320 |
model.submit(); |
|
321 |
else { |
|
322 |
// dataChanged() is not emitted when submitAll() is called |
|
323 |
QCOMPARE(spy.count(), 2); |
|
324 |
QCOMPARE(spy.at(0).count(), 2); |
|
325 |
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(0)), model.index(i, 1)); |
|
326 |
QCOMPARE(qvariant_cast<QModelIndex>(spy.at(0).at(1)), model.index(i, 1)); |
|
327 |
} |
|
328 |
} |
|
329 |
||
330 |
Xsuffix.append('X'); |
|
331 |
||
332 |
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("foo").append(Xsuffix)); |
|
333 |
QCOMPARE(model.data(model.index(0, 2)).toString(), QString("bar").append(Xsuffix)); |
|
334 |
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("baz").append(Xsuffix)); |
|
335 |
QCOMPARE(model.data(model.index(1, 2)).toString(), QString("joe").append(Xsuffix)); |
|
336 |
} |
|
337 |
} |
|
338 |
||
339 |
void tst_QSqlTableModel::insertRow() |
|
340 |
{ |
|
341 |
QFETCH(QString, dbName); |
|
342 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
343 |
CHECK_DATABASE(db); |
|
344 |
||
345 |
QSqlTableModel model(0, db); |
|
346 |
model.setEditStrategy(QSqlTableModel::OnRowChange); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
347 |
model.setTable(test); |
0 | 348 |
model.setSort(0, Qt::AscendingOrder); |
349 |
QVERIFY_SQL(model, select()); |
|
350 |
||
351 |
QVERIFY(model.insertRow(2)); |
|
352 |
QSqlRecord rec = model.record(1); |
|
353 |
rec.setValue(0, 42); |
|
354 |
rec.setValue(1, QString("vohi")); |
|
355 |
QVERIFY(model.setRecord(2, rec)); |
|
356 |
||
357 |
QCOMPARE(model.data(model.index(2, 0)).toInt(), 42); |
|
358 |
QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi")); |
|
359 |
QCOMPARE(model.data(model.index(2, 2)).toInt(), 2); |
|
360 |
||
361 |
QVERIFY(model.submitAll()); |
|
362 |
} |
|
363 |
||
364 |
void tst_QSqlTableModel::insertRecord() |
|
365 |
{ |
|
366 |
QFETCH(QString, dbName); |
|
367 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
368 |
CHECK_DATABASE(db); |
|
369 |
||
370 |
QSqlTableModel model(0, db); |
|
371 |
model.setEditStrategy(QSqlTableModel::OnManualSubmit); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
372 |
model.setTable(test); |
0 | 373 |
model.setSort(0, Qt::AscendingOrder); |
374 |
QVERIFY_SQL(model, select()); |
|
375 |
||
376 |
QSqlRecord rec = model.record(); |
|
377 |
rec.setValue(0, 42); |
|
378 |
rec.setValue(1, QString("vohi")); |
|
379 |
rec.setValue(2, 1); |
|
380 |
QVERIFY(model.insertRecord(1, rec)); |
|
381 |
QCOMPARE(model.rowCount(), 4); |
|
382 |
||
383 |
QCOMPARE(model.data(model.index(1, 0)).toInt(), 42); |
|
384 |
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("vohi")); |
|
385 |
QCOMPARE(model.data(model.index(1, 2)).toInt(), 1); |
|
386 |
||
387 |
model.revertAll(); |
|
388 |
model.setEditStrategy(QSqlTableModel::OnRowChange); |
|
389 |
||
390 |
QVERIFY(model.insertRecord(-1, rec)); |
|
391 |
||
392 |
QCOMPARE(model.data(model.index(3, 0)).toInt(), 42); |
|
393 |
QCOMPARE(model.data(model.index(3, 1)).toString(), QString("vohi")); |
|
394 |
QCOMPARE(model.data(model.index(3, 2)).toInt(), 1); |
|
395 |
} |
|
396 |
||
397 |
void tst_QSqlTableModel::insertMultiRecords() |
|
398 |
{ |
|
399 |
QFETCH(QString, dbName); |
|
400 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
401 |
CHECK_DATABASE(db); |
|
402 |
||
403 |
QSqlTableModel model(0, db); |
|
404 |
model.setEditStrategy(QSqlTableModel::OnManualSubmit); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
405 |
model.setTable(test); |
0 | 406 |
model.setSort(0, Qt::AscendingOrder); |
407 |
QVERIFY_SQL(model, select()); |
|
408 |
||
409 |
QCOMPARE(model.rowCount(), 3); |
|
410 |
||
411 |
QVERIFY(model.insertRow(2)); |
|
412 |
||
413 |
QCOMPARE(model.data(model.index(2, 0)), QVariant()); |
|
414 |
QCOMPARE(model.data(model.index(2, 1)), QVariant()); |
|
415 |
QCOMPARE(model.data(model.index(2, 2)), QVariant()); |
|
416 |
||
417 |
QVERIFY(model.insertRow(3)); |
|
418 |
QVERIFY(model.insertRow(0)); |
|
419 |
||
420 |
QCOMPARE(model.data(model.index(5, 0)).toInt(), 3); |
|
421 |
QCOMPARE(model.data(model.index(5, 1)).toString(), QString("vohi")); |
|
422 |
QCOMPARE(model.data(model.index(5, 2)).toInt(), 3); |
|
423 |
||
424 |
QVERIFY(model.setData(model.index(0, 0), QVariant(42))); |
|
425 |
QVERIFY(model.setData(model.index(3, 0), QVariant(43))); |
|
426 |
QVERIFY(model.setData(model.index(4, 0), QVariant(44))); |
|
427 |
QVERIFY(model.setData(model.index(4, 1), QVariant(QLatin1String("gunnar")))); |
|
428 |
QVERIFY(model.setData(model.index(4, 2), QVariant(1))); |
|
429 |
||
430 |
QVERIFY(model.submitAll()); |
|
431 |
model.clear(); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
432 |
model.setTable(test); |
0 | 433 |
model.setSort(0, Qt::AscendingOrder); |
434 |
QVERIFY_SQL(model, select()); |
|
435 |
||
436 |
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); |
|
437 |
QCOMPARE(model.data(model.index(1, 0)).toInt(), 2); |
|
438 |
QCOMPARE(model.data(model.index(2, 0)).toInt(), 3); |
|
439 |
QCOMPARE(model.data(model.index(3, 0)).toInt(), 42); |
|
440 |
QCOMPARE(model.data(model.index(4, 0)).toInt(), 43); |
|
441 |
QCOMPARE(model.data(model.index(5, 0)).toInt(), 44); |
|
442 |
QCOMPARE(model.data(model.index(5, 1)).toString(), QString("gunnar")); |
|
443 |
QCOMPARE(model.data(model.index(5, 2)).toInt(), 1); |
|
444 |
} |
|
445 |
||
446 |
void tst_QSqlTableModel::submitAll() |
|
447 |
{ |
|
448 |
QFETCH(QString, dbName); |
|
449 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
450 |
CHECK_DATABASE(db); |
|
451 |
||
452 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
453 |
model.setTable(test); |
0 | 454 |
model.setSort(0, Qt::AscendingOrder); |
455 |
model.setEditStrategy(QSqlTableModel::OnManualSubmit); |
|
456 |
QVERIFY_SQL(model, select()); |
|
457 |
||
458 |
QVERIFY(model.setData(model.index(0, 1), "harry2", Qt::EditRole)); |
|
459 |
QVERIFY(model.setData(model.index(1, 1), "trond2", Qt::EditRole)); |
|
460 |
||
461 |
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2")); |
|
462 |
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond2")); |
|
463 |
||
464 |
QVERIFY_SQL(model, submitAll()); |
|
465 |
||
466 |
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry2")); |
|
467 |
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond2")); |
|
468 |
||
469 |
QVERIFY(model.setData(model.index(0, 1), "harry", Qt::EditRole)); |
|
470 |
QVERIFY(model.setData(model.index(1, 1), "trond", Qt::EditRole)); |
|
471 |
||
472 |
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); |
|
473 |
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond")); |
|
474 |
||
475 |
QVERIFY_SQL(model, submitAll()); |
|
476 |
||
477 |
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); |
|
478 |
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond")); |
|
479 |
} |
|
480 |
||
481 |
void tst_QSqlTableModel::removeRow() |
|
482 |
{ |
|
483 |
QFETCH(QString, dbName); |
|
484 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
485 |
CHECK_DATABASE(db); |
|
486 |
||
487 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
488 |
model.setTable(test); |
0 | 489 |
model.setSort(0, Qt::AscendingOrder); |
490 |
model.setEditStrategy(QSqlTableModel::OnManualSubmit); |
|
491 |
QVERIFY_SQL(model, select()); |
|
492 |
QCOMPARE(model.rowCount(), 3); |
|
493 |
||
494 |
// headerDataChanged must be emitted by the model when the edit strategy is OnManualSubmit, |
|
495 |
// when OnFieldChange or OnRowChange it's not needed because the model will re-select. |
|
496 |
qRegisterMetaType<Qt::Orientation>("Qt::Orientation"); |
|
497 |
QSignalSpy headerDataChangedSpy(&model, SIGNAL(headerDataChanged(Qt::Orientation, int, int))); |
|
498 |
||
499 |
QVERIFY(model.removeRow(1)); |
|
500 |
QCOMPARE(headerDataChangedSpy.count(), 1); |
|
501 |
QCOMPARE(*static_cast<const Qt::Orientation *>(headerDataChangedSpy.at(0).value(0).constData()), Qt::Vertical); |
|
502 |
QCOMPARE(headerDataChangedSpy.at(0).at(1).toInt(), 1); |
|
503 |
QCOMPARE(headerDataChangedSpy.at(0).at(2).toInt(), 1); |
|
504 |
QVERIFY(model.submitAll()); |
|
505 |
QCOMPARE(model.rowCount(), 2); |
|
506 |
||
507 |
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); |
|
508 |
QCOMPARE(model.data(model.index(1, 0)).toInt(), 3); |
|
509 |
model.clear(); |
|
510 |
||
511 |
recreateTestTables(); |
|
512 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
513 |
model.setTable(test); |
0 | 514 |
model.setEditStrategy(QSqlTableModel::OnRowChange); |
515 |
QVERIFY_SQL(model, select()); |
|
516 |
QCOMPARE(model.rowCount(), 3); |
|
517 |
||
518 |
headerDataChangedSpy.clear(); |
|
519 |
QVERIFY(model.removeRow(1)); |
|
520 |
QCOMPARE(headerDataChangedSpy.count(), 0); |
|
521 |
QCOMPARE(model.rowCount(), 2); |
|
522 |
||
523 |
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); |
|
524 |
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("vohi")); |
|
525 |
} |
|
526 |
||
527 |
void tst_QSqlTableModel::removeRows() |
|
528 |
{ |
|
529 |
QFETCH(QString, dbName); |
|
530 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
531 |
CHECK_DATABASE(db); |
|
532 |
||
533 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
534 |
model.setTable(test); |
0 | 535 |
model.setSort(0, Qt::AscendingOrder); |
536 |
model.setEditStrategy(QSqlTableModel::OnFieldChange); |
|
537 |
QVERIFY_SQL(model, select()); |
|
538 |
QCOMPARE(model.rowCount(), 3); |
|
539 |
||
540 |
QSignalSpy beforeDeleteSpy(&model, SIGNAL(beforeDelete(int))); |
|
541 |
QVERIFY_SQL(model, removeRows(0, 2)); |
|
542 |
QVERIFY(beforeDeleteSpy.count() == 2); |
|
543 |
QVERIFY(beforeDeleteSpy.at(0).at(0).toInt() == 0); |
|
544 |
QVERIFY(beforeDeleteSpy.at(1).at(0).toInt() == 1); |
|
545 |
QCOMPARE(model.rowCount(), 1); |
|
546 |
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("vohi")); |
|
547 |
model.clear(); |
|
548 |
||
549 |
recreateTestTables(); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
550 |
model.setTable(test); |
0 | 551 |
model.setEditStrategy(QSqlTableModel::OnManualSubmit); |
552 |
QVERIFY_SQL(model, select()); |
|
553 |
QCOMPARE(model.rowCount(), 3); |
|
554 |
beforeDeleteSpy.clear(); |
|
555 |
||
556 |
// When the edit strategy is OnManualSubmit the beforeDelete() signal |
|
557 |
// isn't emitted until submitAll() is called. |
|
558 |
qRegisterMetaType<Qt::Orientation>("Qt::Orientation"); |
|
559 |
QSignalSpy headerDataChangedSpy(&model, SIGNAL(headerDataChanged(Qt::Orientation, int, int))); |
|
560 |
QVERIFY(model.removeRows(0, 2, QModelIndex())); |
|
561 |
QCOMPARE(headerDataChangedSpy.count(), 2); |
|
562 |
QCOMPARE(headerDataChangedSpy.at(0).at(1).toInt(), 0); |
|
563 |
QCOMPARE(headerDataChangedSpy.at(0).at(2).toInt(), 0); |
|
564 |
QCOMPARE(headerDataChangedSpy.at(1).at(1).toInt(), 1); |
|
565 |
QCOMPARE(headerDataChangedSpy.at(1).at(2).toInt(), 1); |
|
566 |
QCOMPARE(model.rowCount(), 3); |
|
567 |
QVERIFY(beforeDeleteSpy.count() == 0); |
|
568 |
QVERIFY(model.submitAll()); |
|
569 |
QVERIFY(beforeDeleteSpy.count() == 2); |
|
570 |
QVERIFY(beforeDeleteSpy.at(0).at(0).toInt() == 0); |
|
571 |
QVERIFY(beforeDeleteSpy.at(1).at(0).toInt() == 1); |
|
572 |
QCOMPARE(model.rowCount(), 1); |
|
573 |
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("vohi")); |
|
574 |
} |
|
575 |
||
576 |
void tst_QSqlTableModel::removeInsertedRow() |
|
577 |
{ |
|
578 |
QFETCH(QString, dbName); |
|
579 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
580 |
CHECK_DATABASE(db); |
|
581 |
||
582 |
for (int i = 0; i <= 1; ++i) { |
|
583 |
||
584 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
585 |
model.setTable(test); |
0 | 586 |
model.setSort(0, Qt::AscendingOrder); |
587 |
||
588 |
model.setEditStrategy(i == 0 |
|
589 |
? QSqlTableModel::OnRowChange : QSqlTableModel::OnManualSubmit); |
|
590 |
QVERIFY_SQL(model, select()); |
|
591 |
QCOMPARE(model.rowCount(), 3); |
|
592 |
||
593 |
QVERIFY(model.insertRow(1)); |
|
594 |
QCOMPARE(model.rowCount(), 4); |
|
595 |
||
596 |
QVERIFY(model.removeRow(1)); |
|
597 |
QCOMPARE(model.rowCount(), 3); |
|
598 |
||
599 |
QCOMPARE(model.data(model.index(0, 1)).toString(), QString("harry")); |
|
600 |
QCOMPARE(model.data(model.index(1, 1)).toString(), QString("trond")); |
|
601 |
QCOMPARE(model.data(model.index(2, 1)).toString(), QString("vohi")); |
|
602 |
model.clear(); |
|
603 |
||
604 |
recreateTestTables(); |
|
605 |
} |
|
606 |
} |
|
607 |
||
608 |
void tst_QSqlTableModel::emptyTable() |
|
609 |
{ |
|
610 |
QFETCH(QString, dbName); |
|
611 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
612 |
CHECK_DATABASE(db); |
|
613 |
||
614 |
QSqlTableModel model(0, db); |
|
615 |
QCOMPARE(model.rowCount(), 0); |
|
616 |
QCOMPARE(model.columnCount(), 0); |
|
617 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
618 |
model.setTable(qTableName("emptytable", __FILE__)); |
0 | 619 |
QCOMPARE(model.rowCount(), 0); |
620 |
QCOMPARE(model.columnCount(), 1); |
|
621 |
||
622 |
QVERIFY_SQL(model, select()); |
|
623 |
QCOMPARE(model.rowCount(), 0); |
|
624 |
QCOMPARE(model.columnCount(), 1); |
|
625 |
} |
|
626 |
||
627 |
void tst_QSqlTableModel::tablesAndSchemas() |
|
628 |
{ |
|
629 |
QFETCH(QString, dbName); |
|
630 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
631 |
CHECK_DATABASE(db); |
|
632 |
||
633 |
QSqlQuery q(db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
634 |
q.exec("DROP SCHEMA " + qTableName("testschema", __FILE__) + " CASCADE"); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
635 |
QVERIFY_SQL( q, exec("create schema " + qTableName("testschema", __FILE__))); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
636 |
QString tableName = qTableName("testschema", __FILE__) + '.' + qTableName("testtable", __FILE__); |
0 | 637 |
QVERIFY_SQL( q, exec("create table " + tableName + "(id int)")); |
638 |
QVERIFY_SQL( q, exec("insert into " + tableName + " values(1)")); |
|
639 |
QVERIFY_SQL( q, exec("insert into " + tableName + " values(2)")); |
|
640 |
||
641 |
QSqlTableModel model(0, db); |
|
642 |
model.setTable(tableName); |
|
643 |
QVERIFY_SQL(model, select()); |
|
644 |
QCOMPARE(model.rowCount(), 2); |
|
645 |
QCOMPARE(model.columnCount(), 1); |
|
646 |
} |
|
647 |
||
648 |
void tst_QSqlTableModel::whitespaceInIdentifiers() |
|
649 |
{ |
|
650 |
QFETCH(QString, dbName); |
|
651 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
652 |
CHECK_DATABASE(db); |
|
653 |
||
654 |
if (!testWhiteSpaceNames(db.driverName())) |
|
655 |
QSKIP("DBMS doesn't support whitespaces in identifiers", SkipSingle); |
|
656 |
||
657 |
QString tableName = qTableName("qtestw hitespace", db.driver()); |
|
658 |
||
659 |
QSqlTableModel model(0, db); |
|
660 |
model.setTable(tableName); |
|
661 |
QVERIFY_SQL(model, select()); |
|
662 |
} |
|
663 |
||
664 |
void tst_QSqlTableModel::primaryKeyOrder() |
|
665 |
{ |
|
666 |
QFETCH(QString, dbName); |
|
667 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
668 |
CHECK_DATABASE(db); |
|
669 |
||
670 |
QSqlQuery q(db); |
|
671 |
||
672 |
if(tst_Databases::isPostgreSQL(db)) |
|
673 |
QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
|
674 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
675 |
QVERIFY_SQL( q, exec("create table "+qTableName("foo", __FILE__)+"(a varchar(20), id int not null primary key, b varchar(20))")); |
0 | 676 |
|
677 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
678 |
model.setTable(qTableName("foo", __FILE__)); |
0 | 679 |
|
680 |
QSqlIndex pk = model.primaryKey(); |
|
681 |
QCOMPARE(pk.count(), 1); |
|
682 |
QCOMPARE(pk.fieldName(0), QLatin1String("id")); |
|
683 |
||
684 |
QVERIFY(model.insertRow(0)); |
|
685 |
QVERIFY(model.setData(model.index(0, 0), "hello")); |
|
686 |
QVERIFY(model.setData(model.index(0, 1), 42)); |
|
687 |
QVERIFY(model.setData(model.index(0, 2), "blah")); |
|
688 |
QVERIFY_SQL(model, submitAll()); |
|
689 |
||
690 |
QVERIFY(model.setData(model.index(0, 1), 43)); |
|
691 |
QVERIFY_SQL(model, submitAll()); |
|
692 |
||
693 |
QCOMPARE(model.data(model.index(0, 1)).toInt(), 43); |
|
694 |
} |
|
695 |
||
696 |
void tst_QSqlTableModel::setInvalidFilter() |
|
697 |
{ |
|
698 |
QFETCH(QString, dbName); |
|
699 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
700 |
CHECK_DATABASE(db); |
|
701 |
||
702 |
// set an invalid filter, make sure it fails |
|
703 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
704 |
model.setTable(test); |
0 | 705 |
model.setFilter("blahfahsel"); |
706 |
||
707 |
QCOMPARE(model.filter(), QString("blahfahsel")); |
|
708 |
QVERIFY(!model.select()); |
|
709 |
||
710 |
// set a valid filter later, make sure if passes |
|
711 |
model.setFilter("id = 1"); |
|
712 |
QVERIFY_SQL(model, select()); |
|
713 |
} |
|
714 |
||
715 |
void tst_QSqlTableModel::setFilter() |
|
716 |
{ |
|
717 |
QFETCH(QString, dbName); |
|
718 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
719 |
CHECK_DATABASE(db); |
|
720 |
||
721 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
722 |
model.setTable(test); |
0 | 723 |
model.setFilter("id = 1"); |
724 |
QCOMPARE(model.filter(), QString("id = 1")); |
|
725 |
QVERIFY_SQL(model, select()); |
|
726 |
||
727 |
QCOMPARE(model.rowCount(), 1); |
|
728 |
QCOMPARE(model.data(model.index(0, 0)).toInt(), 1); |
|
729 |
||
730 |
QSignalSpy rowsRemovedSpy(&model, SIGNAL(rowsRemoved(QModelIndex,int,int))); |
|
731 |
QSignalSpy rowsAboutToBeRemovedSpy(&model, |
|
732 |
SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int))); |
|
733 |
QSignalSpy rowsInsertedSpy(&model, SIGNAL(rowsInserted(QModelIndex,int,int))); |
|
734 |
QSignalSpy rowsAboutToBeInsertedSpy(&model, |
|
735 |
SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int))); |
|
736 |
model.setFilter("id = 2"); |
|
737 |
||
738 |
// check the signals |
|
739 |
QCOMPARE(rowsAboutToBeRemovedSpy.count(), 1); |
|
740 |
QCOMPARE(rowsRemovedSpy.count(), 1); |
|
741 |
QCOMPARE(rowsAboutToBeInsertedSpy.count(), 1); |
|
742 |
QCOMPARE(rowsInsertedSpy.count(), 1); |
|
743 |
QList<QVariant> args = rowsAboutToBeRemovedSpy.takeFirst(); |
|
744 |
QCOMPARE(args.count(), 3); |
|
745 |
QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), QModelIndex()); |
|
746 |
QCOMPARE(args.at(1).toInt(), 0); |
|
747 |
QCOMPARE(args.at(2).toInt(), 0); |
|
748 |
args = rowsRemovedSpy.takeFirst(); |
|
749 |
QCOMPARE(args.count(), 3); |
|
750 |
QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), QModelIndex()); |
|
751 |
QCOMPARE(args.at(1).toInt(), 0); |
|
752 |
QCOMPARE(args.at(2).toInt(), 0); |
|
753 |
args = rowsInsertedSpy.takeFirst(); |
|
754 |
QCOMPARE(args.count(), 3); |
|
755 |
QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), QModelIndex()); |
|
756 |
QCOMPARE(args.at(1).toInt(), 0); |
|
757 |
QCOMPARE(args.at(2).toInt(), 0); |
|
758 |
args = rowsAboutToBeInsertedSpy.takeFirst(); |
|
759 |
QCOMPARE(args.count(), 3); |
|
760 |
QCOMPARE(qvariant_cast<QModelIndex>(args.at(0)), QModelIndex()); |
|
761 |
QCOMPARE(args.at(1).toInt(), 0); |
|
762 |
QCOMPARE(args.at(2).toInt(), 0); |
|
763 |
||
764 |
QCOMPARE(model.rowCount(), 1); |
|
765 |
QCOMPARE(model.data(model.index(0, 0)).toInt(), 2); |
|
766 |
} |
|
767 |
||
768 |
void tst_QSqlTableModel::sqlite_bigTable() |
|
769 |
{ |
|
770 |
QFETCH(QString, dbName); |
|
771 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
772 |
CHECK_DATABASE(db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
773 |
const QString bigtable(qTableName("bigtable", __FILE__)); |
0 | 774 |
|
775 |
bool hasTransactions = db.driver()->hasFeature(QSqlDriver::Transactions); |
|
776 |
if (hasTransactions) QVERIFY(db.transaction()); |
|
777 |
QSqlQuery q(db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
778 |
QVERIFY_SQL( q, exec("create table "+bigtable+"(id int primary key, name varchar)")); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
779 |
QVERIFY_SQL( q, prepare("insert into "+bigtable+"(id, name) values (?, ?)")); |
0 | 780 |
QTime startTime; |
781 |
startTime.start(); |
|
782 |
for (int i = 0; i < 10000; ++i) { |
|
783 |
q.addBindValue(i); |
|
784 |
q.addBindValue(QString::number(i)); |
|
785 |
if(i%1000 == 0 && startTime.elapsed() > 5000) |
|
786 |
qDebug() << i << "records written"; |
|
787 |
QVERIFY_SQL( q, exec()); |
|
788 |
} |
|
789 |
q.clear(); |
|
790 |
if (hasTransactions) QVERIFY(db.commit()); |
|
791 |
||
792 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
793 |
model.setTable(bigtable); |
0 | 794 |
QVERIFY_SQL(model, select()); |
795 |
||
796 |
QSqlRecord rec = model.record(); |
|
797 |
rec.setValue("id", 424242); |
|
798 |
rec.setValue("name", "Guillaume"); |
|
799 |
QVERIFY_SQL(model, insertRecord(-1, rec)); |
|
800 |
||
801 |
model.clear(); |
|
802 |
} |
|
803 |
||
804 |
// For task 118547: couldn't insert records unless select() |
|
805 |
// had first been called. |
|
806 |
void tst_QSqlTableModel::insertRecordBeforeSelect() |
|
807 |
{ |
|
808 |
QFETCH(QString, dbName); |
|
809 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
810 |
CHECK_DATABASE(db); |
|
811 |
||
812 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
813 |
model.setTable(test); |
0 | 814 |
QCOMPARE(model.lastError().type(), QSqlError::NoError); |
815 |
||
816 |
QSqlRecord buffer = model.record(); |
|
817 |
buffer.setValue("id", 13); |
|
818 |
buffer.setValue("name", QString("The Lion King")); |
|
819 |
buffer.setValue("title", 0); |
|
820 |
QVERIFY_SQL(model, insertRecord(-1, buffer)); |
|
821 |
||
822 |
buffer.setValue("id", 26); |
|
823 |
buffer.setValue("name", QString("T. Leary")); |
|
824 |
buffer.setValue("title", 0); |
|
825 |
QVERIFY_SQL(model, insertRecord(1, buffer)); |
|
826 |
||
827 |
int rowCount = model.rowCount(); |
|
828 |
model.clear(); |
|
829 |
QCOMPARE(model.rowCount(), 0); |
|
830 |
||
831 |
QSqlTableModel model2(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
832 |
model2.setTable(test); |
0 | 833 |
QVERIFY_SQL(model2, select()); |
834 |
QCOMPARE(model2.rowCount(), rowCount); |
|
835 |
} |
|
836 |
||
837 |
// For task 118547: set errors if table doesn't exist and if records |
|
838 |
// are inserted and submitted on a non-existing table. |
|
839 |
void tst_QSqlTableModel::submitAllOnInvalidTable() |
|
840 |
{ |
|
841 |
QFETCH(QString, dbName); |
|
842 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
843 |
CHECK_DATABASE(db); |
|
844 |
||
845 |
QSqlTableModel model(0, db); |
|
846 |
model.setEditStrategy(QSqlTableModel::OnManualSubmit); |
|
847 |
||
848 |
// setTable returns a void, so the error can only be caught by |
|
849 |
// manually checking lastError(). ### Qt5: This should be changed! |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
850 |
model.setTable(qTableName("invalidTable", __FILE__)); |
0 | 851 |
QCOMPARE(model.lastError().type(), QSqlError::StatementError); |
852 |
||
853 |
// This will give us an empty record which is expected behavior |
|
854 |
QSqlRecord buffer = model.record(); |
|
855 |
buffer.setValue("bogus", 1000); |
|
856 |
buffer.setValue("bogus2", QString("I will go nowhere!")); |
|
857 |
||
858 |
// Inserting the record into the *model* will work (OnManualSubmit) |
|
859 |
QVERIFY_SQL(model, insertRecord(-1, buffer)); |
|
860 |
||
861 |
// The submit and select shall fail because the table doesn't exist |
|
862 |
QEXPECT_FAIL("", "The table doesn't exist: submitAll() shall fail", |
|
863 |
Continue); |
|
864 |
QVERIFY_SQL(model, submitAll()); |
|
865 |
QEXPECT_FAIL("", "The table doesn't exist: select() shall fail", |
|
866 |
Continue); |
|
867 |
QVERIFY_SQL(model, select()); |
|
868 |
} |
|
869 |
||
870 |
// For task 147575: the rowsRemoved signal emitted from the model was lying |
|
871 |
void tst_QSqlTableModel::insertRecordsInLoop() |
|
872 |
{ |
|
873 |
QFETCH(QString, dbName); |
|
874 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
875 |
CHECK_DATABASE(db); |
|
876 |
||
877 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
878 |
model.setTable(test); |
0 | 879 |
model.setEditStrategy(QSqlTableModel::OnManualSubmit); |
880 |
model.select(); |
|
881 |
||
882 |
QSqlRecord record = model.record(); |
|
883 |
record.setValue(0, 10); |
|
884 |
record.setValue(1, "Testman"); |
|
885 |
record.setValue(2, 1); |
|
886 |
||
887 |
QSignalSpy spyRowsRemoved(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int))); |
|
888 |
QSignalSpy spyRowsInserted(&model, SIGNAL(rowsInserted(const QModelIndex &, int, int))); |
|
889 |
for (int i = 0; i < 10; i++) { |
|
890 |
QVERIFY(model.insertRecord(model.rowCount(), record)); |
|
891 |
QCOMPARE(spyRowsInserted.at(i).at(1).toInt(), i+3); // The table already contains three rows |
|
892 |
QCOMPARE(spyRowsInserted.at(i).at(2).toInt(), i+3); |
|
893 |
} |
|
894 |
model.submitAll(); // submitAll() calls select() which clears and repopulates the table |
|
895 |
||
896 |
int firstRowIndex = 0, lastRowIndex = 12; |
|
897 |
QCOMPARE(spyRowsRemoved.count(), 1); |
|
898 |
QCOMPARE(spyRowsRemoved.at(0).at(1).toInt(), firstRowIndex); |
|
899 |
QCOMPARE(spyRowsRemoved.at(0).at(2).toInt(), lastRowIndex); |
|
900 |
||
901 |
QCOMPARE(spyRowsInserted.at(10).at(1).toInt(), firstRowIndex); |
|
902 |
QCOMPARE(spyRowsInserted.at(10).at(2).toInt(), lastRowIndex); |
|
903 |
QCOMPARE(spyRowsInserted.count(), 11); |
|
904 |
||
905 |
QCOMPARE(model.rowCount(), 13); |
|
906 |
QCOMPARE(model.columnCount(), 3); |
|
907 |
} |
|
908 |
||
909 |
void tst_QSqlTableModel::sqlite_attachedDatabase() |
|
910 |
{ |
|
911 |
QFETCH(QString, dbName); |
|
912 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
913 |
CHECK_DATABASE(db); |
|
914 |
if(db.databaseName() == ":memory:") |
|
915 |
QSKIP(":memory: database, skipping test", SkipSingle); |
|
916 |
||
917 |
QSqlDatabase attachedDb = QSqlDatabase::cloneDatabase(db, db.driverName() + QLatin1String("attached")); |
|
918 |
attachedDb.setDatabaseName(db.databaseName()+QLatin1String("attached.dat")); |
|
919 |
QVERIFY_SQL(attachedDb, open()); |
|
920 |
QSqlQuery q(attachedDb); |
|
921 |
tst_Databases::safeDropTables(attachedDb, QStringList() << "atest" << "atest2"); |
|
922 |
QVERIFY_SQL( q, exec("CREATE TABLE atest(id int, text varchar(20))")); |
|
923 |
QVERIFY_SQL( q, exec("CREATE TABLE atest2(id int, text varchar(20))")); |
|
924 |
QVERIFY_SQL( q, exec("INSERT INTO atest VALUES(1, 'attached-atest')")); |
|
925 |
QVERIFY_SQL( q, exec("INSERT INTO atest2 VALUES(2, 'attached-atest2')")); |
|
926 |
||
927 |
QSqlQuery q2(db); |
|
928 |
tst_Databases::safeDropTable(db, "atest"); |
|
929 |
QVERIFY_SQL(q2, exec("CREATE TABLE atest(id int, text varchar(20))")); |
|
930 |
QVERIFY_SQL(q2, exec("INSERT INTO atest VALUES(3, 'main')")); |
|
931 |
QVERIFY_SQL(q2, exec("ATTACH DATABASE \""+attachedDb.databaseName()+"\" as adb")); |
|
932 |
||
933 |
// This should query the table in the attached database (schema supplied) |
|
934 |
QSqlTableModel model(0, db); |
|
935 |
model.setTable("adb.atest"); |
|
936 |
QVERIFY_SQL(model, select()); |
|
937 |
QCOMPARE(model.rowCount(), 1); |
|
938 |
QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 1); |
|
939 |
QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QLatin1String("attached-atest")); |
|
940 |
||
941 |
// This should query the table in the attached database (unique tablename) |
|
942 |
model.setTable("atest2"); |
|
943 |
QVERIFY_SQL(model, select()); |
|
944 |
QCOMPARE(model.rowCount(), 1); |
|
945 |
QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 2); |
|
946 |
QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QLatin1String("attached-atest2")); |
|
947 |
||
948 |
// This should query the table in the main database (tables in main db has 1st priority) |
|
949 |
model.setTable("atest"); |
|
950 |
QVERIFY_SQL(model, select()); |
|
951 |
QCOMPARE(model.rowCount(), 1); |
|
952 |
QCOMPARE(model.data(model.index(0, 0), Qt::DisplayRole).toInt(), 3); |
|
953 |
QCOMPARE(model.data(model.index(0, 1), Qt::DisplayRole).toString(), QLatin1String("main")); |
|
954 |
} |
|
955 |
||
956 |
||
957 |
void tst_QSqlTableModel::tableModifyWithBlank() |
|
958 |
{ |
|
959 |
QFETCH(QString, dbName); |
|
960 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
961 |
CHECK_DATABASE(db); |
|
962 |
||
963 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
964 |
model.setTable(qTableName("test4", __FILE__)); |
0 | 965 |
model.select(); |
966 |
||
967 |
//generate a time stamp for the test. Add one second to the current time to make sure |
|
968 |
//it is different than the QSqlQuery test. |
|
969 |
QString timeString=QDateTime::currentDateTime().addSecs(1).toString(Qt::ISODate); |
|
970 |
||
971 |
//insert a new row, with column0 being the timestamp. |
|
972 |
//Should be equivalent to QSqlQuery INSERT INTO... command) |
|
973 |
QVERIFY_SQL(model, insertRow(0)); |
|
974 |
QVERIFY_SQL(model, setData(model.index(0,0),timeString)); |
|
975 |
QVERIFY_SQL(model, submitAll()); |
|
976 |
||
977 |
//set a filter on the table so the only record we get is the one we just made |
|
978 |
//I could just do another setData command, but I want to make sure the TableModel |
|
979 |
//matches exactly what is stored in the database |
|
980 |
model.setFilter("column1='"+timeString+"'"); //filter to get just the newly entered row |
|
981 |
QVERIFY_SQL(model, select()); |
|
982 |
||
983 |
//Make sure we only get one record, and that it is the one we just made |
|
984 |
QCOMPARE(model.rowCount(), 1); //verify only one entry |
|
985 |
QCOMPARE(model.record(0).value(0).toString(), timeString); //verify correct record |
|
986 |
||
987 |
//At this point we know that the intial value (timestamp) was succsefully stored in the database |
|
988 |
//Attempt to modify the data in the new record |
|
989 |
//equivalent to query.exec("update test set column3="... command in direct test |
|
990 |
//set the data in the first column to "col1ModelData" |
|
991 |
QVERIFY_SQL(model, setData(model.index(0,1), "col1ModelData")); |
|
992 |
||
993 |
//do a quick check to make sure that the setData command properly set the value in the model |
|
994 |
QCOMPARE(model.record(0).value(1).toString(), QLatin1String("col1ModelData")); |
|
995 |
||
996 |
//submit the changed data to the database |
|
997 |
//This is where I have been getting errors. |
|
998 |
QVERIFY_SQL(model, submitAll()); |
|
999 |
||
1000 |
//make sure the model has the most current data for our record |
|
1001 |
QVERIFY_SQL(model, select()); |
|
1002 |
||
1003 |
//verify that our new record was the only record returned |
|
1004 |
QCOMPARE(model.rowCount(), 1); |
|
1005 |
||
1006 |
//And that the record returned is, in fact, our test record. |
|
1007 |
QCOMPARE(model.record(0).value(0).toString(), timeString); |
|
1008 |
||
1009 |
//Make sure the value of the first column matches what we set it to previously. |
|
1010 |
QCOMPARE(model.record(0).value(1).toString(), QLatin1String("col1ModelData")); |
|
1011 |
} |
|
1012 |
||
1013 |
void tst_QSqlTableModel::removeColumnAndRow() |
|
1014 |
{ |
|
1015 |
QFETCH(QString, dbName); |
|
1016 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
1017 |
CHECK_DATABASE(db); |
|
1018 |
||
1019 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1020 |
model.setTable(test); |
0 | 1021 |
model.setEditStrategy(QSqlTableModel::OnManualSubmit); |
1022 |
QVERIFY_SQL(model, select()); |
|
1023 |
QCOMPARE(model.rowCount(), 3); |
|
1024 |
QCOMPARE(model.columnCount(), 3); |
|
1025 |
||
1026 |
QVERIFY(model.removeColumn(0)); |
|
1027 |
QVERIFY(model.removeRow(0)); |
|
1028 |
QVERIFY(model.submitAll()); |
|
1029 |
QCOMPARE(model.rowCount(), 2); |
|
1030 |
QCOMPARE(model.columnCount(), 2); |
|
1031 |
||
1032 |
// check with another table because the model has been modified |
|
1033 |
// but not the sql table |
|
1034 |
QSqlTableModel model2(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1035 |
model2.setTable(test); |
0 | 1036 |
QVERIFY_SQL(model2, select()); |
1037 |
QCOMPARE(model2.rowCount(), 2); |
|
1038 |
QCOMPARE(model2.columnCount(), 3); |
|
1039 |
} |
|
1040 |
||
1041 |
void tst_QSqlTableModel::insertBeforeDelete() |
|
1042 |
{ |
|
1043 |
QFETCH(QString, dbName); |
|
1044 |
QSqlDatabase db = QSqlDatabase::database(dbName); |
|
1045 |
CHECK_DATABASE(db); |
|
1046 |
||
1047 |
QSqlQuery q(db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1048 |
QVERIFY_SQL( q, exec("insert into " + test + " values(9, 'andrew', 9)")); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1049 |
QVERIFY_SQL( q, exec("insert into " + test + " values(10, 'justin', 10)")); |
0 | 1050 |
|
1051 |
QSqlTableModel model(0, db); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1052 |
model.setTable(test); |
0 | 1053 |
model.setEditStrategy(QSqlTableModel::OnManualSubmit); |
1054 |
QVERIFY_SQL(model, select()); |
|
1055 |
||
1056 |
QSqlRecord rec = model.record(); |
|
1057 |
rec.setValue(0, 4); |
|
1058 |
rec.setValue(1, QString("bill")); |
|
1059 |
rec.setValue(2, 4); |
|
1060 |
QVERIFY_SQL(model, insertRecord(4, rec)); |
|
1061 |
||
1062 |
QVERIFY_SQL(model, removeRow(5)); |
|
1063 |
QVERIFY_SQL(model, submitAll()); |
|
1064 |
QCOMPARE(model.rowCount(), 5); |
|
1065 |
} |
|
1066 |
||
1067 |
QTEST_MAIN(tst_QSqlTableModel) |
|
1068 |
#include "tst_qsqltablemodel.moc" |