1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2021-02-20
 * Description : Unit tests for TagsCache class
 *
 * SPDX-FileCopyrightText: 2021 by David Haslam, <dch dot code at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "tagscache_utest.h"

// C++ includes

#include <set>

// Qt includes

#include <QString>
#include <QDebug>

// Local includes

#include "coredb.h"
#include "coredbaccess.h"

TagsCacheTest::TagsCacheTest(QObject* const parent)
    : QObject  (parent),
      tagsCache(nullptr)
{
}

TagsCacheTest::~TagsCacheTest()
{
}

void TagsCacheTest::initTestCase()
{
    qDebug() << "initTestCase()";

    Digikam::DbEngineParameters db_params
    (
        QString::fromUtf8("QSQLITE"),      // databaseType
        QString::fromUtf8(":memory:"),     // databaseNameCore
        QString::fromUtf8("")              // connectOptions
    );

    Digikam::CoreDbAccess::setParameters(db_params);
    QVERIFY(Digikam::CoreDbAccess::checkReadyForUse());
    tagsCache = Digikam::TagsCache::instance();
}

void TagsCacheTest::cleanupTestCase()
{
    qDebug() << "cleanupTestCase()";
}

void TagsCacheTest::init()
{
    QCOMPARE(countTags(), 0);
}

void TagsCacheTest::cleanup()
{
    auto coredb     = Digikam::CoreDbAccess().db();
    const auto tags = coredb->getTagShortInfos();

    for (const auto& tag : tags)
    {
        coredb->deleteTag(tag.id);
    }

    QCOMPARE(countTags(), 0);
}

void TagsCacheTest::testSimpleHierarchy()
{
    auto id       = tagsCache->createTag(QLatin1String("Top/Middle/Bottom"));

    dumpTags();
    QCOMPARE(countTags(), 3);

    auto bottomId = tagsCache->tagForPath(QLatin1String("Top/Middle/Bottom"));
    auto middleId = tagsCache->parentTag(id);
    auto topId    = tagsCache->parentTag(middleId);

    QCOMPARE(id, bottomId);
    QVERIFY(bottomId);
    QVERIFY(middleId);
    QVERIFY(topId);
    QVERIFY(tagsCache->hasTag(bottomId));
    QVERIFY(tagsCache->hasTag(middleId));
    QVERIFY(tagsCache->hasTag(topId));
    QCOMPARE(tagsCache->tagName(id),       QLatin1String("Bottom"));
    QCOMPARE(tagsCache->tagName(middleId), QLatin1String("Middle"));
    QCOMPARE(tagsCache->tagName(topId),    QLatin1String("Top"));

    auto bottomIds = tagsCache->tagsForName(QLatin1String("Bottom"));

    QCOMPARE(bottomIds.size(), 1);
    QCOMPARE(bottomIds[0], bottomId);
}

void TagsCacheTest::testComplexHierarchy()
{
    tagsCache->createTag(QLatin1String("Top/Middle"));
    tagsCache->createTag(QLatin1String("Top/Middle/First"));
    tagsCache->createTag(QLatin1String("Top/Middle/Second"));
    tagsCache->createTag(QLatin1String("Super/Top"));
    tagsCache->createTag(QLatin1String("Super/Top/First"));
    tagsCache->createTag(QLatin1String("Super/Top/Second"));
    tagsCache->createTag(QLatin1String("Super/Top/Third"));
    tagsCache->createTag(QLatin1String("Mixed Up/Third/Second"));
    tagsCache->createTag(QLatin1String("Mixed Up/Third/First"));

    QCOMPARE(countTags(), 13);

    auto top_ids   = tagsCache->tagsForName(QLatin1String("Top"));
    QCOMPARE(top_ids.size(), 2);

    auto first_ids = tagsCache->tagsForName(QLatin1String("First"));
    QCOMPARE(first_ids.size(), 3);
}

void TagsCacheTest::testRepeatedNames()
{
    tagsCache->createTag(QLatin1String("Repeat Me/Repeat Me/Repeat Me/Repeat Me"));

    const auto repeatMeIds = tagsCache->tagsForName(QLatin1String("Repeat Me"));
    QCOMPARE(repeatMeIds.size(), 4);

    // all ids should be unique

    std::set<int> set;

    for (const auto& id : repeatMeIds)
    {
        auto result = set.insert(id);
        QVERIFY(result.second);
    }
}

void TagsCacheTest::testDuplicateTop()
{
    tagsCache->createTag(QLatin1String("Top/Middle/Bottom"));
    tagsCache->createTag(QLatin1String("Super/Top/Middle/Bottom"));
    tagsCache->createTag(QLatin1String("First/Super/Top"));
    dumpTags();
    QCOMPARE(countTags(), 10);

    // the single word 'Top' should match the top tag

    auto id1 = tagsCache->tagForPath(QLatin1String("Top"));
    QVERIFY(id1);
    QCOMPARE(tagsCache->tagPath(id1), QLatin1String("/Top"));

    // and it doesn't matter if there's a leading slash

    auto id2 = tagsCache->tagForPath(QLatin1String("/Top"));
    QVERIFY(id2);
    QCOMPARE(id1, id2);
    QCOMPARE(tagsCache->tagPath(id2), QLatin1String("/Top"));

    // a more complex request

    auto id3 = tagsCache->tagForPath(QLatin1String("Super/Top"));
    QVERIFY(id3);
    QCOMPARE(tagsCache->tagPath(id3), QLatin1String("/Super/Top"));
}

// utilities

int TagsCacheTest::countTags()
{
    auto coredb = Digikam::CoreDbAccess().db();<--- Variable 'coredb' can be declared as pointer to const
    auto tags   = coredb->getTagShortInfos();

    return tags.size();
}

// debug

void TagsCacheTest::dumpTables()
{
    auto sqlnames = QSqlDatabase::connectionNames();
    auto sqldb    = QSqlDatabase::database(sqlnames[0]);
    qDebug() << sqldb.tables();
}

void TagsCacheTest::dumpTags()
{
    auto coredb = Digikam::CoreDbAccess().db();<--- Variable 'coredb' can be declared as pointer to const
    auto tags   = coredb->getTagShortInfos();

    for (auto it = tags.begin() ; it != tags.end() ; ++it)
    {
        qDebug() << it->id << it->pid << it->name;
    }
}

QTEST_GUILESS_MAIN(TagsCacheTest)

#include "moc_tagscache_utest.cpp"