]> 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 xyz");
45 const QString filename = QStringLiteral("filename:\"%1\"").arg(text);
46 const QString rating = QStringLiteral("rating>=2");
47 const QString modified = QString("modified>=2019-08-07");
48 const QString tagA = QString("tag:tagA");
49 const QString tagB = QString("tag:tagB");
50
51 QTest::addColumn<QString>("searchString");
52 QTest::addColumn<QString>("expectedText");
53 QTest::addColumn<QStringList>("expectedTerms");
54 QTest::addColumn<bool>("hasContent");
55 QTest::addColumn<bool>("hasFileName");
56
57 // Test for "Content"
58 QTest::newRow("content") << text << text << QStringList() << true << false;
59 QTest::newRow("content/empty") << "" << "" << QStringList() << false << false;
60 QTest::newRow("content/singleQuote") << "\"" << "" << QStringList() << false << false;
61 QTest::newRow("content/doubleQuote") << "\"\"" << "" << QStringList() << false << false;
62
63 // Test for "FileName"
64 QTest::newRow("filename") << filename << text << QStringList() << false << true;
65 QTest::newRow("filename/empty") << "filename:" << "" << QStringList() << false << false;
66 QTest::newRow("filename/singleQuote") << "filename:\"" << "" << QStringList() << false << false;
67 QTest::newRow("filename/doubleQuote") << "filename:\"\"" << "" << QStringList() << false << false;
68
69 // Combined content and filename search
70 QTest::newRow("content+filename") << text + " " + filename << text + " " + filename << QStringList() << true << true;
71
72 // Test for rating
73 QTest::newRow("rating") << rating << "" << QStringList({rating}) << false << false;
74 QTest::newRow("rating+content") << rating + " " + text << text << QStringList({rating}) << true << false;
75 QTest::newRow("rating+filename") << rating + " " + filename << text << QStringList({rating}) << false << true;
76
77 // Test for modified date
78 QTest::newRow("modified") << modified << "" << QStringList({modified}) << false << false;
79 QTest::newRow("modified+content") << modified + " " + text << text << QStringList({modified}) << true << false;
80 QTest::newRow("modified+filename") << modified + " " + filename << text << QStringList({modified}) << false << true;
81
82 // Test for tags
83 QTest::newRow("tag") << tagA << "" << QStringList({tagA}) << false << false;
84 QTest::newRow("tag/double") << tagA + " " + tagB << "" << QStringList({tagA, tagB}) << false << false;
85 QTest::newRow("tag+content") << tagA + " " + text << text << QStringList({tagA}) << true << false;
86 QTest::newRow("tag+filename") << tagA + " " + filename << text << QStringList({tagA}) << false << true;
87
88 // Combined search terms
89 QTest::newRow("allTerms")
90 << rating + " AND " + modified + " AND " + tagA + " AND " + tagB
91 << "" << QStringList({modified, rating, tagA, tagB}) << false << false;
92
93 QTest::newRow("allTerms+content")
94 << rating + " AND " + modified + " " + text + " " + tagA + " AND " + tagB
95 << text << QStringList({modified, rating, tagA, tagB}) << true << false;
96
97 QTest::newRow("allTerms+filename")
98 << rating + " AND " + modified + " " + filename + " " + tagA + " AND " + tagB
99 << text << QStringList({modified, rating, tagA, tagB}) << false << true;
100
101 QTest::newRow("allTerms+content+filename")
102 << text + " " + filename + " " + rating + " AND " + modified + " AND " + tagA + " AND " + tagB
103 << text + " " + filename << QStringList({modified, rating, tagA, tagB}) << true << true;
104 }
105
106 /**
107 * Helper function to compose the baloo query URL used for searching
108 */
109 QUrl composeQueryUrl(const QString& searchString)
110 {
111 const QJsonObject jsonObject {
112 {"searchString", searchString}
113 };
114
115 const QJsonDocument doc(jsonObject);
116 const QString queryString = QString::fromUtf8(doc.toJson(QJsonDocument::Compact));
117
118 QUrlQuery urlQuery;
119 urlQuery.addQueryItem(QStringLiteral("json"), queryString);
120
121 QUrl searchUrl;
122 searchUrl.setScheme(QLatin1String("baloosearch"));
123 searchUrl.setQuery(urlQuery);
124
125 return searchUrl;
126 }
127
128 /**
129 * The test verifies whether the different terms of a Baloo search URL ("baloosearch:") are
130 * properly handled by the searchbox, and only "user" or filename terms are added to the
131 * text bar of the searchbox.
132 */
133 void DolphinSearchBoxTest::testBalooSearchParsing()
134 {
135 QFETCH(QString, searchString);
136 QFETCH(QString, expectedText);
137 QFETCH(QStringList, expectedTerms);
138 QFETCH(bool, hasContent);
139 QFETCH(bool, hasFileName);
140
141 const QUrl testUrl = composeQueryUrl(searchString);
142 const DolphinQuery query = DolphinQuery::fromBalooSearchUrl(testUrl);
143
144 QStringList searchTerms = query.searchTerms();
145 searchTerms.sort();
146
147 // Check for parsed text (would be displayed on the input search bar)
148 QCOMPARE(query.text(), expectedText);
149
150 // Check for parsed search terms (would be displayed by the facetsWidget)
151 QCOMPARE(searchTerms.count(), expectedTerms.count());
152 for (int i = 0; i < expectedTerms.count(); i++) {
153 QCOMPARE(searchTerms.at(i), expectedTerms.at(i));
154 }
155
156 // Check for filename and content detection
157 QCOMPARE(query.hasContentSearch(), hasContent);
158 QCOMPARE(query.hasFileName(), hasFileName);
159 }
160
161 QTEST_MAIN(DolphinSearchBoxTest)
162
163 #include "dolphinquerytest.moc"