summaryrefslogtreecommitdiffstats
path: root/tests/auto/sql/kernel/qsqlrecord/tst_qsqlrecord.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/sql/kernel/qsqlrecord/tst_qsqlrecord.cpp')
-rw-r--r--tests/auto/sql/kernel/qsqlrecord/tst_qsqlrecord.cpp168
1 files changed, 67 insertions, 101 deletions
diff --git a/tests/auto/sql/kernel/qsqlrecord/tst_qsqlrecord.cpp b/tests/auto/sql/kernel/qsqlrecord/tst_qsqlrecord.cpp
index 83a61b91d7..6aeae86d7d 100644
--- a/tests/auto/sql/kernel/qsqlrecord/tst_qsqlrecord.cpp
+++ b/tests/auto/sql/kernel/qsqlrecord/tst_qsqlrecord.cpp
@@ -1,33 +1,8 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** 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 https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-
-#include <QtTest/QtTest>
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+
+#include <QTest>
#include "qsqlrecord.h"
#include "qsqlfield.h"
@@ -39,12 +14,7 @@
class tst_QSqlRecord : public QObject
{
-Q_OBJECT
-
-public:
- tst_QSqlRecord();
- virtual ~tst_QSqlRecord();
-
+ Q_OBJECT
public slots:
void init();
@@ -70,28 +40,14 @@ private slots:
void clearValues();
void clear();
void append();
+ void moveSemantics();
private:
- QSqlRecord* rec;
- QSqlField* fields[ NUM_FIELDS ];
+ std::unique_ptr<QSqlRecord> rec;
+ std::array<std::unique_ptr<QSqlField>, NUM_FIELDS> 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();
@@ -99,44 +55,39 @@ void tst_QSqlRecord::init()
void tst_QSqlRecord::cleanup()
{
- delete rec;
- for ( int i = 0; i < NUM_FIELDS; ++i ) {
- delete fields[ i ];
- fields[ i ] = 0;
- }
- rec = 0;
+ rec = nullptr;
+ for (auto &field : fields)
+ field = nullptr;
}
void tst_QSqlRecord::createTestRecord()
{
- delete rec;
- rec = new QSqlRecord();
- fields[0] = new QSqlField(QStringLiteral("string"), QVariant::String, QStringLiteral("stringtable"));
- fields[1] = new QSqlField(QStringLiteral("int"), QVariant::Int, QStringLiteral("inttable"));
- fields[2] = new QSqlField(QStringLiteral("double"), QVariant::Double, QStringLiteral("doubletable"));
- fields[3] = new QSqlField(QStringLiteral("bool"), QVariant::Bool);
- for ( int i = 0; i < NUM_FIELDS; ++i )
- rec->append( *(fields[ i ] ) );
+ rec = std::make_unique<QSqlRecord>();
+ fields[0] = std::make_unique<QSqlField>(QStringLiteral("string"), QMetaType(QMetaType::QString), QStringLiteral("stringtable"));
+ fields[1] = std::make_unique<QSqlField>(QStringLiteral("int"), QMetaType(QMetaType::Int), QStringLiteral("inttable"));
+ fields[2] = std::make_unique<QSqlField>(QStringLiteral("double"), QMetaType(QMetaType::Double), QStringLiteral("doubletable"));
+ fields[3] = std::make_unique<QSqlField>(QStringLiteral("bool"), QMetaType(QMetaType::Bool));
+ for (const auto &field : fields)
+ rec->append(*field);
}
void tst_QSqlRecord::append()
{
- delete rec;
- rec = new QSqlRecord();
- rec->append(QSqlField("string", QVariant::String, QStringLiteral("stringtable")));
+ rec = std::make_unique<QSqlRecord>();
+ rec->append(QSqlField("string", QMetaType(QMetaType::QString), QStringLiteral("stringtable")));
QCOMPARE( rec->field( 0 ).name(), (QString) "string" );
QCOMPARE(rec->field(0).tableName(), QStringLiteral("stringtable"));
QVERIFY( !rec->isEmpty() );
QCOMPARE( (int)rec->count(), 1 );
- rec->append(QSqlField("int", QVariant::Int, QStringLiteral("inttable")));
+ rec->append(QSqlField("int", QMetaType(QMetaType::Int), QStringLiteral("inttable")));
QCOMPARE( rec->field( 1 ).name(), (QString) "int" );
QCOMPARE(rec->field(1).tableName(), QStringLiteral("inttable"));
QCOMPARE( (int)rec->count(), 2 );
- rec->append( QSqlField( "double", QVariant::Double ) );
+ rec->append( QSqlField( "double", QMetaType(QMetaType::Double) ) );
QCOMPARE( rec->field( 2 ).name(), (QString) "double" );
QCOMPARE( (int)rec->count(), 3 );
- rec->append( QSqlField( "bool", QVariant::Bool ) );
+ rec->append( QSqlField( "bool", QMetaType(QMetaType::Bool) ) );
QCOMPARE( rec->field( 3 ).name(), (QString) "bool" );
QCOMPARE( (int)rec->count(), 4 );
QCOMPARE( rec->indexOf( "string" ), 0 );
@@ -182,21 +133,18 @@ void tst_QSqlRecord::clearValues()
QFETCH( double, dval );
QFETCH( int, bval );
- if(rec)
- delete rec;
-
- rec = new QSqlRecord();
- rec->append( QSqlField( "string", QVariant::String ) );
+ rec = std::make_unique<QSqlRecord>();
+ rec->append( QSqlField( "string", QMetaType(QMetaType::QString) ) );
QCOMPARE( rec->field(0).name(), (QString) "string" );
QVERIFY( !rec->isEmpty() );
QCOMPARE( (int)rec->count(), 1 );
- rec->append( QSqlField( "int", QVariant::Int ) );
+ rec->append( QSqlField( "int", QMetaType(QMetaType::Int) ) );
QCOMPARE( rec->field(1).name(), (QString) "int" );
QCOMPARE( (int)rec->count(), 2 );
- rec->append( QSqlField( "double", QVariant::Double ) );
+ rec->append( QSqlField( "double", QMetaType(QMetaType::Double) ) );
QCOMPARE( rec->field(2).name(), (QString) "double" );
QCOMPARE( (int)rec->count(), 3 );
- rec->append( QSqlField( "bool", QVariant::Bool ) );
+ rec->append( QSqlField( "bool", QMetaType(QMetaType::Bool) ) );
QCOMPARE( rec->field(3).name(), (QString) "bool" );
QCOMPARE( (int)rec->count(), 4 );
QCOMPARE( rec->indexOf( "string" ), 0 );
@@ -225,8 +173,8 @@ void tst_QSqlRecord::clearValues()
void tst_QSqlRecord::contains()
{
createTestRecord();
- for ( int i = 0; i < NUM_FIELDS; ++i )
- QVERIFY( rec->contains( fields[ i ]->name() ) );
+ for (const auto &field : fields)
+ QVERIFY(rec->contains(field->name()));
QVERIFY( !rec->contains( "__Harry__" ) );
}
@@ -258,8 +206,8 @@ void tst_QSqlRecord::fieldName()
{
createTestRecord();
- for ( int i = 0; i < NUM_FIELDS; ++i )
- QVERIFY( rec->field( (fields[ i ] )->name() ) == *( fields[ i ] ) );
+ for (const auto &field : fields)
+ QVERIFY(rec->field(field->name()) == *field);
QVERIFY( rec->fieldName( NUM_FIELDS ).isNull() );
}
@@ -268,29 +216,29 @@ void tst_QSqlRecord::insert()
QSqlRecord iRec;
int i;
for ( i = 0; i <= 100; ++i ) {
- iRec.insert( i, QSqlField( QString::number( i ), QVariant::Int ) );
+ iRec.insert( i, QSqlField( QString::number( i ), QMetaType(QMetaType::Int) ) );
}
for ( i = 0; i <= 100; ++i ) {
QCOMPARE( iRec.fieldName( i ), QString::number( i ) );
}
-// iRec.insert( 505, QSqlField( "Harry", QVariant::Double ) );
+// iRec.insert( 505, QSqlField( "Harry", QMetaType(QMetaType::Double) ) );
// QCOMPARE( iRec.fieldName( 505 ), (QString)"Harry" );
-// QVERIFY( iRec.field( 505 ).type() == QVariant::Double );
+// QVERIFY( iRec.field( 505 ).type() == QMetaType(QMetaType::Double) );
- iRec.insert( 42, QSqlField( "Everything", QVariant::String ) );
+ iRec.insert( 42, QSqlField( "Everything", QMetaType(QMetaType::QString) ) );
QCOMPARE( iRec.fieldName( 42 ), (QString)"Everything" );
- QVERIFY( iRec.field( 42 ).type() == QVariant::String );
+ QVERIFY( iRec.field( 42 ).metaType() == QMetaType(QMetaType::QString) );
}
void tst_QSqlRecord::isEmpty()
{
QSqlRecord eRec;
QVERIFY( eRec.isEmpty() );
- eRec.append( QSqlField( "Harry", QVariant::String ) );
+ eRec.append( QSqlField( "Harry", QMetaType(QMetaType::QString) ) );
QVERIFY( !eRec.isEmpty() );
eRec.remove( 0 );
QVERIFY( eRec.isEmpty() );
- eRec.insert( 0, QSqlField( "Harry", QVariant::String ) );
+ eRec.insert( 0, QSqlField( "Harry", QMetaType(QMetaType::QString) ) );
QVERIFY( !eRec.isEmpty() );
eRec.clear();
QVERIFY( eRec.isEmpty() );
@@ -383,7 +331,7 @@ void tst_QSqlRecord::operator_Assign()
buf3.remove( NUM_FIELDS - 1 );
QSqlRecord buf5 = buf3;
for ( i = 0; i < NUM_FIELDS - 1; ++i ) {
- QSqlField fi(fields[i]->name(), fields[i]->type(), fields[i]->tableName());
+ QSqlField fi(fields[i]->name(), fields[i]->metaType(), fields[i]->tableName());
fi.clear();
QVERIFY( buf5.field( i ) == fi );
QVERIFY( buf5.isGenerated( i ) );
@@ -413,7 +361,7 @@ void tst_QSqlRecord::remove()
}
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() ) );
+ rec->insert( i, QSqlField( fields[ i ]->name(), fields[ i ]->metaType() ) );
QVERIFY( rec->isGenerated( i ) );
}
}
@@ -437,19 +385,18 @@ void tst_QSqlRecord::setValue()
{
int i;
- delete rec;
- rec = new QSqlRecord();
- rec->append( QSqlField( "string", QVariant::String ) );
+ rec = std::make_unique<QSqlRecord>();
+ rec->append( QSqlField( "string", QMetaType(QMetaType::QString) ) );
QCOMPARE( rec->field( 0 ).name(), (QString) "string" );
QVERIFY( !rec->isEmpty() );
QCOMPARE( (int)rec->count(), 1 );
- rec->append( QSqlField( "int", QVariant::Int ) );
+ rec->append( QSqlField( "int", QMetaType(QMetaType::Int) ) );
QCOMPARE( rec->field( 1 ).name(), (QString) "int" );
QCOMPARE( (int)rec->count(), 2 );
- rec->append( QSqlField( "double", QVariant::Double ) );
+ rec->append( QSqlField( "double", QMetaType(QMetaType::Double) ) );
QCOMPARE( rec->field( 2 ).name(), (QString) "double" );
QCOMPARE( (int)rec->count(), 3 );
- rec->append( QSqlField( "bool", QVariant::Bool ) );
+ rec->append( QSqlField( "bool", QMetaType(QMetaType::Bool) ) );
QCOMPARE( rec->field( 3 ).name(), (QString) "bool" );
QCOMPARE( (int)rec->count(), 4 );
QCOMPARE( rec->indexOf( "string" ), 0 );
@@ -498,10 +445,29 @@ void tst_QSqlRecord::value()
{
// this test is already covered in setValue()
QSqlRecord rec2;
- rec2.append( QSqlField( "string", QVariant::String ) );
+ rec2.append( QSqlField( "string", QMetaType(QMetaType::QString) ) );
rec2.setValue( "string", "Harry" );
QCOMPARE(rec2.value("string").toString(), QLatin1String("Harry"));
}
+void tst_QSqlRecord::moveSemantics()
+{
+ QSqlRecord rec, empty;
+ rec.append(QSqlField("string", QMetaType(QMetaType::QString)));
+ rec.setValue("string", "Harry");
+ auto moved = std::move(rec);
+ // `rec` is not partially-formed
+
+ // moving transfers state:
+ QCOMPARE(moved.value("string").toString(), QLatin1String("Harry"));
+
+ // moved-from objects can be assigned-to:
+ rec = empty;
+ QVERIFY(rec.value("string").isNull());
+
+ // moved-from object can be destroyed:
+ moved = std::move(rec);
+}
+
QTEST_MAIN(tst_QSqlRecord)
#include "tst_qsqlrecord.moc"