File indexing completed on 2024-11-10 04:40:18
0001 /* 0002 * SPDX-FileCopyrightText: 2016 Daniel Vrátil <dvratil@kde.org> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-or-later 0005 * 0006 */ 0007 0008 #include <QObject> 0009 0010 #include "aktest.h" 0011 #include "dbinitializer.h" 0012 #include "fakeakonadiserver.h" 0013 #include "storage/collectionstatistics.h" 0014 0015 using namespace Akonadi::Server; 0016 0017 Q_DECLARE_METATYPE(Akonadi::Server::Collection) 0018 0019 class IntrospectableCollectionStatistics : public CollectionStatistics 0020 { 0021 public: 0022 explicit IntrospectableCollectionStatistics(bool prefetch) 0023 : CollectionStatistics(prefetch) 0024 , mCalculationsCount(0) 0025 { 0026 } 0027 ~IntrospectableCollectionStatistics() override 0028 { 0029 } 0030 0031 int calculationsCount() const 0032 { 0033 return mCalculationsCount; 0034 } 0035 0036 protected: 0037 Statistics calculateCollectionStatistics(const Collection &col) override 0038 { 0039 ++mCalculationsCount; 0040 return CollectionStatistics::calculateCollectionStatistics(col); 0041 } 0042 0043 private: 0044 int mCalculationsCount; 0045 }; 0046 0047 class CollectionStatisticsTest : public QObject 0048 { 0049 Q_OBJECT 0050 0051 FakeAkonadiServer mAkonadi; 0052 std::unique_ptr<DbInitializer> dbInitializer; 0053 0054 public: 0055 CollectionStatisticsTest() 0056 { 0057 qRegisterMetaType<Collection>(); 0058 0059 mAkonadi.setPopulateDb(false); 0060 mAkonadi.init(); 0061 0062 dbInitializer = std::make_unique<DbInitializer>(); 0063 } 0064 0065 private Q_SLOTS: 0066 void testPrefetch_data() 0067 { 0068 dbInitializer->createResource("testresource"); 0069 auto col1 = dbInitializer->createCollection("col1"); 0070 dbInitializer->createItem("item1", col1); 0071 dbInitializer->createItem("item2", col1); 0072 auto col2 = dbInitializer->createCollection("col2"); 0073 // empty 0074 auto col3 = dbInitializer->createCollection("col3"); 0075 dbInitializer->createItem("item3", col3); 0076 0077 QTest::addColumn<Collection>("collection"); 0078 QTest::addColumn<int>("calculationsCount"); 0079 QTest::addColumn<qint64>("count"); 0080 QTest::addColumn<qint64>("read"); 0081 QTest::addColumn<qint64>("size"); 0082 0083 QTest::newRow("col1") << col1 << 0 << 2LL << 0LL << 0LL; 0084 QTest::newRow("col2") << col2 << 0 << 0LL << 0LL << 0LL; 0085 QTest::newRow("col3") << col3 << 0 << 1LL << 0LL << 0LL; 0086 } 0087 0088 void testPrefetch() 0089 { 0090 QFETCH(Collection, collection); 0091 QFETCH(int, calculationsCount); 0092 QFETCH(qint64, count); 0093 QFETCH(qint64, read); 0094 QFETCH(qint64, size); 0095 0096 IntrospectableCollectionStatistics cs(true); 0097 auto stats = cs.statistics(collection); 0098 QCOMPARE(cs.calculationsCount(), calculationsCount); 0099 QCOMPARE(stats.count, count); 0100 QCOMPARE(stats.read, read); 0101 QCOMPARE(stats.size, size); 0102 } 0103 0104 void testCalculateStats() 0105 { 0106 dbInitializer->cleanup(); 0107 dbInitializer->createResource("testresource"); 0108 auto col = dbInitializer->createCollection("col1"); 0109 dbInitializer->createItem("item1", col); 0110 dbInitializer->createItem("item2", col); 0111 dbInitializer->createItem("item3", col); 0112 0113 IntrospectableCollectionStatistics cs(false); 0114 auto stats = cs.statistics(col); 0115 QCOMPARE(cs.calculationsCount(), 1); 0116 QCOMPARE(stats.count, 3); 0117 QCOMPARE(stats.read, 0); 0118 QCOMPARE(stats.size, 0); 0119 } 0120 0121 void testSeenChanged() 0122 { 0123 dbInitializer->cleanup(); 0124 dbInitializer->createResource("testresource"); 0125 auto col = dbInitializer->createCollection("col1"); 0126 dbInitializer->createItem("item1", col); 0127 dbInitializer->createItem("item2", col); 0128 dbInitializer->createItem("item3", col); 0129 0130 IntrospectableCollectionStatistics cs(false); 0131 auto stats = cs.statistics(col); 0132 QCOMPARE(cs.calculationsCount(), 1); 0133 QCOMPARE(stats.count, 3); 0134 QCOMPARE(stats.read, 0); 0135 QCOMPARE(stats.size, 0); 0136 0137 cs.itemsSeenChanged(col, 2); 0138 stats = cs.statistics(col); 0139 QCOMPARE(cs.calculationsCount(), 1); 0140 QCOMPARE(stats.count, 3); 0141 QCOMPARE(stats.read, 2); 0142 QCOMPARE(stats.size, 0); 0143 0144 cs.itemsSeenChanged(col, -1); 0145 stats = cs.statistics(col); 0146 QCOMPARE(cs.calculationsCount(), 1); 0147 QCOMPARE(stats.count, 3); 0148 QCOMPARE(stats.read, 1); 0149 QCOMPARE(stats.size, 0); 0150 } 0151 0152 void testItemAdded() 0153 { 0154 dbInitializer->cleanup(); 0155 dbInitializer->createResource("testresource"); 0156 auto col = dbInitializer->createCollection("col1"); 0157 dbInitializer->createItem("item1", col); 0158 0159 IntrospectableCollectionStatistics cs(false); 0160 auto stats = cs.statistics(col); 0161 QCOMPARE(cs.calculationsCount(), 1); 0162 QCOMPARE(stats.count, 1); 0163 QCOMPARE(stats.read, 0); 0164 QCOMPARE(stats.size, 0); 0165 0166 cs.itemAdded(col, 5, true); 0167 stats = cs.statistics(col); 0168 QCOMPARE(cs.calculationsCount(), 1); 0169 QCOMPARE(stats.count, 2); 0170 QCOMPARE(stats.read, 1); 0171 QCOMPARE(stats.size, 5); 0172 0173 cs.itemAdded(col, 3, false); 0174 stats = cs.statistics(col); 0175 QCOMPARE(cs.calculationsCount(), 1); 0176 QCOMPARE(stats.count, 3); 0177 QCOMPARE(stats.read, 1); 0178 QCOMPARE(stats.size, 8); 0179 } 0180 }; 0181 0182 AKTEST_MAIN(CollectionStatisticsTest) 0183 0184 #include "collectionstatisticstest.moc"