AlbumTests.cpp 13.3 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
#include "Tests.h"
28

29
30
#include "Album.h"
#include "AlbumTrack.h"
31
#include "Artist.h"
32
#include "Genre.h"
33
#include "Media.h"
34
#include "medialibrary/IMediaLibrary.h"
35

36
class Albums : public Tests
37
38
39
40
41
{
};

TEST_F( Albums, Create )
{
42
    auto a = ml->createAlbum( "album" );
43
44
    ASSERT_NE( a, nullptr );

45
    auto a2 = ml->album( a->id() );
46
    ASSERT_EQ( a, a2 );
47
    ASSERT_EQ( a2->title(), "album" );
48
49
}

50
51
52
53
54
TEST_F( Albums, Fetch )
{
    auto a = ml->createAlbum( "album" );

    // Clear the cache
55
    Reload();
56

57
    auto a2 = ml->album( a->id() );
58
59
60
61
62
    // The shared pointer are expected to point to a different instance
    ASSERT_NE( a, a2 );

    ASSERT_EQ( a->id(), a2->id() );
}
63
64
65
66

TEST_F( Albums, AddTrack )
{
    auto a = ml->createAlbum( "albumtag" );
67
    auto f = std::static_pointer_cast<Media>( ml->addMedia( "track.mp3" ) );
68
    auto track = a->addTrack( f, 10, 0, 0, 0 );
69
    f->save();
70
71
    ASSERT_NE( track, nullptr );

72
    auto tracks = a->tracks( SortingCriteria::Default, false );
73
74
    ASSERT_EQ( tracks.size(), 1u );

75
    Reload();
76

77
    a = std::static_pointer_cast<Album>( ml->album( a->id() ) );
78
    tracks = a->tracks( SortingCriteria::Default, false );
79
    ASSERT_EQ( tracks.size(), 1u );
80
    ASSERT_EQ( tracks[0]->albumTrack()->trackNumber(), track->trackNumber() );
81
}
82

83
84
85
86
87
TEST_F( Albums, NbTracks )
{
    auto a = ml->createAlbum( "albumtag" );
    for ( auto i = 1u; i <= 10; ++i )
    {
88
        auto f = std::static_pointer_cast<Media>( ml->addMedia( "track" + std::to_string(i) + ".mp3" ) );
89
        auto track = a->addTrack( f, i, i, 0, 0 );
90
        f->save();
91
92
        ASSERT_NE( track, nullptr );
    }
93
    auto tracks = a->tracks( SortingCriteria::Default, false );
94
95
96
97
    ASSERT_EQ( tracks.size(), a->nbTracks() );

    Reload();

98
    a = std::static_pointer_cast<Album>( ml->album( a->id() ) );
99
    tracks = a->tracks( SortingCriteria::Default, false );
100
101
102
    ASSERT_EQ( tracks.size(), a->nbTracks() );
}

103
104
105
106
107
108
109
TEST_F( Albums, TracksByGenre )
{
    auto a = ml->createAlbum( "albumtag" );
    auto g = ml->createGenre( "genre" );

    for ( auto i = 1u; i <= 10; ++i )
    {
110
        auto f = std::static_pointer_cast<Media>( ml->addMedia( "track" + std::to_string(i) + ".mp3" ) );
111
        auto track = a->addTrack( f, i, i, 0, 0 );
112
113
114
115
116
        f->save();
        ASSERT_NE( track, nullptr );
        if ( i <= 5 )
            track->setGenre( g );
    }
117
    auto tracks = a->tracks( g, SortingCriteria::Default, false );
118
119
120
121
122
    ASSERT_EQ( 5u, tracks.size() );

    Reload();

    a = std::static_pointer_cast<Album>( ml->album( a->id() ) );
123
    tracks = a->tracks( g, SortingCriteria::Default, false );
124
125
126
127
    ASSERT_NE( tracks.size(), a->nbTracks() );
    ASSERT_EQ( 5u, tracks.size() );
}

128
129
130
131
TEST_F( Albums, SetReleaseDate )
{
    auto a = ml->createAlbum( "album" );

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    ASSERT_EQ( 0u, a->releaseYear() );

    a->setReleaseYear( 1234, false );
    ASSERT_EQ( a->releaseYear(), 1234u );

    a->setReleaseYear( 4321, false );
    // We now have conflicting dates, it should be restored to 0.
    ASSERT_EQ( 0u, a->releaseYear() );

    // Check that this is not considered initial state anymore, and that pretty much any other date will be ignored.
    a->setReleaseYear( 666, false );
    ASSERT_EQ( 0u, a->releaseYear() );

    // Now check that forcing a date actually forces it
    a->setReleaseYear( 9876, true );
    ASSERT_EQ( 9876u, a->releaseYear() );
148
149
150
151
152
153
154

    Reload();

    auto a2 = ml->album( a->id() );
    ASSERT_EQ( a->releaseYear(), a2->releaseYear() );
}

155
156
157
158
159
160
161
TEST_F( Albums, SetShortSummary )
{
    auto a = ml->createAlbum( "album" );

    a->setShortSummary( "summary" );
    ASSERT_EQ( a->shortSummary(), "summary" );

162
    Reload();
163

164
    auto a2 = ml->album( a->id() );
165
166
167
    ASSERT_EQ( a->shortSummary(), a2->shortSummary() );
}

168
TEST_F( Albums, SetArtworkMrl )
169
170
171
{
    auto a = ml->createAlbum( "album" );

172
173
    a->setArtworkMrl( "artwork" );
    ASSERT_EQ( a->artworkMrl(), "artwork" );
174

175
    Reload();
176

177
    auto a2 = ml->album( a->id() );
178
    ASSERT_EQ( a->artworkMrl(), a2->artworkMrl() );
179
}
180
181
182

TEST_F( Albums, FetchAlbumFromTrack )
{
183
    auto a = ml->createAlbum( "album" );
184
    auto f = std::static_pointer_cast<Media>( ml->addMedia( "file.mp3" ) );
185
    auto t = a->addTrack( f, 1, 0, 0, 0 );
186
    f->save();
187

188
    Reload();
189

190
191
192
193
194
    f = ml->media( f->id() );
    auto t2 = f->albumTrack();
    auto a2 = t2->album();
    ASSERT_NE( a2, nullptr );
    ASSERT_EQ( a2->title(), "album" );
195
}
196

197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
TEST_F( Albums, Artists )
{
    auto album = ml->createAlbum( "album" );
    auto artist1 = ml->createArtist( "john" );
    auto artist2 = ml->createArtist( "doe" );

    ASSERT_NE( album, nullptr );
    ASSERT_NE( artist1, nullptr );
    ASSERT_NE( artist2, nullptr );

    auto res = album->addArtist( artist1 );
    ASSERT_EQ( res, true );

    res = album->addArtist( artist2 );
    ASSERT_EQ( res, true );

213
    auto artists = album->artists( false );
214
215
216
217
    ASSERT_EQ( artists.size(), 2u );

    Reload();

218
    album = std::static_pointer_cast<Album>( ml->album( album->id() ) );
219
    artists = album->artists( false );
220
    ASSERT_EQ( album->albumArtist(), nullptr );
221
222
    ASSERT_EQ( artists.size(), 2u );
}
223

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
TEST_F( Albums, SortArtists )
{
    auto album = ml->createAlbum( "album" );
    auto artist1 = ml->createArtist( "john" );
    auto artist2 = ml->createArtist( "doe" );

    album->addArtist( artist1 );
    album->addArtist( artist2 );

    auto artists = album->artists( false );
    ASSERT_EQ( artists.size(), 2u );
    ASSERT_EQ( artist1->id(), artists[1]->id() );
    ASSERT_EQ( artist2->id(), artists[0]->id() );

    artists = album->artists( true );
    ASSERT_EQ( artists.size(), 2u );
    ASSERT_EQ( artist1->id(), artists[0]->id() );
    ASSERT_EQ( artist2->id(), artists[1]->id() );
}

244
245
246
247
248
TEST_F( Albums, AlbumArtist )
{
    auto album = ml->createAlbum( "test" );
    ASSERT_EQ( album->albumArtist(), nullptr );
    auto artist = ml->createArtist( "artist" );
249
    album->setAlbumArtist( artist );
250
251
252
253
254
255
256
257
258
    ASSERT_NE( album->albumArtist(), nullptr );

    Reload();

    album = std::static_pointer_cast<Album>( ml->album( album->id() ) );
    auto albumArtist = album->albumArtist();
    ASSERT_NE( albumArtist, nullptr );
    ASSERT_EQ( albumArtist->name(), artist->name() );
}
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272

TEST_F( Albums, SearchByTitle )
{
    ml->createAlbum( "sea otters" );
    ml->createAlbum( "pangolins of fire" );

    auto albums = ml->searchAlbums( "otte" );
    ASSERT_EQ( 1u, albums.size() );
}

TEST_F( Albums, SearchByArtist )
{
    auto a = ml->createAlbum( "sea otters" );
    auto artist = ml->createArtist( "pangolins" );
273
    a->setAlbumArtist( artist );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
274
275
276
277
278
279
280
281
282

    auto albums = ml->searchAlbums( "pangol" );
    ASSERT_EQ( 1u, albums.size() );
}

TEST_F( Albums, SearchNoDuplicate )
{
    auto a = ml->createAlbum( "sea otters" );
    auto artist = ml->createArtist( "otters" );
283
    a->setAlbumArtist( artist );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316

    auto albums = ml->searchAlbums( "otters" );
    ASSERT_EQ( 1u, albums.size() );
}

TEST_F( Albums, SearchNoUnknownAlbum )
{
    auto artist = ml->createArtist( "otters" );
    auto album = artist->unknownAlbum();
    ASSERT_NE( nullptr, album );

    auto albums = ml->searchAlbums( "otters" );
    ASSERT_EQ( 0u, albums.size() );
    // Can't search by name since there is no name set for unknown albums
}

TEST_F( Albums, SearchAfterDeletion )
{
    auto a = ml->createAlbum( "sea otters" );
    auto albums = ml->searchAlbums( "sea" );
    ASSERT_EQ( 1u, albums.size() );

    ml->deleteAlbum( a->id() );

    albums = ml->searchAlbums( "sea" );
    ASSERT_EQ( 0u, albums.size() );
}

TEST_F( Albums, SearchAfterArtistUpdate )
{
    auto a = ml->createAlbum( "sea otters" );
    auto artist = ml->createArtist( "pangolin of fire" );
    auto artist2 = ml->createArtist( "pangolin of ice" );
317
    a->setAlbumArtist( artist );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
318
319
320
321
322
323
324

    auto albums = ml->searchAlbums( "fire" );
    ASSERT_EQ( 1u, albums.size() );

    albums = ml->searchAlbums( "ice" );
    ASSERT_EQ( 0u, albums.size() );

325
    a->setAlbumArtist( artist2 );
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
326
327
328
329
330
331
332

    albums = ml->searchAlbums( "fire" );
    ASSERT_EQ( 0u, albums.size() );

    albums = ml->searchAlbums( "ice" );
    ASSERT_EQ( 1u, albums.size() );
}
333
334
335
336

TEST_F( Albums, AutoDelete )
{
    auto a = ml->createAlbum( "album" );
337
    auto m = std::static_pointer_cast<Media>( ml->addMedia( "media.mp3" ) );
338
    auto t = a->addTrack( m, 1, 1, 0, 0 );
339
340
341
342
343
344
345
346
347

    auto album = ml->album( a->id() );
    ASSERT_NE( nullptr, album );

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

    album = ml->album( a->id() );
    ASSERT_EQ( nullptr, album );
}
348
349
350
351

TEST_F( Albums, SortTracks )
{
    auto a = ml->createAlbum( "album" );
352
353
    auto m1 = std::static_pointer_cast<Media>( ml->addMedia( "B-track1.mp3" ) );
    auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "A-track2.mp3" ) );
354
355
    auto t1 = a->addTrack( m1, 1, 1, 0, 0 );
    auto t2 = a->addTrack( m2, 2, 1, 0, 0 );
356
357

    // Default order is by disc number & track number
358
    auto tracks = a->tracks( SortingCriteria::Default, false );
359
360
361
362
363
    ASSERT_EQ( 2u, tracks.size() );
    ASSERT_EQ( t1->id(), tracks[0]->id() );
    ASSERT_EQ( t2->id(), tracks[1]->id() );

    // Reverse order
364
    tracks = a->tracks( SortingCriteria::Default, true );
365
366
367
368
369
    ASSERT_EQ( 2u, tracks.size() );
    ASSERT_EQ( t1->id(), tracks[1]->id() );
    ASSERT_EQ( t2->id(), tracks[0]->id() );

    // Try a media based criteria
370
    tracks = a->tracks( SortingCriteria::Alpha, false );
371
372
373
374
    ASSERT_EQ( 2u, tracks.size() );
    ASSERT_EQ( t1->id(), tracks[1]->id() ); // B-track -> first
    ASSERT_EQ( t2->id(), tracks[0]->id() ); // A-track -> second
}
375
376
377
378
379
380
381
382
383
384

TEST_F( Albums, Sort )
{
    auto a1 = ml->createAlbum( "A" );
    a1->setReleaseYear( 1000, false );
    auto a2 = ml->createAlbum( "B" );
    a2->setReleaseYear( 2000, false );
    auto a3 = ml->createAlbum( "C" );
    a3->setReleaseYear( 1000, false );

385
    auto albums = ml->albums( SortingCriteria::ReleaseDate, false );
386
387
388
389
390
    ASSERT_EQ( 3u, albums.size() );
    ASSERT_EQ( a1->id(), albums[0]->id() );
    ASSERT_EQ( a3->id(), albums[1]->id() );
    ASSERT_EQ( a2->id(), albums[2]->id() );

391
    albums = ml->albums( SortingCriteria::ReleaseDate, true );
392
393
394
395
396
397
398
    // We do not invert the lexical order when sorting by DESC release date:
    ASSERT_EQ( 3u, albums.size() );
    ASSERT_EQ( a2->id(), albums[0]->id() );
    ASSERT_EQ( a1->id(), albums[1]->id() );
    ASSERT_EQ( a3->id(), albums[2]->id() );

    // When listing all albums, default order is lexical order
399
    albums = ml->albums( SortingCriteria::Default, false );
400
401
402
403
404
    ASSERT_EQ( 3u, albums.size() );
    ASSERT_EQ( a1->id(), albums[0]->id() );
    ASSERT_EQ( a2->id(), albums[1]->id() );
    ASSERT_EQ( a3->id(), albums[2]->id() );

405
    albums = ml->albums( SortingCriteria::Default, true );
406
407
408
409
410
    ASSERT_EQ( 3u, albums.size() );
    ASSERT_EQ( a3->id(), albums[0]->id() );
    ASSERT_EQ( a2->id(), albums[1]->id() );
    ASSERT_EQ( a1->id(), albums[2]->id() );
}
411

412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
TEST_F( Albums, SortByArtist )
{
    auto artist1 = ml->createArtist( "Artist" );
    auto artist2 = ml->createArtist( "tsitrA" );

    // Create albums with a non-alphabetical order to avoid a false positive (where sorting by pkey
    // is the same as sorting by title)
    auto a1 = ml->createAlbum( "C" );
    a1->setAlbumArtist( artist1 );
    auto a2 = ml->createAlbum( "B" );
    a2->setAlbumArtist( artist2 );
    auto a3 = ml->createAlbum( "A" );
    a3->setAlbumArtist( artist1 );

    auto albums = ml->albums( SortingCriteria::Artist, false );
    ASSERT_EQ( 3u, albums.size() );
    ASSERT_EQ( a3->id(), albums[0]->id() );
    ASSERT_EQ( a1->id(), albums[1]->id() );
    ASSERT_EQ( a2->id(), albums[2]->id() );

    albums = ml->albums( SortingCriteria::Artist, true );
    ASSERT_EQ( 3u, albums.size() );
    // We expect Artist to be sorted in reverse order, but still in alphabetical order for albums
    ASSERT_EQ( a2->id(), albums[0]->id() );
    ASSERT_EQ( a3->id(), albums[1]->id() );
    ASSERT_EQ( a1->id(), albums[2]->id() );
}

440
441
442
443
444
TEST_F( Albums, Duration )
{
    auto a = ml->createAlbum( "album" );
    ASSERT_EQ( 0u, a->duration() );

445
    auto m = std::static_pointer_cast<Media>( ml->addMedia( "track.mp3" ) );
446
447
    m->setDuration( 100 );
    m->save();
448
    a->addTrack( m, 1, 1, 0, 0 );
449
450
    ASSERT_EQ( 100u, a->duration() );

451
    auto m2 = std::static_pointer_cast<Media>( ml->addMedia( "track2.mp3" ) );
452
453
    m2->setDuration( 200 );
    m2->save();
454
    a->addTrack( m2, 1, 1, 0, 0 );
455
456
457
458
459
460
461
    ASSERT_EQ( 300u, a->duration() );

    Reload();

    auto a2 = ml->album( a->id() );
    ASSERT_EQ( 300u, a2->duration() );
}