File indexing completed on 2024-11-10 04:40:10
0001 /* 0002 SPDX-FileCopyrightText: 2009 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "entitycache_p.h" 0008 0009 #include "qtest_akonadi.h" 0010 #include <QSignalSpy> 0011 0012 using namespace Akonadi; 0013 0014 class EntityCacheTest : public QObject 0015 { 0016 Q_OBJECT 0017 private: 0018 template<typename T, typename FetchJob, typename FetchScope> 0019 void testCache() 0020 { 0021 EntityCache<T, FetchJob, FetchScope> cache(2); 0022 QSignalSpy spy(&cache, SIGNAL(dataAvailable())); 0023 QVERIFY(spy.isValid()); 0024 0025 QVERIFY(!cache.isCached(1)); 0026 QVERIFY(!cache.isRequested(1)); 0027 QVERIFY(!cache.retrieve(1).isValid()); 0028 0029 FetchScope scope; 0030 scope.setAncestorRetrieval(FetchScope::All); 0031 0032 cache.request(1, scope); 0033 QVERIFY(!cache.isCached(1)); 0034 QVERIFY(cache.isRequested(1)); 0035 QVERIFY(!cache.retrieve(1).isValid()); 0036 0037 QTRY_COMPARE(spy.count(), 1); 0038 QVERIFY(cache.isCached(1)); 0039 QVERIFY(cache.isRequested(1)); 0040 const T e1 = cache.retrieve(1); 0041 QCOMPARE(e1.id(), 1LL); 0042 QVERIFY(e1.parentCollection().isValid()); 0043 QVERIFY(!e1.parentCollection().remoteId().isEmpty() || e1.parentCollection() == Collection::root()); 0044 0045 spy.clear(); 0046 cache.request(2, FetchScope()); 0047 cache.request(3, FetchScope()); 0048 0049 QVERIFY(!cache.isCached(1)); 0050 QVERIFY(!cache.isRequested(1)); 0051 QVERIFY(cache.isRequested(2)); 0052 QVERIFY(cache.isRequested(3)); 0053 0054 cache.invalidate(2); 0055 0056 QTRY_COMPARE(spy.count(), 2); 0057 QVERIFY(cache.isCached(2)); 0058 QVERIFY(cache.isCached(3)); 0059 0060 const T e2 = cache.retrieve(2); 0061 const T e3a = cache.retrieve(3); 0062 QCOMPARE(e3a.id(), 3LL); 0063 QVERIFY(!e2.isValid()); 0064 0065 cache.invalidate(3); 0066 const T e3b = cache.retrieve(3); 0067 QVERIFY(!e3b.isValid()); 0068 0069 spy.clear(); 0070 // updating a cached entry removes it 0071 cache.update(3, FetchScope()); 0072 cache.update(3, FetchScope()); 0073 QVERIFY(!cache.isCached(3)); 0074 QVERIFY(!cache.isRequested(3)); 0075 QVERIFY(!cache.retrieve(3).isValid()); 0076 0077 // updating a pending entry re-fetches 0078 cache.request(3, FetchScope()); 0079 cache.update(3, FetchScope()); 0080 QVERIFY(!cache.isCached(3)); 0081 QVERIFY(cache.isRequested(3)); 0082 cache.update(3, FetchScope()); 0083 QVERIFY(!cache.isCached(3)); 0084 QVERIFY(cache.isRequested(3)); 0085 0086 QTRY_COMPARE(spy.count(), 3); 0087 QVERIFY(cache.isCached(3)); 0088 QVERIFY(cache.retrieve(3).isValid()); 0089 } 0090 0091 private Q_SLOTS: 0092 void initTestCase() 0093 { 0094 AkonadiTest::checkTestIsIsolated(); 0095 } 0096 void testCacheGeneric_data() 0097 { 0098 QTest::addColumn<bool>("collection"); 0099 QTest::newRow("collection") << true; 0100 QTest::newRow("item") << false; 0101 } 0102 0103 void testCacheGeneric() 0104 { 0105 QFETCH(bool, collection); 0106 if (collection) { 0107 testCache<Collection, CollectionFetchJob, CollectionFetchScope>(); 0108 } else { 0109 testCache<Item, ItemFetchJob, ItemFetchScope>(); 0110 } 0111 } 0112 0113 void testListCacheGeneric_data() 0114 { 0115 QTest::addColumn<bool>("collection"); 0116 QTest::newRow("collection") << true; 0117 QTest::newRow("item") << false; 0118 } 0119 0120 void testItemCache() 0121 { 0122 ItemCache cache(1); 0123 QSignalSpy spy(&cache, &EntityCacheBase::dataAvailable); 0124 QVERIFY(spy.isValid()); 0125 0126 ItemFetchScope scope; 0127 scope.fetchFullPayload(true); 0128 cache.request(1, scope); 0129 0130 QTRY_COMPARE(spy.count(), 1); 0131 QVERIFY(cache.isCached(1)); 0132 QVERIFY(cache.isRequested(1)); 0133 const Item item = cache.retrieve(1); 0134 QCOMPARE(item.id(), 1LL); 0135 QVERIFY(item.hasPayload<QByteArray>()); 0136 } 0137 0138 void testListCache_ensureCached() 0139 { 0140 ItemFetchScope scope; 0141 0142 EntityListCache<Item, ItemFetchJob, ItemFetchScope> cache(3); 0143 QSignalSpy spy(&cache, &EntityCacheBase::dataAvailable); 0144 QVERIFY(spy.isValid()); 0145 0146 cache.request(QList<Item::Id>() << 1 << 2 << 3, scope); 0147 QTRY_COMPARE(spy.count(), 1); 0148 QVERIFY(cache.isCached(QList<Item::Id>() << 1 << 2 << 3)); 0149 0150 cache.ensureCached(QList<Item::Id>() << 1 << 2 << 3 << 4, scope); 0151 QTRY_COMPARE(spy.count(), 2); 0152 QVERIFY(cache.isCached(QList<Item::Id>() << 1 << 2 << 3 << 4)); 0153 } 0154 }; 0155 0156 QTEST_AKONADIMAIN(EntityCacheTest) 0157 0158 #include "entitycachetest.moc"