File indexing completed on 2022-10-04 15:18:54

0001 /* This file is part of the KDE project
0002    Copyright (C) 2016 Jarosław Staniek <staniek@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "ConnectionOptionsTest.h"
0021 
0022 #include <KDbConnectionOptions>
0023 #include <QtTest>
0024 
0025 QTEST_GUILESS_MAIN(ConnectionOptionsTest)
0026 
0027 void ConnectionOptionsTest::initTestCase()
0028 {
0029 }
0030 
0031 void ConnectionOptionsTest::testEmpty()
0032 {
0033     KDbConnectionOptions empty;
0034     QVERIFY(!empty.isReadOnly());
0035     QVERIFY(empty.property("fooBar").isNull());
0036     QVERIFY(empty == KDbConnectionOptions());
0037     KDbConnectionOptions opt;
0038     opt.insert("someName", 17);
0039     QVERIFY(empty != opt);
0040     opt = KDbConnectionOptions();
0041     QVERIFY(empty == opt);
0042     QTest::ignoreMessage(QtWarningMsg, "\"/\" cannot be used as property name");
0043     opt.insert("/", "bar"); // no effect, name must be a valid identifier
0044     QVERIFY(opt.property("/").isNull());
0045 }
0046 
0047 void ConnectionOptionsTest::testCopyAndCompare()
0048 {
0049     KDbConnectionOptions empty;
0050     QVERIFY(empty == KDbConnectionOptions(empty));
0051     KDbConnectionOptions opt;
0052     opt.insert("someName", 17, "Some Name");
0053     KDbConnectionOptions opt2(opt);
0054     QVERIFY(opt == opt2);
0055     opt.remove("nonExisting");
0056     QVERIFY(opt == opt2);
0057     opt.remove("someName");
0058     QVERIFY(opt != opt2);
0059     opt2.remove("someName");
0060     QVERIFY(opt == opt2);
0061     opt.insert("someName2", 171, "Some Name 2");
0062     opt2.insert("someName2", 171, "Some Name 2");
0063     QVERIFY(opt == opt2);
0064     opt2.setCaption("nonExisting", "Nope");
0065     QVERIFY(opt == opt2);
0066     // overwrite existing caption
0067     opt.setCaption("someName2", "Other");
0068     QVERIFY(opt != opt2);
0069     opt2.setCaption("someName2", "Other");
0070     QVERIFY(opt == opt2);
0071     // can't overwrite existing caption, v2
0072     opt2.insert("someName2", opt2.property("someName2").value(), "Other");
0073     QVERIFY(opt == opt2);
0074 }
0075 
0076 void ConnectionOptionsTest::testValue()
0077 {
0078     KDbConnectionOptions opt;
0079     opt.setValue("foo", 1);
0080     QVERIFY(opt.property("foo").isNull());
0081     QVERIFY(opt.property("foo").value().isNull());
0082     opt.insert("foo", 17);
0083     QCOMPARE(opt.property("foo").value().type(), QVariant::Int);
0084     QCOMPARE(opt.property("foo").value().toInt(), 17);
0085     opt.insert("foo", 18);
0086     QCOMPARE(opt.property("foo").value().toInt(), 18);
0087     opt.setValue("foo", 19);
0088     QCOMPARE(opt.property("foo").value().toInt(), 19);
0089 }
0090 
0091 void ConnectionOptionsTest::testReadOnly()
0092 {
0093     {
0094         KDbConnectionOptions empty;
0095         QVERIFY(!empty.isReadOnly());
0096         QVERIFY(!empty.property("readOnly").isNull());
0097         QVERIFY(empty.property("readonly").isNull());
0098         QVERIFY(!empty.property("readOnly").value().toBool());
0099         empty.setReadOnly(true);
0100         QVERIFY(empty.isReadOnly());
0101         QVERIFY(!empty.property("readOnly").isNull());
0102         QVERIFY(empty.property("readOnly").value().toBool());
0103     }
0104     {
0105         KDbConnectionOptions opt;
0106         opt.insert("someName", 17);
0107         QVERIFY(!opt.isReadOnly());
0108         QVERIFY(!opt.property("readOnly").isNull());
0109         QVERIFY(!opt.property("readOnly").value().toBool());
0110         opt.setReadOnly(true);
0111 
0112         KDbConnectionOptions opt2(opt);
0113         QVERIFY(opt2.isReadOnly());
0114         QVERIFY(!opt2.property("readOnly").isNull());
0115         QVERIFY(opt2.property("readOnly").value().toBool());
0116     }
0117     {
0118         // setting readOnly property is special
0119         KDbConnectionOptions opt;
0120         opt.insert("readOnly", true, "foo");
0121         QVERIFY(opt.isReadOnly());
0122         QVERIFY(opt.property("readOnly").value().toBool());
0123         opt.insert("readOnly", 1, "foo");
0124         QVERIFY(opt.isReadOnly());
0125         QVERIFY(opt.property("readOnly").value().toBool());
0126     }
0127     {
0128         // can't set readOnly property caption
0129         KDbConnectionOptions opt;
0130         opt.setCaption("readOnly", "foo");
0131         QVERIFY(opt.property("readOnly").caption() != "foo");
0132     }
0133     {
0134         // can't remove readOnly property
0135         KDbConnectionOptions opt;
0136         opt.remove("readOnly");
0137         QVERIFY(!opt.property("readOnly").isNull());
0138     }
0139 }
0140 
0141 void ConnectionOptionsTest::cleanupTestCase()
0142 {
0143 }