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

0001 /*
0002     SPDX-FileCopyrightText: 2019 David Faure <faure@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "invalidatecachejob_p.h"
0008 
0009 #include "collectionpathresolver.h"
0010 #include "control.h"
0011 #include "itemfetchjob.h"
0012 #include "itemfetchscope.h"
0013 #include "qtest_akonadi.h"
0014 
0015 using namespace Akonadi;
0016 
0017 class InvalidateCacheJobTest : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 private Q_SLOTS:
0022     void initTestCase();
0023     void shouldClearPayload();
0024 };
0025 
0026 void InvalidateCacheJobTest::initTestCase()
0027 {
0028     AkonadiTest::checkTestIsIsolated();
0029     Control::start();
0030 }
0031 
0032 void InvalidateCacheJobTest::shouldClearPayload()
0033 {
0034     // Find collection by name
0035     Collection col(AkonadiTest::collectionIdFromPath(QStringLiteral("res1/foo")));
0036     const int colId = col.id();
0037     QVERIFY(colId > 0);
0038 
0039     // Find item with remote id "C"
0040     auto listJob = new ItemFetchJob(Collection(colId), this);
0041     AKVERIFYEXEC(listJob);
0042     const Item::List items = listJob->items();
0043     QVERIFY(!items.isEmpty());
0044     auto it = std::find_if(items.cbegin(), items.cend(), [](const Item &item) {
0045         return item.remoteId() == QLatin1Char('C');
0046     });
0047     QVERIFY(it != items.cend());
0048     const Item::Id itemId = it->id();
0049 
0050     // Fetch item, from resource, with payload
0051     auto fetchJob = new ItemFetchJob(Item(itemId), this);
0052     fetchJob->fetchScope().fetchFullPayload();
0053     AKVERIFYEXEC(fetchJob);
0054     QCOMPARE(fetchJob->items().first().payload<QByteArray>(), "testmailbody2");
0055 
0056     auto invCacheJob = new InvalidateCacheJob(Collection(colId), this);
0057     AKVERIFYEXEC(invCacheJob);
0058 
0059     // Fetch item from cache, should have no payload anymore
0060     auto fetchFromCacheJob = new ItemFetchJob(Item(itemId), this);
0061     fetchFromCacheJob->fetchScope().fetchFullPayload();
0062     fetchFromCacheJob->fetchScope().setCacheOnly(true);
0063     AKVERIFYEXEC(fetchFromCacheJob);
0064     QVERIFY(fetchFromCacheJob->items().first().payload<QByteArray>().isEmpty());
0065 
0066     // Fetch item from resource again
0067     auto fetchAgainJob = new ItemFetchJob(Item(itemId), this);
0068     fetchAgainJob->fetchScope().fetchFullPayload();
0069     AKVERIFYEXEC(fetchAgainJob);
0070     QCOMPARE(fetchAgainJob->items().first().payload<QByteArray>(), "testmailbody2");
0071 }
0072 
0073 QTEST_AKONADIMAIN(InvalidateCacheJobTest)
0074 
0075 #include "invalidatecachejobtest.moc"