]>
cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinquerytest.cpp
2 * SPDX-FileCopyrightText: 2019 Ismael Asensio <isma.af@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "search/dolphinquery.h"
11 #include <QJsonDocument>
12 #include <QJsonObject>
13 #include <QStringList>
17 class DolphinSearchBoxTest
: public QObject
22 void testBalooSearchParsing_data();
23 void testBalooSearchParsing();
27 * Helper function to compose the baloo query URL used for searching
29 QUrl
balooQueryUrl(const QString
& searchString
)
31 const QJsonObject jsonObject
{
32 {"searchString", searchString
}
35 const QJsonDocument
doc(jsonObject
);
36 const QString queryString
= QString::fromUtf8(doc
.toJson(QJsonDocument::Compact
));
39 urlQuery
.addQueryItem(QStringLiteral("json"), queryString
);
42 searchUrl
.setScheme(QLatin1String("baloosearch"));
43 searchUrl
.setQuery(urlQuery
);
49 * Defines the parameters for the test cases in testBalooSearchParsing()
51 void DolphinSearchBoxTest::testBalooSearchParsing_data()
54 QTest::addColumn
<QUrl
>("searchUrl");
55 QTest::addColumn
<QString
>("expectedText");
56 QTest::addColumn
<QStringList
>("expectedTerms");
57 QTest::addColumn
<bool>("hasContent");
58 QTest::addColumn
<bool>("hasFileName");
60 const QString text
= QStringLiteral("abc");
61 const QString textS
= QStringLiteral("abc xyz");
62 const QString textQ
= QStringLiteral("\"abc xyz\"");
63 const QString textM
= QStringLiteral("\"abc xyz\" tuv");
65 const QString filename
= QStringLiteral("filename:\"%1\"").arg(text
);
66 const QString filenameS
= QStringLiteral("filename:\"%1\"").arg(textS
);
67 const QString filenameQ
= QStringLiteral("filename:\"%1\"").arg(textQ
);
68 const QString filenameM
= QStringLiteral("filename:\"%1\"").arg(textM
);
70 const QString rating
= QStringLiteral("rating>=2");
71 const QString modified
= QStringLiteral("modified>=2019-08-07");
73 const QString tag
= QStringLiteral("tag:tagA");
74 const QString tagS
= QStringLiteral("tag:\"tagB with spaces\""); // in search url
75 const QString tagR
= QStringLiteral("tag:tagB with spaces"); // in result term
78 QTest::newRow("content") << balooQueryUrl(text
) << text
<< QStringList() << true << false;
79 QTest::newRow("content/space") << balooQueryUrl(textS
) << textS
<< QStringList() << true << false;
80 QTest::newRow("content/quoted") << balooQueryUrl(textQ
) << textS
<< QStringList() << true << false;
81 QTest::newRow("content/empty") << balooQueryUrl("") << "" << QStringList() << false << false;
82 QTest::newRow("content/single_quote") << balooQueryUrl("\"") << "\"" << QStringList() << true << false;
83 QTest::newRow("content/double_quote") << balooQueryUrl("\"\"") << "" << QStringList() << false << false;
85 // Test for "FileName"
86 QTest::newRow("filename") << balooQueryUrl(filename
) << text
<< QStringList() << false << true;
87 QTest::newRow("filename/space") << balooQueryUrl(filenameS
) << textS
<< QStringList() << false << true;
88 QTest::newRow("filename/quoted") << balooQueryUrl(filenameQ
) << textQ
<< QStringList() << false << true;
89 QTest::newRow("filename/mixed") << balooQueryUrl(filenameM
) << textM
<< QStringList() << false << true;
90 QTest::newRow("filename/empty") << balooQueryUrl("filename:") << "" << QStringList() << false << false;
91 QTest::newRow("filename/single_quote") << balooQueryUrl("filename:\"") << "\"" << QStringList() << false << true;
92 QTest::newRow("filename/double_quote") << balooQueryUrl("filename:\"\"") << "" << QStringList() << false << false;
94 // Combined content and filename search
95 QTest::newRow("content+filename")
96 << balooQueryUrl(text
+ " " + filename
)
97 << text
+ " " + filename
<< QStringList() << true << true;
99 QTest::newRow("content+filename/quoted")
100 << balooQueryUrl(textQ
+ " " + filenameQ
)
101 << textS
+ " " + filenameQ
<< QStringList() << true << true;
104 QTest::newRow("rating") << balooQueryUrl(rating
) << "" << QStringList({rating
}) << false << false;
105 QTest::newRow("rating+content") << balooQueryUrl(rating
+ " " + text
) << text
<< QStringList({rating
}) << true << false;
106 QTest::newRow("rating+filename") << balooQueryUrl(rating
+ " " + filename
) << text
<< QStringList({rating
}) << false << true;
108 // Test for modified date
109 QTest::newRow("modified") << balooQueryUrl(modified
) << "" << QStringList({modified
}) << false << false;
110 QTest::newRow("modified+content") << balooQueryUrl(modified
+ " " + text
) << text
<< QStringList({modified
}) << true << false;
111 QTest::newRow("modified+filename") << balooQueryUrl(modified
+ " " + filename
) << text
<< QStringList({modified
}) << false << true;
114 QTest::newRow("tag") << balooQueryUrl(tag
) << "" << QStringList({tag
}) << false << false;
115 QTest::newRow("tag/space" ) << balooQueryUrl(tagS
) << "" << QStringList({tagR
}) << false << false;
116 QTest::newRow("tag/double") << balooQueryUrl(tag
+ " " + tagS
) << "" << QStringList({tag
, tagR
}) << false << false;
117 QTest::newRow("tag+content") << balooQueryUrl(tag
+ " " + text
) << text
<< QStringList({tag
}) << true << false;
118 QTest::newRow("tag+filename") << balooQueryUrl(tag
+ " " + filename
) << text
<< QStringList({tag
}) << false << true;
120 // Combined search terms
121 QTest::newRow("searchTerms")
122 << balooQueryUrl(rating
+ " AND " + modified
+ " AND " + tag
+ " AND " + tagS
)
123 << "" << QStringList({modified
, rating
, tag
, tagR
}) << false << false;
125 QTest::newRow("searchTerms+content")
126 << balooQueryUrl(rating
+ " AND " + modified
+ " " + text
+ " " + tag
+ " AND " + tagS
)
127 << text
<< QStringList({modified
, rating
, tag
, tagR
}) << true << false;
129 QTest::newRow("searchTerms+filename")
130 << balooQueryUrl(rating
+ " AND " + modified
+ " " + filename
+ " " + tag
+ " AND " + tagS
)
131 << text
<< QStringList({modified
, rating
, tag
, tagR
}) << false << true;
133 QTest::newRow("allTerms")
134 << balooQueryUrl(text
+ " " + filename
+ " " + rating
+ " AND " + modified
+ " AND " + tag
)
135 << text
+ " " + filename
<< QStringList({modified
, rating
, tag
}) << true << true;
137 QTest::newRow("allTerms/space")
138 << balooQueryUrl(textS
+ " " + filenameS
+ " " + rating
+ " AND " + modified
+ " AND " + tagS
)
139 << textS
+ " " + filenameS
<< QStringList({modified
, rating
, tagR
}) << true << true;
141 // Test tags:/ URL scheme
142 const auto tagUrl
= [](const QString
&tag
) { return QUrl(QStringLiteral("tags:/%1/").arg(tag
)); };
143 const auto tagTerms
= [](const QString
&tag
) { return QStringList
{QStringLiteral("tag:%1").arg(tag
)}; };
145 QTest::newRow("tagsUrl") << tagUrl("tagA") << "" << tagTerms("tagA") << false << false;
146 QTest::newRow("tagsUrl/space") << tagUrl("tagB with spaces") << "" << tagTerms("tagB with spaces") << false << false;
147 QTest::newRow("tagsUrl/hash") << tagUrl("tagC#hash") << "" << tagTerms("tagC#hash") << false << false;
148 QTest::newRow("tagsUrl/slash") << tagUrl("tagD/with/slash") << "" << tagTerms("tagD/with/slash") << false << false;
152 * The test verifies whether the different terms search URL (e.g. "baloosearch:/") are
153 * properly handled by the searchbox, and only "user" or filename terms are added to the
154 * text bar of the searchbox.
156 void DolphinSearchBoxTest::testBalooSearchParsing()
158 QFETCH(QUrl
, searchUrl
);
159 QFETCH(QString
, expectedText
);
160 QFETCH(QStringList
, expectedTerms
);
161 QFETCH(bool, hasContent
);
162 QFETCH(bool, hasFileName
);
164 const DolphinQuery query
= DolphinQuery::fromSearchUrl(searchUrl
);
166 // Checkt that the URL is supported
167 QVERIFY(DolphinQuery::supportsScheme(searchUrl
.scheme()));
169 // Check for parsed text (would be displayed on the input search bar)
170 QCOMPARE(query
.text(), expectedText
);
172 // Check for parsed search terms (would be displayed by the facetsWidget)
173 QStringList searchTerms
= query
.searchTerms();
176 QCOMPARE(searchTerms
.count(), expectedTerms
.count());
177 for (int i
= 0; i
< expectedTerms
.count(); i
++) {
178 QCOMPARE(searchTerms
.at(i
), expectedTerms
.at(i
));
181 // Check for filename and content detection
182 QCOMPARE(query
.hasContentSearch(), hasContent
);
183 QCOMPARE(query
.hasFileName(), hasFileName
);
186 QTEST_MAIN(DolphinSearchBoxTest
)
188 #include "dolphinquerytest.moc"