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