]>
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 <QStringList>
14 #include <QStandardPaths>
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
{
34 {"searchString", searchString
}
37 const QJsonDocument
doc(jsonObject
);
38 const QString queryString
= QString::fromUtf8(doc
.toJson(QJsonDocument::Compact
));
41 urlQuery
.addQueryItem(QStringLiteral("json"), queryString
);
44 searchUrl
.setScheme(QLatin1String("baloosearch"));
45 searchUrl
.setQuery(urlQuery
);
50 void DolphinQueryTest::initTestCase()
52 QStandardPaths::setTestModeEnabled(true);
56 * Defines the parameters for the test cases in testBalooSearchParsing()
58 void DolphinQueryTest::testBalooSearchParsing_data()
61 QTest::addColumn
<QUrl
>("searchUrl");
62 QTest::addColumn
<QString
>("expectedText");
63 QTest::addColumn
<QStringList
>("expectedTerms");
64 QTest::addColumn
<bool>("hasContent");
65 QTest::addColumn
<bool>("hasFileName");
67 const QString text
= QStringLiteral("abc");
68 const QString textS
= QStringLiteral("abc xyz");
69 const QString textQ
= QStringLiteral("\"abc xyz\"");
70 const QString textM
= QStringLiteral("\"abc xyz\" tuv");
72 const QString filename
= QStringLiteral("filename:\"%1\"").arg(text
);
73 const QString filenameS
= QStringLiteral("filename:\"%1\"").arg(textS
);
74 const QString filenameQ
= QStringLiteral("filename:\"%1\"").arg(textQ
);
75 const QString filenameM
= QStringLiteral("filename:\"%1\"").arg(textM
);
77 const QString rating
= QStringLiteral("rating>=2");
78 const QString modified
= QStringLiteral("modified>=2019-08-07");
80 const QString tag
= QStringLiteral("tag:tagA");
81 const QString tagS
= QStringLiteral("tag:\"tagB with spaces\""); // in search url
82 const QString tagR
= QStringLiteral("tag:tagB with spaces"); // in result term
85 QTest::newRow("content") << balooQueryUrl(text
) << text
<< QStringList() << true << false;
86 QTest::newRow("content/space") << balooQueryUrl(textS
) << textS
<< QStringList() << true << false;
87 QTest::newRow("content/quoted") << balooQueryUrl(textQ
) << textS
<< QStringList() << true << false;
88 QTest::newRow("content/empty") << balooQueryUrl("") << "" << QStringList() << false << false;
89 QTest::newRow("content/single_quote") << balooQueryUrl("\"") << "\"" << QStringList() << true << false;
90 QTest::newRow("content/double_quote") << balooQueryUrl("\"\"") << "" << QStringList() << false << false;
92 // Test for "FileName"
93 QTest::newRow("filename") << balooQueryUrl(filename
) << text
<< QStringList() << false << true;
94 QTest::newRow("filename/space") << balooQueryUrl(filenameS
) << textS
<< QStringList() << false << true;
95 QTest::newRow("filename/quoted") << balooQueryUrl(filenameQ
) << textQ
<< QStringList() << false << true;
96 QTest::newRow("filename/mixed") << balooQueryUrl(filenameM
) << textM
<< QStringList() << false << true;
97 QTest::newRow("filename/empty") << balooQueryUrl("filename:") << "" << QStringList() << false << false;
98 QTest::newRow("filename/single_quote") << balooQueryUrl("filename:\"") << "\"" << QStringList() << false << true;
99 QTest::newRow("filename/double_quote") << balooQueryUrl("filename:\"\"") << "" << QStringList() << false << false;
101 // Combined content and filename search
102 QTest::newRow("content+filename")
103 << balooQueryUrl(text
+ " " + filename
)
104 << text
+ " " + filename
<< QStringList() << true << true;
106 QTest::newRow("content+filename/quoted")
107 << balooQueryUrl(textQ
+ " " + filenameQ
)
108 << textS
+ " " + filenameQ
<< QStringList() << true << true;
111 QTest::newRow("rating") << balooQueryUrl(rating
) << "" << QStringList({rating
}) << false << false;
112 QTest::newRow("rating+content") << balooQueryUrl(rating
+ " " + text
) << text
<< QStringList({rating
}) << true << false;
113 QTest::newRow("rating+filename") << balooQueryUrl(rating
+ " " + filename
) << text
<< QStringList({rating
}) << false << true;
115 // Test for modified date
116 QTest::newRow("modified") << balooQueryUrl(modified
) << "" << QStringList({modified
}) << false << false;
117 QTest::newRow("modified+content") << balooQueryUrl(modified
+ " " + text
) << text
<< QStringList({modified
}) << true << false;
118 QTest::newRow("modified+filename") << balooQueryUrl(modified
+ " " + filename
) << text
<< QStringList({modified
}) << false << true;
121 QTest::newRow("tag") << balooQueryUrl(tag
) << "" << QStringList({tag
}) << false << false;
122 QTest::newRow("tag/space" ) << balooQueryUrl(tagS
) << "" << QStringList({tagR
}) << false << false;
123 QTest::newRow("tag/double") << balooQueryUrl(tag
+ " " + tagS
) << "" << QStringList({tag
, tagR
}) << false << false;
124 QTest::newRow("tag+content") << balooQueryUrl(tag
+ " " + text
) << text
<< QStringList({tag
}) << true << false;
125 QTest::newRow("tag+filename") << balooQueryUrl(tag
+ " " + filename
) << text
<< QStringList({tag
}) << false << true;
127 // Combined search terms
128 QTest::newRow("searchTerms")
129 << balooQueryUrl(rating
+ " AND " + modified
+ " AND " + tag
+ " AND " + tagS
)
130 << "" << QStringList({modified
, rating
, tag
, tagR
}) << false << false;
132 QTest::newRow("searchTerms+content")
133 << balooQueryUrl(rating
+ " AND " + modified
+ " " + text
+ " " + tag
+ " AND " + tagS
)
134 << text
<< QStringList({modified
, rating
, tag
, tagR
}) << true << false;
136 QTest::newRow("searchTerms+filename")
137 << balooQueryUrl(rating
+ " AND " + modified
+ " " + filename
+ " " + tag
+ " AND " + tagS
)
138 << text
<< QStringList({modified
, rating
, tag
, tagR
}) << false << true;
140 QTest::newRow("allTerms")
141 << balooQueryUrl(text
+ " " + filename
+ " " + rating
+ " AND " + modified
+ " AND " + tag
)
142 << text
+ " " + filename
<< QStringList({modified
, rating
, tag
}) << true << true;
144 QTest::newRow("allTerms/space")
145 << balooQueryUrl(textS
+ " " + filenameS
+ " " + rating
+ " AND " + modified
+ " AND " + tagS
)
146 << textS
+ " " + filenameS
<< QStringList({modified
, rating
, tagR
}) << true << true;
148 // Test tags:/ URL scheme
149 const auto tagUrl
= [](const QString
&tag
) { return QUrl(QStringLiteral("tags:/%1/").arg(tag
)); };
150 const auto tagTerms
= [](const QString
&tag
) { return QStringList
{QStringLiteral("tag:%1").arg(tag
)}; };
152 QTest::newRow("tagsUrl") << tagUrl("tagA") << "" << tagTerms("tagA") << false << false;
153 QTest::newRow("tagsUrl/space") << tagUrl("tagB with spaces") << "" << tagTerms("tagB with spaces") << false << false;
154 QTest::newRow("tagsUrl/hash") << tagUrl("tagC#hash") << "" << tagTerms("tagC#hash") << false << false;
155 QTest::newRow("tagsUrl/slash") << tagUrl("tagD/with/slash") << "" << tagTerms("tagD/with/slash") << false << false;
159 * The test verifies whether the different terms search URL (e.g. "baloosearch:/") are
160 * properly handled by the searchbox, and only "user" or filename terms are added to the
161 * text bar of the searchbox.
163 void DolphinQueryTest::testBalooSearchParsing()
165 QFETCH(QUrl
, searchUrl
);
166 QFETCH(QString
, expectedText
);
167 QFETCH(QStringList
, expectedTerms
);
168 QFETCH(bool, hasContent
);
169 QFETCH(bool, hasFileName
);
171 const DolphinQuery query
= DolphinQuery::fromSearchUrl(searchUrl
);
173 // Checkt that the URL is supported
174 QVERIFY(DolphinQuery::supportsScheme(searchUrl
.scheme()));
176 // Check for parsed text (would be displayed on the input search bar)
177 QCOMPARE(query
.text(), expectedText
);
179 // Check for parsed search terms (would be displayed by the facetsWidget)
180 QStringList searchTerms
= query
.searchTerms();
183 QCOMPARE(searchTerms
.count(), expectedTerms
.count());
184 for (int i
= 0; i
< expectedTerms
.count(); i
++) {
185 QCOMPARE(searchTerms
.at(i
), expectedTerms
.at(i
));
188 // Check for filename and content detection
189 QCOMPARE(query
.hasContentSearch(), hasContent
);
190 QCOMPARE(query
.hasFileName(), hasFileName
);
193 QTEST_MAIN(DolphinQueryTest
)
195 #include "dolphinquerytest.moc"