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

0001 /*
0002     SPDX-FileCopyrightText: 2009 Thomas McGuire <mcguire@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include "autoincrementtest.h"
0007 
0008 #include "agentinstance.h"
0009 #include "agentmanager.h"
0010 #include "collectioncreatejob.h"
0011 #include "collectiondeletejob.h"
0012 #include "control.h"
0013 #include "item.h"
0014 #include "itemcreatejob.h"
0015 #include "itemdeletejob.h"
0016 
0017 #include "qtest_akonadi.h"
0018 
0019 using namespace Akonadi;
0020 
0021 QTEST_AKONADIMAIN(AutoIncrementTest)
0022 
0023 void AutoIncrementTest::initTestCase()
0024 {
0025     AkonadiTest::checkTestIsIsolated();
0026     Control::start();
0027     AkonadiTest::setAllResourcesOffline();
0028 
0029     itemTargetCollection = Collection(AkonadiTest::collectionIdFromPath(QStringLiteral("res2/space folder")));
0030     QVERIFY(itemTargetCollection.isValid());
0031 
0032     collectionTargetCollection = Collection(AkonadiTest::collectionIdFromPath(QStringLiteral("res3")));
0033     QVERIFY(collectionTargetCollection.isValid());
0034 }
0035 
0036 Akonadi::ItemCreateJob *AutoIncrementTest::createItemCreateJob()
0037 {
0038     QByteArray payload("Hello world");
0039     Item item(-1);
0040     item.setMimeType(QStringLiteral("application/octet-stream"));
0041     item.setPayload(payload);
0042     return new ItemCreateJob(item, itemTargetCollection);
0043 }
0044 
0045 Akonadi::CollectionCreateJob *AutoIncrementTest::createCollectionCreateJob(int number)
0046 {
0047     Collection collection;
0048     collection.setParentCollection(collectionTargetCollection);
0049     collection.setName(QStringLiteral("testCollection") + QString::number(number));
0050     return new CollectionCreateJob(collection);
0051 }
0052 
0053 void AutoIncrementTest::testItemAutoIncrement()
0054 {
0055     QList<Item> itemsToDelete;
0056     Item::Id lastId = -1;
0057 
0058     // Create 20 test items
0059     for (int i = 0; i < 20; i++) {
0060         ItemCreateJob *job = createItemCreateJob();
0061         AKVERIFYEXEC(job);
0062         Item newItem = job->item();
0063         QVERIFY(newItem.id() > lastId);
0064         lastId = newItem.id();
0065         itemsToDelete.append(newItem);
0066     }
0067 
0068     // Delete the 20 items
0069     for (const Item &item : std::as_const(itemsToDelete)) {
0070         auto job = new ItemDeleteJob(item);
0071         AKVERIFYEXEC(job);
0072     }
0073 
0074     // Restart the server, then test item creation again. The new id of the item
0075     // should be higher than all ids before.
0076     AkonadiTest::restartAkonadiServer();
0077     ItemCreateJob *job = createItemCreateJob();
0078     AKVERIFYEXEC(job);
0079     Item newItem = job->item();
0080 
0081     QVERIFY(newItem.id() > lastId);
0082 }
0083 
0084 void AutoIncrementTest::testCollectionAutoIncrement()
0085 {
0086     Collection::List collectionsToDelete;
0087     Collection::Id lastId = -1;
0088 
0089     // Create 20 test collections
0090     for (int i = 0; i < 20; i++) {
0091         CollectionCreateJob *job = createCollectionCreateJob(i);
0092         AKVERIFYEXEC(job);
0093         Collection newCollection = job->collection();
0094         QVERIFY(newCollection.id() > lastId);
0095         lastId = newCollection.id();
0096         collectionsToDelete.append(newCollection);
0097     }
0098 
0099     // Delete the 20 collections
0100     for (const Collection &collection : std::as_const(collectionsToDelete)) {
0101         auto job = new CollectionDeleteJob(collection);
0102         AKVERIFYEXEC(job);
0103     }
0104 
0105     // Restart the server, then test collection creation again. The new id of the collection
0106     // should be higher than all ids before.
0107     AkonadiTest::restartAkonadiServer();
0108 
0109     CollectionCreateJob *job = createCollectionCreateJob(0);
0110     AKVERIFYEXEC(job);
0111     Collection newCollection = job->collection();
0112 
0113     QVERIFY(newCollection.id() > lastId);
0114 }
0115 
0116 #include "moc_autoincrementtest.cpp"