summaryrefslogtreecommitdiffstats
path: root/tests/auto/sql/kernel/qsqlindex/tst_qsqlindex.cpp
blob: 2f4435913324f535a2b1412657285e294eb01f74 (plain)
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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QTest>
#include <QtSql/QtSql>

#include <QtCore/QDateTime>
#include <QtCore/QTimeZone>

#include <numeric>

#include "../qsqldatabase/tst_databases.h"

using namespace Qt::StringLiterals;

QString qtest;

class tst_QSqlIndex : public QObject
{
    Q_OBJECT

public:
    tst_QSqlIndex();

private slots:
    void construction_data();
    void construction();
    void assignment_data();
    void assignment();
    void basicFunctions();
};

tst_QSqlIndex::tst_QSqlIndex()
{
}

void tst_QSqlIndex::construction_data()
{
    QTest::addColumn<QSqlIndex>("sqlIndex");
    QTest::addColumn<QString>("cursorName");
    QTest::addColumn<QString>("name");

    const QString cursorName("cusorName"_L1);
    const QString name("name"_L1);
    QSqlIndex sqlIndex(cursorName, name);
    QTest::newRow("ctor1") << QSqlIndex() << QString() << QString();
    QTest::newRow("ctor2") << sqlIndex << cursorName << name;
    QTest::newRow("copy ctor") << QSqlIndex(sqlIndex) << cursorName << name;
    QTest::newRow("move ctor") << QSqlIndex(std::move(sqlIndex)) << cursorName << name;
}

void tst_QSqlIndex::construction()
{
    QFETCH(QSqlIndex, sqlIndex);
    QFETCH(QString, cursorName);
    QFETCH(QString, name);

    QCOMPARE(sqlIndex.cursorName(), cursorName);
    QCOMPARE(sqlIndex.name(), name);
    QCOMPARE(sqlIndex.isDescending(0), false);
    QCOMPARE(sqlIndex.count(), 0);
}

void tst_QSqlIndex::assignment_data()
{
    QTest::addColumn<QSqlIndex>("sqlIndex");
    QTest::addColumn<QString>("cursorName");
    QTest::addColumn<QString>("name");

    const QString cursorName("cusorName"_L1);
    const QString name("name"_L1);
    QSqlIndex sqlIndex(cursorName, name);
    QSqlIndex sqlIndex1 = sqlIndex;
    QSqlIndex sqlIndex2 = std::move(sqlIndex);
    sqlIndex = std::move(sqlIndex2);
    QTest::newRow("copy assignment") << sqlIndex1 << cursorName << name;
    QTest::newRow("move assignment") << sqlIndex << cursorName << name;
}

void tst_QSqlIndex::assignment()
{
    QFETCH(QSqlIndex, sqlIndex);
    QFETCH(QString, cursorName);
    QFETCH(QString, name);

    QCOMPARE(sqlIndex.cursorName(), cursorName);
    QCOMPARE(sqlIndex.name(), name);
    QCOMPARE(sqlIndex.isDescending(0), false);
    QCOMPARE(sqlIndex.count(), 0);
}

void tst_QSqlIndex::basicFunctions()
{
    QSqlIndex sqlIndex("cursorName"_L1, "name"_L1);
    const QSqlField f1("field1"_L1, QMetaType(QMetaType::UInt), "table1"_L1);
    const QSqlField f2("field2"_L1, QMetaType(QMetaType::Double), "table2"_L1);

    QCOMPARE(sqlIndex.cursorName(), "cursorName"_L1);
    sqlIndex.setCursorName("updatedCursorName"_L1);
    QCOMPARE(sqlIndex.name(), "name"_L1);
    sqlIndex.setName("updatedName"_L1);
    QCOMPARE(sqlIndex.cursorName(), "updatedCursorName"_L1);
    QCOMPARE(sqlIndex.name(), "updatedName"_L1);

    sqlIndex.append(f1);
    QCOMPARE(sqlIndex.count(), 1);
    QCOMPARE(sqlIndex.isDescending(0), false);

    sqlIndex.append(f2, true);
    QCOMPARE(sqlIndex.count(), 2);
    QCOMPARE(sqlIndex.isDescending(0), false);
    QCOMPARE(sqlIndex.isDescending(1), true);

    sqlIndex.setDescending(0, true);
    sqlIndex.setDescending(1, false);
    sqlIndex.setDescending(2, true);
    QCOMPARE(sqlIndex.count(), 2);
    QCOMPARE(sqlIndex.isDescending(0), true);
    QCOMPARE(sqlIndex.isDescending(1), false);

    QCOMPARE(sqlIndex.field(0), f1);
    QCOMPARE(sqlIndex.field(1), f2);
}

QTEST_MAIN(tst_QSqlIndex)
#include "tst_qsqlindex.moc"