]>
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("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");
51 QTest::addColumn
<QString
>("searchString");
52 QTest::addColumn
<QString
>("expectedText");
53 QTest::addColumn
<QStringList
>("expectedTerms");
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();
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();
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
});
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
});
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
});
84 QTest::newRow("rating+modified")
85 << rating
+ " AND " + modified
86 << "" << QStringList({modified
, rating
});
88 QTest::newRow("allTerms")
89 << rating
+ " AND " + modified
+ " AND " + tagA
+ " AND " + tagB
90 << "" << QStringList({modified
, rating
, tagA
, tagB
});
92 QTest::newRow("allTerms+content")
93 << rating
+ " AND " + modified
+ " " + text
+ " " + tagA
+ " AND " + tagB
94 << text
<< QStringList({modified
, rating
, tagA
, tagB
});
96 QTest::newRow("allTerms+filename")
97 << rating
+ " AND " + modified
+ " " + filename
+ " " + tagA
+ " AND " + tagB
98 << text
<< QStringList({modified
, rating
, tagA
, tagB
});
102 * Helper function to compose the baloo query URL used for searching
104 QUrl
composeQueryUrl(const QString
& searchString
)
106 const QJsonObject jsonObject
{
107 {"searchString", searchString
}
110 const QJsonDocument
doc(jsonObject
);
111 const QString queryString
= QString::fromUtf8(doc
.toJson(QJsonDocument::Compact
));
114 urlQuery
.addQueryItem(QStringLiteral("json"), queryString
);
117 searchUrl
.setScheme(QLatin1String("baloosearch"));
118 searchUrl
.setQuery(urlQuery
);
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.
128 void DolphinSearchBoxTest::testBalooSearchParsing()
130 QFETCH(QString
, searchString
);
131 QFETCH(QString
, expectedText
);
132 QFETCH(QStringList
, expectedTerms
);
134 const QUrl testUrl
= composeQueryUrl(searchString
);
135 const DolphinQuery query
= DolphinQuery::fromBalooSearchUrl(testUrl
);
137 QStringList searchTerms
= query
.searchTerms();
140 // Check for parsed text (would be displayed on the input search bar)
141 QCOMPARE(query
.text(), expectedText
);
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
));
150 QTEST_MAIN(DolphinSearchBoxTest
)
152 #include "dolphinquerytest.moc"