File indexing completed on 2025-01-05 03:58:02

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-06-21
0007  * Description : unit test program for digiKam Mysql databse init
0008  *
0009  * SPDX-FileCopyrightText: 2013 by Michael G. Hansen <mike at mghansen dot de>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "databasemysqlinit_utest.h"
0016 
0017 // Qt includes
0018 
0019 #include <QApplication>
0020 #include <QSqlDatabase>
0021 #include <QTimer>
0022 #include <QCommandLineParser>
0023 
0024 // KDE includes
0025 
0026 #include <kaboutdata.h>
0027 
0028 // Local includes
0029 
0030 #include "digikam_debug.h"
0031 #include "daboutdata.h"
0032 #include "albummanager.h"
0033 #include "coredbaccess.h"
0034 #include "thumbsdbaccess.h"
0035 #include "facedbaccess.h"
0036 #include "similaritydbaccess.h"
0037 #include "dbengineparameters.h"
0038 #include "scancontroller.h"
0039 #include "digikam_version.h"
0040 #include "dtestdatadir.h"
0041 #include "wstoolutils.h"
0042 #include "mysqlupgradebinary.h"
0043 #include "mysqlserverbinary.h"
0044 #include "mysqladminbinary.h"
0045 #include "mysqlinitbinary.h"
0046 #include "databaseserverstarter.h"
0047 
0048 using namespace Digikam;
0049 
0050 QTEST_MAIN(DatabaseMysqlInitTest)
0051 
0052 void DatabaseMysqlInitTest::cleanupTestCase()
0053 {
0054     WSToolUtils::removeTemporaryDir(m_tempPath.toLatin1().data());
0055 }
0056 
0057 void DatabaseMysqlInitTest::initTestCase()
0058 {
0059     m_tempPath  = QString::fromLatin1(QTest::currentAppName());
0060     m_tempPath.replace(QLatin1String("./"), QString());
0061     m_tempDir   = WSToolUtils::makeTemporaryDir(m_tempPath.toLatin1().data());
0062     qCDebug(DIGIKAM_TESTS_LOG) << "Database Dir:" << m_tempDir.path();
0063 
0064     m_filesPath = DTestDataDir::TestData(QString::fromUtf8("core/tests/database/testimages"))
0065                            .root().path() + QLatin1Char('/');
0066     qCDebug(DIGIKAM_TESTS_LOG) << "Test Data Dir:" << m_filesPath;
0067 
0068     KAboutData aboutData(QLatin1String("digikam"),
0069                          QLatin1String("digiKam"), // No need i18n here.
0070                          digiKamVersion());
0071 
0072     QCommandLineParser parser;
0073     KAboutData::setApplicationData(aboutData);
0074     parser.addVersionOption();
0075     parser.addHelpOption();
0076     aboutData.setupCommandLine(&parser);
0077     parser.process(*QCoreApplication::instance());
0078     aboutData.processCommandLine(&parser);
0079 }
0080 
0081 void DatabaseMysqlInitTest::testMysqlInit()
0082 {
0083     qCDebug(DIGIKAM_TESTS_LOG) << "Setup Mysql Database...";
0084 
0085     MysqlUpgradeBinary mysqlUpgradeBin;
0086 
0087     if (!mysqlUpgradeBin.recheckDirectories())
0088     {
0089         QWARN("Not able to found the Mysql Upgrade binary program. Test is aborted...");
0090         return;
0091     }
0092 
0093     MysqlServerBinary  mysqlServerBin;
0094     mysqlServerBin.slotAddPossibleSearchDirectory(QLatin1String("/usr/sbin"));
0095 
0096     if (!mysqlServerBin.recheckDirectories())
0097     {
0098         QWARN("Not able to found the Mysql Server binary program. Test is aborted...");
0099         return;
0100     }
0101 
0102     MysqlAdminBinary   mysqlAdminBin;
0103 
0104     if (!mysqlAdminBin.recheckDirectories())
0105     {
0106         QWARN("Not able to found the Mysql Admin binary program. Test is aborted...");
0107         return;
0108     }
0109 
0110     MysqlInitBinary    mysqlInitBin;
0111 
0112     if (!mysqlInitBin.recheckDirectories())
0113     {
0114         QWARN("Not able to found the Mysql Init binary program. Test is aborted...");
0115         return;
0116     }
0117 
0118     if (!QSqlDatabase::isDriverAvailable(DbEngineParameters::MySQLDatabaseType()))
0119     {
0120         QWARN("Qt MySQL plugin is missing.");
0121         return;
0122     }
0123 
0124     DbEngineParameters params;
0125     QString defaultAkDir                 = DbEngineParameters::serverPrivatePath();
0126     QString miscDir                      = QDir(defaultAkDir).absoluteFilePath(QLatin1String("db_misc"));
0127     params.databaseType                  = DbEngineParameters::MySQLDatabaseType();
0128     params.databaseNameCore              = QLatin1String("digikam");
0129     params.databaseNameThumbnails        = QLatin1String("digikam");
0130     params.databaseNameFace              = QLatin1String("digikam");
0131     params.databaseNameSimilarity        = QLatin1String("digikam");
0132     params.userName                      = QLatin1String("root");
0133     params.password                      = QString();
0134     params.internalServer                = true;
0135     params.internalServerDBPath          = m_tempDir.path();
0136     params.internalServerMysqlUpgradeCmd = mysqlUpgradeBin.path();
0137     params.internalServerMysqlServerCmd  = mysqlServerBin.path();
0138     params.internalServerMysqlAdminCmd   = mysqlAdminBin.path();
0139     params.internalServerMysqlInitCmd    = mysqlInitBin.path();
0140     params.hostName                      = QString();
0141     params.port                          = -1;
0142     params.connectOptions                = QString::fromLatin1("UNIX_SOCKET=%1/mysql.socket").arg(miscDir);
0143 
0144     // ------------------------------------------------------------------------------------
0145 
0146     qCDebug(DIGIKAM_TESTS_LOG) << "Initializing Mysql database...";
0147     QVERIFY2(AlbumManager::instance()->setDatabase(params, false, m_filesPath),
0148              "Cannot initialize Mysql database");
0149 
0150     QTest::qWait(3000);
0151 
0152     qCDebug(DIGIKAM_TESTS_LOG) << "Shutting down Mysql database";
0153     ScanController::instance()->shutDown();
0154     AlbumManager::instance()->cleanUp();
0155 
0156     qCDebug(DIGIKAM_TESTS_LOG) << "Cleaning Mysql database";
0157     CoreDbAccess::cleanUpDatabase();
0158     ThumbsDbAccess::cleanUpDatabase();
0159     FaceDbAccess::cleanUpDatabase();
0160     SimilarityDbAccess::cleanUpDatabase();
0161 
0162     DatabaseServerStarter::instance()->stopServerManagerProcess();
0163 }
0164 
0165 #include "moc_databasemysqlinit_utest.cpp"