#include <gtest/gtest.h>
#include <QtDebug>
#include <QDir>
#include "test/librarytest.h"
#include "library/searchqueryparser.h"
#include "util/assert.h"
class SearchQueryParserTest : public LibraryTest {
protected:
SearchQueryParserTest()
: m_parser(collection()) {
}
virtual ~SearchQueryParserTest() {
}
TrackId addTrackToCollection(const QString& trackLocation) {
TrackPointer pTrack(collection()->getTrackDAO().addSingleTrack(trackLocation, false));
return pTrack ? pTrack->getId() : TrackId();
}
SearchQueryParser m_parser;
// The expected query to be returned by CrateFilterNode
const QString m_crateFilterQuery =
"id IN (SELECT DISTINCT track_id FROM crate_tracks"
" JOIN crates ON crate_id=id WHERE name LIKE '%%1%' ORDER BY track_id)";
};
TEST_F(SearchQueryParserTest, EmptySearch) {
auto pQuery(
m_parser.parseQuery("", QStringList(), ""));
// An empty query matches all tracks.
TrackPointer pTrack(Track::newTemporary());
EXPECT_TRUE(pQuery->match(pTrack));
EXPECT_STREQ(qPrintable(QString("")),
qPrintable(pQuery->toSql()));
}
TEST_F(SearchQueryParserTest, OneTermOneColumn) {
QStringList searchColumns;
searchColumns << "artist";
auto pQuery(
m_parser.parseQuery("asdf", searchColumns, ""));
TrackPointer pTrack(Track::newTemporary());
pTrack->setTitle("testASDFtest");
EXPECT_FALSE(pQuery->match(pTrack));
pTrack->setArtist("testASDFtest");
EXPECT_TRUE(pQuery->match(pTrack));
EXPECT_STREQ(
qPrintable(QString("artist LIKE '%asdf%'")),
qPrintable(pQuery->toSql()));
}
TEST_F(SearchQueryParserTest, OneTermMultipleColumns) {
QStringList searchColumns;
searchColumns << "artist"
<< "album";
auto pQuery(
m_parser.parseQuery("asdf", searchColumns, ""));
TrackPointer pTrack(Track::newTemporary());
pTrack->setTitle("testASDFtest");
EXPECT_FALSE(pQuery->match(pTrack));
pTrack->setAlbum("testASDFtest");
EXPECT_TRUE(pQuery->match(pTrack));
EXPECT_STREQ(
qPrintable(QString("(artist LIKE '%asdf%') OR (album LIKE '%asdf%')")),
qPrintable(pQuery->toSql()));
}
TEST_F(SearchQueryParserTest, OneTermMultipleColumnsNegation) {
QStringList searchColumns;
searchColumns << "artist"
<< "album";
auto pQuery(
m_parser.parseQuery("-asdf", searchColumns, ""));
TrackPointer pTrack(Track::newTemporary());
pTrack->setTitle("testASDFtest");
EXPECT_TRUE(pQuery->match(pTrack));
pTrack->setAlbum("testASDFtest");
EXPECT_FALSE(pQuery->match(pTrack));
EXPECT_STREQ(
qPrintable(QString("NOT ((artist LIKE '%asdf%') OR (album LIKE '%asdf%'))")),
qPrintable(pQuery->toSql()));
}
TEST_F(SearchQueryParserTest, MultipleTermsOneColumn) {
QStringList searchColumns;
searchColumns << "artist";
auto pQuery(
m_parser.parseQuery("asdf zxcv", searchColumns, ""));
TrackPointer pTrack(Track::newTemporary());
pTrack->setTitle("test zXcV test");
EXPECT_FALSE(pQuery->match(pTrack));
pTrack->setArtist("test zXcV test asDf");
EXPECT_TRUE(pQuery->match(pTrack));
EXPECT_STREQ(
qPrintable(QString("(artist LIKE '%asdf%') AND (artist LIKE '%zxcv%')")),
qPrintable(pQuery->toSql()));
}
TEST_F(SearchQueryParserTest, MultipleTermsMultipleColumns) {
QStringList searchColumns;
searchColumns << "artist"
<< "album";
auto pQuery(
m_parser.parseQuery("asdf zxcv", searchColumns, ""));
TrackPointer pTrack(Track::newTemporary());
pTrack->setTitle("asdf zxcv");
EXPECT_FALSE(pQuery->match(pTrack));
pTrack->setArtist("zXcV");
pTrack->setAlbum("asDF");
EXPECT_TRUE(pQuery->match(pTrack));
pTrack->setArtist("");
pTrack->setAlbum("ASDF ZXCV");
EXPECT_TRUE(pQuery->match(pTrack));
EXPECT_STREQ(
qPrintable(QString(
"((artist LIKE '%asdf%') OR (album LIKE '%asdf%')) "
"AND ((artist LIKE '%zxcv%') OR (album LIKE '%zxcv%'))")),
qPrintable(pQuery->toSql()));
}
TEST_F(SearchQueryParserTest, MultipleTermsMultipleColumnsNegation) {
QStringList searchColumns;
searchColumns << "artist"
<<