]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinquerytest.cpp
[DolphinQueryTest] Fix class name duplication
[dolphin.git] / src / tests / dolphinquerytest.cpp
1 /*
2 * SPDX-FileCopyrightText: 2019 Ismael Asensio <isma.af@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "search/dolphinquery.h"
8
9 #include <QTest>
10
11 #include <QJsonDocument>
12 #include <QJsonObject>
13 #include <QStringList>
14 #include <QStandardPaths>
15 #include <QUrl>
16 #include <QUrlQuery>
17
18 class DolphinQueryTest : public QObject
19 {
20 Q_OBJECT
21
22 private Q_SLOTS:
23 void initTestCase();
24 void testBalooSearchParsing_data();
25 void testBalooSearchParsing();
26 };
27
28 /**
29 * Helper function to compose the baloo query URL used for searching
30 */
31 QUrl balooQueryUrl(const QString& searchString)
32 {
33 const QJsonObject jsonObject {
34 {"searchString", searchString}
35 };
36
37 const QJsonDocument doc(jsonObject);
38 const QString queryString = QString::fromUtf8(doc.toJson(QJsonDocument::Compact));
39
40 QUrlQuery urlQuery;
41 urlQuery.addQueryItem(QStringLiteral("json"), queryString);
42
43 QUrl searchUrl;
44 searchUrl.setScheme(QLatin1String("baloosearch"));
45 searchUrl.setQuery(urlQuery);
46
47 return searchUrl;
48 }
49
50 void DolphinQueryTest::initTestCase()
51 {
52 QStandardPaths::setTestModeEnabled(true);
53 }
54
55 /**
56 * Defines the parameters for the test cases in testBalooSearchParsing()
57 */
58 void DolphinQueryTest::testBalooSearchParsing_data()
59 {
60
61 QTest::addColumn<QUrl>("searchUrl");
62 QTest::addColumn<QString>("expectedText");
63 QTest::addColumn<QStringList>("expectedTerms");
64 QTest::addColumn<bool>("hasContent");
65 QTest::addColumn<bool>("hasFileName");
66
67 const QString text = QStringLiteral("abc");
68 const QString textS = QStringLiteral("abc xyz");
69 const QString textQ = QStringLiteral("\"abc xyz\"");
70 const QString textM = QStringLiteral("\"abc xyz\" tuv");
71
72 const QString filename = QStringLiteral("filename:\"%1\"").arg(text);
73 const QString filenameS = QStringLiteral("filename:\"%1\"").arg(textS);
74 const QString filenameQ = QStringLiteral("filename:\"%1\"").arg(textQ);
75 const QString filenameM = QStringLiteral("filename:\"%1\"").arg(textM);
76
77 const QString rating = QStringLiteral("rating>=2");
78 const QString modified = QStringLiteral("modified>=2019-08-07");
79
80 const QString tag = QStringLiteral("tag:tagA");
81 const QString tagS = QStringLiteral("tag:\"tagB with spaces\""); // in search url
82 const QString tagR = QStringLiteral("tag:tagB with spaces"); // in result term
83
84 // Test for "Content"
85 QTest::newRow("content") << balooQueryUrl(text) << text << QStringList() << true << false;
86 QTest::newRow("content/space") << balooQueryUrl(textS) << textS << QStringList() << true << false;
87 QTest::newRow("content/quoted") << balooQueryUrl(textQ) << textS << QStringList() << true << false;
88 QTest::newRow("content/empty") << balooQueryUrl("") << "" << QStringList() << false << false;
89 QTest::newRow("content/single_quote") << balooQueryUrl("\"") << "\"" << QStringList() << true << false;
90 QTest::newRow("content/double_quote") << balooQueryUrl("\"\"") << "" << QStringList() << false << false;
91
92 // Test for "FileName"
93 QTest::newRow("filename") << balooQueryUrl(filename) << text << QStringList() << false << true;
94 QTest::newRow("filename/space") << balooQueryUrl(filenameS) << textS << QStringList() << false << true;
95 QTest::newRow("filename/quoted") << balooQueryUrl(filenameQ) << textQ << QStringList() << false << true;
96 QTest::newRow("filename/mixed") << balooQueryUrl(filenameM) << textM << QStringList() << false << true;
97 QTest::newRow("filename/empty") << balooQueryUrl("filename:") << "" << QStringList() << false << false;
98 QTest::newRow("filename/single_quote") << balooQueryUrl("filename:\"") << "\"" << QStringList() << false << true;
99 QTest::newRow("filename/double_quote") << balooQueryUrl("filename:\"\"") << "" << QStringList() << false << false;
100
101 // Combined content and filename search
102 QTest::newRow("content+filename")
103 << balooQueryUrl(text + " " + filename)
104 << text + " " + filename << QStringList() << true << true;
105
106 QTest::newRow("content+filename/quoted")
107 << balooQueryUrl(textQ + " " + filenameQ)
108 << textS + " " + filenameQ << QStringList() << true << true;
109
110 // Test for rating
111 QTest::newRow("rating") << balooQueryUrl(rating) << "" << QStringList({rating}) << false << false;
112 QTest::newRow("rating+content") << balooQueryUrl(rating + " " + text) << text << QStringList({rating}) << true << false;
113 QTest::newRow("rating+filename") << balooQueryUrl(rating + " " + filename) << text << QStringList({rating}) << false << true;
114
115 // Test for modified date
116 QTest::newRow("modified") << balooQueryUrl(modified) << "" << QStringList({modified}) << false << false;
117 QTest::newRow("modified+content") << balooQueryUrl(modified + " " + text) << text << QStringList({modified}) << true << false;
118 QTest::newRow("modified+filename") << balooQueryUrl(modified + " " + filename) << text << QStringList({modified}) << false << true;
119
120 // Test for tags
121 QTest::newRow("tag") << balooQueryUrl(tag) << "" << QStringList({tag}) << false << false;
122 QTest::newRow("tag/space" ) << balooQueryUrl(tagS) << "" << QStringList({tagR}) << false << false;
123 QTest::newRow("tag/double") << balooQueryUrl(tag + " " + tagS) << "" << QStringList({tag, tagR}) << false << false;
124 QTest::newRow("tag+content") << balooQueryUrl(tag + " " + text) << text << QStringList({tag}) << true << false;
125 QTest::newRow("tag+filename") << balooQueryUrl(tag + " " + filename) << text << QStringList({tag}) << false << true;
126
127 // Combined search terms
128 QTest::newRow("searchTerms")
129 << balooQueryUrl(rating + " AND " + modified + " AND " + tag + " AND " + tagS)
130 << "" << QStringList({modified, rating, tag, tagR}) << false << false;
131
132 QTest::newRow("searchTerms+content")
133 << balooQueryUrl(rating + " AND " + modified + " " + text + " " + tag + " AND " + tagS)
134 << text << QStringList({modified, rating, tag, tagR}) << true << false;
135
136 QTest::newRow("searchTerms+filename")
137 << balooQueryUrl(rating + " AND " + modified + " " + filename + " " + tag + " AND " + tagS)
138 << text << QStringList({modified, rating, tag, tagR}) << false << true;
139
140 QTest::newRow("allTerms")
141 << balooQueryUrl(text + " " + filename + " " + rating + " AND " + modified + " AND " + tag)
142 << text + " " + filename << QStringList({modified, rating, tag}) << true << true;
143
144 QTest::newRow("allTerms/space")
145 << balooQueryUrl(textS + " " + filenameS + " " + rating + " AND " + modified + " AND " + tagS)
146 << textS + " " + filenameS << QStringList({modified, rating, tagR}) << true << true;
147
148 // Test tags:/ URL scheme
149 const auto tagUrl = [](const QString &tag) { return QUrl(QStringLiteral("tags:/%1/").arg(tag)); };
150 const auto tagTerms = [](const QString &tag) { return QStringList{QStringLiteral("tag:%1").arg(tag)}; };
151
152 QTest::newRow("tagsUrl") << tagUrl("tagA") << "" << tagTerms("tagA") << false << false;
153 QTest::newRow("tagsUrl/space") << tagUrl("tagB with spaces") << "" << tagTerms("tagB with spaces") << false << false;
154 QTest::newRow("tagsUrl/hash") << tagUrl("tagC#hash") << "" << tagTerms("tagC#hash") << false << false;
155 QTest::newRow("tagsUrl/slash") << tagUrl("tagD/with/slash") << "" << tagTerms("tagD/with/slash") << false << false;
156 }
157
158 /**
159 * The test verifies whether the different terms search URL (e.g. "baloosearch:/") are
160 * properly handled by the searchbox, and only "user" or filename terms are added to the
161 * text bar of the searchbox.
162 */
163 void DolphinQueryTest::testBalooSearchParsing()
164 {
165 QFETCH(QUrl, searchUrl);
166 QFETCH(QString, expectedText);
167 QFETCH(QStringList, expectedTerms);
168 QFETCH(bool, hasContent);
169 QFETCH(bool, hasFileName);
170
171 const DolphinQuery query = DolphinQuery::fromSearchUrl(searchUrl);
172
173 // Checkt that the URL is supported
174 QVERIFY(DolphinQuery::supportsScheme(searchUrl.scheme()));
175
176 // Check for parsed text (would be displayed on the input search bar)
177 QCOMPARE(query.text(), expectedText);
178
179 // Check for parsed search terms (would be displayed by the facetsWidget)
180 QStringList searchTerms = query.searchTerms();
181 searchTerms.sort();
182
183 QCOMPARE(searchTerms.count(), expectedTerms.count());
184 for (int i = 0; i < expectedTerms.count(); i++) {
185 QCOMPARE(searchTerms.at(i), expectedTerms.at(i));
186 }
187
188 // Check for filename and content detection
189 QCOMPARE(query.hasContentSearch(), hasContent);
190 QCOMPARE(query.hasFileName(), hasFileName);
191 }
192
193 QTEST_MAIN(DolphinQueryTest)
194
195 #include "dolphinquerytest.moc"