File indexing completed on 2024-12-22 04:52:56

0001 /*
0002  * This file is part of the KDE Akonadi Search Project
0003  * SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  *
0007  */
0008 
0009 #include <Akonadi/Collection>
0010 #include <QDir>
0011 #include <QTest>
0012 
0013 #include "../lib/collectionquery.h"
0014 #include "../lib/resultiterator.h"
0015 #include "index.h"
0016 #include "query.h"
0017 #include <QDebug>
0018 
0019 Q_DECLARE_METATYPE(QSet<qint64>)
0020 Q_DECLARE_METATYPE(QList<qint64>)
0021 
0022 class CollectionQueryTest : public QObject
0023 {
0024     Q_OBJECT
0025 private:
0026     QString collectionsDir;
0027 
0028     bool removeDir(const QString &dirName)
0029     {
0030         bool result = true;
0031         QDir dir(dirName);
0032 
0033         if (dir.exists(dirName)) {
0034             const QFileInfoList infoDirs = dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst);
0035             for (const QFileInfo &info : infoDirs) {
0036                 if (info.isDir()) {
0037                     result = removeDir(info.absoluteFilePath());
0038                 } else {
0039                     result = QFile::remove(info.absoluteFilePath());
0040                 }
0041 
0042                 if (!result) {
0043                     return result;
0044                 }
0045             }
0046             result = dir.rmdir(dirName);
0047         }
0048         return result;
0049     }
0050 
0051     QString dbPrefix;
0052     Index index;
0053 
0054 private Q_SLOTS:
0055     void init()
0056     {
0057         dbPrefix = QDir::tempPath() + QLatin1StringView("/collectiontest");
0058         index.setOverrideDbPrefixPath(dbPrefix);
0059         index.createIndexers();
0060     }
0061 
0062     void cleanup()
0063     {
0064         removeDir(dbPrefix);
0065     }
0066 
0067     void searchByName()
0068     {
0069         Akonadi::Collection col1(3);
0070         col1.setName(QStringLiteral("col3"));
0071         index.index(col1);
0072 
0073         Akonadi::Collection col2(4);
0074         col2.setName(QStringLiteral("col4"));
0075         col2.setParentCollection(col1);
0076         index.index(col2);
0077 
0078         Akonadi::Search::PIM::CollectionQuery query;
0079         query.setDatabaseDir(dbPrefix + QStringLiteral("/collections/"));
0080         query.nameMatches(col1.name());
0081         Akonadi::Search::PIM::ResultIterator it = query.exec();
0082         QList<qint64> results;
0083         while (it.next()) {
0084             // qDebug() << "result " << it.id();
0085             results << it.id();
0086         }
0087         QCOMPARE(results.size(), 1);
0088         QCOMPARE(results.at(0), col1.id());
0089     }
0090 
0091     void searchHierarchy()
0092     {
0093         Akonadi::Collection col1(1);
0094         col1.setName(QStringLiteral("col1"));
0095         index.index(col1);
0096 
0097         Akonadi::Collection col2(2);
0098         col2.setName(QStringLiteral("col2"));
0099         col2.setParentCollection(col1);
0100         index.index(col2);
0101 
0102         {
0103             Akonadi::Search::PIM::CollectionQuery query;
0104             query.setDatabaseDir(dbPrefix + QLatin1StringView("/collections/"));
0105             query.pathMatches(col1.name());
0106             Akonadi::Search::PIM::ResultIterator it = query.exec();
0107             QList<qint64> results;
0108             while (it.next()) {
0109                 // qDebug() << "result " << it.id();
0110                 results << it.id();
0111             }
0112             std::sort(results.begin(), results.end());
0113             QCOMPARE(results.size(), 2);
0114             QCOMPARE(results.at(0), col1.id());
0115             QCOMPARE(results.at(1), col2.id());
0116         }
0117         {
0118             Akonadi::Search::PIM::CollectionQuery query;
0119             query.setDatabaseDir(dbPrefix + QLatin1StringView("/collections/"));
0120             query.pathMatches(QStringLiteral("/col1/col2"));
0121             Akonadi::Search::PIM::ResultIterator it = query.exec();
0122             QList<qint64> results;
0123             while (it.next()) {
0124                 // qDebug() << "result " << it.id();
0125                 results << it.id();
0126             }
0127             std::sort(results.begin(), results.end());
0128             QCOMPARE(results.size(), 1);
0129             QCOMPARE(results.at(0), col2.id());
0130         }
0131     }
0132 
0133     QList<qint64> getResults(Akonadi::Search::PIM::ResultIterator it)
0134     {
0135         QList<qint64> results;
0136         while (it.next()) {
0137             // qDebug() << "result " << it.id();
0138             results << it.id();
0139         }
0140         return results;
0141     }
0142 
0143     void collectionChanged()
0144     {
0145         Akonadi::Collection col1(1);
0146         col1.setName(QStringLiteral("col1"));
0147         index.index(col1);
0148 
0149         Akonadi::Collection col2(2);
0150         col2.setName(QStringLiteral("col2"));
0151         col2.setParentCollection(col1);
0152         index.index(col2);
0153 
0154         Akonadi::Collection col3(3);
0155         col3.setName(QStringLiteral("col3"));
0156         col3.setParentCollection(col2);
0157         index.index(col3);
0158 
0159         col1.setName(QStringLiteral("colX"));
0160         index.change(col1);
0161         col2.setParentCollection(col1);
0162         index.change(col2);
0163         col3.setParentCollection(col2);
0164         index.change(col3);
0165 
0166         // Old name gone
0167         {
0168             Akonadi::Search::PIM::CollectionQuery query;
0169             query.setDatabaseDir(dbPrefix + QLatin1StringView("/collections/"));
0170             query.nameMatches(QStringLiteral("col1"));
0171             QList<qint64> results = getResults(query.exec());
0172             QCOMPARE(results.size(), 0);
0173         }
0174         // New name
0175         {
0176             Akonadi::Search::PIM::CollectionQuery query;
0177             query.setDatabaseDir(dbPrefix + QLatin1StringView("/collections/"));
0178             query.nameMatches(QStringLiteral("colX"));
0179             QList<qint64> results = getResults(query.exec());
0180             QCOMPARE(results.size(), 1);
0181             QCOMPARE(results.at(0), col1.id());
0182         }
0183         // Old path gone
0184         {
0185             Akonadi::Search::PIM::CollectionQuery query;
0186             query.setDatabaseDir(dbPrefix + QLatin1StringView("/collections/"));
0187             query.pathMatches(QStringLiteral("/col1/col2"));
0188             QList<qint64> results = getResults(query.exec());
0189             QCOMPARE(results.size(), 0);
0190         }
0191         // New paths
0192         {
0193             Akonadi::Search::PIM::CollectionQuery query;
0194             query.setDatabaseDir(dbPrefix + QLatin1StringView("/collections/"));
0195             query.pathMatches(QStringLiteral("/colX/col2"));
0196             QList<qint64> results = getResults(query.exec());
0197             QCOMPARE(results.size(), 2);
0198             QCOMPARE(results.at(0), col2.id());
0199             QCOMPARE(results.at(1), col3.id());
0200         }
0201         {
0202             Akonadi::Search::PIM::CollectionQuery query;
0203             query.setDatabaseDir(dbPrefix + QLatin1StringView("/collections/"));
0204             query.pathMatches(QStringLiteral("/colX/col2/col3"));
0205             QList<qint64> results = getResults(query.exec());
0206             QCOMPARE(results.size(), 1);
0207             QCOMPARE(results.at(0), col3.id());
0208         }
0209     }
0210 };
0211 
0212 QTEST_GUILESS_MAIN(CollectionQueryTest)
0213 
0214 #include "collectionquerytest.moc"