collectionindexingjobtest.cpp 5.21 KB
Newer Older
1
/*
2
 * This file is part of the KDE Akonadi Search Project
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 * Copyright (C) 2014  Christian Mollekopf <mollekopf@kolabsys.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3, or any
 * later version accepted by the membership of KDE e.V. (or its
 * successor approved by the membership of KDE e.V.), which shall
 * act as a proxy defined in Section 6 of version 3 of the license.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library.  If not, see <https://www.gnu.org/licenses/>.
20
21
22
23
24
 *
 */
#include <collectionindexingjob.h>

#include <QTest>
25
26
27
#include <AkonadiCore/Collection>
#include <AkonadiCore/ServerManager>
#include <AkonadiCore/CollectionFetchJob>
28
#include <AkonadiCore/qtest_akonadi.h>
29

Laurent Montel's avatar
Laurent Montel committed
30
31
class TestIndex : public Index
{
32
public:
33
34
    using Index::move; // So we don't trigger -Woverloaded-virtual
    using Index::index; // So we don't trigger -Woverloaded-virtual
35
36
37
38
    QList<Akonadi::Item::Id> itemsIndexed;
    QList<Akonadi::Item::Id> alreadyIndexed;
    QList<Akonadi::Item::Id> itemsRemoved;

Laurent Montel's avatar
Laurent Montel committed
39
40
41
42
43
44
    void commit() override
    {
    }

    bool createIndexers() override
    {
Laurent Montel's avatar
Laurent Montel committed
45
        return true;
Laurent Montel's avatar
Laurent Montel committed
46
    }
Laurent Montel's avatar
Laurent Montel committed
47
48
49

    void findIndexed(QSet< Akonadi::Item::Id> &indexed, Akonadi::Collection::Id) override
    {
Laurent Montel's avatar
Laurent Montel committed
50
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
Laurent Montel's avatar
Laurent Montel committed
51
        indexed = alreadyIndexed.toSet();
Laurent Montel's avatar
Laurent Montel committed
52
53
54
#else
        indexed = QSet< Akonadi::Item::Id>(alreadyIndexed.begin(), alreadyIndexed.end());
#endif
Laurent Montel's avatar
Laurent Montel committed
55
    }
Laurent Montel's avatar
Laurent Montel committed
56
57
58

    void index(const Akonadi::Item &item) override
    {
Laurent Montel's avatar
Laurent Montel committed
59
        itemsIndexed << item.id();
Laurent Montel's avatar
Laurent Montel committed
60
    }
Laurent Montel's avatar
Laurent Montel committed
61
62
63

    qlonglong indexedItems(Akonadi::Collection::Id) override
    {
Laurent Montel's avatar
Laurent Montel committed
64
        return alreadyIndexed.size();
Laurent Montel's avatar
Laurent Montel committed
65
    }
Laurent Montel's avatar
Laurent Montel committed
66
67
68
69
70
71
72
73
74
75
76

    void move(const Akonadi::Item::List & /* items */, const Akonadi::Collection & /* from */, const Akonadi::Collection & /* to */) override
    {
    }

    void remove(const Akonadi::Collection & /* col */) override
    {
    }

    void remove(const QSet<Akonadi::Item::Id> &ids, const QStringList & /* mimeTypes */) override
    {
77
        itemsRemoved += ids.values();
Laurent Montel's avatar
Laurent Montel committed
78
    }
Laurent Montel's avatar
Laurent Montel committed
79
80
81
82
83
84
85
86
87
88
89

    void remove(const Akonadi::Item::List & /* items */) override
    {
    }

    void removeDatabase() override
    {
    }

    bool haveIndexerForMimeTypes(const QStringList &) override
    {
Laurent Montel's avatar
Laurent Montel committed
90
        return true;
Laurent Montel's avatar
Laurent Montel committed
91
    }
92
93
94
95
96
97
98
99
100
101
102
};

class CollectionIndexingJobTest : public QObject
{
    Q_OBJECT

private:
    Akonadi::Collection itemCollection;

private Q_SLOTS:

Laurent Montel's avatar
Laurent Montel committed
103
104
    void init()
    {
105
106
        AkonadiTest::checkTestIsIsolated();
        AkonadiTest::setAllResourcesOffline();
Laurent Montel's avatar
Laurent Montel committed
107
        Akonadi::AgentInstance agent = Akonadi::AgentManager::self()->instance(QStringLiteral("akonadi_knut_resource_0"));
108
109
110
111
112
        QVERIFY(agent.isValid());
        agent.setIsOnline(true);

        Akonadi::CollectionFetchJob *fetchJob = new Akonadi::CollectionFetchJob(Akonadi::Collection::root(), Akonadi::CollectionFetchJob::Recursive);
        fetchJob->exec();
113
114
        const Akonadi::Collection::List lstCols = fetchJob->collections();
        for (const Akonadi::Collection &col : lstCols) {
115
116
117
118
119
120
121
122
123
124
            if (col.name() == QLatin1String("foo")) {
                itemCollection = col;
            }
        }
        QVERIFY(itemCollection.isValid());
    }

    void testFullSync()
    {
        TestIndex index;
125
        CollectionIndexingJob *job = new CollectionIndexingJob(index, itemCollection, QList<Akonadi::Item::Id>());
126
127
128
129
130
131
132
133
        job->setFullSync(true);
        AKVERIFYEXEC(job);
        QCOMPARE(index.itemsIndexed.size(), 3);
    }

    void testNoFullSync()
    {
        TestIndex index;
134
        CollectionIndexingJob *job = new CollectionIndexingJob(index, itemCollection, QList<Akonadi::Item::Id>());
135
136
137
138
139
140
141
142
        job->setFullSync(false);
        AKVERIFYEXEC(job);
        QCOMPARE(index.itemsIndexed.size(), 0);
    }

    void testNoFullSyncWithPending()
    {
        TestIndex index;
143
        CollectionIndexingJob *job = new CollectionIndexingJob(index, itemCollection, QList<Akonadi::Item::Id>() << 1);
144
145
146
147
148
149
150
151
152
        job->setFullSync(false);
        AKVERIFYEXEC(job);
        QCOMPARE(index.itemsIndexed.size(), 1);
    }

    void testFullSyncButUpToDate()
    {
        TestIndex index;
        index.alreadyIndexed << 1 << 2 << 3;
153
        CollectionIndexingJob *job = new CollectionIndexingJob(index, itemCollection, QList<Akonadi::Item::Id>());
154
155
156
157
158
159
160
161
162
        job->setFullSync(true);
        AKVERIFYEXEC(job);
        QCOMPARE(index.itemsIndexed.size(), 0);
    }

    void testFullSyncWithRemove()
    {
        TestIndex index;
        index.alreadyIndexed << 15 << 16 << 17;
163
        CollectionIndexingJob *job = new CollectionIndexingJob(index, itemCollection, QList<Akonadi::Item::Id>());
164
165
166
167
168
169
170
171
172
173
        job->setFullSync(true);
        AKVERIFYEXEC(job);
        QCOMPARE(index.itemsIndexed.size(), 3);
        QCOMPARE(index.itemsRemoved.size(), 3);
    }
};

QTEST_MAIN(CollectionIndexingJobTest)

#include "collectionindexingjobtest.moc"