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 #include "collectionindexingjob.h" 0009 0010 #include <Akonadi/Collection> 0011 #include <Akonadi/CollectionFetchJob> 0012 #include <Akonadi/ServerManager> 0013 #include <QTest> 0014 #include <akonadi/qtest_akonadi.h> 0015 0016 class TestIndex : public Index 0017 { 0018 public: 0019 using Index::index; // So we don't trigger -Woverloaded-virtual 0020 using Index::move; // So we don't trigger -Woverloaded-virtual 0021 QList<Akonadi::Item::Id> itemsIndexed; 0022 QList<Akonadi::Item::Id> alreadyIndexed; 0023 QList<Akonadi::Item::Id> itemsRemoved; 0024 0025 void commit() override 0026 { 0027 } 0028 0029 bool createIndexers() override 0030 { 0031 return true; 0032 } 0033 0034 void findIndexed(QSet<Akonadi::Item::Id> &indexed, Akonadi::Collection::Id) override 0035 { 0036 indexed = QSet<Akonadi::Item::Id>(alreadyIndexed.begin(), alreadyIndexed.end()); 0037 } 0038 0039 void index(const Akonadi::Item &item) override 0040 { 0041 itemsIndexed << item.id(); 0042 } 0043 0044 qlonglong indexedItems(Akonadi::Collection::Id) override 0045 { 0046 return alreadyIndexed.size(); 0047 } 0048 0049 void move(const Akonadi::Item::List & /* items */, const Akonadi::Collection & /* from */, const Akonadi::Collection & /* to */) override 0050 { 0051 } 0052 0053 void remove(const Akonadi::Collection & /* col */) override 0054 { 0055 } 0056 0057 void remove(const QSet<Akonadi::Item::Id> &ids, const QStringList & /* mimeTypes */) override 0058 { 0059 itemsRemoved += ids.values(); 0060 } 0061 0062 void remove(const Akonadi::Item::List & /* items */) override 0063 { 0064 } 0065 0066 void removeDatabase() override 0067 { 0068 } 0069 0070 bool haveIndexerForMimeTypes(const QStringList &) override 0071 { 0072 return true; 0073 } 0074 }; 0075 0076 class CollectionIndexingJobTest : public QObject 0077 { 0078 Q_OBJECT 0079 0080 private: 0081 Akonadi::Collection itemCollection; 0082 0083 private Q_SLOTS: 0084 0085 void init() 0086 { 0087 AkonadiTest::checkTestIsIsolated(); 0088 AkonadiTest::setAllResourcesOffline(); 0089 Akonadi::AgentInstance agent = Akonadi::AgentManager::self()->instance(QStringLiteral("akonadi_knut_resource_0")); 0090 QVERIFY(agent.isValid()); 0091 agent.setIsOnline(true); 0092 0093 auto fetchJob = new Akonadi::CollectionFetchJob(Akonadi::Collection::root(), Akonadi::CollectionFetchJob::Recursive); 0094 fetchJob->exec(); 0095 const Akonadi::Collection::List lstCols = fetchJob->collections(); 0096 for (const Akonadi::Collection &col : lstCols) { 0097 if (col.name() == QLatin1StringView("foo")) { 0098 itemCollection = col; 0099 } 0100 } 0101 QVERIFY(itemCollection.isValid()); 0102 } 0103 0104 void testFullSync() 0105 { 0106 TestIndex index; 0107 auto job = new CollectionIndexingJob(index, itemCollection, QList<Akonadi::Item::Id>()); 0108 job->setFullSync(true); 0109 AKVERIFYEXEC(job); 0110 QCOMPARE(index.itemsIndexed.size(), 3); 0111 } 0112 0113 void testNoFullSync() 0114 { 0115 TestIndex index; 0116 auto job = new CollectionIndexingJob(index, itemCollection, QList<Akonadi::Item::Id>()); 0117 job->setFullSync(false); 0118 AKVERIFYEXEC(job); 0119 QCOMPARE(index.itemsIndexed.size(), 0); 0120 } 0121 0122 void testNoFullSyncWithPending() 0123 { 0124 TestIndex index; 0125 auto job = new CollectionIndexingJob(index, itemCollection, QList<Akonadi::Item::Id>() << 1); 0126 job->setFullSync(false); 0127 AKVERIFYEXEC(job); 0128 QCOMPARE(index.itemsIndexed.size(), 1); 0129 } 0130 0131 void testFullSyncButUpToDate() 0132 { 0133 TestIndex index; 0134 index.alreadyIndexed << 1 << 2 << 3; 0135 auto job = new CollectionIndexingJob(index, itemCollection, QList<Akonadi::Item::Id>()); 0136 job->setFullSync(true); 0137 AKVERIFYEXEC(job); 0138 QCOMPARE(index.itemsIndexed.size(), 0); 0139 } 0140 0141 void testFullSyncWithRemove() 0142 { 0143 TestIndex index; 0144 index.alreadyIndexed << 15 << 16 << 17; 0145 auto job = new CollectionIndexingJob(index, itemCollection, QList<Akonadi::Item::Id>()); 0146 job->setFullSync(true); 0147 AKVERIFYEXEC(job); 0148 QCOMPARE(index.itemsIndexed.size(), 3); 0149 QCOMPARE(index.itemsRemoved.size(), 3); 0150 } 0151 }; 0152 0153 QTEST_MAIN(CollectionIndexingJobTest) 0154 0155 #include "collectionindexingjobtest.moc"