File indexing completed on 2024-05-12 05:26:22
0001 #include <QTest> 0002 0003 #include <QString> 0004 0005 #include "definitions.h" 0006 #include "storage.h" 0007 #include "fulltextindex.h" 0008 0009 /** 0010 * Test of the index implementation 0011 */ 0012 class FulltextIndexTest : public QObject 0013 { 0014 Q_OBJECT 0015 private slots: 0016 void initTestCase() 0017 { 0018 Sink::Storage::DataStore store(Sink::storageLocation(), "sink.dummy.instance1", Sink::Storage::DataStore::ReadWrite); 0019 store.removeFromDisk(); 0020 } 0021 0022 void cleanup() 0023 { 0024 Sink::Storage::DataStore store(Sink::storageLocation(), "sink.dummy.instance1", Sink::Storage::DataStore::ReadWrite); 0025 store.removeFromDisk(); 0026 } 0027 0028 void testIndex() 0029 { 0030 FulltextIndex index("sink.dummy.instance1", Sink::Storage::DataStore::ReadWrite); 0031 // qInfo() << QString("Found document 1 with terms: ") + index.getIndexContent(id1).terms.join(", "); 0032 // qInfo() << QString("Found document 2 with terms: ") + index.getIndexContent(id2).terms.join(", "); 0033 0034 const auto key1 = Sink::Storage::Identifier::createIdentifier(); 0035 const auto key2 = Sink::Storage::Identifier::createIdentifier(); 0036 const auto key3 = Sink::Storage::Identifier::createIdentifier(); 0037 0038 index.add(key1, "value1"); 0039 index.add(key2, "value2"); 0040 index.commitTransaction(); 0041 0042 //Basic lookups 0043 QCOMPARE(index.lookup("value1").size(), 1); 0044 QCOMPARE(index.lookup("value1*").size(), 1); 0045 QCOMPARE(index.lookup("value").size(), 2); 0046 QCOMPARE(index.lookup("\"value1\"").size(), 1); 0047 QCOMPARE(index.lookup("\"value\"").size(), 0); 0048 QCOMPARE(index.lookup("value1 value2").size(), 0); 0049 QCOMPARE(index.lookup("value1 OR value2").size(), 2); 0050 0051 //Rollback 0052 index.add(key3, "value3"); 0053 QCOMPARE(index.lookup("value3").size(), 1); 0054 index.abortTransaction(); 0055 QCOMPARE(index.lookup("value3").size(), 0); 0056 } 0057 0058 void testIndexOrdering() 0059 { 0060 FulltextIndex index("sink.dummy.instance1", Sink::Storage::DataStore::ReadWrite); 0061 const auto key1 = Sink::Storage::Identifier::createIdentifier(); 0062 const auto key2 = Sink::Storage::Identifier::createIdentifier(); 0063 const auto key3 = Sink::Storage::Identifier::createIdentifier(); 0064 0065 const QDateTime dt{{2022,5,26},{9,38,0}}; 0066 0067 index.add(key1, "value1", dt.addDays(1)); 0068 index.add(key2, "value2", dt); 0069 index.add(key3, "value3", dt.addDays(2)); 0070 index.commitTransaction(); 0071 const auto values = index.lookup("value"); 0072 QCOMPARE(values.size(), 3); 0073 QCOMPARE(values[0], key3); 0074 QCOMPARE(values[1], key1); 0075 QCOMPARE(values[2], key2); 0076 } 0077 }; 0078 0079 QTEST_MAIN(FulltextIndexTest) 0080 #include "fulltextindextest.moc"