Commit 8ae74e99 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Album: Handle sorting by insertion date

Fix #301
parent a381d792
Pipeline #144796 passed with stages
in 5 minutes and 31 seconds
......@@ -221,6 +221,7 @@ std::string Album::addRequestJoin( const QueryParameters* params,
/* No other tables required for this criterias */
break;
case SortingCriteria::PlayCount:
case SortingCriteria::InsertionDate:
albumTrack = true;
media = true;
break;
......@@ -319,6 +320,12 @@ std::string Album::orderBy( const QueryParameters* params )
req += "DESC "; // Most played first by default
req += ", alb.title";
break;
case SortingCriteria::InsertionDate:
req = "GROUP BY alb.id_album "
"ORDER BY MIN(m.insertion_date) ";
if ( desc == true )
req += "DESC ";
break;
default:
LOG_WARN( "Unsupported sorting criteria, falling back to SortingCriteria::Default (Alpha)" );
/* fall-through */
......
......@@ -865,6 +865,70 @@ static void SortByDuration( Tests* T )
ASSERT_EQ( long1->id(), tracks[1]->id() );
}
static void SortByInsertionDate( Tests* T )
{
auto alb1 = T->ml->createAlbum( "album 1" );
auto alb2 = T->ml->createAlbum( "album 2" );
ASSERT_NON_NULL( alb1 );
ASSERT_NON_NULL( alb2 );
auto m1 = std::static_pointer_cast<Media>(
T->ml->addMedia( "media1.mp3", IMedia::Type::Audio ) );
auto m2 = std::static_pointer_cast<Media>(
T->ml->addMedia( "media2.mp3", IMedia::Type::Audio ) );
ASSERT_NON_NULL( m1 );
ASSERT_NON_NULL( m2 );
auto res = T->ml->setMediaInsertionDate( m1->id(), 987 );
ASSERT_TRUE( res );
res = T->ml->setMediaInsertionDate( m2->id(), 123 );
ASSERT_TRUE( res );
alb1->addTrack( m1, 1, 1, 0, nullptr );
res = m1->save();
ASSERT_TRUE( res );
alb2->addTrack( m2, 1, 1, 0, nullptr );
res = m1->save();
ASSERT_TRUE( res );
QueryParameters params{};
params.sort = SortingCriteria::InsertionDate;
params.desc = false;
auto albums = T->ml->albums( &params )->all();
ASSERT_EQ( albums.size(), 2u );
ASSERT_EQ( albums[0]->id(), alb2->id() );
ASSERT_EQ( albums[1]->id(), alb1->id() );
params.desc = true;
albums = T->ml->albums( &params )->all();
ASSERT_EQ( albums.size(), 2u );
ASSERT_EQ( albums[0]->id(), alb1->id() );
ASSERT_EQ( albums[1]->id(), alb2->id() );
auto m3 = std::static_pointer_cast<Media>(
T->ml->addMedia( "media3.mp3", IMedia::Type::Audio ) );
ASSERT_NON_NULL( m3 );
// Now insert a new track to album2 and force its insertion date before album1's media
res = T->ml->setMediaInsertionDate( m3->id(), 12 );
ASSERT_TRUE( res );
alb1->addTrack( m3, 2, 1, 0, nullptr );
res = m3->save();
ASSERT_TRUE( res );
params.desc = false;
albums = T->ml->albums( &params )->all();
ASSERT_EQ( albums.size(), 2u );
ASSERT_EQ( albums[0]->id(), alb1->id() );
ASSERT_EQ( albums[1]->id(), alb2->id() );
params.desc = true;
albums = T->ml->albums( &params )->all();
ASSERT_EQ( albums.size(), 2u );
ASSERT_EQ( albums[0]->id(), alb2->id() );
ASSERT_EQ( albums[1]->id(), alb1->id() );
}
int main( int ac, char** av )
{
INIT_TESTS( Album )
......@@ -899,6 +963,7 @@ int main( int ac, char** av )
ADD_TEST( NbDiscs );
ADD_TEST( CheckDbModel );
ADD_TEST( SortByDuration );
ADD_TEST( SortByInsertionDate );
END_TESTS
}
......@@ -57,6 +57,7 @@ album_tests = [
'NbDiscs',
'CheckDbModel',
'SortByDuration',
'SortByInsertionDate',
]
foreach t : album_tests
test('Album.' + t, album_test_exe, args: t, suite: ['unittest', 'Album'])
......
Supports Markdown
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