File indexing completed on 2024-11-10 04:40:11
0001 /* 0002 SPDX-FileCopyrightText: 2013 Christian Mollekopf <mollekopf@kolabsys.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "gidtest.h" 0008 0009 #include "agentinstance.h" 0010 #include "agentmanager.h" 0011 #include "collectionfetchjob.h" 0012 #include "control.h" 0013 #include "itemcreatejob.h" 0014 #include "itemdeletejob.h" 0015 #include "itemfetchjob.h" 0016 #include "itemfetchscope.h" 0017 #include "itemmodifyjob.h" 0018 #include "qtest_akonadi.h" 0019 #include "testattribute.h" 0020 0021 using namespace Akonadi; 0022 0023 QTEST_AKONADIMAIN(GidTest) 0024 0025 bool TestSerializer::deserialize(Akonadi::Item &item, const QByteArray &label, QIODevice &data, int version) 0026 { 0027 qDebug() << item.id(); 0028 if (label != Akonadi::Item::FullPayload) { 0029 return false; 0030 } 0031 Q_UNUSED(version) 0032 0033 item.setPayload(data.readAll()); 0034 return true; 0035 } 0036 0037 void TestSerializer::serialize(const Akonadi::Item &item, const QByteArray &label, QIODevice &data, int &version) 0038 { 0039 qDebug(); 0040 Q_ASSERT(label == Akonadi::Item::FullPayload); 0041 Q_UNUSED(label) 0042 Q_UNUSED(version) 0043 data.write(item.payload<QByteArray>()); 0044 } 0045 0046 QString TestSerializer::extractGid(const Akonadi::Item &item) const 0047 { 0048 if (item.gid().isEmpty()) { 0049 return item.url().url(); 0050 } 0051 return item.gid(); 0052 } 0053 0054 void GidTest::initTestCase() 0055 { 0056 AkonadiTest::checkTestIsIsolated(); 0057 Control::start(); 0058 0059 ItemSerializer::overridePluginLookup(new TestSerializer); 0060 } 0061 0062 void GidTest::testSetAndFetch_data() 0063 { 0064 QTest::addColumn<Akonadi::Item::List>("input"); 0065 QTest::addColumn<Akonadi::Item>("toFetch"); 0066 QTest::addColumn<Akonadi::Item::List>("expected"); 0067 0068 Item item1(1); 0069 item1.setGid(QStringLiteral("gid1")); 0070 Item item2(2); 0071 item2.setGid(QStringLiteral("gid2")); 0072 Item toFetch; 0073 toFetch.setGid(QStringLiteral("gid1")); 0074 QTest::newRow("single") << (Item::List() << item1) << toFetch << (Item::List() << item1); 0075 QTest::newRow("multi") << (Item::List() << item1 << item2) << toFetch << (Item::List() << item1); 0076 { 0077 Item item3(3); 0078 item2.setGid(QStringLiteral("gid1")); 0079 QTest::newRow("multi") << (Item::List() << item1 << item2 << item3) << toFetch << (Item::List() << item1 << item3); 0080 } 0081 } 0082 0083 static void fetchAndSetGid(const Item &item) 0084 { 0085 auto prefetchjob = new ItemFetchJob(item); 0086 prefetchjob->fetchScope().fetchFullPayload(); 0087 AKVERIFYEXEC(prefetchjob); 0088 Item fetchedItem = prefetchjob->items()[0]; 0089 0090 // Write the gid to the db 0091 fetchedItem.setGid(item.gid()); 0092 auto store = new ItemModifyJob(fetchedItem); 0093 store->setUpdateGid(true); 0094 AKVERIFYEXEC(store); 0095 } 0096 0097 void GidTest::testSetAndFetch() 0098 { 0099 QFETCH(Item::List, input); 0100 QFETCH(Item, toFetch); 0101 QFETCH(Item::List, expected); 0102 0103 for (const Item &item : std::as_const(input)) { 0104 fetchAndSetGid(item); 0105 } 0106 0107 auto fetch = new ItemFetchJob(toFetch, this); 0108 fetch->fetchScope().setFetchGid(true); 0109 AKVERIFYEXEC(fetch); 0110 Item::List fetched = fetch->items(); 0111 QCOMPARE(fetched.count(), expected.size()); 0112 for (const Item &item : std::as_const(expected)) { 0113 QVERIFY(expected.removeOne(item)); 0114 } 0115 QVERIFY(expected.isEmpty()); 0116 } 0117 0118 void GidTest::testCreate() 0119 { 0120 const int colId = AkonadiTest::collectionIdFromPath(QStringLiteral("res1/foo/bar")); 0121 QVERIFY(colId > -1); 0122 0123 Item item; 0124 item.setMimeType(QStringLiteral("application/octet-stream")); 0125 item.setPayload<QByteArray>(QByteArray("test")); 0126 item.setGid(QStringLiteral("createGid")); 0127 auto createJob = new ItemCreateJob(item, Collection(colId), this); 0128 AKVERIFYEXEC(createJob); 0129 auto fetch = new ItemFetchJob(item, this); 0130 AKVERIFYEXEC(fetch); 0131 Item::List fetched = fetch->items(); 0132 QCOMPARE(fetched.count(), 1); 0133 } 0134 0135 void GidTest::testSetWithIgnorePayload() 0136 { 0137 Item item(5); 0138 auto prefetchjob = new ItemFetchJob(item); 0139 prefetchjob->fetchScope().fetchFullPayload(); 0140 AKVERIFYEXEC(prefetchjob); 0141 Item fetchedItem = prefetchjob->items()[0]; 0142 QVERIFY(fetchedItem.gid().isEmpty()); 0143 0144 // Write the gid to the db 0145 fetchedItem.setGid(QStringLiteral("gid5")); 0146 auto store = new ItemModifyJob(fetchedItem); 0147 store->setIgnorePayload(true); 0148 store->setUpdateGid(true); 0149 AKVERIFYEXEC(store); 0150 Item toFetch; 0151 toFetch.setGid(QStringLiteral("gid5")); 0152 auto fetch = new ItemFetchJob(toFetch, this); 0153 AKVERIFYEXEC(fetch); 0154 Item::List fetched = fetch->items(); 0155 QCOMPARE(fetched.count(), 1); 0156 QCOMPARE(fetched.at(0).id(), Item::Id(5)); 0157 } 0158 0159 void GidTest::testFetchScope() 0160 { 0161 const int colId = AkonadiTest::collectionIdFromPath(QStringLiteral("res1/foo/bar")); 0162 QVERIFY(colId > -1); 0163 0164 Item item; 0165 item.setMimeType(QStringLiteral("application/octet-stream")); 0166 item.setPayload<QByteArray>(QByteArray("test")); 0167 item.setGid(QStringLiteral("createGid2")); 0168 auto createJob = new ItemCreateJob(item, Collection(colId), this); 0169 AKVERIFYEXEC(createJob); 0170 { 0171 auto fetch = new ItemFetchJob(item, this); 0172 AKVERIFYEXEC(fetch); 0173 Item::List fetched = fetch->items(); 0174 QCOMPARE(fetched.count(), 1); 0175 QVERIFY(fetched.at(0).gid().isNull()); 0176 } 0177 { 0178 auto fetch = new ItemFetchJob(item, this); 0179 fetch->fetchScope().setFetchGid(true); 0180 AKVERIFYEXEC(fetch); 0181 Item::List fetched = fetch->items(); 0182 QCOMPARE(fetched.count(), 1); 0183 QVERIFY(!fetched.at(0).gid().isNull()); 0184 } 0185 } 0186 0187 #include "moc_gidtest.cpp"