1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31
32#include <qsqlfield.h>
33#include <qvariant.h>
34#include <qsqlfield.h>
35
36class tst_QSqlField : public QObject
37{
38Q_OBJECT
39
40public:
41 tst_QSqlField();
42 virtual ~tst_QSqlField();
43
44
45public slots:
46 void init();
47 void cleanup();
48private slots:
49 void getSetCheck();
50 void type();
51 void setValue_data();
52 void setValue();
53 void setReadOnly();
54 void setNull();
55 void setName_data();
56 void setName();
57 void operator_Equal();
58 void operator_Assign();
59 void name_data();
60 void name();
61 void isReadOnly();
62 void isNull();
63 void clear_data();
64 void clear();
65 void setTableName_data();
66 void setTableName();
67};
68
69// Testing get/set functions
70void tst_QSqlField::getSetCheck()
71{
72 QSqlField obj1;
73 // RequiredStatus QSqlField::requiredStatus()
74 // void QSqlField::setRequiredStatus(RequiredStatus)
75 obj1.setRequiredStatus(QSqlField::RequiredStatus(QSqlField::Unknown));
76 QCOMPARE(QSqlField::RequiredStatus(QSqlField::Unknown), obj1.requiredStatus());
77 obj1.setRequiredStatus(QSqlField::RequiredStatus(QSqlField::Optional));
78 QCOMPARE(QSqlField::RequiredStatus(QSqlField::Optional), obj1.requiredStatus());
79 obj1.setRequiredStatus(QSqlField::RequiredStatus(QSqlField::Required));
80 QCOMPARE(QSqlField::RequiredStatus(QSqlField::Required), obj1.requiredStatus());
81
82 // int QSqlField::length()
83 // void QSqlField::setLength(int)
84 obj1.setLength(0);
85 QCOMPARE(0, obj1.length());
86 obj1.setLength(INT_MIN);
87 QCOMPARE(INT_MIN, obj1.length());
88 obj1.setLength(INT_MAX);
89 QCOMPARE(INT_MAX, obj1.length());
90
91 // int QSqlField::precision()
92 // void QSqlField::setPrecision(int)
93 obj1.setPrecision(0);
94 QCOMPARE(0, obj1.precision());
95 obj1.setPrecision(INT_MIN);
96 QCOMPARE(INT_MIN, obj1.precision());
97 obj1.setPrecision(INT_MAX);
98 QCOMPARE(INT_MAX, obj1.precision());
99}
100
101tst_QSqlField::tst_QSqlField()
102{
103}
104
105tst_QSqlField::~tst_QSqlField()
106{
107
108}
109
110void tst_QSqlField::init()
111{
112// TODO: Add initialization code here.
113// This will be executed immediately before each test is run.
114}
115
116void tst_QSqlField::cleanup()
117{
118// TODO: Add cleanup code here.
119// This will be executed immediately after each test is run.
120}
121
122void tst_QSqlField::clear_data()
123{
124 QTest::addColumn<int>(name: "val");
125 QTest::addColumn<bool>(name: "bval");
126 QTest::addColumn<QString>(name: "strVal");
127 QTest::addColumn<double>(name: "fval");
128
129 //next we fill it with data
130 QTest::newRow( dataTag: "data0" ) << (int)5 << true << QString("Hallo") << (double)0;
131 QTest::newRow( dataTag: "data1" ) << -5 << false << QString("NULL") << (double)-4;
132 QTest::newRow( dataTag: "data2" ) << 0 << false << QString("0") << (double)0;
133}
134
135void tst_QSqlField::clear()
136{
137 QSqlField field( "Testfield", QVariant::Int );
138 QFETCH( int, val );
139 field.setValue( val );
140 field.setReadOnly(true);
141 field.clear();
142 QVERIFY( field.value() == val );
143 QVERIFY( !field.isNull() );
144
145 QSqlField bfield( "Testfield", QVariant::Bool );
146 QFETCH( bool, bval );
147 bfield.setValue( QVariant(bval) );
148 bfield.setReadOnly(true);
149 bfield.clear();
150
151 QVERIFY( bfield.value() == QVariant(bval) );
152 QVERIFY( !bfield.isNull() );
153
154 QSqlField ffield( "Testfield", QVariant::Double );
155 QFETCH( double, fval );
156 ffield.setValue( fval );
157 ffield.setReadOnly(true);
158 ffield.clear();
159 QVERIFY( ffield.value() == fval );
160 QVERIFY( !ffield.isNull() );
161
162 QSqlField sfield( "Testfield", QVariant::String );
163 QFETCH( QString, strVal );
164 sfield.setValue( strVal );
165 sfield.setReadOnly(true);
166 sfield.clear();
167 QVERIFY( sfield.value() == strVal );
168 QVERIFY( !sfield.isNull() );
169}
170
171void tst_QSqlField::isNull()
172{
173 QSqlField field( "test", QVariant::String );
174 QVERIFY( field.isNull() );
175}
176
177void tst_QSqlField::isReadOnly()
178{
179 QSqlField field( "test", QVariant::String );
180 QVERIFY( !field.isReadOnly() );
181 field.setReadOnly( true );
182 QVERIFY( field.isReadOnly() );
183 field.setReadOnly( false );
184 QVERIFY( !field.isReadOnly() );
185}
186
187void tst_QSqlField::name_data()
188{
189 QTest::addColumn<QString>(name: "val");
190
191 //next we fill it with data
192 QTest::newRow( dataTag: "data0" ) << QString("test");
193 QTest::newRow( dataTag: "data1" ) << QString("Harry");
194 QTest::newRow( dataTag: "data2" ) << QString("");
195}
196
197void tst_QSqlField::name()
198{
199 QSqlField field( "test", QVariant::String );
200 QFETCH( QString, val );
201 QCOMPARE(field.name(), QLatin1String("test"));
202 field.setName( val );
203 QCOMPARE(field.name(), val);
204}
205
206void tst_QSqlField::operator_Assign()
207{
208 QSqlField field1( "test", QVariant::String );
209 field1.setValue( "Harry" );
210 field1.setReadOnly( true );
211 QSqlField field2 = field1;
212 QVERIFY( field1 == field2 );
213 QSqlField field3( "test", QVariant::Double );
214 field3.clear();
215 field1 = field3;
216 QVERIFY( field1 == field3 );
217 QSqlField field4("test", QVariant::String, "ATable");
218 field1 = field4;
219 QVERIFY(field1 == field4);
220}
221
222void tst_QSqlField::operator_Equal()
223{
224 QSqlField field1( "test", QVariant::String );
225 QSqlField field2( "test2", QVariant::String );
226 QSqlField field3( "test", QVariant::Int );
227 QSqlField field4("test", QVariant::String, QString("ATable"));
228 QSqlField field5("test2", QVariant::String, QString("ATable"));
229 QSqlField field6("test", QVariant::String, QString("BTable"));
230
231 QVERIFY( !(field1 == field2) );
232 QVERIFY( !(field1 == field3) );
233 QVERIFY(field1 != field4);
234 QVERIFY(field1 != field5);
235 QVERIFY(field1 != field6);
236 QVERIFY(field4 != field5);
237 QVERIFY(field4 != field6);
238
239 field2.setName( "test" );
240 QVERIFY( field1 == field2 );
241 QVERIFY( field1 == field2 );
242 field1.setValue( "Harry" );
243 QVERIFY( !(field1 == field2) );
244 field2.setValue( "Harry" );
245 QVERIFY( field1 == field2 );
246 field1.setReadOnly( true );
247 QVERIFY( !(field1 == field2) );
248 field2.setReadOnly( true );
249 QVERIFY( field1 == field2 );
250 field4.setTableName("BTable");
251 QCOMPARE(field4, field6);
252 field6.setName("test3");
253 QVERIFY(field4 != field6);
254}
255
256void tst_QSqlField::setName_data()
257{
258 QTest::addColumn<QString>(name: "val");
259
260 //next we fill it with data
261 QTest::newRow( dataTag: "data0" ) << QString("test");
262 QTest::newRow( dataTag: "data1" ) << QString("Harry");
263 QTest::newRow( dataTag: "data2" ) << QString("");
264}
265
266void tst_QSqlField::setName()
267{
268 QSqlField field( "test", QVariant::String );
269 QFETCH( QString, val );
270 QCOMPARE(field.name(), QLatin1String("test"));
271 field.setName( val );
272 QCOMPARE(field.name(), val);
273}
274
275void tst_QSqlField::setNull()
276{
277 QSqlField field( "test", QVariant::String );
278 field.setValue( "test" );
279 field.clear();
280 QVERIFY( field.value() == QVariant().toString() );
281 QVERIFY( field.isNull() );
282}
283
284void tst_QSqlField::setReadOnly()
285{
286 QSqlField field( "test", QVariant::String );
287 field.setValue( "test" );
288 field.setReadOnly( true );
289 field.setValue( "Harry" );
290 QCOMPARE(field.value().toString(), QLatin1String("test"));
291 field.clear();
292 QCOMPARE(field.value().toString(), QLatin1String("test"));
293 QVERIFY( !field.isNull() );
294 field.clear();
295 QCOMPARE(field.value().toString(), QLatin1String("test"));
296 QVERIFY( !field.isNull() );
297 field.setReadOnly( false );
298 field.setValue( "Harry" );
299 QCOMPARE(field.value().toString(), QLatin1String("Harry"));
300 field.clear();
301 QVERIFY( field.value() == QVariant().toString() );
302 QVERIFY( field.isNull() );
303}
304
305void tst_QSqlField::setValue_data()
306{
307 QTest::addColumn<int>(name: "ival");
308 QTest::addColumn<bool>(name: "bval");
309 QTest::addColumn<double>(name: "dval");
310 QTest::addColumn<QString>(name: "sval");
311
312 //next we fill it with data
313 QTest::newRow( dataTag: "data0" ) << 0 << false << (double)223.232 << QString("");
314 QTest::newRow( dataTag: "data1" ) << 123 << true << (double)-232.232 << QString("Harry");
315 QTest::newRow( dataTag: "data2" ) << -123 << false << (double)232222.323223233338 << QString("Woipertinger");
316}
317
318void tst_QSqlField::setValue()
319{
320 QSqlField field1 ( "test", QVariant::Int );
321 QSqlField field2 ( "test", QVariant::String );
322 QSqlField field3 ( "test", QVariant::Bool );
323 QSqlField field4 ( "test", QVariant::Double );
324 field1.clear();
325 QFETCH( int, ival );
326 QFETCH( QString, sval );
327 QFETCH( double, dval );
328 QFETCH( bool, bval );
329 field1.setValue( ival );
330 QCOMPARE( field1.value().toInt(), ival );
331 // setValue should also reset the NULL flag
332 QVERIFY( !field1.isNull() );
333
334 field2.setValue( sval );
335 QCOMPARE( field2.value().toString(), sval );
336 field3.setValue( QVariant( bval) );
337 QVERIFY( field3.value().toBool() == bval );
338 field4.setValue( dval );
339 QCOMPARE( field4.value().toDouble(), dval );
340 field4.setReadOnly( true );
341 field4.setValue( "Something_that's_not_a_double" );
342 QCOMPARE( field4.value().toDouble(), dval );
343}
344
345void tst_QSqlField::type()
346{
347 QSqlField field1( "string", QVariant::String );
348 QSqlField field2( "string", QVariant::Bool );
349 QSqlField field3( "string", QVariant::Double );
350 QVERIFY( field1.type() == QVariant::String );
351 QVERIFY( field2.type() == QVariant::Bool );
352 QVERIFY( field3.type() == QVariant::Double );
353}
354
355void tst_QSqlField::setTableName_data()
356{
357 QTest::addColumn<QString>(name: "tableName");
358
359 QTest::newRow(dataTag: "data0") << QString("");
360 QTest::newRow(dataTag: "data1") << QString("tbl");
361}
362
363void tst_QSqlField::setTableName()
364{
365 QSqlField field("test", QVariant::String, "test");
366 QFETCH(QString, tableName);
367 QCOMPARE(field.tableName(), QLatin1String("test"));
368 field.setTableName(tableName);
369 QCOMPARE(field.tableName(), tableName);
370}
371
372QTEST_MAIN(tst_QSqlField)
373#include "tst_qsqlfield.moc"
374

source code of qtbase/tests/auto/sql/kernel/qsqlfield/tst_qsqlfield.cpp