Commit bfe8fef5 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

sqlite: Query: Fix count results when request is using GROUP BY

parent a96eea4d
......@@ -57,7 +57,8 @@ public:
if ( m_hasCount == true )
return m_count;
std::string req = "SELECT COUNT() " + m_base;
std::string req = "SELECT COUNT(DISTINCT " + Impl::Table::PrimaryKeyColumn +
" ) " + m_base;
auto dbConn = m_ml->getConn();
auto ctx = dbConn->acquireReadContext();
auto chrono = std::chrono::steady_clock::now();
......
......@@ -479,7 +479,9 @@ TEST_F( Albums, SortByPlayCount )
ASSERT_TRUE( f5->increasePlayCount() );
QueryParameters params { SortingCriteria::PlayCount, false };
auto albums = ml->albums( &params )->all(); // Expect descending order
auto query = ml->albums( &params );
ASSERT_EQ( 4u, query->count() );
auto albums = query->all(); // Expect descending order
ASSERT_EQ( 4u, albums.size() );
ASSERT_EQ( a2->id(), albums[0]->id() ); // 4 plays
ASSERT_EQ( a1->id(), albums[1]->id() ); // 3 plays
......
......@@ -132,10 +132,20 @@ TEST_F( Artists, Albums )
ASSERT_NE( nullptr, media2 );
album2->addTrack( media2, 1, 0, artist->id(), nullptr );
auto media3 = std::static_pointer_cast<Media>( ml->addMedia( "track3.mp3" ) );
ASSERT_NE( nullptr, media3 );
album2->addTrack( media3, 2, 0, artist->id(), nullptr );
auto media4 = std::static_pointer_cast<Media>( ml->addMedia( "track4.mp3" ) );
ASSERT_NE( nullptr, media4 );
album2->addTrack( media4, 3, 0, artist->id(), nullptr );
album1->setAlbumArtist( artist );
album2->setAlbumArtist( artist );
auto albums = artist->albums( nullptr )->all();
auto query = artist->albums( nullptr );
ASSERT_EQ( 2u, query->count() );
auto albums = query->all();
ASSERT_EQ( albums.size(), 2u );
Reload();
......
......@@ -310,10 +310,19 @@ TEST_F( Genres, SearchAlbums )
auto t2 = a2->addTrack( m2, 1, 1, 0, nullptr );
m2->save();
auto albums = ml->searchAlbums( "album", nullptr)->all();
auto m3 = std::static_pointer_cast<Media>( ml->addMedia( "track3.mp3" ) );
m3->setType( IMedia::Type::Audio );
auto t3 = a1->addTrack( m3, 2, 1, 0, g.get() );
m3->save();
auto query = ml->searchAlbums( "album", nullptr);
ASSERT_EQ( 2u, query->count() );
auto albums = query->all();
ASSERT_EQ( 2u, albums.size() );
albums = g->searchAlbums( "album", nullptr )->all();
query = g->searchAlbums( "album", nullptr );
ASSERT_EQ( 1u, query->count() );
albums = query->all();
ASSERT_EQ( 1u, albums.size() );
ASSERT_EQ( a1->id(), albums[0]->id() );
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment