1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
43
#include <QtTest/QtTest>
45
#include <qsqlfield.h>
47
#include <qsqlfield.h>
49
class tst_QSqlField : public QObject
55
virtual ~tst_QSqlField();
70
void operator_Equal();
71
void operator_Assign();
80
// Testing get/set functions
81
void tst_QSqlField::getSetCheck()
84
// RequiredStatus QSqlField::requiredStatus()
85
// void QSqlField::setRequiredStatus(RequiredStatus)
86
obj1.setRequiredStatus(QSqlField::RequiredStatus(QSqlField::Unknown));
87
QCOMPARE(QSqlField::RequiredStatus(QSqlField::Unknown), obj1.requiredStatus());
88
obj1.setRequiredStatus(QSqlField::RequiredStatus(QSqlField::Optional));
89
QCOMPARE(QSqlField::RequiredStatus(QSqlField::Optional), obj1.requiredStatus());
90
obj1.setRequiredStatus(QSqlField::RequiredStatus(QSqlField::Required));
91
QCOMPARE(QSqlField::RequiredStatus(QSqlField::Required), obj1.requiredStatus());
93
// int QSqlField::length()
94
// void QSqlField::setLength(int)
96
QCOMPARE(0, obj1.length());
97
obj1.setLength(INT_MIN);
98
QCOMPARE(INT_MIN, obj1.length());
99
obj1.setLength(INT_MAX);
100
QCOMPARE(INT_MAX, obj1.length());
102
// int QSqlField::precision()
103
// void QSqlField::setPrecision(int)
104
obj1.setPrecision(0);
105
QCOMPARE(0, obj1.precision());
106
obj1.setPrecision(INT_MIN);
107
QCOMPARE(INT_MIN, obj1.precision());
108
obj1.setPrecision(INT_MAX);
109
QCOMPARE(INT_MAX, obj1.precision());
112
tst_QSqlField::tst_QSqlField()
116
tst_QSqlField::~tst_QSqlField()
121
void tst_QSqlField::init()
123
// TODO: Add initialization code here.
124
// This will be executed immediately before each test is run.
127
void tst_QSqlField::cleanup()
129
// TODO: Add cleanup code here.
130
// This will be executed immediately after each test is run.
133
void tst_QSqlField::clear_data()
135
QTest::addColumn<int>("val");
136
QTest::addColumn<bool>("bval");
137
QTest::addColumn<QString>("strVal");
138
QTest::addColumn<double>("fval");
140
//next we fill it with data
141
QTest::newRow( "data0" ) << (int)5 << true << QString("Hallo") << (double)0;
142
QTest::newRow( "data1" ) << -5 << false << QString("NULL") << (double)-4;
143
QTest::newRow( "data2" ) << 0 << false << QString("0") << (double)0;
146
void tst_QSqlField::clear()
148
QSqlField field( "Testfield", QVariant::Int );
150
field.setValue( val );
151
field.setReadOnly(true);
153
QVERIFY( field.value() == val );
154
QVERIFY( !field.isNull() );
156
QSqlField bfield( "Testfield", QVariant::Bool );
157
QFETCH( bool, bval );
158
bfield.setValue( QVariant(bval) );
159
bfield.setReadOnly(true);
162
QVERIFY( bfield.value() == QVariant(bval) );
163
QVERIFY( !bfield.isNull() );
165
QSqlField ffield( "Testfield", QVariant::Double );
166
QFETCH( double, fval );
167
ffield.setValue( fval );
168
ffield.setReadOnly(true);
170
QVERIFY( ffield.value() == fval );
171
QVERIFY( !ffield.isNull() );
173
QSqlField sfield( "Testfield", QVariant::String );
174
QFETCH( QString, strVal );
175
sfield.setValue( strVal );
176
sfield.setReadOnly(true);
178
QVERIFY( sfield.value() == strVal );
179
QVERIFY( !sfield.isNull() );
182
void tst_QSqlField::isNull()
184
QSqlField field( "test", QVariant::String );
185
QVERIFY( field.isNull() );
188
void tst_QSqlField::isReadOnly()
190
QSqlField field( "test", QVariant::String );
191
QVERIFY( !field.isReadOnly() );
192
field.setReadOnly( true );
193
QVERIFY( field.isReadOnly() );
194
field.setReadOnly( false );
195
QVERIFY( !field.isReadOnly() );
198
void tst_QSqlField::name_data()
200
QTest::addColumn<QString>("val");
202
//next we fill it with data
203
QTest::newRow( "data0" ) << QString("test");
204
QTest::newRow( "data1" ) << QString("Harry");
205
QTest::newRow( "data2" ) << QString("");
208
void tst_QSqlField::name()
210
QSqlField field( "test", QVariant::String );
211
QFETCH( QString, val );
212
QVERIFY( field.name() == "test" );
213
field.setName( val );
214
QVERIFY( field.name() == val );
217
void tst_QSqlField::operator_Assign()
219
QSqlField field1( "test", QVariant::String );
220
field1.setValue( "Harry" );
221
field1.setReadOnly( true );
222
QSqlField field2 = field1;
223
QVERIFY( field1 == field2 );
224
QSqlField field3( "test", QVariant::Double );
227
QVERIFY( field1 == field3 );
230
void tst_QSqlField::operator_Equal()
232
QSqlField field1( "test", QVariant::String );
233
QSqlField field2( "test2", QVariant::String );
234
QSqlField field3( "test", QVariant::Int );
235
QVERIFY( !(field1 == field2) );
236
QVERIFY( !(field1 == field3) );
237
field2.setName( "test" );
238
QVERIFY( field1 == field2 );
239
QVERIFY( field1 == field2 );
240
field1.setValue( "Harry" );
241
QVERIFY( !(field1 == field2) );
242
field2.setValue( "Harry" );
243
QVERIFY( field1 == field2 );
244
field1.setReadOnly( true );
245
QVERIFY( !(field1 == field2) );
246
field2.setReadOnly( true );
247
QVERIFY( field1 == field2 );
250
void tst_QSqlField::setName_data()
252
QTest::addColumn<QString>("val");
254
//next we fill it with data
255
QTest::newRow( "data0" ) << QString("test");
256
QTest::newRow( "data1" ) << QString("Harry");
257
QTest::newRow( "data2" ) << QString("");
260
void tst_QSqlField::setName()
262
QSqlField field( "test", QVariant::String );
263
QFETCH( QString, val );
264
QVERIFY( field.name() == "test" );
265
field.setName( val );
266
QVERIFY( field.name() == val );
269
void tst_QSqlField::setNull()
271
QSqlField field( "test", QVariant::String );
272
field.setValue( "test" );
274
QVERIFY( field.value() == QVariant().toString() );
275
QVERIFY( field.isNull() );
278
void tst_QSqlField::setReadOnly()
280
QSqlField field( "test", QVariant::String );
281
field.setValue( "test" );
282
field.setReadOnly( true );
283
field.setValue( "Harry" );
284
QVERIFY( field.value() == "test" );
286
QVERIFY( field.value() == "test" );
287
QVERIFY( !field.isNull() );
289
QVERIFY( field.value() == "test" );
290
QVERIFY( !field.isNull() );
291
field.setReadOnly( false );
292
field.setValue( "Harry" );
293
QVERIFY( field.value() == "Harry" );
295
QVERIFY( field.value() == QVariant().toString() );
296
QVERIFY( field.isNull() );
299
void tst_QSqlField::setValue_data()
301
QTest::addColumn<int>("ival");
302
QTest::addColumn<bool>("bval");
303
QTest::addColumn<double>("dval");
304
QTest::addColumn<QString>("sval");
306
//next we fill it with data
307
QTest::newRow( "data0" ) << 0 << false << (double)223.232 << QString("");
308
QTest::newRow( "data1" ) << 123 << true << (double)-232.232 << QString("Harry");
309
QTest::newRow( "data2" ) << -123 << false << (double)232222.323223233338 << QString("Woipertinger");
312
void tst_QSqlField::setValue()
314
QSqlField field1 ( "test", QVariant::Int );
315
QSqlField field2 ( "test", QVariant::String );
316
QSqlField field3 ( "test", QVariant::Bool );
317
QSqlField field4 ( "test", QVariant::Double );
320
QFETCH( QString, sval );
321
QFETCH( double, dval );
322
QFETCH( bool, bval );
323
field1.setValue( ival );
324
QCOMPARE( field1.value().toInt(), ival );
325
// setValue should also reset the NULL flag
326
QVERIFY( !field1.isNull() );
328
field2.setValue( sval );
329
QCOMPARE( field2.value().toString(), sval );
330
field3.setValue( QVariant( bval) );
331
QVERIFY( field3.value().toBool() == bval );
332
field4.setValue( dval );
333
QCOMPARE( field4.value().toDouble(), dval );
334
field4.setReadOnly( true );
335
field4.setValue( "Something_that's_not_a_double" );
336
QCOMPARE( field4.value().toDouble(), dval );
339
void tst_QSqlField::type()
341
QSqlField field1( "string", QVariant::String );
342
QSqlField field2( "string", QVariant::Bool );
343
QSqlField field3( "string", QVariant::Double );
344
QVERIFY( field1.type() == QVariant::String );
345
QVERIFY( field2.type() == QVariant::Bool );
346
QVERIFY( field3.type() == QVariant::Double );
349
QTEST_MAIN(tst_QSqlField)
350
#include "tst_qsqlfield.moc"