]>
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");
49 QTest::addColumn
<QString
>("searchString");
50 QTest::addColumn
<QString
>("expectedText");
51 QTest::addColumn
<QStringList
>("expectedTerms");
54 QTest::newRow("content") << text
<< text
<< QStringList();
55 QTest::newRow("content/empty") << "" << "" << QStringList();
56 QTest::newRow("content/singleQuote") << "\"" << "" << QStringList();
57 QTest::newRow("content/doubleQuote") << "\"\"" << "" << QStringList();
58 // Test for empty `filename`
59 QTest::newRow("filename") << filename
<< text
<< QStringList();
60 QTest::newRow("filename/empty") << "filename:" << "" << QStringList();
61 QTest::newRow("filename/singleQuote") << "filename:\"" << "" << QStringList();
62 QTest::newRow("filename/doubleQuote") << "filename:\"\"" << "" << QStringList();
65 QTest::newRow("rating") << rating
<< "" << QStringList({rating
});
66 QTest::newRow("rating+content") << rating
+ " " + text
<< text
<< QStringList({rating
});
67 QTest::newRow("rating+filename") << rating
+ " " + filename
<< text
<< QStringList({rating
});
68 // Test for modified date
69 QTest::newRow("modified") << modified
<< "" << QStringList({modified
});
70 QTest::newRow("modified+content") << modified
+ " " + text
<< text
<< QStringList({modified
});
71 QTest::newRow("modified+filename") << modified
+ " " + filename
<< text
<< QStringList({modified
});
73 QTest::newRow("rating+modified") << rating
+ " AND " + modified
<< "" << QStringList({modified
, rating
});
74 QTest::newRow("rating+modified+content") << rating
+ " AND " + modified
+ " " + text
<< text
<< QStringList({modified
, rating
});
75 QTest::newRow("rating+modified+filename") << rating
+ " AND " + modified
+ " " + filename
<< text
<< QStringList({modified
, rating
});
79 * Helper function to compose the baloo query URL used for searching
81 QUrl
composeQueryUrl(const QString
& searchString
)
83 const QJsonObject jsonObject
{
84 {"searchString", searchString
}
87 const QJsonDocument
doc(jsonObject
);
88 const QString queryString
= QString::fromUtf8(doc
.toJson(QJsonDocument::Compact
));
91 urlQuery
.addQueryItem(QStringLiteral("json"), queryString
);
94 searchUrl
.setScheme(QLatin1String("baloosearch"));
95 searchUrl
.setQuery(urlQuery
);
101 * The test verifies whether the different terms of a Baloo search URL ("baloosearch:") are
102 * properly handled by the searchbox, and only "user" or filename terms are added to the
103 * text bar of the searchbox.
105 void DolphinSearchBoxTest::testBalooSearchParsing()
107 QFETCH(QString
, searchString
);
108 QFETCH(QString
, expectedText
);
109 QFETCH(QStringList
, expectedTerms
);
111 const QUrl testUrl
= composeQueryUrl(searchString
);
112 const DolphinQuery query
= DolphinQuery::fromBalooSearchUrl(testUrl
);
114 QStringList searchTerms
= query
.searchTerms();
117 // FIXME: Current parsing bugs
118 QEXPECT_FAIL("content/singleQuote", "Quotes around text are shown", Continue
);
119 QEXPECT_FAIL("content/doubleQuote", "Quotes around text are shown", Continue
);
121 QEXPECT_FAIL("filename", "Quotes around text are shown", Continue
);
122 QEXPECT_FAIL("filename/singleQuote", "Quotes around text are shown", Continue
);
123 QEXPECT_FAIL("filename/doubleQuote", "Quotes around text are shown", Continue
);
125 QEXPECT_FAIL("rating" , "Text includes also search terms", Continue
);
126 QEXPECT_FAIL("rating+content" , "Text includes also search terms", Continue
);
127 QEXPECT_FAIL("rating+filename" , "Text includes also search terms", Continue
);
128 QEXPECT_FAIL("modified" , "Text includes also search terms", Continue
);
129 QEXPECT_FAIL("modified+content" , "Text includes also search terms", Continue
);
130 QEXPECT_FAIL("modified+filename" , "Text includes also search terms", Continue
);
131 QEXPECT_FAIL("rating+modified" , "Text includes also search terms", Continue
);
132 QEXPECT_FAIL("rating+modified+content" , "Text includes also search terms", Continue
);
133 QEXPECT_FAIL("rating+modified+filename", "Text includes also search terms", Continue
);
135 // Check for parsed text (would be displayed on the input search bar)
136 QCOMPARE(query
.text(), expectedText
);
138 // Check for parsed search terms (would be displayed by the facetsWidget)
139 QCOMPARE(searchTerms
.count(), expectedTerms
.count());
140 for (int i
= 0; i
< expectedTerms
.count(); i
++) {
141 QCOMPARE(searchTerms
.at(i
), expectedTerms
.at(i
));
145 QTEST_MAIN(DolphinSearchBoxTest
)
147 #include "dolphinquerytest.moc"