File: TestSqlAlbum.cpp

package info (click to toggle)
amarok 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 112,344 kB
  • sloc: cpp: 195,053; xml: 4,329; ansic: 2,634; javascript: 673; ruby: 528; python: 507; sh: 252; makefile: 12
file content (431 lines) | stat: -rw-r--r-- 19,516 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
240
241
242
243
244
245
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
275
276
277
278
279
280
281
282
283
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/****************************************************************************************
 * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
 * Copyright (c) 2010 Ralf Engels <ralf-engels@gmx.de>                                  *
 *                                                                                      *
 * This program is free software; you can redistribute it and/or modify it under        *
 * the terms of the GNU General Public License as published by the Free Software        *
 * Foundation; either version 2 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 General Public License for more details.             *
 *                                                                                      *
 * You should have received a copy of the GNU General Public License along with         *
 * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
 ****************************************************************************************/

#include "TestSqlAlbum.h"

#include "amarokconfig.h"
#include "core/meta/Meta.h"
#include "core-impl/storage/sql/mysqlestorage/MySqlEmbeddedStorage.h"
#include "SqlCollection.h"
#include "SqlMountPointManagerMock.h"

#include "core/capabilities/ActionsCapability.h"
#include "core/capabilities/BookmarkThisCapability.h"
#include "covermanager/CoverFetcher.h"

#include <KLocalizedString>

#include <QFile>
#include <QImage>

QTEST_MAIN( TestSqlAlbum )

QTemporaryDir *TestSqlAlbum::s_tmpDir = nullptr;

TestSqlAlbum::TestSqlAlbum()
    : QObject()
    , m_collection( nullptr )
    , m_storage( nullptr )
{
    KLocalizedString::setApplicationDomain("amarok-test");
    std::atexit([]() { delete TestSqlAlbum::s_tmpDir; } );
}

TestSqlAlbum::~TestSqlAlbum()
{ }

void
TestSqlAlbum::initTestCase()
{
    AmarokConfig::instance(QStringLiteral("amarokrc"));
    if( !s_tmpDir )
        s_tmpDir = new QTemporaryDir();
    QVERIFY( s_tmpDir->isValid() );
    m_storage = QSharedPointer<MySqlEmbeddedStorage>( new MySqlEmbeddedStorage() );
    QVERIFY( m_storage->init( s_tmpDir->path() ) );
    m_collection = new Collections::SqlCollection( m_storage );
    m_collection->setMountPointManager( new SqlMountPointManagerMock( this, m_storage ) );
}

void
TestSqlAlbum::cleanupTestCase()
{
    delete m_collection;
}

void
TestSqlAlbum::init()
{
    //setup base data
    m_storage->query( QStringLiteral("INSERT INTO artists(id, name) VALUES (1, 'artist1');") );
    m_storage->query( QStringLiteral("INSERT INTO artists(id, name) VALUES (2, 'artist2');") );
    m_storage->query( QStringLiteral("INSERT INTO artists(id, name) VALUES (3, 'artist3');") );

    m_storage->query( QStringLiteral("INSERT INTO composers(id, name) VALUES (1, 'composer1');") );
    m_storage->query( QStringLiteral("INSERT INTO genres(id, name) VALUES (1, 'genre1');") );
    m_storage->query( QStringLiteral("INSERT INTO years(id, name) VALUES (1, '1');") );

    m_storage->query( QStringLiteral("INSERT INTO directories(id, deviceid, dir) VALUES (1, -1, './');") );

    m_storage->query( QStringLiteral("INSERT INTO urls(id, deviceid, rpath, directory, uniqueid) VALUES (1, -1, './IDoNotExist.mp3', 1, 'uid://1');") );
    m_storage->query( QStringLiteral("INSERT INTO urls(id, deviceid, rpath, directory, uniqueid) VALUES (2, -1, './IDoNotExistAsWell.mp3', 1, 'uid://2');") );
    m_storage->query( QStringLiteral("INSERT INTO urls(id, deviceid, rpath, directory, uniqueid) VALUES (3, -1, './MeNeither.mp3', 1, 'uid:/3');") );
    m_storage->query( QStringLiteral("INSERT INTO urls(id, deviceid, rpath, directory, uniqueid) VALUES (4, -1, './MeNeitherToo.mp3', 1, 'uid:/4');") );

    CoverFetcher::destroy();

    m_collection->registry()->emptyCache();
}

void
TestSqlAlbum::cleanup()
{
    m_storage->query( QStringLiteral("TRUNCATE TABLE years;") );
    m_storage->query( QStringLiteral("TRUNCATE TABLE genres;") );
    m_storage->query( QStringLiteral("TRUNCATE TABLE composers;") );
    m_storage->query( QStringLiteral("TRUNCATE TABLE albums;") );
    m_storage->query( QStringLiteral("TRUNCATE TABLE artists;") );
    m_storage->query( QStringLiteral("TRUNCATE TABLE tracks;") );
    m_storage->query( QStringLiteral("TRUNCATE TABLE urls;") );
    m_storage->query( QStringLiteral("TRUNCATE TABLE directories;") );
    m_storage->query( QStringLiteral("TRUNCATE TABLE urls_labels;") );
}

void
TestSqlAlbum::testTracks()
{
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (1, 'album1',1);") );
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (2, 'album2',1);") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (1,1,'track1',1,1,1,1,1 );") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (2,2,'track2',1,1,1,1,1 );") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (3,3,'trackInOtherAlbum',1,2,1,1,1 );") );

    Meta::AlbumPtr album = m_collection->registry()->getAlbum( 1 );
    Meta::SqlAlbum *sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() );

    // check two tracks
    Meta::TrackList tracks = sqlAlbum->tracks();
    QCOMPARE( sqlAlbum->tracks().count(), 2 );

    // remove one track
    Meta::SqlTrack *sqlTrack = static_cast<Meta::SqlTrack*>( tracks.first().data() );
    sqlTrack->setAlbum( QStringLiteral("newAlbum") );
    QCOMPARE( sqlAlbum->tracks().count(), 1 );
}

void
TestSqlAlbum::testIsCompilation()
{
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (1, 'album1',1);") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (1,1,'track1',1,1,1,1,1 );") );

    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (2, 'compilation',NULL);") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (2,2,'track2',1,1,1,1,1 );") );

    Meta::AlbumPtr album = m_collection->registry()->getAlbum( 1 );
    QVERIFY( !album->isCompilation() );
    QVERIFY( album->hasAlbumArtist() );

    album = m_collection->registry()->getAlbum( 2 );
    QVERIFY( album->isCompilation() );
    QVERIFY( !album->hasAlbumArtist() );
}

void
TestSqlAlbum::testAlbumArtist()
{
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (1, 'album1',1);") );

    Meta::AlbumPtr album = m_collection->registry()->getAlbum( 1 );
    QVERIFY( !album->isCompilation() );
    QVERIFY( album->hasAlbumArtist() );
    QVERIFY( album->albumArtist() );
    QCOMPARE( album->albumArtist()->name(), QStringLiteral( "artist1" ) );
}

void
TestSqlAlbum::testImage()
{
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (1, 'imageTestAlbum',1);") );

    Meta::AlbumPtr album = m_collection->registry()->getAlbum( 1 );
    Meta::SqlAlbum *sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() );
    sqlAlbum->setSuppressImageAutoFetch( true );

    QVERIFY( !album->hasImage() );
    QVERIFY( !album->hasImage(100) );
    QVERIFY( !album->hasImage(0) );

    QVERIFY( album->canUpdateImage() );

    // set an image
    sqlAlbum->setImage( QImage( QSize(100, 100), QImage::Format_Mono ) );
    QVERIFY( album->hasImage() );
    QVERIFY( album->hasImage(100) );
    QVERIFY( album->hasImage(0) );
    QCOMPARE( album->image(47).width(), 47 );
    QString largeImagePath = album->imageLocation().path();
    QVERIFY( QFile::exists( largeImagePath ) );

    // remove the image
    album->removeImage();
    QVERIFY( !album->hasImage() );
    QVERIFY( !album->hasImage(100) );
    QVERIFY( !album->hasImage(0) );
    QVERIFY( !QFile::exists( largeImagePath ) );

    // IMPROVEMENT: also test embedded image and an image outside the cache directory
}

void
TestSqlAlbum::testCapabilities()
{
    Meta::AlbumPtr album = m_collection->registry()->getAlbum( QStringLiteral("Test album"), QStringLiteral("test artist") );

    QVERIFY( album->hasCapabilityInterface( Capabilities::Capability::Actions ) );
    Capabilities::ActionsCapability *ac = album->create<Capabilities::ActionsCapability>();
    QVERIFY( ac );
    QVERIFY( ac->actions().count() > 4 );

    QVERIFY( album->hasCapabilityInterface( Capabilities::Capability::BookmarkThis ) );
    Capabilities::BookmarkThisCapability *btc = album->create<Capabilities::BookmarkThisCapability>();
    QVERIFY( btc );
    QVERIFY( btc->bookmarkAction() );

    // need to delete the actions. They hold pointers that prevent the registry from cleaning
    // it's state.
    for( auto action : ac->actions() )
        delete action;
    delete btc->bookmarkAction();
    delete ac;
    delete btc;
}

void
TestSqlAlbum::testSetCompilationWithoutExistingCompilation()
{
    m_collection->registry()->emptyCache();

    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (1, 'album1',1);") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (1,1,'track1',1,1,1,1,1 );") );

    Meta::TrackPtr track = m_collection->registry()->getTrack( 1 );
//     auto newAlbum = m_collection->registry()->getAlbum(1);
    Meta::AlbumPtr album = track->album();
    auto sqlAlbum = AmarokSharedPointer<Meta::SqlAlbum>::staticCast( album );
    QVERIFY( album->hasAlbumArtist() );
    QVERIFY( !album->isCompilation() );
    QCOMPARE( sqlAlbum->id(), 1 );
    QCOMPARE( album->tracks().count(), 1 );
    sqlAlbum->setCompilation( true );

    // now the track should be in the compilation
    album = track->album();
    sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() );
    QCOMPARE( sqlAlbum->id(), 2 ); // a new album
    QVERIFY( !album->hasAlbumArtist() );
    QVERIFY( album->isCompilation() );
}

void
TestSqlAlbum::testSetCompilationWithExistingCompilation()
{
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (1, 'albumAndCompilation1',1);") );
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name) VALUES (2, 'albumAndCompilation1');") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (1,1,'track1',1,1,1,1,1 );") );
    Meta::AlbumPtr compilation = m_collection->registry()->getAlbum( 2 );
    QVERIFY( compilation->isCompilation() );

    Meta::TrackPtr track = m_collection->registry()->getTrack( 1 );
    Meta::AlbumPtr album = track->album();
    Meta::SqlAlbum *sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() );
    QVERIFY( album->hasAlbumArtist() );
    QVERIFY( !album->isCompilation() );
    QCOMPARE( sqlAlbum->id(), 1 );
    QCOMPARE( album->tracks().count(), 1 );
    sqlAlbum->setCompilation( true );

    // now the track should be in the compilation
    album = track->album();
    sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() );
    QCOMPARE( sqlAlbum->id(), 2 ); // the existing compilation album
    QVERIFY( !album->hasAlbumArtist() );
    QVERIFY( album->isCompilation() );
    QCOMPARE( album, compilation ); //track returns new album, but the same object that we retrieved above

    QStringList albumResult = m_storage->query( QStringLiteral("SELECT name, artist FROM albums WHERE id = 1") );
}

void
TestSqlAlbum::testUnsetCompilationWithoutExistingAlbum()
{
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (1, 'album1',NULL);") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (1,1,'track1',1,1,1,1,1 );") );

    Meta::TrackPtr track = m_collection->registry()->getTrack( 1 );
    Meta::AlbumPtr album = track->album();
    Meta::SqlAlbum *sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() );
    QVERIFY( !album->hasAlbumArtist() );
    QVERIFY( album->isCompilation() );
    sqlAlbum->setCompilation( false );

    // now the track should be in a normal album
    album = track->album();
    sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() );
    QVERIFY( album->hasAlbumArtist() );
    QVERIFY( !album->isCompilation() );
    QCOMPARE( album->name(), QStringLiteral( "album1" ) ); //album name did not change
    QCOMPARE( album->albumArtist()->name(), track->artist()->name() ); //artist is the same
}

void
TestSqlAlbum::testUnsetCompilationWithExistingAlbum()
{
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (1, 'albumAndCompilation1',NULL);") );
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (2, 'albumAndCompilation1',1);") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (1,1,'track1',1,1,1,1,1 );") );

    Meta::TrackPtr track = m_collection->registry()->getTrack( 1 );
    Meta::AlbumPtr compilation = track->album();
    Meta::SqlAlbum *sqlCompilation = static_cast<Meta::SqlAlbum*>( compilation.data() );
    QVERIFY( compilation->isCompilation() );
    sqlCompilation->setCompilation( false );

    // now the track should be in a normal album
    Meta::AlbumPtr album = track->album();
    Meta::SqlAlbum *sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() );
    QCOMPARE( sqlAlbum->id(), 2 ); // the albums should be the already existing one
    QVERIFY( album->hasAlbumArtist() );
    QVERIFY( !album->isCompilation() );
    QCOMPARE( album->name(), compilation->name() ); //album name did not change
    QCOMPARE( album->albumArtist()->name(), track->artist()->name() ); //artist is the same
}

void
TestSqlAlbum::testUnsetCompilationWithMultipleExistingAlbums()
{
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (1,'albumAndCompilation1',NULL);") );
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (2,'albumAndCompilation1',1);") );
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (3,'albumAndCompilation1',2);") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (1,1,'track1',1,1,1,1,1 );") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (2,2,'track2',2,3,1,1,1 );") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (3,3,'track3',1,2,1,1,1 );") );

    Meta::TrackPtr track = m_collection->registry()->getTrack( 1 );
    Meta::AlbumPtr compilation = track->album();
    Meta::SqlAlbum *sqlCompilation = static_cast<Meta::SqlAlbum*>( compilation.data() );
    QVERIFY( compilation->isCompilation() );
    sqlCompilation->setCompilation( false );

    Meta::AlbumPtr album;
    Meta::SqlAlbum *sqlAlbum;

    // now the tracks should be in a normal album
    album = track->album();
    sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() );
    QVERIFY( album->hasAlbumArtist() );
    QVERIFY( !album->isCompilation() );
    QCOMPARE( album->name(), compilation->name() ); //album name did not change
    QCOMPARE( album->albumArtist()->name(), track->artist()->name() ); //artist is the same
    QCOMPARE( sqlAlbum->id(), 2 ); // the albums should be the already existing one

    // tracks 2 and 3 should just stay
}

void
TestSqlAlbum::testUnsetCompilationWithArtistAFeaturingB()
{
    m_storage->query( QStringLiteral("INSERT INTO artists(id,name) VALUES (4,'artist1 feat. artist2');") );

    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (1,'albumAndCompilation1',NULL);") );
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (2,'albumAndCompilation1',1);") );

    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (1,1,'track1',4,1,1,1,1 );") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (2,2,'track2',1,2,1,1,1 );") );

    Meta::TrackPtr track = m_collection->registry()->getTrack( 1 );
    Meta::AlbumPtr compilation = track->album();
    Meta::SqlAlbum *sqlCompilation = static_cast<Meta::SqlAlbum*>( compilation.data() );
    QVERIFY( compilation->isCompilation() );
    sqlCompilation->setCompilation( false );

    Meta::AlbumPtr album;
    Meta::SqlAlbum *sqlAlbum;

    // now the tracks should be in a normal album
    album = track->album();
    sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() );
    QVERIFY( album->hasAlbumArtist() );
    QVERIFY( !album->isCompilation() );
    QCOMPARE( album->name(), compilation->name() ); //album name did not change
    QCOMPARE( track->artist()->name(), QStringLiteral( "artist1 feat. artist2" ) );
    QCOMPARE( album->albumArtist()->name(), QStringLiteral("artist1") ); // artist is the same
    QCOMPARE( sqlAlbum->id(), 2 ); // the albums should be the already existing one
}

void
TestSqlAlbum::testUnsetCompilationWithMultipleArtists()
{
    m_storage->query( QStringLiteral("INSERT INTO albums(id,name,artist) VALUES (1,'album1',NULL);") );

    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (1,1,'track1',1,1,1,1,1 );") );
    m_storage->query( QStringLiteral("INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) "
                      "VALUES (2,2,'track2',2,1,1,1,1 );") );

    Meta::TrackPtr track = m_collection->registry()->getTrack( 1 );
    Meta::AlbumPtr compilation = track->album();
    Meta::SqlAlbum *sqlCompilation = static_cast<Meta::SqlAlbum*>( compilation.data() );
    QVERIFY( compilation->isCompilation() );
    sqlCompilation->setCompilation( false );

    Meta::AlbumPtr album;
    Meta::SqlAlbum *sqlAlbum;

    // now the tracks should be in a normal album
    album = track->album();
    sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() );
    QVERIFY( album->hasAlbumArtist() );
    QVERIFY( !album->isCompilation() );
    QCOMPARE( album->name(), compilation->name() ); //album name did not change
    QCOMPARE( album->albumArtist()->name(), track->artist()->name() ); //artist is the same
    QVERIFY( sqlAlbum->id() != 1 ); // the albums should be a new one

    track = m_collection->registry()->getTrack( 2 );
    album = track->album();
    sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() );
    QVERIFY( album->hasAlbumArtist() );
    QVERIFY( !album->isCompilation() );
    QCOMPARE( album->name(), compilation->name() ); //album name did not change
    QCOMPARE( album->albumArtist()->name(), track->artist()->name() ); //artist is the same
    QVERIFY( sqlAlbum->id() != 1 ); // the albums should be a new one
}