diff --git a/autotests/integration/querytest.cpp b/autotests/integration/querytest.cpp --- a/autotests/integration/querytest.cpp +++ b/autotests/integration/querytest.cpp @@ -50,6 +50,9 @@ m_id2 = touchFile(dir->path() + "/file2"); m_id3 = touchFile(dir->path() + "/file3"); m_id4 = touchFile(dir->path() + "/file4"); + + m_id5 = touchFile(dir->path() + "/tagFile1"); + m_id6 = touchFile(dir->path() + "/tagFile2"); } void init() { @@ -71,6 +74,11 @@ void testTermOr(); void testTermPhrase(); + void testTagTermAnd_data(); + void testTagTermAnd(); + void testTagTermPhrase_data(); + void testTagTermPhrase(); + private: QScopedPointer dir; QTemporaryDir* dbDir; @@ -94,10 +102,33 @@ tr->addDocument(doc); } + void insertTagDocuments(); + void addTagDocument(Transaction* tr,const QStringList& tags, quint64 id, const QString& url) + { + Document doc; + doc.setUrl(QFile::encodeName(url)); + + QString fileName = url.mid(url.lastIndexOf('/') + 1); + + TermGenerator tg(doc); + tg.indexText("text/plain", QByteArray("M")); + for (const QString& tag : tags) { + tg.indexXattrText(tag, QByteArray("TA")); + } + tg.indexFileNameText(fileName); + doc.setId(id); + doc.setMTime(3); + doc.setCTime(4); + + tr->addDocument(doc); + } + quint64 m_id1; quint64 m_id2; quint64 m_id3; quint64 m_id4; + quint64 m_id5; + quint64 m_id6; }; @@ -111,6 +142,14 @@ tr.commit(); } +void QueryTest::insertTagDocuments() +{ + Transaction tr(db, Transaction::ReadWrite); + addTagDocument(&tr, {"One", "Two", "Three", "Four", "F1"}, m_id5, dir->path() + "/tagFile1"); + addTagDocument(&tr, {"One", "Two-Three", "Four", "F2"}, m_id6, dir->path() + "/tagFile2"); + tr.commit(); +} + void QueryTest::testTermEqual() { EngineQuery q("the"); @@ -168,6 +207,72 @@ QCOMPARE(tr.exec(q), result); } +void QueryTest::testTagTermAnd_data() +{ + QTest::addColumn("terms"); + QTest::addColumn>("matchIds"); + + QTest::addRow("Simple match") << QByteArrayList({"one", "four"}) + << QVector { m_id5, m_id6 }; + QTest::addRow("Only one") << QByteArrayList({"one", "f1"}) + << QVector { m_id5 }; + QTest::addRow("Also from phrase") << QByteArrayList({"two", "three"}) + << QVector { m_id5, m_id6 }; +} + +void QueryTest::testTagTermAnd() +{ + insertTagDocuments(); + QFETCH(QByteArrayList, terms); + QFETCH(QVector, matchIds); + + QByteArray prefix{"TA"}; + QVector queries; + for (const QByteArray& term : terms) { + queries << EngineQuery(prefix + term); + } + + EngineQuery q(queries, EngineQuery::And); + + Transaction tr(db, Transaction::ReadOnly); + qDebug() << matchIds << tr.exec(q); + QCOMPARE(tr.exec(q), matchIds); +} + +void QueryTest::testTagTermPhrase_data() +{ + QTest::addColumn("terms"); + QTest::addColumn>("matchIds"); + + QTest::addRow("Simple match") << QByteArrayList({"one"}) + << QVector { m_id5, m_id6 }; + QTest::addRow("Apart") << QByteArrayList({"two", "four"}) + << QVector { }; + QTest::addRow("Adjacent") << QByteArrayList({"three", "four"}) + << QVector { }; + QTest::addRow("Only phrase") << QByteArrayList({"two", "three"}) + << QVector { m_id6 }; +} + +void QueryTest::testTagTermPhrase() +{ + insertTagDocuments(); + QFETCH(QByteArrayList, terms); + QFETCH(QVector, matchIds); + + QByteArray prefix{"TA"}; + QVector queries; + for (const QByteArray& term : terms) { + queries << EngineQuery(prefix + term); + } + + EngineQuery q(queries, EngineQuery::Phrase); + + Transaction tr(db, Transaction::ReadOnly); + auto res = tr.exec(q); + qDebug() << matchIds << res; + QCOMPARE(res, matchIds); +} QTEST_MAIN(QueryTest)