File indexing completed on 2024-04-28 03:51:39
0001 /* 0002 This file is part of the KDE Baloo project. 0003 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.1-or-later 0006 */ 0007 0008 #include "documenturldb.h" 0009 #include "singledbtest.h" 0010 #include "idutils.h" 0011 0012 #include <memory> 0013 0014 using namespace Baloo; 0015 0016 class DocumentUrlDBTest : public QObject 0017 { 0018 Q_OBJECT 0019 0020 void touchFile(const QByteArray& path) { 0021 QFile file(QString::fromUtf8(path)); 0022 file.open(QIODevice::WriteOnly); 0023 file.write("data"); 0024 } 0025 0026 private Q_SLOTS: 0027 void init() 0028 { 0029 m_tempDir = std::make_unique<QTemporaryDir>(); 0030 0031 mdb_env_create(&m_env); 0032 mdb_env_set_maxdbs(m_env, 2); 0033 0034 // The directory needs to be created before opening the environment 0035 QByteArray path = QFile::encodeName(m_tempDir->path()); 0036 mdb_env_open(m_env, path.constData(), 0, 0664); 0037 mdb_txn_begin(m_env, nullptr, 0, &m_txn); 0038 } 0039 0040 void cleanup() 0041 { 0042 mdb_txn_abort(m_txn); 0043 m_txn = nullptr; 0044 mdb_env_close(m_env); 0045 m_env = nullptr; 0046 m_tempDir.reset(); 0047 } 0048 0049 void testNonExistingPath() { 0050 /* 0051 DocumentUrlDB db(m_txn); 0052 db.put(1, "/fire/does/not/exist"); 0053 0054 // FIXME: What about error handling? 0055 QCOMPARE(db.get(1), QByteArray()); 0056 */ 0057 } 0058 0059 void testSingleFile() 0060 { 0061 QByteArray filePath("file1"); 0062 quint64 id = 1; 0063 0064 DocumentUrlDB db(IdTreeDB::create(m_txn), IdFilenameDB::create(m_txn), m_txn); 0065 db.put(id, 0, filePath); 0066 0067 QVERIFY(db.contains(id)); 0068 QCOMPARE(db.get(id), "/" + filePath); 0069 0070 db.del(id); 0071 QCOMPARE(db.get(id), QByteArray()); 0072 } 0073 0074 void testTwoFilesAndAFolder() 0075 { 0076 QByteArray dirPath("<root>"); 0077 QByteArray filePath1("file"); 0078 QByteArray filePath2("file2"); 0079 0080 quint64 did = 99; 0081 quint64 id1 = 1; 0082 quint64 id2 = 2; 0083 0084 DocumentUrlDB db(IdTreeDB::create(m_txn), IdFilenameDB::create(m_txn), m_txn); 0085 db.put(did, 0, dirPath); 0086 db.put(id1, did, filePath1); 0087 db.put(id2, did, filePath2); 0088 0089 QVERIFY(db.contains(id1)); 0090 QVERIFY(db.contains(id2)); 0091 QCOMPARE(db.get(id1), "/<root>/" + filePath1); 0092 QCOMPARE(db.get(id2), "/<root>/" + filePath2); 0093 QCOMPARE(db.get(did), "/" + dirPath); 0094 0095 db.del(id1); 0096 0097 QCOMPARE(db.get(id1), QByteArray()); 0098 QCOMPARE(db.get(id2), "/<root>/" + filePath2); 0099 QCOMPARE(db.get(did), "/" + dirPath); 0100 0101 db.del(id2); 0102 0103 QCOMPARE(db.get(id1), QByteArray()); 0104 QCOMPARE(db.get(id2), QByteArray()); 0105 QCOMPARE(db.get(did), "/" + dirPath); 0106 0107 db.del(did); 0108 QCOMPARE(db.get(id1), QByteArray()); 0109 QCOMPARE(db.get(id2), QByteArray()); 0110 QCOMPARE(db.get(did), QByteArray()); 0111 } 0112 0113 void testFileRename() 0114 { 0115 const QByteArray path{"<root>"}; 0116 quint64 did = 1; 0117 QByteArray filePath("file"); 0118 quint64 id = 2; 0119 0120 DocumentUrlDB db(IdTreeDB::create(m_txn), IdFilenameDB::create(m_txn), m_txn); 0121 0122 db.put(did, 0, path); 0123 db.put(id, did, filePath); 0124 0125 QCOMPARE(db.getId(did, filePath), id); 0126 QCOMPARE(db.get(id), QByteArray("/<root>/file")); 0127 0128 db.updateUrl(id, did, "file2"); 0129 0130 QCOMPARE(db.getId(did, QByteArray("file2")), id); 0131 QCOMPARE(db.get(id), QByteArray("/<root>/file2")); 0132 0133 db.del(id); 0134 QCOMPARE(db.get(id), QByteArray()); 0135 } 0136 0137 void testDuplicateId() 0138 { 0139 } 0140 0141 void testGetId() 0142 { 0143 quint64 id = 1; 0144 0145 QByteArray filePath1("file"); 0146 quint64 id1 = 2; 0147 QByteArray filePath2("file2"); 0148 quint64 id2 = 3; 0149 0150 DocumentUrlDB db(IdTreeDB::create(m_txn), IdFilenameDB::create(m_txn), m_txn); 0151 0152 db.put(id1, id, filePath1); 0153 db.put(id2, id, filePath2); 0154 0155 QCOMPARE(db.getId(id, QByteArray("file")), id1); 0156 QCOMPARE(db.getId(id, QByteArray("file2")), id2); 0157 } 0158 0159 void testSortedIdInsert() 0160 { 0161 // test sorted insert used in Baloo::DocumentUrlDB::add, bug 367991 0162 std::vector<quint64> test; 0163 test.push_back(9); 0164 0165 // shall not crash 0166 sortedIdInsert(test, quint64(1)); 0167 0168 // stuff shall be ok inserted 0169 QVERIFY(test.size() == 2); 0170 QVERIFY(test[0] == 1); 0171 QVERIFY(test[1] == 9); 0172 0173 // shall not crash 0174 sortedIdInsert(test, quint64(1)); 0175 0176 // no insert please 0177 QVERIFY(test.size() == 2); 0178 0179 // shall not crash 0180 sortedIdInsert(test, quint64(10)); 0181 0182 // stuff shall be ok inserted 0183 QVERIFY(test.size() == 3); 0184 QVERIFY(test[0] == 1); 0185 QVERIFY(test[1] == 9); 0186 QVERIFY(test[2] == 10); 0187 0188 // shall not crash 0189 sortedIdInsert(test, quint64(2)); 0190 0191 // stuff shall be ok inserted 0192 QVERIFY(test.size() == 4); 0193 QVERIFY(test[0] == 1); 0194 QVERIFY(test[1] == 2); 0195 QVERIFY(test[2] == 9); 0196 QVERIFY(test[3] == 10); 0197 0198 // shall not crash 0199 sortedIdInsert(test, quint64(2)); 0200 0201 // no insert please 0202 QVERIFY(test.size() == 4); 0203 } 0204 0205 protected: 0206 MDB_env* m_env = nullptr; 0207 MDB_txn* m_txn = nullptr; 0208 std::unique_ptr<QTemporaryDir> m_tempDir; 0209 }; 0210 0211 QTEST_MAIN(DocumentUrlDBTest) 0212 0213 #include "documenturldbtest.moc"