~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to tests/auto/sql/kernel/qsqlfield/tst_qsqlfield.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
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.
 
16
**
 
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.
 
24
**
 
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.
 
28
**
 
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.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
 
 
43
#include <QtTest/QtTest>
 
44
 
 
45
#include <qsqlfield.h>
 
46
#include <qvariant.h>
 
47
#include <qsqlfield.h>
 
48
 
 
49
class tst_QSqlField : public QObject
 
50
{
 
51
Q_OBJECT
 
52
 
 
53
public:
 
54
    tst_QSqlField();
 
55
    virtual ~tst_QSqlField();
 
56
 
 
57
 
 
58
public slots:
 
59
    void init();
 
60
    void cleanup();
 
61
private slots:
 
62
    void getSetCheck();
 
63
    void type();
 
64
    void setValue_data();
 
65
    void setValue();
 
66
    void setReadOnly();
 
67
    void setNull();
 
68
    void setName_data();
 
69
    void setName();
 
70
    void operator_Equal();
 
71
    void operator_Assign();
 
72
    void name_data();
 
73
    void name();
 
74
    void isReadOnly();
 
75
    void isNull();
 
76
    void clear_data();
 
77
    void clear();
 
78
};
 
79
 
 
80
// Testing get/set functions
 
81
void tst_QSqlField::getSetCheck()
 
82
{
 
83
    QSqlField obj1;
 
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());
 
92
 
 
93
    // int QSqlField::length()
 
94
    // void QSqlField::setLength(int)
 
95
    obj1.setLength(0);
 
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());
 
101
 
 
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());
 
110
}
 
111
 
 
112
tst_QSqlField::tst_QSqlField()
 
113
{
 
114
}
 
115
 
 
116
tst_QSqlField::~tst_QSqlField()
 
117
{
 
118
 
 
119
}
 
120
 
 
121
void tst_QSqlField::init()
 
122
{
 
123
// TODO: Add initialization code here.
 
124
// This will be executed immediately before each test is run.
 
125
}
 
126
 
 
127
void tst_QSqlField::cleanup()
 
128
{
 
129
// TODO: Add cleanup code here.
 
130
// This will be executed immediately after each test is run.
 
131
}
 
132
 
 
133
void tst_QSqlField::clear_data()
 
134
{
 
135
    QTest::addColumn<int>("val");
 
136
    QTest::addColumn<bool>("bval");
 
137
    QTest::addColumn<QString>("strVal");
 
138
    QTest::addColumn<double>("fval");
 
139
 
 
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;
 
144
}
 
145
 
 
146
void tst_QSqlField::clear()
 
147
{
 
148
    QSqlField field( "Testfield", QVariant::Int );
 
149
    QFETCH( int, val );
 
150
    field.setValue( val );
 
151
    field.setReadOnly(true);
 
152
    field.clear();
 
153
    QVERIFY( field.value() == val );
 
154
    QVERIFY( !field.isNull() );
 
155
 
 
156
    QSqlField bfield( "Testfield", QVariant::Bool );
 
157
    QFETCH( bool, bval );
 
158
    bfield.setValue( QVariant(bval) );
 
159
    bfield.setReadOnly(true);
 
160
    bfield.clear();
 
161
 
 
162
    QVERIFY( bfield.value() == QVariant(bval) );
 
163
    QVERIFY( !bfield.isNull() );
 
164
 
 
165
    QSqlField ffield( "Testfield", QVariant::Double );
 
166
    QFETCH( double, fval );
 
167
    ffield.setValue( fval );
 
168
    ffield.setReadOnly(true);
 
169
    ffield.clear();
 
170
    QVERIFY( ffield.value() == fval );
 
171
    QVERIFY( !ffield.isNull() );
 
172
 
 
173
    QSqlField sfield( "Testfield", QVariant::String );
 
174
    QFETCH( QString, strVal );
 
175
    sfield.setValue( strVal );
 
176
    sfield.setReadOnly(true);
 
177
    sfield.clear();
 
178
    QVERIFY( sfield.value() == strVal );
 
179
    QVERIFY( !sfield.isNull() );
 
180
}
 
181
 
 
182
void tst_QSqlField::isNull()
 
183
{
 
184
    QSqlField field( "test", QVariant::String );
 
185
    QVERIFY( field.isNull() );
 
186
}
 
187
 
 
188
void tst_QSqlField::isReadOnly()
 
189
{
 
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() );
 
196
}
 
197
 
 
198
void tst_QSqlField::name_data()
 
199
{
 
200
    QTest::addColumn<QString>("val");
 
201
 
 
202
    //next we fill it with data
 
203
    QTest::newRow( "data0" )  << QString("test");
 
204
    QTest::newRow( "data1" )  << QString("Harry");
 
205
    QTest::newRow( "data2" )  << QString("");
 
206
}
 
207
 
 
208
void tst_QSqlField::name()
 
209
{
 
210
    QSqlField field( "test", QVariant::String );
 
211
    QFETCH( QString, val );
 
212
    QVERIFY( field.name() == "test" );
 
213
    field.setName( val );
 
214
    QVERIFY( field.name() == val );
 
215
}
 
216
 
 
217
void tst_QSqlField::operator_Assign()
 
218
{
 
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 );
 
225
    field3.clear();
 
226
    field1 = field3;
 
227
    QVERIFY( field1 == field3 );
 
228
}
 
229
 
 
230
void tst_QSqlField::operator_Equal()
 
231
{
 
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 );
 
248
}
 
249
 
 
250
void tst_QSqlField::setName_data()
 
251
{
 
252
    QTest::addColumn<QString>("val");
 
253
 
 
254
    //next we fill it with data
 
255
    QTest::newRow( "data0" )  << QString("test");
 
256
    QTest::newRow( "data1" )  << QString("Harry");
 
257
    QTest::newRow( "data2" )  << QString("");
 
258
}
 
259
 
 
260
void tst_QSqlField::setName()
 
261
{
 
262
    QSqlField field( "test", QVariant::String );
 
263
    QFETCH( QString, val );
 
264
    QVERIFY( field.name() == "test" );
 
265
    field.setName( val );
 
266
    QVERIFY( field.name() == val );
 
267
}
 
268
 
 
269
void tst_QSqlField::setNull()
 
270
{
 
271
    QSqlField field( "test", QVariant::String );
 
272
    field.setValue( "test" );
 
273
    field.clear();
 
274
    QVERIFY( field.value() == QVariant().toString() );
 
275
    QVERIFY( field.isNull() );
 
276
}
 
277
 
 
278
void tst_QSqlField::setReadOnly()
 
279
{
 
280
    QSqlField field( "test", QVariant::String );
 
281
    field.setValue( "test" );
 
282
    field.setReadOnly( true );
 
283
    field.setValue( "Harry" );
 
284
    QVERIFY( field.value() == "test" );
 
285
    field.clear();
 
286
    QVERIFY( field.value() == "test" );
 
287
    QVERIFY( !field.isNull() );
 
288
    field.clear();
 
289
    QVERIFY( field.value() == "test" );
 
290
    QVERIFY( !field.isNull() );
 
291
    field.setReadOnly( false );
 
292
    field.setValue( "Harry" );
 
293
    QVERIFY( field.value() == "Harry" );
 
294
    field.clear();
 
295
    QVERIFY( field.value() == QVariant().toString() );
 
296
    QVERIFY( field.isNull() );
 
297
}
 
298
 
 
299
void tst_QSqlField::setValue_data()
 
300
{
 
301
    QTest::addColumn<int>("ival");
 
302
    QTest::addColumn<bool>("bval");
 
303
    QTest::addColumn<double>("dval");
 
304
    QTest::addColumn<QString>("sval");
 
305
 
 
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");
 
310
}
 
311
 
 
312
void tst_QSqlField::setValue()
 
313
{
 
314
    QSqlField field1 ( "test", QVariant::Int );
 
315
    QSqlField field2 ( "test", QVariant::String );
 
316
    QSqlField field3 ( "test", QVariant::Bool );
 
317
    QSqlField field4 ( "test", QVariant::Double );
 
318
    field1.clear();
 
319
    QFETCH( int, ival );
 
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() );
 
327
 
 
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 );
 
337
}
 
338
 
 
339
void tst_QSqlField::type()
 
340
{
 
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 );
 
347
}
 
348
 
 
349
QTEST_MAIN(tst_QSqlField)
 
350
#include "tst_qsqlfield.moc"