]>
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 xyz");
45 const QString filename
= QStringLiteral("filename:\"%1\"").arg(text
);
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");
51 QTest::addColumn
<QString
>("searchString");
52 QTest::addColumn
<QString
>("expectedText");
53 QTest::addColumn
<QStringList
>("expectedTerms");
54 QTest::addColumn
<bool>("hasContent");
55 QTest::addColumn
<bool>("hasFileName");
58 QTest::newRow("content") << text
<< text
<< QStringList() << true << false;
59 QTest::newRow("content/empty") << "" << "" << QStringList() << false << false;
60 QTest::newRow("content/singleQuote") << "\"" << "" << QStringList() << false << false;
61 QTest::newRow("content/doubleQuote") << "\"\"" << "" << QStringList() << false << false;
63 // Test for "FileName"
64 QTest::newRow("filename") << filename
<< text
<< QStringList() << false << true;
65 QTest::newRow("filename/empty") << "filename:" << "" << QStringList() << false << false;
66 QTest::newRow("filename/singleQuote") << "filename:\"" << "" << QStringList() << false << false;
67 QTest::newRow("filename/doubleQuote") << "filename:\"\"" << "" << QStringList() << false << false;
69 // Combined content and filename search
70 QTest::newRow("content+filename") << text
+ " " + filename
<< text
+ " " + filename
<< QStringList() << true << true;
73 QTest::newRow("rating") << rating
<< "" << QStringList({rating
}) << false << false;
74 QTest::newRow("rating+content") << rating
+ " " + text
<< text
<< QStringList({rating
}) << true << false;
75 QTest::newRow("rating+filename") << rating
+ " " + filename
<< text
<< QStringList({rating
}) << false << true;
77 // Test for modified date
78 QTest::newRow("modified") << modified
<< "" << QStringList({modified
}) << false << false;
79 QTest::newRow("modified+content") << modified
+ " " + text
<< text
<< QStringList({modified
}) << true << false;
80 QTest::newRow("modified+filename") << modified
+ " " + filename
<< text
<< QStringList({modified
}) << false << true;
83 QTest::newRow("tag") << tagA
<< "" << QStringList({tagA
}) << false << false;
84 QTest::newRow("tag/double") << tagA
+ " " + tagB
<< "" << QStringList({tagA
, tagB
}) << false << false;
85 QTest::newRow("tag+content") << tagA
+ " " + text
<< text
<< QStringList({tagA
}) << true << false;
86 QTest::newRow("tag+filename") << tagA
+ " " + filename
<< text
<< QStringList({tagA
}) << false << true;
88 // Combined search terms
89 QTest::newRow("allTerms")
90 << rating
+ " AND " + modified
+ " AND " + tagA
+ " AND " + tagB
91 << "" << QStringList({modified
, rating
, tagA
, tagB
}) << false << false;
93 QTest::newRow("allTerms+content")
94 << rating
+ " AND " + modified
+ " " + text
+ " " + tagA
+ " AND " + tagB
95 << text
<< QStringList({modified
, rating
, tagA
, tagB
}) << true << false;
97 QTest::newRow("allTerms+filename")
98 << rating
+ " AND " + modified
+ " " + filename
+ " " + tagA
+ " AND " + tagB
99 << text
<< QStringList({modified
, rating
, tagA
, tagB
}) << false << true;
101 QTest::newRow("allTerms+content+filename")
102 << text
+ " " + filename
+ " " + rating
+ " AND " + modified
+ " AND " + tagA
+ " AND " + tagB
103 << text
+ " " + filename
<< QStringList({modified
, rating
, tagA
, tagB
}) << true << true;
107 * Helper function to compose the baloo query URL used for searching
109 QUrl
composeQueryUrl(const QString
& searchString
)
111 const QJsonObject jsonObject
{
112 {"searchString", searchString
}
115 const QJsonDocument
doc(jsonObject
);
116 const QString queryString
= QString::fromUtf8(doc
.toJson(QJsonDocument::Compact
));
119 urlQuery
.addQueryItem(QStringLiteral("json"), queryString
);
122 searchUrl
.setScheme(QLatin1String("baloosearch"));
123 searchUrl
.setQuery(urlQuery
);
129 * The test verifies whether the different terms of a Baloo search URL ("baloosearch:") are
130 * properly handled by the searchbox, and only "user" or filename terms are added to the
131 * text bar of the searchbox.
133 void DolphinSearchBoxTest::testBalooSearchParsing()
135 QFETCH(QString
, searchString
);
136 QFETCH(QString
, expectedText
);
137 QFETCH(QStringList
, expectedTerms
);
138 QFETCH(bool, hasContent
);
139 QFETCH(bool, hasFileName
);
141 const QUrl testUrl
= composeQueryUrl(searchString
);
142 const DolphinQuery query
= DolphinQuery::fromBalooSearchUrl(testUrl
);
144 QStringList searchTerms
= query
.searchTerms();
147 // Check for parsed text (would be displayed on the input search bar)
148 QCOMPARE(query
.text(), expectedText
);
150 // Check for parsed search terms (would be displayed by the facetsWidget)
151 QCOMPARE(searchTerms
.count(), expectedTerms
.count());
152 for (int i
= 0; i
< expectedTerms
.count(); i
++) {
153 QCOMPARE(searchTerms
.at(i
), expectedTerms
.at(i
));
156 // Check for filename and content detection
157 QCOMPARE(query
.hasContentSearch(), hasContent
);
158 QCOMPARE(query
.hasFileName(), hasFileName
);
161 QTEST_MAIN(DolphinSearchBoxTest
)
163 #include "dolphinquerytest.moc"