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

0001 /*
0002     SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "qtest_akonadi.h"
0008 
0009 #include "agentinstance.h"
0010 #include "agentmanager.h"
0011 #include "collection.h"
0012 #include "item.h"
0013 #include "itemsearchjob.h"
0014 #include "searchquery.h"
0015 
0016 Q_DECLARE_METATYPE(QSet<qint64>)
0017 Q_DECLARE_METATYPE(Akonadi::SearchQuery)
0018 
0019 using namespace Akonadi;
0020 class ItemSearchJobTest : public QObject
0021 {
0022     Q_OBJECT
0023 private:
0024     Akonadi::SearchQuery createQuery(const QString &key, const QSet<qint64> &resultSet)
0025     {
0026         Akonadi::SearchQuery query;
0027         for (qint64 id : resultSet) {
0028             query.addTerm(Akonadi::SearchTerm(key, id));
0029         }
0030         return query;
0031     }
0032 
0033 private Q_SLOTS:
0034     void initTestCase()
0035     {
0036         AkonadiTest::checkTestIsIsolated();
0037     }
0038 
0039     void testItemSearch_data()
0040     {
0041         QTest::addColumn<bool>("remoteSearchEnabled");
0042         QTest::addColumn<SearchQuery>("query");
0043         QTest::addColumn<QSet<qint64>>("resultSet");
0044 
0045         {
0046             QSet<qint64> resultSet;
0047             resultSet << 1 << 2 << 3;
0048             QTest::newRow("plugin search") << false << createQuery(QStringLiteral("plugin"), resultSet) << resultSet;
0049         }
0050         {
0051             QSet<qint64> resultSet;
0052             resultSet << 1 << 2 << 3;
0053             QTest::newRow("resource search") << true << createQuery(QStringLiteral("resource"), resultSet) << resultSet;
0054         }
0055         {
0056             QSet<qint64> resultSet;
0057             resultSet << 1 << 2 << 3 << 4;
0058             Akonadi::SearchQuery query;
0059             query.addTerm(Akonadi::SearchTerm(QStringLiteral("plugin"), 1));
0060             query.addTerm(Akonadi::SearchTerm(QStringLiteral("resource"), 2));
0061             query.addTerm(Akonadi::SearchTerm(QStringLiteral("plugin"), 3));
0062             query.addTerm(Akonadi::SearchTerm(QStringLiteral("resource"), 4));
0063             QTest::newRow("mixed search: results are merged") << true << query << resultSet;
0064         }
0065     }
0066 
0067     void testItemSearch()
0068     {
0069         QFETCH(bool, remoteSearchEnabled);
0070         QFETCH(SearchQuery, query);
0071         QFETCH(QSet<qint64>, resultSet);
0072 
0073         ItemSearchJob *itemSearchJob = new ItemSearchJob(query, this);
0074         itemSearchJob->setRemoteSearchEnabled(remoteSearchEnabled);
0075         itemSearchJob->setSearchCollections(Collection::List() << Collection::root());
0076         itemSearchJob->setRecursive(true);
0077         AKVERIFYEXEC(itemSearchJob);
0078         QSet<qint64> actualResultSet;
0079         const Item::List foundItems = itemSearchJob->items();
0080         for (const Item &item : foundItems) {
0081             actualResultSet << item.id();
0082         }
0083         qDebug() << actualResultSet << resultSet;
0084         QCOMPARE(actualResultSet, resultSet);
0085     }
0086 };
0087 
0088 QTEST_AKONADIMAIN(ItemSearchJobTest)
0089 
0090 #include "itemsearchjobtest.moc"