]>
cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinquerytest.cpp
2 * SPDX-FileCopyrightText: 2019 Ismael Asensio <isma.af@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "search/dolphinquery.h"
11 #include <QJsonDocument>
12 #include <QJsonObject>
13 #include <QStandardPaths>
14 #include <QStringList>
18 class DolphinQueryTest
: public QObject
24 void testBalooSearchParsing_data();
25 void testBalooSearchParsing();
29 * Helper function to compose the baloo query URL used for searching
31 QUrl
balooQueryUrl(const QString
&searchString
)
33 const QJsonObject jsonObject
{{"searchString", searchString
}};
35 const QJsonDocument
doc(jsonObject
);
36 const QString queryString
= QString::fromUtf8(doc
.toJson(QJsonDocument::Compact
));
39 urlQuery
.addQueryItem(QStringLiteral("json"), queryString
);
42 searchUrl
.setScheme(QLatin1String("baloosearch"));
43 searchUrl
.setQuery(urlQuery
);
48 void DolphinQueryTest::initTestCase()
50 QStandardPaths::setTestModeEnabled(true);
54 * Defines the parameters for the test cases in testBalooSearchParsing()
56 void DolphinQueryTest::testBalooSearchParsing_data()
58 QTest::addColumn
<QUrl
>("searchUrl");
59 QTest::addColumn
<QString
>("expectedText");
60 QTest::addColumn
<QStringList
>("expectedTerms");
61 QTest::addColumn
<bool>("hasContent");
62 QTest::addColumn
<bool>("hasFileName");
64 const QString text
= QStringLiteral("abc");
65 const QString textS
= QStringLiteral("abc xyz");
66 const QString textQ
= QStringLiteral("\"abc xyz\"");
67 const QString textM
= QStringLiteral("\"abc xyz\" tuv");
69 const QString filename
= QStringLiteral("filename:\"%1\"").arg(text
);
70 const QString filenameS
= QStringLiteral("filename:\"%1\"").arg(textS
);
71 const QString filenameQ
= QStringLiteral("filename:\"%1\"").arg(textQ
);
72 const QString filenameM
= QStringLiteral("filename:\"%1\"").arg(textM
);
74 const QString rating
= QStringLiteral("rating>=2");
75 const QString modified
= QStringLiteral("modified>=2019-08-07");
77 const QString tag
= QStringLiteral("tag:tagA");
78 const QString tagS
= QStringLiteral("tag:\"tagB with spaces\""); // in search url
79 const QString tagR
= QStringLiteral("tag:tagB with spaces"); // in result term
82 QTest::newRow("content") << balooQueryUrl(text
) << text
<< QStringList() << true << false;
83 QTest::newRow("content/space") << balooQueryUrl(textS
) << textS
<< QStringList() << true << false;
84 QTest::newRow("content/quoted") << balooQueryUrl(textQ
) << textS
<< QStringList() << true << false;
85 QTest::newRow("content/empty") << balooQueryUrl("") << "" << QStringList() << false << false;
86 QTest::newRow("content/single_quote") << balooQueryUrl("\"") << "\"" << QStringList() << true << false;
87 QTest::newRow("content/double_quote") << balooQueryUrl("\"\"") << "" << QStringList() << false << false;
89 // Test for "FileName"
90 QTest::newRow("filename") << balooQueryUrl(filename
) << text
<< QStringList() << false << true;
91 QTest::newRow("filename/space") << balooQueryUrl(filenameS
) << textS
<< QStringList() << false << true;
92 QTest::newRow("filename/quoted") << balooQueryUrl(filenameQ
) << textQ
<< QStringList() << false << true;
93 QTest::newRow("filename/mixed") << balooQueryUrl(filenameM
) << textM
<< QStringList() << false << true;
94 QTest::newRow("filename/empty") << balooQueryUrl("filename:") << "" << QStringList() << false << false;
95 QTest::newRow("filename/single_quote") << balooQueryUrl("filename:\"") << "\"" << QStringList() << false << true;
96 QTest::newRow("filename/double_quote") << balooQueryUrl("filename:\"\"") << "" << QStringList() << false << false;
98 // Combined content and filename search
99 QTest::newRow("content+filename") << balooQueryUrl(text
+ " " + filename
) << text
+ " " + filename
<< QStringList() << true << true;
101 QTest::newRow("content+filename/quoted") << balooQueryUrl(textQ
+ " " + filenameQ
) << textS
+ " " + filenameQ
<< QStringList() << true << true;
104 QTest::newRow("rating") << balooQueryUrl(rating
) << "" << QStringList({rating
}) << false << false;
105 QTest::newRow("rating+content") << balooQueryUrl(rating
+ " " + text
) << text
<< QStringList({rating
}) << true << false;
106 QTest::newRow("rating+filename") << balooQueryUrl(rating
+ " " + filename
) << text
<< QStringList({rating
}) << false << true;
108 // Test for modified date
109 QTest::newRow("modified") << balooQueryUrl(modified
) << "" << QStringList({modified
}) << false << false;
110 QTest::newRow("modified+content") << balooQueryUrl(modified
+ " " + text
) << text
<< QStringList({modified
}) << true << false;
111 QTest::newRow("modified+filename") << balooQueryUrl(modified
+ " " + filename
) << text
<< QStringList({modified
}) << false << true;
114 QTest::newRow("tag") << balooQueryUrl(tag
) << "" << QStringList({tag
}) << false << false;
115 QTest::newRow("tag/space") << balooQueryUrl(tagS
) << "" << QStringList({tagR
}) << false << false;
116 QTest::newRow("tag/double") << balooQueryUrl(tag
+ " " + tagS
) << "" << QStringList({tag
, tagR
}) << false << false;
117 QTest::newRow("tag+content") << balooQueryUrl(tag
+ " " + text
) << text
<< QStringList({tag
}) << true << false;
118 QTest::newRow("tag+filename") << balooQueryUrl(tag
+ " " + filename
) << text
<< QStringList({tag
}) << false << true;
120 // Combined search terms
121 QTest::newRow("searchTerms") << balooQueryUrl(rating
+ " AND " + modified
+ " AND " + tag
+ " AND " + tagS
) << ""
122 << QStringList({modified
, rating
, tag
, tagR
}) << false << false;
124 QTest::newRow("searchTerms+content") << balooQueryUrl(rating
+ " AND " + modified
+ " " + text
+ " " + tag
+ " AND " + tagS
) << text
125 << QStringList({modified
, rating
, tag
, tagR
}) << true << false;
127 QTest::newRow("searchTerms+filename") << balooQueryUrl(rating
+ " AND " + modified
+ " " + filename
+ " " + tag
+ " AND " + tagS
) << text
128 << QStringList({modified
, rating
, tag
, tagR
}) << false << true;
130 QTest::newRow("allTerms") << balooQueryUrl(text
+ " " + filename
+ " " + rating
+ " AND " + modified
+ " AND " + tag
) << text
+ " " + filename
131 << QStringList({modified
, rating
, tag
}) << true << true;
133 QTest::newRow("allTerms/space") << balooQueryUrl(textS
+ " " + filenameS
+ " " + rating
+ " AND " + modified
+ " AND " + tagS
) << textS
+ " " + filenameS
134 << QStringList({modified
, rating
, tagR
}) << true << true;
136 // Test tags:/ URL scheme
137 const auto tagUrl
= [](const QString
&tag
) {
138 return QUrl(QStringLiteral("tags:/%1/").arg(tag
));
140 const auto tagTerms
= [](const QString
&tag
) {
141 return QStringList
{QStringLiteral("tag:%1").arg(tag
)};
144 QTest::newRow("tagsUrl") << tagUrl("tagA") << "" << tagTerms("tagA") << false << false;
145 QTest::newRow("tagsUrl/space") << tagUrl("tagB with spaces") << "" << tagTerms("tagB with spaces") << false << false;
146 QTest::newRow("tagsUrl/hash") << tagUrl("tagC#hash") << "" << tagTerms("tagC#hash") << false << false;
147 QTest::newRow("tagsUrl/slash") << tagUrl("tagD/with/slash") << "" << tagTerms("tagD/with/slash") << false << false;
151 * The test verifies whether the different terms search URL (e.g. "baloosearch:/") are
152 * properly handled by the searchbox, and only "user" or filename terms are added to the
153 * text bar of the searchbox.
155 void DolphinQueryTest::testBalooSearchParsing()
157 QFETCH(QUrl
, searchUrl
);
158 QFETCH(QString
, expectedText
);
159 QFETCH(QStringList
, expectedTerms
);
160 QFETCH(bool, hasContent
);
161 QFETCH(bool, hasFileName
);
163 const DolphinQuery query
= DolphinQuery::fromSearchUrl(searchUrl
);
165 // Checkt that the URL is supported
166 QVERIFY(DolphinQuery::supportsScheme(searchUrl
.scheme()));
168 // Check for parsed text (would be displayed on the input search bar)
169 QCOMPARE(query
.text(), expectedText
);
171 // Check for parsed search terms (would be displayed by the facetsWidget)
172 QStringList searchTerms
= query
.searchTerms();
175 QCOMPARE(searchTerms
.count(), expectedTerms
.count());
176 for (int i
= 0; i
< expectedTerms
.count(); i
++) {
177 QCOMPARE(searchTerms
.at(i
), expectedTerms
.at(i
));
180 // Check for filename and content detection
181 QCOMPARE(query
.hasContentSearch(), hasContent
);
182 QCOMPARE(query
.hasFileName(), hasFileName
);
185 QTEST_MAIN(DolphinQueryTest
)
187 #include "dolphinquerytest.moc"