]>
cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinquerytest.cpp
1 /***************************************************************************
2 * Copyright (C) 2019 by Ismael Asensio <isma.af@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "search/dolphinquery.h"
24 #include <QJsonDocument>
25 #include <QJsonObject>
26 #include <QStringList>
30 class DolphinSearchBoxTest
: public QObject
35 void testBalooSearchParsing_data();
36 void testBalooSearchParsing();
40 * Defines the parameters for the test cases in testBalooSearchParsing()
42 void DolphinSearchBoxTest::testBalooSearchParsing_data()
44 const QString text
= QStringLiteral("abc");
45 const QString textS
= QStringLiteral("abc xyz");
46 const QString filename
= QStringLiteral("filename:\"%1\"").arg(text
);
47 const QString filenameS
= QStringLiteral("filename:\"%1\"").arg(textS
);
49 const QString rating
= QStringLiteral("rating>=2");
50 const QString modified
= QStringLiteral("modified>=2019-08-07");
52 const QString tag
= QStringLiteral("tag:tagA");
53 const QString tagS
= QStringLiteral("tag:\"tagB with spaces\""); // in search url
54 const QString tagR
= QStringLiteral("tag:tagB with spaces"); // in result term
56 QTest::addColumn
<QString
>("searchString");
57 QTest::addColumn
<QString
>("expectedText");
58 QTest::addColumn
<QStringList
>("expectedTerms");
59 QTest::addColumn
<bool>("hasContent");
60 QTest::addColumn
<bool>("hasFileName");
63 QTest::newRow("content") << text
<< text
<< QStringList() << true << false;
64 QTest::newRow("content/space") << textS
<< textS
<< QStringList() << true << false;
65 QTest::newRow("content/empty") << "" << "" << QStringList() << false << false;
66 QTest::newRow("content/single_quote") << "\"" << "\"" << QStringList() << true << false;
67 QTest::newRow("content/double_quote") << "\"\"" << "" << QStringList() << false << false;
69 // Test for "FileName"
70 QTest::newRow("filename") << filename
<< text
<< QStringList() << false << true;
71 QTest::newRow("filename/space") << filenameS
<< textS
<< QStringList() << false << true;
72 QTest::newRow("filename/empty") << "filename:" << "" << QStringList() << false << false;
73 QTest::newRow("filename/single_quote") << "filename:\"" << "\"" << QStringList() << false << true;
74 QTest::newRow("filename/double_quote") << "filename:\"\"" << "" << QStringList() << false << false;
76 // Combined content and filename search
77 QTest::newRow("content+filename")
78 << text
+ " " + filename
79 << text
+ " " + filename
<< QStringList() << true << true;
82 QTest::newRow("rating") << rating
<< "" << QStringList({rating
}) << false << false;
83 QTest::newRow("rating+content") << rating
+ " " + text
<< text
<< QStringList({rating
}) << true << false;
84 QTest::newRow("rating+filename") << rating
+ " " + filename
<< text
<< QStringList({rating
}) << false << true;
86 // Test for modified date
87 QTest::newRow("modified") << modified
<< "" << QStringList({modified
}) << false << false;
88 QTest::newRow("modified+content") << modified
+ " " + text
<< text
<< QStringList({modified
}) << true << false;
89 QTest::newRow("modified+filename") << modified
+ " " + filename
<< text
<< QStringList({modified
}) << false << true;
92 QTest::newRow("tag") << tag
<< "" << QStringList({tag
}) << false << false;
93 QTest::newRow("tag/space" ) << tagS
<< "" << QStringList({tagR
}) << false << false;
94 QTest::newRow("tag/double") << tag
+ " " + tagS
<< "" << QStringList({tag
, tagR
}) << false << false;
95 QTest::newRow("tag+content") << tag
+ " " + text
<< text
<< QStringList({tag
}) << true << false;
96 QTest::newRow("tag+filename") << tag
+ " " + filename
<< text
<< QStringList({tag
}) << false << true;
98 // Combined search terms
99 QTest::newRow("searchTerms")
100 << rating
+ " AND " + modified
+ " AND " + tag
+ " AND " + tagS
101 << "" << QStringList({modified
, rating
, tag
, tagR
}) << false << false;
103 QTest::newRow("searchTerms+content")
104 << rating
+ " AND " + modified
+ " " + text
+ " " + tag
+ " AND " + tagS
105 << text
<< QStringList({modified
, rating
, tag
, tagR
}) << true << false;
107 QTest::newRow("searchTerms+filename")
108 << rating
+ " AND " + modified
+ " " + filename
+ " " + tag
+ " AND " + tagS
109 << text
<< QStringList({modified
, rating
, tag
, tagR
}) << false << true;
111 QTest::newRow("allTerms")
112 << text
+ " " + filename
+ " " + rating
+ " AND " + modified
+ " AND " + tag
113 << text
+ " " + filename
<< QStringList({modified
, rating
, tag
}) << true << true;
115 QTest::newRow("allTerms/space")
116 << textS
+ " " + filenameS
+ " " + rating
+ " AND " + modified
+ " AND " + tagS
117 << textS
+ " " + filenameS
<< QStringList({modified
, rating
, tagR
}) << true << true;
121 * Helper function to compose the baloo query URL used for searching
123 QUrl
composeQueryUrl(const QString
& searchString
)
125 const QJsonObject jsonObject
{
126 {"searchString", searchString
}
129 const QJsonDocument
doc(jsonObject
);
130 const QString queryString
= QString::fromUtf8(doc
.toJson(QJsonDocument::Compact
));
133 urlQuery
.addQueryItem(QStringLiteral("json"), queryString
);
136 searchUrl
.setScheme(QLatin1String("baloosearch"));
137 searchUrl
.setQuery(urlQuery
);
143 * The test verifies whether the different terms of a Baloo search URL ("baloosearch:") are
144 * properly handled by the searchbox, and only "user" or filename terms are added to the
145 * text bar of the searchbox.
147 void DolphinSearchBoxTest::testBalooSearchParsing()
149 QFETCH(QString
, searchString
);
150 QFETCH(QString
, expectedText
);
151 QFETCH(QStringList
, expectedTerms
);
152 QFETCH(bool, hasContent
);
153 QFETCH(bool, hasFileName
);
155 const QUrl testUrl
= composeQueryUrl(searchString
);
156 const DolphinQuery query
= DolphinQuery::fromBalooSearchUrl(testUrl
);
158 QStringList searchTerms
= query
.searchTerms();
161 // Check for parsed text (would be displayed on the input search bar)
162 QCOMPARE(query
.text(), expectedText
);
164 // Check for parsed search terms (would be displayed by the facetsWidget)
165 QCOMPARE(searchTerms
.count(), expectedTerms
.count());
166 for (int i
= 0; i
< expectedTerms
.count(); i
++) {
167 QCOMPARE(searchTerms
.at(i
), expectedTerms
.at(i
));
170 // Check for filename and content detection
171 QCOMPARE(query
.hasContentSearch(), hasContent
);
172 QCOMPARE(query
.hasFileName(), hasFileName
);
175 QTEST_MAIN(DolphinSearchBoxTest
)
177 #include "dolphinquerytest.moc"