File indexing completed on 2024-11-10 04:40:18

0001 /*
0002     SPDX-FileCopyrightText: 2011 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QObject>
0008 #include <QScopeGuard>
0009 #include <QSettings>
0010 #include <QTemporaryDir>
0011 #include <QTest>
0012 
0013 #include "aktest.h"
0014 #include "private/standarddirs_p.h"
0015 #include "shared/akranges.h"
0016 #include <qtenvironmentvariables.h>
0017 
0018 #include <QSettings>
0019 #include <QStandardPaths>
0020 
0021 #include "storage/dbconfig.h"
0022 #include "storage/dbconfigpostgresql.h"
0023 #include <config-akonadi.h>
0024 
0025 using namespace Akonadi;
0026 using namespace Akonadi::Server;
0027 using namespace AkRanges;
0028 
0029 class TestableDbConfigPostgresql : public DbConfigPostgresql
0030 {
0031 public:
0032     QStringList postgresSearchPaths(const QTemporaryDir &dir)
0033     {
0034         return DbConfigPostgresql::postgresSearchPaths(dir.path());
0035     }
0036 };
0037 
0038 class DbConfigTest : public QObject
0039 {
0040     Q_OBJECT
0041 private Q_SLOTS:
0042     void testDbConfig_data()
0043     {
0044         QTest::addColumn<QString>("driverName");
0045         QTest::addColumn<QString>("dbName");
0046         QTest::addColumn<bool>("useInternalServer");
0047 
0048         QStandardPaths::setTestModeEnabled(true);
0049         akTestSetInstanceIdentifier(QStringLiteral("unit-test"));
0050 
0051         const QString sqlitedb = StandardDirs::saveDir("data") + QStringLiteral("/akonadi.db");
0052         QTest::newRow("QSQLITE") << QStringLiteral("QSQLITE") << sqlitedb << false;
0053         QTest::newRow("QMYSQL") << QStringLiteral("QMYSQL") << QStringLiteral("akonadi") << true;
0054         QTest::newRow("QPSQL") << QStringLiteral("QPSQL") << QStringLiteral("akonadi_unit_test") << true;
0055     }
0056     void testDbConfig()
0057     {
0058         QFETCH(QString, driverName);
0059         QFETCH(QString, dbName);
0060         QFETCH(bool, useInternalServer);
0061 
0062         // isolated config file to not conflict with a running instance
0063         QStandardPaths::setTestModeEnabled(true);
0064         akTestSetInstanceIdentifier(QStringLiteral("unit-test"));
0065 
0066         {
0067             QSettings s(StandardDirs::serverConfigFile(StandardDirs::ReadWrite), QSettings::IniFormat);
0068             s.setValue(QStringLiteral("General/Driver"), driverName);
0069         }
0070 
0071         const auto destroyDbConfig = qScopeGuard([] {
0072             DbConfig::destroy();
0073         });
0074         auto *cfg = DbConfig::configuredDatabase();
0075 
0076         QVERIFY(cfg != nullptr);
0077 
0078         QCOMPARE(cfg->driverName(), driverName);
0079         QCOMPARE(cfg->databaseName(), dbName);
0080         QCOMPARE(cfg->useInternalServer(), useInternalServer);
0081         QCOMPARE(cfg->sizeThreshold(), 4096LL);
0082     }
0083 
0084     void testPostgresVersionedLookup()
0085     {
0086         QTemporaryDir dir;
0087         QVERIFY(dir.isValid());
0088 
0089         const QStringList versions{QStringLiteral("10.2"),
0090                                    QStringLiteral("10.0"),
0091                                    QStringLiteral("9.5"),
0092                                    QStringLiteral("12.4"),
0093                                    QStringLiteral("8.0"),
0094                                    QStringLiteral("12.0")};
0095         for (const auto &version : versions) {
0096             QVERIFY(QDir(dir.path()).mkdir(version));
0097         }
0098 
0099         TestableDbConfigPostgresql dbConfig;
0100         const auto paths = dbConfig.postgresSearchPaths(dir) | Views::filter([&dir](const auto &path) {
0101                                return path.startsWith(dir.path());
0102                            })
0103             | Views::transform([&dir](const auto &path) {
0104                                return QString(path).remove(dir.path() + QStringLiteral("/")).remove(QStringLiteral("/bin"));
0105                            })
0106             | Actions::toQList;
0107 
0108         const QStringList expected{QStringLiteral("12.4"),
0109                                    QStringLiteral("12.0"),
0110                                    QStringLiteral("10.2"),
0111                                    QStringLiteral("10.0"),
0112                                    QStringLiteral("9.5"),
0113                                    QStringLiteral("8.0")};
0114         QCOMPARE(paths, expected);
0115     }
0116 };
0117 
0118 AKTEST_MAIN(DbConfigTest)
0119 
0120 #include "dbconfigtest.moc"