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