1 /* This file is part of the KDE project
2    Copyright (C) 2016 Jarosław Staniek <staniek@kde.org>
3 
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this program; see the file COPYING.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 
20 #include "ConnectionOptionsTest.h"
21 
22 #include <KDbConnectionOptions>
23 #include <QtTest>
24 
QTEST_GUILESS_MAIN(ConnectionOptionsTest)25 QTEST_GUILESS_MAIN(ConnectionOptionsTest)
26 
27 void ConnectionOptionsTest::initTestCase()
28 {
29 }
30 
testEmpty()31 void ConnectionOptionsTest::testEmpty()
32 {
33     KDbConnectionOptions empty;
34     QVERIFY(!empty.isReadOnly());
35     QVERIFY(empty.property("fooBar").isNull());
36     QVERIFY(empty == KDbConnectionOptions());
37     KDbConnectionOptions opt;
38     opt.insert("someName", 17);
39     QVERIFY(empty != opt);
40     opt = KDbConnectionOptions();
41     QVERIFY(empty == opt);
42     QTest::ignoreMessage(QtWarningMsg, "\"/\" cannot be used as property name");
43     opt.insert("/", "bar"); // no effect, name must be a valid identifier
44     QVERIFY(opt.property("/").isNull());
45 }
46 
testCopyAndCompare()47 void ConnectionOptionsTest::testCopyAndCompare()
48 {
49     KDbConnectionOptions empty;
50     QVERIFY(empty == KDbConnectionOptions(empty));
51     KDbConnectionOptions opt;
52     opt.insert("someName", 17, "Some Name");
53     KDbConnectionOptions opt2(opt);
54     QVERIFY(opt == opt2);
55     opt.remove("nonExisting");
56     QVERIFY(opt == opt2);
57     opt.remove("someName");
58     QVERIFY(opt != opt2);
59     opt2.remove("someName");
60     QVERIFY(opt == opt2);
61     opt.insert("someName2", 171, "Some Name 2");
62     opt2.insert("someName2", 171, "Some Name 2");
63     QVERIFY(opt == opt2);
64     opt2.setCaption("nonExisting", "Nope");
65     QVERIFY(opt == opt2);
66     // overwrite existing caption
67     opt.setCaption("someName2", "Other");
68     QVERIFY(opt != opt2);
69     opt2.setCaption("someName2", "Other");
70     QVERIFY(opt == opt2);
71     // can't overwrite existing caption, v2
72     opt2.insert("someName2", opt2.property("someName2").value(), "Other");
73     QVERIFY(opt == opt2);
74 }
75 
testValue()76 void ConnectionOptionsTest::testValue()
77 {
78     KDbConnectionOptions opt;
79     opt.setValue("foo", 1);
80     QVERIFY(opt.property("foo").isNull());
81     QVERIFY(opt.property("foo").value().isNull());
82     opt.insert("foo", 17);
83     QCOMPARE(opt.property("foo").value().type(), QVariant::Int);
84     QCOMPARE(opt.property("foo").value().toInt(), 17);
85     opt.insert("foo", 18);
86     QCOMPARE(opt.property("foo").value().toInt(), 18);
87     opt.setValue("foo", 19);
88     QCOMPARE(opt.property("foo").value().toInt(), 19);
89 }
90 
testReadOnly()91 void ConnectionOptionsTest::testReadOnly()
92 {
93     {
94         KDbConnectionOptions empty;
95         QVERIFY(!empty.isReadOnly());
96         QVERIFY(!empty.property("readOnly").isNull());
97         QVERIFY(empty.property("readonly").isNull());
98         QVERIFY(!empty.property("readOnly").value().toBool());
99         empty.setReadOnly(true);
100         QVERIFY(empty.isReadOnly());
101         QVERIFY(!empty.property("readOnly").isNull());
102         QVERIFY(empty.property("readOnly").value().toBool());
103     }
104     {
105         KDbConnectionOptions opt;
106         opt.insert("someName", 17);
107         QVERIFY(!opt.isReadOnly());
108         QVERIFY(!opt.property("readOnly").isNull());
109         QVERIFY(!opt.property("readOnly").value().toBool());
110         opt.setReadOnly(true);
111 
112         KDbConnectionOptions opt2(opt);
113         QVERIFY(opt2.isReadOnly());
114         QVERIFY(!opt2.property("readOnly").isNull());
115         QVERIFY(opt2.property("readOnly").value().toBool());
116     }
117     {
118         // setting readOnly property is special
119         KDbConnectionOptions opt;
120         opt.insert("readOnly", true, "foo");
121         QVERIFY(opt.isReadOnly());
122         QVERIFY(opt.property("readOnly").value().toBool());
123         opt.insert("readOnly", 1, "foo");
124         QVERIFY(opt.isReadOnly());
125         QVERIFY(opt.property("readOnly").value().toBool());
126     }
127     {
128         // can't set readOnly property caption
129         KDbConnectionOptions opt;
130         opt.setCaption("readOnly", "foo");
131         QVERIFY(opt.property("readOnly").caption() != "foo");
132     }
133     {
134         // can't remove readOnly property
135         KDbConnectionOptions opt;
136         opt.remove("readOnly");
137         QVERIFY(!opt.property("readOnly").isNull());
138     }
139 }
140 
cleanupTestCase()141 void ConnectionOptionsTest::cleanupTestCase()
142 {
143 }
144