diff --git a/autotests/libs/CMakeLists.txt b/autotests/libs/CMakeLists.txt --- a/autotests/libs/CMakeLists.txt +++ b/autotests/libs/CMakeLists.txt @@ -145,6 +145,7 @@ add_akonadi_isolated_test(SOURCE transactiontest.cpp) add_akonadi_isolated_test(SOURCE itemcopytest.cpp) add_akonadi_isolated_test(SOURCE itemmovetest.cpp) +add_akonadi_isolated_test(SOURCE invalidatecachejobtest.cpp) add_akonadi_isolated_test(SOURCE collectioncreatetest.cpp) add_akonadi_isolated_test(SOURCE collectioncopytest.cpp) add_akonadi_isolated_test(SOURCE collectionmovetest.cpp) diff --git a/autotests/libs/invalidatecachejobtest.cpp b/autotests/libs/invalidatecachejobtest.cpp new file mode 100644 --- /dev/null +++ b/autotests/libs/invalidatecachejobtest.cpp @@ -0,0 +1,89 @@ +/* + Copyright (c) 2019 David Faure + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include "invalidatecachejob_p.h" + +#include +#include +#include +#include +#include "test_utils.h" +#include "control.h" + +using namespace Akonadi; + +class InvalidateCacheJobTest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void initTestCase(); + void shouldClearPayload(); +}; + + +void InvalidateCacheJobTest::initTestCase() +{ + AkonadiTest::checkTestIsIsolated(); + Control::start(); +} + +void InvalidateCacheJobTest::shouldClearPayload() +{ + // Find collection by name + Collection col(collectionIdFromPath(QStringLiteral("res1/foo"))); + const int colId = col.id(); + QVERIFY(colId > 0); + + // Find item with remote id "C" + auto *listJob = new ItemFetchJob(Collection(colId), this); + AKVERIFYEXEC(listJob); + const Item::List items = listJob->items(); + QVERIFY(!items.isEmpty()); + auto it = std::find_if(items.cbegin(), items.cend(), [](const Item &item) { return item.remoteId() == QLatin1String("C"); }); + QVERIFY(it != items.cend()); + const Item::Id itemId = it->id(); + + // Fetch item, from resource, with payload + auto *fetchJob = new ItemFetchJob(Item(itemId), this); + fetchJob->fetchScope().fetchFullPayload(); + AKVERIFYEXEC(fetchJob); + QCOMPARE(fetchJob->items().first().payloadData(), "testmailbody2"); + + // Invalidate cache + auto *invCacheJob = new InvalidateCacheJob(Collection(colId), this); + AKVERIFYEXEC(invCacheJob); + + // Fetch item from cache, should have no payload anymore + auto *fetchFromCacheJob = new ItemFetchJob(Item(itemId), this); + fetchFromCacheJob->fetchScope().fetchFullPayload(); + fetchFromCacheJob->fetchScope().setCacheOnly(true); + AKVERIFYEXEC(fetchFromCacheJob); + QVERIFY(fetchFromCacheJob->items().first().payloadData().isEmpty()); + + // Fetch item from resource again + auto *fetchAgainJob = new ItemFetchJob(Item(itemId), this); + fetchAgainJob->fetchScope().fetchFullPayload(); + AKVERIFYEXEC(fetchAgainJob); + QCOMPARE(fetchAgainJob->items().first().payloadData(), "testmailbody2"); +} + +QTEST_AKONADIMAIN(InvalidateCacheJobTest) + +#include "invalidatecachejobtest.moc"