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 <qcoreapplication.h>
33#include <qdebug.h>
34#include <qsqlerror.h>
35
36class tst_QSqlError : public QObject
37{
38Q_OBJECT
39
40public:
41 tst_QSqlError();
42 virtual ~tst_QSqlError();
43
44private slots:
45#if QT_DEPRECATED_SINCE(5, 1)
46 void getSetCheck();
47#endif
48 void construction();
49 void moveOperator();
50 void operators();
51 void qtbug_74575();
52};
53
54tst_QSqlError::tst_QSqlError()
55{
56}
57
58tst_QSqlError::~tst_QSqlError()
59{
60}
61
62#if QT_DEPRECATED_SINCE(5, 1)
63// Testing get/set functions
64void tst_QSqlError::getSetCheck()
65{
66 QSqlError obj1;
67 // ErrorType QSqlError::type()
68 // void QSqlError::setType(ErrorType)
69 obj1.setType(QSqlError::ErrorType(QSqlError::NoError));
70 QCOMPARE(QSqlError::ErrorType(QSqlError::NoError), obj1.type());
71 obj1.setType(QSqlError::ErrorType(QSqlError::ConnectionError));
72 QCOMPARE(QSqlError::ErrorType(QSqlError::ConnectionError), obj1.type());
73 obj1.setType(QSqlError::ErrorType(QSqlError::StatementError));
74 QCOMPARE(QSqlError::ErrorType(QSqlError::StatementError), obj1.type());
75 obj1.setType(QSqlError::ErrorType(QSqlError::TransactionError));
76 QCOMPARE(QSqlError::ErrorType(QSqlError::TransactionError), obj1.type());
77 obj1.setType(QSqlError::ErrorType(QSqlError::UnknownError));
78 QCOMPARE(QSqlError::ErrorType(QSqlError::UnknownError), obj1.type());
79
80 // int QSqlError::number()
81 // void QSqlError::setNumber(int)
82 obj1.setNumber(0);
83 QCOMPARE(0, obj1.number());
84 obj1.setNumber(INT_MIN);
85 QCOMPARE(INT_MIN, obj1.number());
86 obj1.setNumber(INT_MAX);
87 QCOMPARE(INT_MAX, obj1.number());
88}
89#endif
90
91void tst_QSqlError::construction()
92{
93#if QT_DEPRECATED_SINCE(5, 3)
94 {
95 QSqlError obj1("drivertext", "databasetext", QSqlError::UnknownError, 123);
96 QCOMPARE(obj1.driverText(), QString("drivertext"));
97 QCOMPARE(obj1.databaseText(), QString("databasetext"));
98 QCOMPARE(obj1.type(), QSqlError::UnknownError);
99 QCOMPARE(obj1.number(), 123);
100 QCOMPARE(obj1.nativeErrorCode(), QStringLiteral("123"));
101 QVERIFY(obj1.isValid());
102 }
103#endif
104 QSqlError obj1("drivertext", "databasetext", QSqlError::UnknownError, QStringLiteral("123"));
105 QCOMPARE(obj1.driverText(), QString("drivertext"));
106 QCOMPARE(obj1.databaseText(), QString("databasetext"));
107 QCOMPARE(obj1.type(), QSqlError::UnknownError);
108#if QT_DEPRECATED_SINCE(5, 3)
109 QCOMPARE(obj1.number(), 123);
110#endif
111 QCOMPARE(obj1.nativeErrorCode(), QStringLiteral("123"));
112 QVERIFY(obj1.isValid());
113
114 QSqlError obj2(obj1);
115 QCOMPARE(obj2.driverText(), obj1.driverText());
116 QCOMPARE(obj2.databaseText(), obj1.databaseText());
117 QCOMPARE(obj2.type(), obj1.type());
118#if QT_DEPRECATED_SINCE(5, 3)
119 QCOMPARE(obj2.number(), obj1.number());
120#endif
121 QCOMPARE(obj2.nativeErrorCode(), obj1.nativeErrorCode());
122 QVERIFY(obj2.isValid());
123
124 QSqlError obj3 = obj2;
125 QCOMPARE(obj3.driverText(), obj2.driverText());
126 QCOMPARE(obj3.databaseText(), obj2.databaseText());
127 QCOMPARE(obj3.type(), obj2.type());
128#if QT_DEPRECATED_SINCE(5, 3)
129 QCOMPARE(obj3.number(), obj2.number());
130#endif
131 QCOMPARE(obj3.nativeErrorCode(), obj2.nativeErrorCode());
132 QVERIFY(obj3.isValid());
133
134 QSqlError obj4;
135 QVERIFY(!obj4.isValid());
136 QCOMPARE(obj4.driverText(), QString());
137 QCOMPARE(obj4.databaseText(), QString());
138 QCOMPARE(obj4.text(), QString());
139 QCOMPARE(obj4.type(), QSqlError::NoError);
140#if QT_DEPRECATED_SINCE(5, 3)
141 QCOMPARE(obj4.number(), -1);
142#endif
143 QCOMPARE(obj4.nativeErrorCode(), QString());
144
145 QSqlError obj5(QStringLiteral("drivertext"), QStringLiteral("databasetext"),
146 QSqlError::UnknownError, QStringLiteral("123"));
147 QCOMPARE(obj5.driverText(), QString("drivertext"));
148 QCOMPARE(obj5.databaseText(), QString("databasetext"));
149 QCOMPARE(obj5.type(), QSqlError::UnknownError);
150#if QT_DEPRECATED_SINCE(5, 3)
151 QCOMPARE(obj5.number(), 123);
152#endif
153 QCOMPARE(obj5.nativeErrorCode(), QStringLiteral("123"));
154 QVERIFY(obj5.isValid());
155
156 QSqlError obj6(QStringLiteral("drivertext"), QStringLiteral("databasetext"),
157 QSqlError::UnknownError, QStringLiteral("Err123"));
158 QCOMPARE(obj6.driverText(), QString("drivertext"));
159 QCOMPARE(obj6.databaseText(), QString("databasetext"));
160 QCOMPARE(obj6.type(), QSqlError::UnknownError);
161#if QT_DEPRECATED_SINCE(5, 3)
162 QCOMPARE(obj6.number(), 0);
163#endif
164 QCOMPARE(obj6.nativeErrorCode(), QStringLiteral("Err123"));
165 QVERIFY(obj6.isValid());
166
167#if QT_DEPRECATED_SINCE(5, 3)
168 {
169 // Default constructed object as constructed before Qt 5.3
170 QSqlError obj7(QString(), QString(), QSqlError::NoError, -1);
171 QVERIFY(!obj7.isValid());
172 QCOMPARE(obj7.driverText(), QString());
173 QCOMPARE(obj7.databaseText(), QString());
174 QCOMPARE(obj7.type(), QSqlError::NoError);
175 QCOMPARE(obj7.number(), -1);
176 QCOMPARE(obj7.nativeErrorCode(), QString());
177 }
178#endif
179
180 // Default constructed object
181 QSqlError obj7;
182 QVERIFY(!obj7.isValid());
183 QCOMPARE(obj7.driverText(), QString());
184 QCOMPARE(obj7.databaseText(), QString());
185 QCOMPARE(obj7.type(), QSqlError::NoError);
186 QCOMPARE(obj7.nativeErrorCode(), QString());
187
188 // Move constructor
189 QSqlError obj8(std::move(obj3));
190 QCOMPARE(obj8.driverText(), obj2.driverText());
191 QCOMPARE(obj8.databaseText(), obj2.databaseText());
192 QCOMPARE(obj8.type(), obj2.type());
193#if QT_DEPRECATED_SINCE(5, 3)
194 QCOMPARE(obj8.number(), obj2.number());
195#endif
196 QCOMPARE(obj8.nativeErrorCode(), obj2.nativeErrorCode());
197 QVERIFY(obj8.isValid());
198}
199
200void tst_QSqlError::moveOperator()
201{
202 QSqlError obj1("drivertext", "databasetext", QSqlError::UnknownError, QStringLiteral("123")), obj2;
203 obj2 = std::move(obj1);
204 QCOMPARE(obj2.driverText(), QString("drivertext"));
205 QCOMPARE(obj2.databaseText(), QString("databasetext"));
206 QCOMPARE(obj2.type(), QSqlError::UnknownError);
207#if QT_DEPRECATED_SINCE(5, 3)
208 QCOMPARE(obj2.number(), 123);
209#endif
210 QCOMPARE(obj2.nativeErrorCode(), QStringLiteral("123"));
211 QVERIFY(obj2.isValid());
212}
213
214void tst_QSqlError::operators()
215{
216 QSqlError error1(QString(), QString(), QSqlError::NoError);
217 QSqlError error2(QString(), QString(), QSqlError::NoError);
218 QSqlError error3(QString(), QString(), QSqlError::UnknownError);
219
220 QCOMPARE(error1, error2);
221 QVERIFY(error1 != error3);
222}
223
224void tst_QSqlError::qtbug_74575()
225{
226 const QString driverText(QStringLiteral("drivertext"));
227 const QString databaseText(QStringLiteral("databasetext"));
228 const QString databaseTextNewline(QStringLiteral("databasetext\n"));
229
230 QSqlError error1(driverText, databaseText,
231 QSqlError::UnknownError, QStringLiteral("123"));
232 QCOMPARE(error1.text(), databaseText + QLatin1Char(' ') + driverText);
233
234 QSqlError error2(QString(), databaseText,
235 QSqlError::UnknownError, QStringLiteral("123"));
236 QCOMPARE(error2.text(), databaseText);
237
238 QSqlError error3(driverText, QString(),
239 QSqlError::UnknownError, QStringLiteral("123"));
240 QCOMPARE(error3.text(), driverText);
241
242 QSqlError error4(driverText, databaseTextNewline,
243 QSqlError::UnknownError, QStringLiteral("123"));
244 QCOMPARE(error4.text(), databaseTextNewline + driverText);
245}
246
247QTEST_MAIN(tst_QSqlError)
248#include "tst_qsqlerror.moc"
249

source code of qtbase/tests/auto/sql/kernel/qsqlerror/tst_qsqlerror.cpp