1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
|
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include "qsqlrecord.h"
#include "qsqlfield.h"
#include "qstringlist.h"
#include <qsqlrecord.h>
//TESTED_CLASS=
//TESTED_FILES=
#define NUM_FIELDS 4
class tst_QSqlRecord : public QObject
{
Q_OBJECT
public:
tst_QSqlRecord();
virtual ~tst_QSqlRecord();
public slots:
void init();
void cleanup();
private slots:
void value();
#ifdef QT3_SUPPORT
void toStringList_data();
void toStringList();
void toString_data();
void toString();
#endif // QT3_SUPPORT
void setValue_data();
void setValue();
void setNull();
void setGenerated();
void remove();
void position();
void operator_Assign();
void isNull();
void isGenerated();
void isEmpty();
void insert();
void fieldName();
void field();
void count();
void contains();
void clearValues_data();
void clearValues();
void clear();
void append();
private:
QSqlRecord* rec;
QSqlField* fields[ NUM_FIELDS ];
void createTestRecord();
};
tst_QSqlRecord::tst_QSqlRecord()
{
rec = 0;
for ( int i = 0; i < NUM_FIELDS; ++i )
fields[ i ] = 0;
}
tst_QSqlRecord::~tst_QSqlRecord()
{
delete rec;
for ( int i = 0; i < NUM_FIELDS; ++i )
delete fields[ i ];
rec = 0;
}
void tst_QSqlRecord::init()
{
cleanup();
}
void tst_QSqlRecord::cleanup()
{
delete rec;
for ( int i = 0; i < NUM_FIELDS; ++i ) {
delete fields[ i ];
fields[ i ] = 0;
}
rec = 0;
}
void tst_QSqlRecord::createTestRecord()
{
delete rec;
rec = new QSqlRecord();
fields[ 0 ] = new QSqlField( "string", QVariant::String );
fields[ 1 ] = new QSqlField( "int", QVariant::Int );
fields[ 2 ] = new QSqlField( "double", QVariant::Double );
fields[ 3 ] = new QSqlField( "bool", QVariant::Bool );
for ( int i = 0; i < NUM_FIELDS; ++i )
rec->append( *(fields[ i ] ) );
}
void tst_QSqlRecord::append()
{
delete rec;
rec = new QSqlRecord();
rec->append( QSqlField( "string", QVariant::String ) );
QCOMPARE( rec->field( 0 ).name(), (QString) "string" );
QVERIFY( !rec->isEmpty() );
QCOMPARE( (int)rec->count(), 1 );
rec->append( QSqlField( "int", QVariant::Int ) );
QCOMPARE( rec->field( 1 ).name(), (QString) "int" );
QCOMPARE( (int)rec->count(), 2 );
rec->append( QSqlField( "double", QVariant::Double ) );
QCOMPARE( rec->field( 2 ).name(), (QString) "double" );
QCOMPARE( (int)rec->count(), 3 );
rec->append( QSqlField( "bool", QVariant::Bool ) );
QCOMPARE( rec->field( 3 ).name(), (QString) "bool" );
QCOMPARE( (int)rec->count(), 4 );
QCOMPARE( rec->indexOf( "string" ), 0 );
QCOMPARE( rec->indexOf( "int" ), 1 );
QCOMPARE( rec->indexOf( "double" ), 2 );
QCOMPARE( rec->indexOf( "bool" ), 3 );
#ifdef QT3_SUPPORT
QCOMPARE( rec->position( "string" ), 0 );
QCOMPARE( rec->position( "int" ), 1 );
QCOMPARE( rec->position( "double" ), 2 );
QCOMPARE( rec->position( "bool" ), 3 );
#endif
}
void tst_QSqlRecord::clear()
{
createTestRecord();
rec->clear();
QCOMPARE( (int)rec->count(), 0 );
QVERIFY( rec->isEmpty() );
#ifdef QT3_SUPPORT
QVERIFY( rec->fieldPtr( 0 ) == 0 );
QVERIFY( rec->fieldPtr( "_This should give a warning!_" ) == 0 );
#endif
QVERIFY( !rec->contains( fields[0]->name() ) );
}
void tst_QSqlRecord::clearValues_data()
{
QTest::addColumn<QString>("prefix");
QTest::addColumn<QString>("sep");
QTest::addColumn<int>("ival");
QTest::addColumn<QString>("sval");
QTest::addColumn<double>("dval");
QTest::addColumn<int>("bval");
QTest::newRow( "data0" ) << QString::fromLatin1("tablename") << QString::fromLatin1(",") << 10
<< QString::fromLatin1("Trond K.") << 2222.231234441 << 0;
QTest::newRow( "data1" ) << QString::fromLatin1("mytable") << QString::fromLatin1(".") << 12
<< QString::fromLatin1("Josten") << 544444444444423232.32334441 << 1;
QTest::newRow( "data2" ) << QString::fromLatin1("tabby") << QString::fromLatin1("-") << 12
<< QString::fromLatin1("Odvin") << 899129389283.32334441 << 1;
QTest::newRow( "data3" ) << QString::fromLatin1("per") << QString::fromLatin1("00") << 12
<< QString::fromLatin1("Brge") << 29382939182.99999919 << 0;
}
void tst_QSqlRecord::clearValues()
{
int i;
QFETCH( int, ival );
QFETCH( QString, sval );
QFETCH( double, dval );
QFETCH( int, bval );
if(rec)
delete rec;
rec = new QSqlRecord();
rec->append( QSqlField( "string", QVariant::String ) );
QCOMPARE( rec->field(0).name(), (QString) "string" );
QVERIFY( !rec->isEmpty() );
QCOMPARE( (int)rec->count(), 1 );
rec->append( QSqlField( "int", QVariant::Int ) );
QCOMPARE( rec->field(1).name(), (QString) "int" );
QCOMPARE( (int)rec->count(), 2 );
rec->append( QSqlField( "double", QVariant::Double ) );
QCOMPARE( rec->field(2).name(), (QString) "double" );
QCOMPARE( (int)rec->count(), 3 );
rec->append( QSqlField( "bool", QVariant::Bool ) );
QCOMPARE( rec->field(3).name(), (QString) "bool" );
QCOMPARE( (int)rec->count(), 4 );
QCOMPARE( rec->indexOf( "string" ), 0 );
QCOMPARE( rec->indexOf( "int" ), 1 );
QCOMPARE( rec->indexOf( "double" ), 2 );
QCOMPARE( rec->indexOf( "bool" ), 3 );
#ifdef QT3_SUPPORT
QCOMPARE( rec->position( "string" ), 0 );
QCOMPARE( rec->position( "int" ), 1 );
QCOMPARE( rec->position( "double" ), 2 );
QCOMPARE( rec->position( "bool" ), 3 );
#endif
for ( i = 0; i < 4; ++i )
rec->setNull( i );
rec->setValue( 0, sval );
rec->setValue( 1, ival );
rec->setValue( 2, dval );
rec->setValue( 3, QVariant(bval) );
QVERIFY( rec->value( 0 ) == sval );
QVERIFY( rec->value( 1 ) == ival );
QVERIFY( rec->value( 2 ) == dval );
QVERIFY( rec->value( 3 ) == QVariant(bval) );
rec->clearValues();
for ( i = 0; i < 4; ++i )
QVERIFY( rec->isNull( i ) );
}
void tst_QSqlRecord::contains()
{
createTestRecord();
for ( int i = 0; i < NUM_FIELDS; ++i )
QVERIFY( rec->contains( fields[ i ]->name() ) );
QVERIFY( !rec->contains( "__Harry__" ) );
}
void tst_QSqlRecord::count()
{
createTestRecord();
QCOMPARE( (int)rec->count(), NUM_FIELDS );
rec->remove( 3 );
QCOMPARE( (int)rec->count(), NUM_FIELDS - 1 );
rec->clear();
QCOMPARE( (int)rec->count(), 0 );
QCOMPARE( (int)QSqlRecord().count(), 0 );
}
void tst_QSqlRecord::field()
{
createTestRecord();
int i;
for ( i = 0; i < NUM_FIELDS; ++i )
QVERIFY( rec->field( i ) == *fields[ i ] );
#ifdef QT3_SUPPORT
QVERIFY( rec->fieldPtr( NUM_FIELDS ) == 0 );
#endif
for ( i = 0; i < NUM_FIELDS; ++i )
QVERIFY( rec->field( (fields[ i ] )->name() ) == *( fields[ i ] ) );
QVERIFY( rec->indexOf( "_This should give a warning!_" ) == -1 );
}
void tst_QSqlRecord::fieldName()
{
createTestRecord();
for ( int i = 0; i < NUM_FIELDS; ++i )
QVERIFY( rec->field( (fields[ i ] )->name() ) == *( fields[ i ] ) );
QVERIFY( rec->fieldName( NUM_FIELDS ).isNull() );
}
void tst_QSqlRecord::insert()
{
QSqlRecord iRec;
int i;
for ( i = 0; i <= 100; ++i ) {
iRec.insert( i, QSqlField( QString::number( i ), QVariant::Int ) );
}
for ( i = 0; i <= 100; ++i ) {
QCOMPARE( iRec.fieldName( i ), QString::number( i ) );
}
// iRec.insert( 505, QSqlField( "Harry", QVariant::Double ) );
// QCOMPARE( iRec.fieldName( 505 ), (QString)"Harry" );
// QVERIFY( iRec.field( 505 ).type() == QVariant::Double );
iRec.insert( 42, QSqlField( "Everything", QVariant::String ) );
QCOMPARE( iRec.fieldName( 42 ), (QString)"Everything" );
QVERIFY( iRec.field( 42 ).type() == QVariant::String );
}
void tst_QSqlRecord::isEmpty()
{
QSqlRecord eRec;
QVERIFY( eRec.isEmpty() );
eRec.append( QSqlField( "Harry", QVariant::String ) );
QVERIFY( !eRec.isEmpty() );
eRec.remove( 0 );
QVERIFY( eRec.isEmpty() );
eRec.insert( 0, QSqlField( "Harry", QVariant::String ) );
QVERIFY( !eRec.isEmpty() );
eRec.clear();
QVERIFY( eRec.isEmpty() );
}
void tst_QSqlRecord::isGenerated()
{
createTestRecord();
int i;
for ( i = 0; i < NUM_FIELDS; ++i )
QVERIFY( rec->isGenerated( i ) );
for ( i = 0; i < NUM_FIELDS; ++i )
QVERIFY( rec->isGenerated( fields[ i ]->name() ) );
for ( i = 0; i < NUM_FIELDS; ++i ) {
if ( i % 2 )
rec->setGenerated( i, false );
}
rec->setGenerated( NUM_FIELDS * 2, false ); // nothing should happen here
for ( i = 0; i < NUM_FIELDS; ++i ) {
if ( i % 2 ) {
QVERIFY( !rec->isGenerated( i ) );
} else {
QVERIFY( rec->isGenerated( i ) );
}
}
for ( i = 0; i < NUM_FIELDS; ++i )
if ( i % 2 ) {
QVERIFY( !rec->isGenerated( fields[ i ]->name() ) );
} else {
QVERIFY( rec->isGenerated( fields[ i ]->name() ) );
}
rec->setGenerated( "_This should give a warning!_", false ); // nothing should happen here
}
void tst_QSqlRecord::isNull()
{
createTestRecord();
int i;
for ( i = 0; i < NUM_FIELDS; ++i ) {
QVERIFY( rec->isNull( i ) );
QVERIFY( rec->isNull( fields[ i ]->name() ) );
}
for ( i = 0; i < NUM_FIELDS; ++i ) {
if ( i % 2 )
rec->setNull( i );
}
rec->setNull( NUM_FIELDS ); // nothing should happen here
for ( i = 0; i < NUM_FIELDS; ++i ) {
if ( i % 2 ) {
QVERIFY( rec->isNull( i ) );
QVERIFY( rec->isNull( fields[ i ]->name() ) );
}
}
for ( i = 0; i < NUM_FIELDS; ++i ) {
rec->setNull( fields[ i ]->name() );
}
rec->setNull( "_This should give a warning!_" ); // nothing should happen here
for ( i = 0; i < NUM_FIELDS; ++i ) {
QVERIFY( rec->isNull( i ) );
QVERIFY( rec->isNull( fields[ i ]->name() ) );
}
}
void tst_QSqlRecord::operator_Assign()
{
createTestRecord();
int i;
QSqlRecord buf2, buf3, buf4; // since buffers are implicitely shared, we exaggerate a bit here
buf2 = *rec;
buf3 = *rec;
buf4 = *rec;
for ( i = 0; i < NUM_FIELDS; ++i ) {
QVERIFY( buf2.field( i ) == *fields[ i ] );
QVERIFY( buf3.field( i ) == *( fields[ i ] ) );
QVERIFY( buf4.field( i ) == *( fields[ i ] ) );
}
for ( i = 0; i < NUM_FIELDS; ++i )
buf3.setNull( i );
buf3.remove( NUM_FIELDS - 1 );
QSqlRecord buf5 = buf3;
for ( i = 0; i < NUM_FIELDS - 1; ++i ) {
QSqlField fi ( fields[ i ]->name(), fields[ i ]->type() );
fi.clear();
QVERIFY( buf5.field( i ) == fi );
QVERIFY( buf5.isGenerated( i ) );
}
}
void tst_QSqlRecord::position()
{
createTestRecord();
int i;
for ( i = 0; i < NUM_FIELDS; ++i ) {
QCOMPARE( rec->indexOf( fields[ i ]->name() ), i );
#ifdef QT3_SUPPORT
QCOMPARE( rec->position( fields[ i ]->name() ), i );
#endif
}
}
void tst_QSqlRecord::remove()
{
createTestRecord();
int i;
for ( i = 0; i < NUM_FIELDS; ++i ) {
rec->setGenerated( i, false );
QCOMPARE( (int)rec->count(), NUM_FIELDS - i );
rec->remove( 0 );
QCOMPARE( (int)rec->count(), NUM_FIELDS - i - 1 );
}
rec->remove( NUM_FIELDS * 2 ); // nothing should happen
for ( i = 0; i < NUM_FIELDS; ++i ) {
rec->insert( i, QSqlField( fields[ i ]->name(), fields[ i ]->type() ) );
QVERIFY( rec->isGenerated( i ) );
}
}
void tst_QSqlRecord::setGenerated()
{
isGenerated();
}
void tst_QSqlRecord::setNull()
{
isNull();
}
void tst_QSqlRecord::setValue_data()
{
clearValues_data();
}
void tst_QSqlRecord::setValue()
{
int i;
delete rec;
rec = new QSqlRecord();
rec->append( QSqlField( "string", QVariant::String ) );
QCOMPARE( rec->field( 0 ).name(), (QString) "string" );
QVERIFY( !rec->isEmpty() );
QCOMPARE( (int)rec->count(), 1 );
rec->append( QSqlField( "int", QVariant::Int ) );
QCOMPARE( rec->field( 1 ).name(), (QString) "int" );
QCOMPARE( (int)rec->count(), 2 );
rec->append( QSqlField( "double", QVariant::Double ) );
QCOMPARE( rec->field( 2 ).name(), (QString) "double" );
QCOMPARE( (int)rec->count(), 3 );
rec->append( QSqlField( "bool", QVariant::Bool ) );
QCOMPARE( rec->field( 3 ).name(), (QString) "bool" );
QCOMPARE( (int)rec->count(), 4 );
QCOMPARE( rec->indexOf( "string" ), 0 );
QCOMPARE( rec->indexOf( "int" ), 1 );
QCOMPARE( rec->indexOf( "double" ), 2 );
QCOMPARE( rec->indexOf( "bool" ), 3 );
#ifdef QT3_SUPPORT
QCOMPARE( rec->position( "string" ), 0 );
QCOMPARE( rec->position( "int" ), 1 );
QCOMPARE( rec->position( "double" ), 2 );
QCOMPARE( rec->position( "bool" ), 3 );
#endif
QFETCH( int, ival );
QFETCH( QString, sval );
QFETCH( double, dval );
QFETCH( int, bval );
for ( i = 0; i < 4; ++i )
rec->setNull( i );
rec->setValue( 0, sval );
rec->setValue( 1, ival );
rec->setValue( 2, dval );
rec->setValue( 3, QVariant(bval) );
QVERIFY( rec->value( 0 ) == sval );
QVERIFY( rec->value( 1 ) == ival );
QVERIFY( rec->value( 2 ) == dval );
QVERIFY( rec->value( 3 ) == QVariant(bval) );
for ( i = 0; i < 4; ++i )
QVERIFY( !rec->isNull( i ) );
QSqlRecord rec2 = *rec;
QVERIFY( rec2.value( 0 ) == sval );
QVERIFY( rec2.value( 1 ) == ival );
QVERIFY( rec2.value( 2 ) == dval );
QVERIFY( rec2.value( 3 ) == QVariant(bval) );
rec2.setValue( "string", "__Harry__" );
QVERIFY( rec2.value( 0 ) == "__Harry__" );
for ( i = 0; i < 4; ++i )
QVERIFY( !rec2.isNull( i ) );
QCOMPARE( rec->value( 0 ).toString(), sval );
QCOMPARE( rec->value( 1 ).toInt(), ival );
QCOMPARE( rec->value( 2 ).toDouble(), dval );
QCOMPARE( rec->value( 3 ), QVariant(bval) );
}
#ifdef QT3_SUPPORT
void tst_QSqlRecord::toString_data()
{
clearValues_data( t );
}
void tst_QSqlRecord::toString()
{
createTestRecord();
QString result;
QFETCH( QString, prefix );
QFETCH( QString, sep );
rec->setGenerated( 1, false );
for (int i = 0; i < NUM_FIELDS; ++i ) {
if ( i == 1 )
continue;
if ( prefix.isEmpty() ) {
result += fields[ i ]->name();
} else {
result += prefix + '.' + fields[ i ]->name();
}
if ( i != NUM_FIELDS - 1 )
result += sep + ' ';
}
QCOMPARE( rec->toString( prefix, sep ), result );
}
void tst_QSqlRecord::toStringList_data()
{
clearValues_data( t );
}
void tst_QSqlRecord::toStringList()
{
createTestRecord();
QStringList result;
QFETCH( QString, prefix );
rec->setGenerated( 1, false );
for (int i = 0; i < NUM_FIELDS; ++i ) {
if ( i == 1 )
continue;
if ( prefix.isEmpty() ) {
result << fields[ i ]->name();
} else {
result << ( prefix + '.' + fields[ i ]->name() );
}
}
QCOMPARE(rec->toStringList( prefix ), result);
}
#endif // QT3_SUPPORT
void tst_QSqlRecord::value()
{
// this test is already covered in setValue()
QSqlRecord rec2;
rec2.append( QSqlField( "string", QVariant::String ) );
rec2.setValue( "string", "Harry" );
QVERIFY( rec2.value( "string" ) == "Harry" );
}
QTEST_MAIN(tst_QSqlRecord)
#include "tst_qsqlrecord.moc"
|