Changeset View
Changeset View
Standalone View
Standalone View
autotests/libs/itemsynctest.cpp
Show First 20 Lines • Show All 55 Lines • ▼ Show 20 Line(s) | 52 | { | |||
---|---|---|---|---|---|
56 | fetch->fetchScope().fetchAllAttributes(); | 56 | fetch->fetchScope().fetchAllAttributes(); | ||
57 | fetch->fetchScope().setCacheOnly(true); // resources are switched off anyway | 57 | fetch->fetchScope().setCacheOnly(true); // resources are switched off anyway | ||
58 | if (!fetch->exec()) { | 58 | if (!fetch->exec()) { | ||
59 | []() { QFAIL("Failed to fetch items!"); }(); | 59 | []() { QFAIL("Failed to fetch items!"); }(); | ||
60 | } | 60 | } | ||
61 | return fetch->items(); | 61 | return fetch->items(); | ||
62 | } | 62 | } | ||
63 | 63 | | |||
64 | void createItems(const Collection &col, int itemCount) | 64 | static void createItems(const Collection &col, int itemCount) | ||
65 | { | 65 | { | ||
66 | for (int i = 0; i < itemCount; ++i) { | 66 | for (int i = 0; i < itemCount; ++i) { | ||
67 | Item item(QStringLiteral("application/octet-stream")); | 67 | Item item(QStringLiteral("application/octet-stream")); | ||
68 | item.setRemoteId(QStringLiteral("rid") + QString::number(i)); | 68 | item.setRemoteId(QStringLiteral("rid") + QString::number(i)); | ||
69 | item.setGid(QStringLiteral("gid") + QString::number(i)); | 69 | item.setGid(QStringLiteral("gid") + QString::number(i)); | ||
70 | item.setPayload<QByteArray>("payload1"); | 70 | item.setPayload<QByteArray>("payload1"); | ||
71 | ItemCreateJob *job = new ItemCreateJob(item, col); | 71 | ItemCreateJob *job = new ItemCreateJob(item, col); | ||
72 | AKVERIFYEXEC(job); | 72 | AKVERIFYEXEC(job); | ||
73 | } | 73 | } | ||
74 | } | 74 | } | ||
75 | 75 | | |||
76 | private Q_SLOTS: | 76 | static Item duplicateItem(const Item &item, const Collection &col) | ||
77 | void initTestCase() | | |||
78 | { | 77 | { | ||
79 | AkonadiTest::checkTestIsIsolated(); | 78 | Item duplicate = item; | ||
80 | Control::start(); | 79 | duplicate.setId(-1); | ||
81 | AkonadiTest::setAllResourcesOffline(); | 80 | ItemCreateJob *job = new ItemCreateJob(duplicate, col); | ||
82 | qRegisterMetaType<KJob *>(); | 81 | [job]() { AKVERIFYEXEC(job); }(); | ||
83 | qRegisterMetaType<ItemSync::TransactionMode>(); | 82 | return job->item(); | ||
84 | } | 83 | } | ||
85 | 84 | | |||
86 | static Item modifyItem(Item item) | 85 | static Item modifyItem(Item item) | ||
87 | { | 86 | { | ||
88 | static int counter = 0; | 87 | static int counter = 0; | ||
89 | item.setFlag(QByteArray("\\READ") + QByteArray::number(counter)); | 88 | item.setFlag(QByteArray("\\READ") + QByteArray::number(counter)); | ||
90 | counter++; | 89 | counter++; | ||
91 | return item; | 90 | return item; | ||
92 | } | 91 | } | ||
93 | 92 | | |||
93 | private Q_SLOTS: | ||||
94 | void initTestCase() | ||||
95 | { | ||||
96 | AkonadiTest::checkTestIsIsolated(); | ||||
97 | Control::start(); | ||||
98 | AkonadiTest::setAllResourcesOffline(); | ||||
99 | qRegisterMetaType<KJob *>(); | ||||
100 | qRegisterMetaType<ItemSync::TransactionMode>(); | ||||
101 | } | ||||
102 | | ||||
94 | void testFullSync() | 103 | void testFullSync() | ||
95 | { | 104 | { | ||
96 | const Collection col = Collection(collectionIdFromPath(QStringLiteral("res1/foo"))); | 105 | const Collection col = Collection(collectionIdFromPath(QStringLiteral("res1/foo"))); | ||
97 | QVERIFY(col.isValid()); | 106 | QVERIFY(col.isValid()); | ||
98 | Item::List origItems = fetchItems(col); | 107 | Item::List origItems = fetchItems(col); | ||
99 | 108 | | |||
100 | //Since the item sync affects the knut resource we ensure we actually managed to load all items | 109 | //Since the item sync affects the knut resource we ensure we actually managed to load all items | ||
101 | //This needs to be adjusted should the testdataset change | 110 | //This needs to be adjusted should the testdataset change | ||
▲ Show 20 Lines • Show All 435 Lines • ▼ Show 20 Line(s) | |||||
537 | */ | 546 | */ | ||
538 | void testFailingDueToDuplicateItem() | 547 | void testFailingDueToDuplicateItem() | ||
539 | { | 548 | { | ||
540 | const Collection col = Collection(collectionIdFromPath(QStringLiteral("res1/foo"))); | 549 | const Collection col = Collection(collectionIdFromPath(QStringLiteral("res1/foo"))); | ||
541 | QVERIFY(col.isValid()); | 550 | QVERIFY(col.isValid()); | ||
542 | Item::List origItems = fetchItems(col); | 551 | Item::List origItems = fetchItems(col); | ||
543 | 552 | | |||
544 | //Create a duplicate that will trigger an error during the first batch | 553 | //Create a duplicate that will trigger an error during the first batch | ||
545 | Item duplicate = origItems.first(); | 554 | Item dupe = duplicateItem(origItems.at(0), col); | ||
546 | duplicate.setId(-1); | | |||
547 | { | | |||
548 | ItemCreateJob *job = new ItemCreateJob(duplicate, col); | | |||
549 | AKVERIFYEXEC(job); | | |||
550 | } | | |||
551 | origItems = fetchItems(col); | 555 | origItems = fetchItems(col); | ||
552 | 556 | | |||
553 | ItemSync *syncer = new ItemSync(col); | 557 | ItemSync *syncer = new ItemSync(col); | ||
554 | QSignalSpy transactionSpy(syncer, SIGNAL(transactionCommitted())); | 558 | QSignalSpy transactionSpy(syncer, SIGNAL(transactionCommitted())); | ||
555 | QVERIFY(transactionSpy.isValid()); | 559 | QVERIFY(transactionSpy.isValid()); | ||
556 | QSignalSpy spy(syncer, SIGNAL(result(KJob*))); | 560 | QSignalSpy spy(syncer, SIGNAL(result(KJob*))); | ||
557 | QVERIFY(spy.isValid()); | 561 | QVERIFY(spy.isValid()); | ||
558 | syncer->setStreamingEnabled(true); | 562 | syncer->setStreamingEnabled(true); | ||
Show All 23 Lines | 581 | for (int i = syncer->batchSize(); i < origItems.count(); ++i) { | |||
582 | if (i < origItems.count() - 1) { | 586 | if (i < origItems.count() - 1) { | ||
583 | QTest::qWait(0); // enter the event loop so itemsync actually can do something | 587 | QTest::qWait(0); // enter the event loop so itemsync actually can do something | ||
584 | } | 588 | } | ||
585 | QCOMPARE(spy.count(), 0); | 589 | QCOMPARE(spy.count(), 0); | ||
586 | } | 590 | } | ||
587 | 591 | | |||
588 | syncer->deliveryDone(); | 592 | syncer->deliveryDone(); | ||
589 | QTRY_COMPARE(spy.count(), 1); | 593 | QTRY_COMPARE(spy.count(), 1); | ||
594 | | ||||
595 | // cleanup | ||||
596 | ItemDeleteJob *del = new ItemDeleteJob(dupe, this); | ||||
597 | AKVERIFYEXEC(del); | ||||
598 | } | ||||
599 | | ||||
600 | void testFullSyncFailingDueToDuplicateItem() | ||||
601 | { | ||||
602 | const Collection col = Collection(collectionIdFromPath(QStringLiteral("res1/foo"))); | ||||
603 | QVERIFY(col.isValid()); | ||||
604 | Item::List origItems = fetchItems(col); | ||||
605 | //Create a duplicate that will trigger an error during the first batch | ||||
606 | Item dupe = duplicateItem(origItems.at(0), col); | ||||
607 | origItems = fetchItems(col); | ||||
608 | | ||||
609 | Akonadi::Monitor monitor; | ||||
610 | monitor.setCollectionMonitored(col); | ||||
611 | QSignalSpy deletedSpy(&monitor, SIGNAL(itemRemoved(Akonadi::Item))); | ||||
612 | QVERIFY(deletedSpy.isValid()); | ||||
613 | QSignalSpy addedSpy(&monitor, SIGNAL(itemAdded(Akonadi::Item,Akonadi::Collection))); | ||||
614 | QVERIFY(addedSpy.isValid()); | ||||
615 | QSignalSpy changedSpy(&monitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>))); | ||||
616 | QVERIFY(changedSpy.isValid()); | ||||
617 | | ||||
618 | ItemSync *syncer = new ItemSync(col); | ||||
619 | syncer->setTransactionMode(ItemSync::SingleTransaction); | ||||
620 | QSignalSpy transactionSpy(syncer, SIGNAL(transactionCommitted())); | ||||
621 | QVERIFY(transactionSpy.isValid()); | ||||
622 | syncer->setFullSyncItems(origItems); | ||||
623 | QVERIFY(!syncer->exec()); | ||||
624 | QCOMPARE(transactionSpy.count(), 1); | ||||
625 | | ||||
626 | Item::List resultItems = fetchItems(col); | ||||
627 | QCOMPARE(resultItems.count(), origItems.count()); | ||||
628 | QTest::qWait(100); | ||||
629 | QCOMPARE(deletedSpy.count(), 1); // ## is this correct? | ||||
630 | QCOMPARE(addedSpy.count(), 1); // ## is this correct? | ||||
631 | QCOMPARE(changedSpy.count(), 0); | ||||
632 | | ||||
633 | // cleanup | ||||
634 | ItemDeleteJob *del = new ItemDeleteJob(dupe, this); | ||||
635 | AKVERIFYEXEC(del); | ||||
590 | } | 636 | } | ||
591 | 637 | | |||
592 | void testFullSyncManyItems() | 638 | void testFullSyncManyItems() | ||
593 | { | 639 | { | ||
594 | // Given a collection with 1000 items | 640 | // Given a collection with 1000 items | ||
595 | const Collection col = Collection(collectionIdFromPath(QStringLiteral("res2/foo2"))); | 641 | const Collection col = Collection(collectionIdFromPath(QStringLiteral("res2/foo2"))); | ||
596 | QVERIFY(col.isValid()); | 642 | QVERIFY(col.isValid()); | ||
597 | 643 | | |||
▲ Show 20 Lines • Show All 79 Lines • Show Last 20 Lines |