]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinquerytest.cpp
Merge branch 'release/19.12'
[dolphin.git] / src / tests / dolphinquerytest.cpp
1 /***************************************************************************
2 * Copyright (C) 2019 by Ismael Asensio <isma.af@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "search/dolphinquery.h"
21
22 #include <QTest>
23
24 #include <QJsonDocument>
25 #include <QJsonObject>
26 #include <QStringList>
27 #include <QUrl>
28 #include <QUrlQuery>
29
30 class DolphinSearchBoxTest : public QObject
31 {
32 Q_OBJECT
33
34 private slots:
35 void testBalooSearchParsing_data();
36 void testBalooSearchParsing();
37 };
38
39 /**
40 * Defines the parameters for the test cases in testBalooSearchParsing()
41 */
42 void DolphinSearchBoxTest::testBalooSearchParsing_data()
43 {
44 const QString text = QStringLiteral("abc");
45 const QString textS = QStringLiteral("abc xyz");
46 const QString filename = QStringLiteral("filename:\"%1\"").arg(text);
47 const QString filenameS = QStringLiteral("filename:\"%1\"").arg(textS);
48
49 const QString rating = QStringLiteral("rating>=2");
50 const QString modified = QStringLiteral("modified>=2019-08-07");
51
52 const QString tag = QStringLiteral("tag:tagA");
53 const QString tagS = QStringLiteral("tag:\"tagB with spaces\""); // in search url
54 const QString tagR = QStringLiteral("tag:tagB with spaces"); // in result term
55
56 QTest::addColumn<QString>("searchString");
57 QTest::addColumn<QString>("expectedText");
58 QTest::addColumn<QStringList>("expectedTerms");
59 QTest::addColumn<bool>("hasContent");
60 QTest::addColumn<bool>("hasFileName");
61
62 // Test for "Content"
63 QTest::newRow("content") << text << text << QStringList() << true << false;
64 QTest::newRow("content/space") << textS << textS << QStringList() << true << false;
65 QTest::newRow("content/empty") << "" << "" << QStringList() << false << false;
66 QTest::newRow("content/single_quote") << "\"" << "\"" << QStringList() << true << false;
67 QTest::newRow("content/double_quote") << "\"\"" << "" << QStringList() << false << false;
68
69 // Test for "FileName"
70 QTest::newRow("filename") << filename << text << QStringList() << false << true;
71 QTest::newRow("filename/space") << filenameS << textS << QStringList() << false << true;
72 QTest::newRow("filename/empty") << "filename:" << "" << QStringList() << false << false;
73 QTest::newRow("filename/single_quote") << "filename:\"" << "\"" << QStringList() << false << true;
74 QTest::newRow("filename/double_quote") << "filename:\"\"" << "" << QStringList() << false << false;
75
76 // Combined content and filename search
77 QTest::newRow("content+filename")
78 << text + " " + filename
79 << text + " " + filename << QStringList() << true << true;
80
81 // Test for rating
82 QTest::newRow("rating") << rating << "" << QStringList({rating}) << false << false;
83 QTest::newRow("rating+content") << rating + " " + text << text << QStringList({rating}) << true << false;
84 QTest::newRow("rating+filename") << rating + " " + filename << text << QStringList({rating}) << false << true;
85
86 // Test for modified date
87 QTest::newRow("modified") << modified << "" << QStringList({modified}) << false << false;
88 QTest::newRow("modified+content") << modified + " " + text << text << QStringList({modified}) << true << false;
89 QTest::newRow("modified+filename") << modified + " " + filename << text << QStringList({modified}) << false << true;
90
91 // Test for tags
92 QTest::newRow("tag") << tag << "" << QStringList({tag}) << false << false;
93 QTest::newRow("tag/space" ) << tagS << "" << QStringList({tagR}) << false << false;
94 QTest::newRow("tag/double") << tag + " " + tagS << "" << QStringList({tag, tagR}) << false << false;
95 QTest::newRow("tag+content") << tag + " " + text << text << QStringList({tag}) << true << false;
96 QTest::newRow("tag+filename") << tag + " " + filename << text << QStringList({tag}) << false << true;
97
98 // Combined search terms
99 QTest::newRow("searchTerms")
100 << rating + " AND " + modified + " AND " + tag + " AND " + tagS
101 << "" << QStringList({modified, rating, tag, tagR}) << false << false;
102
103 QTest::newRow("searchTerms+content")
104 << rating + " AND " + modified + " " + text + " " + tag + " AND " + tagS
105 << text << QStringList({modified, rating, tag, tagR}) << true << false;
106
107 QTest::newRow("searchTerms+filename")
108 << rating + " AND " + modified + " " + filename + " " + tag + " AND " + tagS
109 << text << QStringList({modified, rating, tag, tagR}) << false << true;
110
111 QTest::newRow("allTerms")
112 << text + " " + filename + " " + rating + " AND " + modified + " AND " + tag
113 << text + " " + filename << QStringList({modified, rating, tag}) << true << true;
114
115 QTest::newRow("allTerms/space")
116 << textS + " " + filenameS + " " + rating + " AND " + modified + " AND " + tagS
117 << textS + " " + filenameS << QStringList({modified, rating, tagR}) << true << true;
118 }
119
120 /**
121 * Helper function to compose the baloo query URL used for searching
122 */
123 QUrl composeQueryUrl(const QString& searchString)
124 {
125 const QJsonObject jsonObject {
126 {"searchString", searchString}
127 };
128
129 const QJsonDocument doc(jsonObject);
130 const QString queryString = QString::fromUtf8(doc.toJson(QJsonDocument::Compact));
131
132 QUrlQuery urlQuery;
133 urlQuery.addQueryItem(QStringLiteral("json"), queryString);
134
135 QUrl searchUrl;
136 searchUrl.setScheme(QLatin1String("baloosearch"));
137 searchUrl.setQuery(urlQuery);
138
139 return searchUrl;
140 }
141
142 /**
143 * The test verifies whether the different terms of a Baloo search URL ("baloosearch:") are
144 * properly handled by the searchbox, and only "user" or filename terms are added to the
145 * text bar of the searchbox.
146 */
147 void DolphinSearchBoxTest::testBalooSearchParsing()
148 {
149 QFETCH(QString, searchString);
150 QFETCH(QString, expectedText);
151 QFETCH(QStringList, expectedTerms);
152 QFETCH(bool, hasContent);
153 QFETCH(bool, hasFileName);
154
155 const QUrl testUrl = composeQueryUrl(searchString);
156 const DolphinQuery query = DolphinQuery::fromBalooSearchUrl(testUrl);
157
158 QStringList searchTerms = query.searchTerms();
159 searchTerms.sort();
160
161 // Check for parsed text (would be displayed on the input search bar)
162 QCOMPARE(query.text(), expectedText);
163
164 // Check for parsed search terms (would be displayed by the facetsWidget)
165 QCOMPARE(searchTerms.count(), expectedTerms.count());
166 for (int i = 0; i < expectedTerms.count(); i++) {
167 QCOMPARE(searchTerms.at(i), expectedTerms.at(i));
168 }
169
170 // Check for filename and content detection
171 QCOMPARE(query.hasContentSearch(), hasContent);
172 QCOMPARE(query.hasFileName(), hasFileName);
173 }
174
175 QTEST_MAIN(DolphinSearchBoxTest)
176
177 #include "dolphinquerytest.moc"