File indexing completed on 2024-11-10 04:22:06
0001 /**************************************************************************************** 0002 * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick@googlemail.com> * 0003 * Copyright (c) 2010 Ralf Engels <ralf-engels@gmx.de> * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify it under * 0006 * the terms of the GNU General Public License as published by the Free Software * 0007 * Foundation; either version 2 of the License, or (at your option) any later * 0008 * version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0012 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License along with * 0015 * this program. If not, see <http://www.gnu.org/licenses/>. * 0016 ****************************************************************************************/ 0017 0018 #include "TestSqlAlbum.h" 0019 0020 #include "amarokconfig.h" 0021 #include "core/meta/Meta.h" 0022 #include "core-impl/storage/sql/mysqlestorage/MySqlEmbeddedStorage.h" 0023 #include "SqlCollection.h" 0024 #include "SqlMountPointManagerMock.h" 0025 0026 #include "core/capabilities/ActionsCapability.h" 0027 #include "core/capabilities/BookmarkThisCapability.h" 0028 #include "covermanager/CoverFetcher.h" 0029 0030 #include <QFile> 0031 #include <QImage> 0032 0033 QTEST_MAIN( TestSqlAlbum ) 0034 0035 TestSqlAlbum::TestSqlAlbum() 0036 : QObject() 0037 , m_collection( nullptr ) 0038 , m_storage( nullptr ) 0039 , m_tmpDir( nullptr ) 0040 { 0041 } 0042 0043 TestSqlAlbum::~TestSqlAlbum() 0044 { } 0045 0046 void 0047 TestSqlAlbum::initTestCase() 0048 { 0049 AmarokConfig::instance("amarokrc"); 0050 0051 m_tmpDir = new QTemporaryDir(); 0052 QVERIFY( m_tmpDir->isValid() ); 0053 m_storage = QSharedPointer<MySqlEmbeddedStorage>( new MySqlEmbeddedStorage() ); 0054 QVERIFY( m_storage->init( m_tmpDir->path() ) ); 0055 m_collection = new Collections::SqlCollection( m_storage ); 0056 m_collection->setMountPointManager( new SqlMountPointManagerMock( this, m_storage ) ); 0057 } 0058 0059 void 0060 TestSqlAlbum::cleanupTestCase() 0061 { 0062 delete m_collection; 0063 delete m_tmpDir; 0064 } 0065 0066 void 0067 TestSqlAlbum::init() 0068 { 0069 //setup base data 0070 m_storage->query( "INSERT INTO artists(id, name) VALUES (1, 'artist1');" ); 0071 m_storage->query( "INSERT INTO artists(id, name) VALUES (2, 'artist2');" ); 0072 m_storage->query( "INSERT INTO artists(id, name) VALUES (3, 'artist3');" ); 0073 0074 m_storage->query( "INSERT INTO composers(id, name) VALUES (1, 'composer1');" ); 0075 m_storage->query( "INSERT INTO genres(id, name) VALUES (1, 'genre1');" ); 0076 m_storage->query( "INSERT INTO years(id, name) VALUES (1, '1');" ); 0077 0078 m_storage->query( "INSERT INTO directories(id, deviceid, dir) VALUES (1, -1, './');" ); 0079 0080 m_storage->query( "INSERT INTO urls(id, deviceid, rpath, directory, uniqueid) VALUES (1, -1, './IDoNotExist.mp3', 1, 'uid://1');" ); 0081 m_storage->query( "INSERT INTO urls(id, deviceid, rpath, directory, uniqueid) VALUES (2, -1, './IDoNotExistAsWell.mp3', 1, 'uid://2');" ); 0082 m_storage->query( "INSERT INTO urls(id, deviceid, rpath, directory, uniqueid) VALUES (3, -1, './MeNeither.mp3', 1, 'uid:/3');" ); 0083 m_storage->query( "INSERT INTO urls(id, deviceid, rpath, directory, uniqueid) VALUES (4, -1, './MeNeitherToo.mp3', 1, 'uid:/4');" ); 0084 0085 CoverFetcher::destroy(); 0086 0087 m_collection->registry()->emptyCache(); 0088 } 0089 0090 void 0091 TestSqlAlbum::cleanup() 0092 { 0093 m_storage->query( "TRUNCATE TABLE years;" ); 0094 m_storage->query( "TRUNCATE TABLE genres;" ); 0095 m_storage->query( "TRUNCATE TABLE composers;" ); 0096 m_storage->query( "TRUNCATE TABLE albums;" ); 0097 m_storage->query( "TRUNCATE TABLE artists;" ); 0098 m_storage->query( "TRUNCATE TABLE tracks;" ); 0099 m_storage->query( "TRUNCATE TABLE urls;" ); 0100 m_storage->query( "TRUNCATE TABLE directories;" ); 0101 m_storage->query( "TRUNCATE TABLE urls_labels;" ); 0102 } 0103 0104 void 0105 TestSqlAlbum::testTracks() 0106 { 0107 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (1, 'album1',1);" ); 0108 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (2, 'album2',1);" ); 0109 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0110 "VALUES (1,1,'track1',1,1,1,1,1 );" ); 0111 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0112 "VALUES (2,2,'track2',1,1,1,1,1 );" ); 0113 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0114 "VALUES (3,3,'trackInOtherAlbum',1,2,1,1,1 );" ); 0115 0116 Meta::AlbumPtr album = m_collection->registry()->getAlbum( 1 ); 0117 Meta::SqlAlbum *sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() ); 0118 0119 // check two tracks 0120 Meta::TrackList tracks = sqlAlbum->tracks(); 0121 QCOMPARE( sqlAlbum->tracks().count(), 2 ); 0122 0123 // remove one track 0124 Meta::SqlTrack *sqlTrack = static_cast<Meta::SqlTrack*>( tracks.first().data() ); 0125 sqlTrack->setAlbum( "newAlbum" ); 0126 QCOMPARE( sqlAlbum->tracks().count(), 1 ); 0127 } 0128 0129 void 0130 TestSqlAlbum::testIsCompilation() 0131 { 0132 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (1, 'album1',1);" ); 0133 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0134 "VALUES (1,1,'track1',1,1,1,1,1 );" ); 0135 0136 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (2, 'compilation',NULL);" ); 0137 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0138 "VALUES (2,2,'track2',1,1,1,1,1 );" ); 0139 0140 Meta::AlbumPtr album = m_collection->registry()->getAlbum( 1 ); 0141 QVERIFY( !album->isCompilation() ); 0142 QVERIFY( album->hasAlbumArtist() ); 0143 0144 album = m_collection->registry()->getAlbum( 2 ); 0145 QVERIFY( album->isCompilation() ); 0146 QVERIFY( !album->hasAlbumArtist() ); 0147 } 0148 0149 void 0150 TestSqlAlbum::testAlbumArtist() 0151 { 0152 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (1, 'album1',1);" ); 0153 0154 Meta::AlbumPtr album = m_collection->registry()->getAlbum( 1 ); 0155 QVERIFY( !album->isCompilation() ); 0156 QVERIFY( album->hasAlbumArtist() ); 0157 QVERIFY( album->albumArtist() ); 0158 QCOMPARE( album->albumArtist()->name(), QString( "artist1" ) ); 0159 } 0160 0161 void 0162 TestSqlAlbum::testImage() 0163 { 0164 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (1, 'imageTestAlbum',1);" ); 0165 0166 Meta::AlbumPtr album = m_collection->registry()->getAlbum( 1 ); 0167 Meta::SqlAlbum *sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() ); 0168 sqlAlbum->setSuppressImageAutoFetch( true ); 0169 0170 QVERIFY( !album->hasImage() ); 0171 QVERIFY( !album->hasImage(100) ); 0172 QVERIFY( !album->hasImage(0) ); 0173 0174 QVERIFY( album->canUpdateImage() ); 0175 0176 // set an image 0177 sqlAlbum->setImage( QImage( QSize(100, 100), QImage::Format_Mono ) ); 0178 QVERIFY( album->hasImage() ); 0179 QVERIFY( album->hasImage(100) ); 0180 QVERIFY( album->hasImage(0) ); 0181 QCOMPARE( album->image(47).width(), 47 ); 0182 QString largeImagePath = album->imageLocation().path(); 0183 QVERIFY( QFile::exists( largeImagePath ) ); 0184 0185 // remove the image 0186 album->removeImage(); 0187 QVERIFY( !album->hasImage() ); 0188 QVERIFY( !album->hasImage(100) ); 0189 QVERIFY( !album->hasImage(0) ); 0190 QVERIFY( !QFile::exists( largeImagePath ) ); 0191 0192 // IMPROVEMENT: also test embedded image and an image outside the cache directory 0193 } 0194 0195 void 0196 TestSqlAlbum::testCapabilities() 0197 { 0198 Meta::AlbumPtr album = m_collection->registry()->getAlbum( "Test album", "test artist" ); 0199 0200 QVERIFY( album->hasCapabilityInterface( Capabilities::Capability::Actions ) ); 0201 Capabilities::ActionsCapability *ac = album->create<Capabilities::ActionsCapability>(); 0202 QVERIFY( ac ); 0203 QVERIFY( ac->actions().count() > 4 ); 0204 0205 QVERIFY( album->hasCapabilityInterface( Capabilities::Capability::BookmarkThis ) ); 0206 Capabilities::BookmarkThisCapability *btc = album->create<Capabilities::BookmarkThisCapability>(); 0207 QVERIFY( btc ); 0208 QVERIFY( btc->bookmarkAction() ); 0209 0210 // need to delete the actions. They hold pointers that prevent the registry from cleaning 0211 // it's state. 0212 foreach( QAction *action, ac->actions() ) 0213 delete action; 0214 delete btc->bookmarkAction(); 0215 delete ac; 0216 delete btc; 0217 } 0218 0219 void 0220 TestSqlAlbum::testSetCompilationWithoutExistingCompilation() 0221 { 0222 m_collection->registry()->emptyCache(); 0223 0224 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (1, 'album1',1);" ); 0225 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0226 "VALUES (1,1,'track1',1,1,1,1,1 );" ); 0227 0228 Meta::TrackPtr track = m_collection->registry()->getTrack( 1 ); 0229 // auto newAlbum = m_collection->registry()->getAlbum(1); 0230 Meta::AlbumPtr album = track->album(); 0231 auto sqlAlbum = AmarokSharedPointer<Meta::SqlAlbum>::staticCast( album ); 0232 QVERIFY( album->hasAlbumArtist() ); 0233 QVERIFY( !album->isCompilation() ); 0234 QCOMPARE( sqlAlbum->id(), 1 ); 0235 QCOMPARE( album->tracks().count(), 1 ); 0236 sqlAlbum->setCompilation( true ); 0237 0238 // now the track should be in the compilation 0239 album = track->album(); 0240 sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() ); 0241 QCOMPARE( sqlAlbum->id(), 2 ); // a new album 0242 QVERIFY( !album->hasAlbumArtist() ); 0243 QVERIFY( album->isCompilation() ); 0244 } 0245 0246 void 0247 TestSqlAlbum::testSetCompilationWithExistingCompilation() 0248 { 0249 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (1, 'albumAndCompilation1',1);" ); 0250 m_storage->query( "INSERT INTO albums(id,name) VALUES (2, 'albumAndCompilation1');" ); 0251 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0252 "VALUES (1,1,'track1',1,1,1,1,1 );" ); 0253 Meta::AlbumPtr compilation = m_collection->registry()->getAlbum( 2 ); 0254 QVERIFY( compilation->isCompilation() ); 0255 0256 Meta::TrackPtr track = m_collection->registry()->getTrack( 1 ); 0257 Meta::AlbumPtr album = track->album(); 0258 Meta::SqlAlbum *sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() ); 0259 QVERIFY( album->hasAlbumArtist() ); 0260 QVERIFY( !album->isCompilation() ); 0261 QCOMPARE( sqlAlbum->id(), 1 ); 0262 QCOMPARE( album->tracks().count(), 1 ); 0263 sqlAlbum->setCompilation( true ); 0264 0265 // now the track should be in the compilation 0266 album = track->album(); 0267 sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() ); 0268 QCOMPARE( sqlAlbum->id(), 2 ); // the existing compilation album 0269 QVERIFY( !album->hasAlbumArtist() ); 0270 QVERIFY( album->isCompilation() ); 0271 QCOMPARE( album, compilation ); //track returns new album, but the same object that we retrieved above 0272 0273 QStringList albumResult = m_storage->query( "SELECT name, artist FROM albums WHERE id = 1" ); 0274 } 0275 0276 void 0277 TestSqlAlbum::testUnsetCompilationWithoutExistingAlbum() 0278 { 0279 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (1, 'album1',NULL);" ); 0280 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0281 "VALUES (1,1,'track1',1,1,1,1,1 );" ); 0282 0283 Meta::TrackPtr track = m_collection->registry()->getTrack( 1 ); 0284 Meta::AlbumPtr album = track->album(); 0285 Meta::SqlAlbum *sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() ); 0286 QVERIFY( !album->hasAlbumArtist() ); 0287 QVERIFY( album->isCompilation() ); 0288 sqlAlbum->setCompilation( false ); 0289 0290 // now the track should be in a normal album 0291 album = track->album(); 0292 sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() ); 0293 QVERIFY( album->hasAlbumArtist() ); 0294 QVERIFY( !album->isCompilation() ); 0295 QCOMPARE( album->name(), QString( "album1" ) ); //album name did not change 0296 QCOMPARE( album->albumArtist()->name(), track->artist()->name() ); //artist is the same 0297 } 0298 0299 void 0300 TestSqlAlbum::testUnsetCompilationWithExistingAlbum() 0301 { 0302 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (1, 'albumAndCompilation1',NULL);" ); 0303 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (2, 'albumAndCompilation1',1);" ); 0304 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0305 "VALUES (1,1,'track1',1,1,1,1,1 );" ); 0306 0307 Meta::TrackPtr track = m_collection->registry()->getTrack( 1 ); 0308 Meta::AlbumPtr compilation = track->album(); 0309 Meta::SqlAlbum *sqlCompilation = static_cast<Meta::SqlAlbum*>( compilation.data() ); 0310 QVERIFY( compilation->isCompilation() ); 0311 sqlCompilation->setCompilation( false ); 0312 0313 // now the track should be in a normal album 0314 Meta::AlbumPtr album = track->album(); 0315 Meta::SqlAlbum *sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() ); 0316 QCOMPARE( sqlAlbum->id(), 2 ); // the albums should be the already existing one 0317 QVERIFY( album->hasAlbumArtist() ); 0318 QVERIFY( !album->isCompilation() ); 0319 QCOMPARE( album->name(), compilation->name() ); //album name did not change 0320 QCOMPARE( album->albumArtist()->name(), track->artist()->name() ); //artist is the same 0321 } 0322 0323 void 0324 TestSqlAlbum::testUnsetCompilationWithMultipleExistingAlbums() 0325 { 0326 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (1,'albumAndCompilation1',NULL);" ); 0327 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (2,'albumAndCompilation1',1);" ); 0328 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (3,'albumAndCompilation1',2);" ); 0329 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0330 "VALUES (1,1,'track1',1,1,1,1,1 );" ); 0331 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0332 "VALUES (2,2,'track2',2,3,1,1,1 );" ); 0333 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0334 "VALUES (3,3,'track3',1,2,1,1,1 );" ); 0335 0336 Meta::TrackPtr track = m_collection->registry()->getTrack( 1 ); 0337 Meta::AlbumPtr compilation = track->album(); 0338 Meta::SqlAlbum *sqlCompilation = static_cast<Meta::SqlAlbum*>( compilation.data() ); 0339 QVERIFY( compilation->isCompilation() ); 0340 sqlCompilation->setCompilation( false ); 0341 0342 Meta::AlbumPtr album; 0343 Meta::SqlAlbum *sqlAlbum; 0344 0345 // now the tracks should be in a normal album 0346 album = track->album(); 0347 sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() ); 0348 QVERIFY( album->hasAlbumArtist() ); 0349 QVERIFY( !album->isCompilation() ); 0350 QCOMPARE( album->name(), compilation->name() ); //album name did not change 0351 QCOMPARE( album->albumArtist()->name(), track->artist()->name() ); //artist is the same 0352 QCOMPARE( sqlAlbum->id(), 2 ); // the albums should be the already existing one 0353 0354 // tracks 2 and 3 should just stay 0355 } 0356 0357 void 0358 TestSqlAlbum::testUnsetCompilationWithArtistAFeaturingB() 0359 { 0360 m_storage->query( "INSERT INTO artists(id,name) VALUES (4,'artist1 feat. artist2');" ); 0361 0362 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (1,'albumAndCompilation1',NULL);" ); 0363 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (2,'albumAndCompilation1',1);" ); 0364 0365 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0366 "VALUES (1,1,'track1',4,1,1,1,1 );" ); 0367 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0368 "VALUES (2,2,'track2',1,2,1,1,1 );" ); 0369 0370 Meta::TrackPtr track = m_collection->registry()->getTrack( 1 ); 0371 Meta::AlbumPtr compilation = track->album(); 0372 Meta::SqlAlbum *sqlCompilation = static_cast<Meta::SqlAlbum*>( compilation.data() ); 0373 QVERIFY( compilation->isCompilation() ); 0374 sqlCompilation->setCompilation( false ); 0375 0376 Meta::AlbumPtr album; 0377 Meta::SqlAlbum *sqlAlbum; 0378 0379 // now the tracks should be in a normal album 0380 album = track->album(); 0381 sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() ); 0382 QVERIFY( album->hasAlbumArtist() ); 0383 QVERIFY( !album->isCompilation() ); 0384 QCOMPARE( album->name(), compilation->name() ); //album name did not change 0385 QCOMPARE( track->artist()->name(), QString( "artist1 feat. artist2" ) ); 0386 QCOMPARE( album->albumArtist()->name(), QString("artist1") ); // artist is the same 0387 QCOMPARE( sqlAlbum->id(), 2 ); // the albums should be the already existing one 0388 } 0389 0390 void 0391 TestSqlAlbum::testUnsetCompilationWithMultipleArtists() 0392 { 0393 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (1,'album1',NULL);" ); 0394 0395 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0396 "VALUES (1,1,'track1',1,1,1,1,1 );" ); 0397 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0398 "VALUES (2,2,'track2',2,1,1,1,1 );" ); 0399 0400 Meta::TrackPtr track = m_collection->registry()->getTrack( 1 ); 0401 Meta::AlbumPtr compilation = track->album(); 0402 Meta::SqlAlbum *sqlCompilation = static_cast<Meta::SqlAlbum*>( compilation.data() ); 0403 QVERIFY( compilation->isCompilation() ); 0404 sqlCompilation->setCompilation( false ); 0405 0406 Meta::AlbumPtr album; 0407 Meta::SqlAlbum *sqlAlbum; 0408 0409 // now the tracks should be in a normal album 0410 album = track->album(); 0411 sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() ); 0412 QVERIFY( album->hasAlbumArtist() ); 0413 QVERIFY( !album->isCompilation() ); 0414 QCOMPARE( album->name(), compilation->name() ); //album name did not change 0415 QCOMPARE( album->albumArtist()->name(), track->artist()->name() ); //artist is the same 0416 QVERIFY( sqlAlbum->id() != 1 ); // the albums should be a new one 0417 0418 track = m_collection->registry()->getTrack( 2 ); 0419 album = track->album(); 0420 sqlAlbum = static_cast<Meta::SqlAlbum*>( album.data() ); 0421 QVERIFY( album->hasAlbumArtist() ); 0422 QVERIFY( !album->isCompilation() ); 0423 QCOMPARE( album->name(), compilation->name() ); //album name did not change 0424 QCOMPARE( album->albumArtist()->name(), track->artist()->name() ); //artist is the same 0425 QVERIFY( sqlAlbum->id() != 1 ); // the albums should be a new one 0426 } 0427