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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2012-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 "ConnectionTest.h"
0021 
0022 #include <KDbConnectionData>
0023 #include <KDbDriverManager>
0024 #include <KDbDriverMetaData>
0025 
0026 #include <QDir>
0027 #include <QFile>
0028 #include <QTest>
0029 
0030 QTEST_GUILESS_MAIN(ConnectionTest)
0031 
0032 void ConnectionTest::initTestCase()
0033 {
0034 }
0035 
0036 void ConnectionTest::testConnectionData()
0037 {
0038     KDbConnectionData cdata;
0039     QVERIFY(cdata.databaseName().isEmpty());
0040     QVERIFY(cdata.driverId().isEmpty());
0041     QVERIFY(cdata.caption().isEmpty());
0042     QVERIFY(cdata.description().isEmpty());
0043     QVERIFY(cdata.userName().isEmpty());
0044     QVERIFY(cdata.hostName().isEmpty());
0045     QCOMPARE(cdata.port(), 0);
0046     QVERIFY(cdata.useLocalSocketFile());
0047     QVERIFY(cdata.localSocketFileName().isEmpty());
0048     QVERIFY(cdata.password().isEmpty());
0049     QVERIFY(!cdata.savePassword());
0050     QCOMPARE(cdata, cdata);
0051     QVERIFY2(!cdata.isPasswordNeeded(), "Password-needed is not false for empty data");
0052     QVERIFY(cdata.toUserVisibleString().isEmpty());
0053     QCOMPARE(cdata, KDbConnectionData());
0054 
0055     QString db = "mydb";
0056     cdata.setDatabaseName(db);
0057     QCOMPARE(cdata.databaseName(), db);
0058     QCOMPARE(db, cdata.toUserVisibleString());
0059     QCOMPARE(db, cdata.toUserVisibleString(KDbConnectionData::UserVisibleStringOption::None));
0060 
0061     cdata.setDriverId("INVALID.ID");
0062     QCOMPARE(db, cdata.toUserVisibleString()); // driver ID invalid: still just returns the db name
0063     QCOMPARE(db, cdata.toUserVisibleString(KDbConnectionData::UserVisibleStringOption::None)); // like above
0064 
0065     KDbDriverManager manager;
0066     //! @todo more drivers
0067     if (manager.driver("org.kde.kdb.sqlite")) { // only if sqlite is present
0068         qDebug() << "org.kde.kdb.sqlite driver found, testing...";
0069         cdata = KDbConnectionData();
0070         cdata.setDriverId("org.kde.kdb.sqlite");
0071         QCOMPARE(cdata.toUserVisibleString(), KDbConnection::tr("<file>"));
0072         cdata.setDatabaseName("my.db");
0073         QCOMPARE(cdata.toUserVisibleString(), KDbConnection::tr("file: %1").arg("my.db"));
0074         KDbConnectionData copy(cdata);
0075         QCOMPARE(cdata, copy);
0076     }
0077     if (manager.driver("org.kde.kdb.mysql")) { // only if mysql is present
0078         qDebug() << "org.kde.kdb.mysql driver found, testing...";
0079         cdata = KDbConnectionData();
0080         cdata.setDriverId("org.kde.kdb.mysql");
0081         QCOMPARE(cdata.toUserVisibleString(), QLatin1String("localhost"));
0082         QCOMPARE(cdata.toUserVisibleString(KDbConnectionData::UserVisibleStringOption::None),
0083                  QLatin1String("localhost")); // like above
0084         cdata.setUserName("joe");
0085         QCOMPARE(cdata.toUserVisibleString(), QLatin1String("joe@localhost"));
0086         cdata.setUserName(QString());
0087         cdata.setHostName("example.com");
0088         QCOMPARE(cdata.toUserVisibleString(), QLatin1String("example.com"));
0089         cdata.setUserName("joe");
0090         QCOMPARE(cdata.toUserVisibleString(), QLatin1String("joe@example.com"));
0091         QCOMPARE(cdata.toUserVisibleString(KDbConnectionData::UserVisibleStringOption::None),
0092                  QLatin1String("example.com"));
0093         cdata.setPort(12345);
0094         QCOMPARE(cdata.toUserVisibleString(), QLatin1String("joe@example.com:12345"));
0095         QCOMPARE(cdata.toUserVisibleString(KDbConnectionData::UserVisibleStringOption::None),
0096                  QLatin1String("example.com:12345"));
0097         KDbConnectionData copy(cdata);
0098         QCOMPARE(cdata, copy);
0099     }
0100 }
0101 
0102 void ConnectionTest::testCreateDb()
0103 {
0104     QVERIFY(utils.testCreateDb("ConnectionTest"));
0105     QVERIFY(utils.testUse());
0106     QVERIFY(utils.testProperties());
0107     QVERIFY(utils.testCreateTables());
0108     QVERIFY(utils.testDisconnectAndDropDb());
0109 }
0110 
0111 void ConnectionTest::testConnectToNonexistingDb()
0112 {
0113     QVERIFY(utils.driver);
0114 
0115     //open connection
0116     KDbConnectionData cdata;
0117     cdata.setDatabaseName(QLatin1String("/really-non-existing/path/fiuwehf2349f8h23c2jcoeqw"));
0118     QVERIFY(utils.testConnect(cdata));
0119     QVERIFY(utils.connection());
0120     KDB_VERIFY(utils.connection(), !utils.connection()->databaseExists(utils.connection()->data().databaseName()),
0121                     "Database should not exist");
0122     KDB_EXPECT_FAIL(utils.connection(), utils.connection()->useDatabase(),
0123                     ERR_OBJECT_NOT_FOUND, "Should fail to use database");
0124     KDB_EXPECT_FAIL(utils.connection(), utils.connection()->isDatabaseUsed(),
0125                     ERR_OBJECT_NOT_FOUND, "Database can't be used after call to useDatabase()");
0126     QVERIFY2(utils.connection()->closeDatabase(), "Closing after failed USE should work");
0127     KDB_VERIFY(utils.connection(), utils.connection()->disconnect(), "Failed to disconnect database");
0128     QVERIFY2(!utils.connection()->isConnected(), "Should not be connected");
0129 }
0130 
0131 void ConnectionTest::cleanupTestCase()
0132 {
0133 }