GenreTests.cpp 8.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23 24 25 26
#if HAVE_CONFIG_H
# include "config.h"
#endif

27 28 29 30 31
#include "Tests.h"

#include "Genre.h"
#include "Album.h"
#include "AlbumTrack.h"
32
#include "Media.h"
33
#include "Artist.h"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

class Genres : public Tests
{
protected:
    std::shared_ptr<Genre> g;

    virtual void SetUp() override
    {
        Tests::SetUp();
        g = ml->createGenre( "genre" );
    }
};

TEST_F( Genres, Create )
{
    ASSERT_NE( nullptr, g );
    ASSERT_EQ( "genre", g->name() );
51
    auto tracks = g->tracks( nullptr )->all();
52 53 54 55 56 57 58
    ASSERT_EQ( 0u, tracks.size() );
}

TEST_F( Genres, List )
{
    auto g2 = ml->createGenre( "genre 2" );
    ASSERT_NE( nullptr, g2 );
59
    auto genres = ml->genres( nullptr )->all();
60 61 62 63 64 65 66 67 68
    ASSERT_EQ( 2u, genres.size() );
}

TEST_F( Genres, ListAlbumTracks )
{
    auto a = ml->createAlbum( "album" );

    for ( auto i = 1u; i <= 3; i++ )
    {
69
        auto m = std::static_pointer_cast<Media>( ml->addMedia( "track" + std::to_string( i ) + ".mp3" ) );
70
        auto t = a->addTrack( m, i, 1, 0, nullptr );
71 72 73
        if ( i != 1 )
            t->setGenre( g );
    }
74
    auto tracks = g->tracks( nullptr )->all();
75 76
    ASSERT_EQ( 2u, tracks.size() );
}
77 78 79

TEST_F( Genres, ListArtists )
{
80
    auto artists = g->artists( nullptr )->all();
81 82 83 84 85 86 87 88 89 90 91
    ASSERT_EQ( 0u, artists.size() );

    auto a = ml->createArtist( "artist" );
    auto a2 = ml->createArtist( "artist 2" );
    // Ensure we're not just returning all the artists:
    auto a3 = ml->createArtist( "artist 3" );
    auto album = ml->createAlbum( "album" );
    auto album2 = ml->createAlbum( "album2" );

    for ( auto i = 1u; i <= 5; ++i )
    {
92
        auto m = std::static_pointer_cast<Media>( ml->addMedia( std::to_string( i ) + ".mp3" ) );
93
        auto track = album->addTrack( m, i, 1, a->id(), nullptr );
94 95 96 97
        track->setGenre( g );
    }
    for ( auto i = 1u; i <= 5; ++i )
    {
98
        auto m = std::static_pointer_cast<Media>( ml->addMedia( std::to_string( i ) + "_2.mp3" ) );
99
        auto track = album2->addTrack( m, i, 1, a2->id(), nullptr );
100 101
        track->setGenre( g );
    }
102
    artists = g->artists( nullptr )->all();
103 104
    ASSERT_EQ( 2u, artists.size() );
}
105 106 107 108

TEST_F( Genres, ListAlbums )
{
    auto album = ml->createAlbum( "album" );
109
    auto m = std::static_pointer_cast<Media>( ml->addMedia( "some track.mp3" ) );
110
    auto t = album->addTrack( m, 10, 1, 0, nullptr );
111 112 113
    t->setGenre( g );

    auto album2 = ml->createAlbum( "album2" );
114
    m = std::static_pointer_cast<Media>( ml->addMedia( "some other track.mp3" ) );
115
    t = album2->addTrack( m, 10, 1, 0, nullptr );
116 117 118 119 120 121
    t->setGenre( g );

    // We have 2 albums with at least a song with genre "g" (as defined in SetUp)
    // Now we create more albums with "random" genre, all of them should have 1 album
    for ( auto i = 1u; i <= 5u; ++i )
    {
122
        auto m = std::static_pointer_cast<Media>( ml->addMedia( std::to_string( i ) + ".mp3" ) );
123
        auto track = album->addTrack( m, i, 1, 0, nullptr );
124 125 126 127
        auto g = ml->createGenre( std::to_string( i ) );
        track->setGenre( g );
    }

128
    auto genres = ml->genres( nullptr )->all();
129 130
    for ( auto& genre : genres )
    {
131
        auto albums = genre->albums( nullptr )->all();
132 133 134 135 136 137 138 139 140 141 142 143 144

        if ( genre->id() == g->id() )
        {
            // Initial genre with 2 albums:
            ASSERT_EQ( 2u, albums.size() );
        }
        else
        {
            ASSERT_EQ( 1u, albums.size() );
            ASSERT_EQ( album->id(), albums[0]->id() );
        }
    }
}
145 146 147 148 149 150

TEST_F( Genres, Search )
{
    ml->createGenre( "something" );
    ml->createGenre( "blork" );

151
    auto genres = ml->searchGenre( "genr", nullptr )->all();
152 153 154 155 156
    ASSERT_EQ( 1u, genres.size() );
}

TEST_F( Genres, SearchAfterDelete )
{
157
    auto genres = ml->searchGenre( "genre", nullptr )->all();
158 159 160 161
    ASSERT_EQ( 1u, genres.size() );

    ml->deleteGenre( g->id() );

162
    genres = ml->searchGenre( "genre", nullptr )->all();
163 164
    ASSERT_EQ( 0u, genres.size() );
}
165 166 167 168 169 170 171

TEST_F( Genres, SortTracks )
{
    auto a = ml->createAlbum( "album" );

    for ( auto i = 1u; i <= 2; i++ )
    {
172
        auto m = std::static_pointer_cast<Media>( ml->addMedia( "track" + std::to_string( i ) + ".mp3" ) );
173
        auto t = a->addTrack( m, i, 1, 0, nullptr );
174 175 176 177
        m->setDuration( i );
        m->save();
        t->setGenre( g );
    }
178 179
    QueryParameters params { SortingCriteria::Duration, false };
    auto tracks = g->tracks( &params )->all();
180
    ASSERT_EQ( 2u, tracks.size() );
181 182
    ASSERT_EQ( 1u, tracks[0]->albumTrack()->trackNumber() );
    ASSERT_EQ( 2u, tracks[1]->albumTrack()->trackNumber() );
183

184 185
    params.desc = true;
    tracks = g->tracks( &params )->all();
186
    ASSERT_EQ( 2u, tracks.size() );
187 188
    ASSERT_EQ( 1u, tracks[1]->albumTrack()->trackNumber() );
    ASSERT_EQ( 2u, tracks[0]->albumTrack()->trackNumber() );
189
}
190 191 192 193 194

TEST_F( Genres, Sort )
{
    auto g2 = ml->createGenre( "metal" );

195
    auto genres = ml->genres( nullptr )->all();
196 197 198 199
    ASSERT_EQ( 2u, genres.size() );
    ASSERT_EQ( g->id(), genres[0]->id() );
    ASSERT_EQ( g2->id(), genres[1]->id() );

200 201
    QueryParameters params { SortingCriteria::Default, true };
    genres = ml->genres( &params )->all();
202 203 204 205
    ASSERT_EQ( 2u, genres.size() );
    ASSERT_EQ( g->id(), genres[1]->id() );
    ASSERT_EQ( g2->id(), genres[0]->id() );
}
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

TEST_F( Genres, NbTracks )
{
    ASSERT_EQ( 0u, g->nbTracks() );

    auto a = ml->createAlbum( "album" );
    auto m = std::static_pointer_cast<Media>( ml->addMedia( "track.mp3" ) );
    auto t = a->addTrack( m, 1, 1, g->id(), g.get() );

    ASSERT_EQ( 1u, g->nbTracks() );
    Reload();
    g = std::static_pointer_cast<Genre>( ml->genre( g->id() ) );
    ASSERT_EQ( 1u, g->nbTracks() );

    auto g2 = ml->createGenre( "otter metal" );
    t = std::static_pointer_cast<AlbumTrack>( ml->albumTrack( t->id() ) );
    t->setGenre( g2 );
    ASSERT_EQ( 1u, g2->nbTracks() );
    // check that the cache got updated, check for DB deletion afterward
    ASSERT_EQ( 0u, g->nbTracks() );

    Reload();

    g = std::static_pointer_cast<Genre>( ml->genre( g->id() ) );
    g2 = std::static_pointer_cast<Genre>( ml->genre( g2->id() ) );
    ASSERT_EQ( nullptr, g );
    ASSERT_EQ( 1u, g2->nbTracks() );

    ml->deleteTrack( t->id() );
    Reload();

    g2 = std::static_pointer_cast<Genre>( ml->genre( g2->id() ) );
    ASSERT_EQ( nullptr, g2 );
}
240 241 242 243 244 245

TEST_F( Genres, CaseInsensitive )
{
    auto g2 = Genre::fromName( ml.get(), "GENRE" );
    ASSERT_EQ( g->id(), g2->id() );
}
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

TEST_F( Genres, SearchArtists )
{
    auto artists = g->artists( nullptr )->all();
    ASSERT_EQ( 0u, artists.size() );

    auto a = ml->createArtist( "loutre 1" );
    auto a2 = ml->createArtist( "loutre 2" );
    auto album = ml->createAlbum( "album" );
    auto album2 = ml->createAlbum( "album2" );

    for ( auto i = 1u; i <= 5; ++i )
    {
        auto m = std::static_pointer_cast<Media>( ml->addMedia( std::to_string( i ) + ".mp3" ) );
        auto track = album->addTrack( m, i, 1, a->id(), nullptr );
        track->setGenre( g );
    }
    for ( auto i = 1u; i <= 5; ++i )
    {
        auto m = std::static_pointer_cast<Media>( ml->addMedia( std::to_string( i ) + "_2.mp3" ) );
        auto track = album2->addTrack( m, i, 1, a2->id(), nullptr );
    }
    artists = ml->searchArtists( "loutre", nullptr )->all();
    ASSERT_EQ( 2u, artists.size() );

    artists = g->searchArtist( "loutre" )->all();
    ASSERT_EQ( 1u, artists.size() );
    ASSERT_EQ( a->id(), artists[0]->id() );
}