File indexing completed on 2024-09-22 04:23:31
0001 /**************************************************************************************** 0002 * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick@googlemail.com> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify it under * 0005 * the terms of the GNU General Public License as published by the Free Software * 0006 * Foundation; either version 2 of the License, or (at your option) any later * 0007 * version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0011 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0012 * * 0013 * You should have received a copy of the GNU General Public License along with * 0014 * this program. If not, see <http://www.gnu.org/licenses/>. * 0015 ****************************************************************************************/ 0016 0017 #include "TestDatabaseUpdater.h" 0018 0019 #include "SqlCollection.h" 0020 #include "DatabaseUpdater.h" 0021 #include "core-impl/storage/sql/mysqlestorage/MySqlEmbeddedStorage.h" 0022 0023 #include <QString> 0024 #include <QStringList> 0025 #include <QTemporaryDir> 0026 0027 0028 QTEST_MAIN( DatabaseUpdaterTest ) 0029 0030 0031 DatabaseUpdaterTest::DatabaseUpdaterTest() 0032 : QObject() 0033 { 0034 } 0035 0036 void 0037 DatabaseUpdaterTest::initTestCase() 0038 { 0039 m_tmpDir = new QTemporaryDir(); 0040 QVERIFY( m_tmpDir->isValid() ); 0041 m_storage = QSharedPointer<MySqlEmbeddedStorage>( new MySqlEmbeddedStorage() ); 0042 QVERIFY( m_storage->init( m_tmpDir->path() ) ); 0043 m_collection = new Collections::SqlCollection( m_storage ); 0044 } 0045 0046 void 0047 DatabaseUpdaterTest::cleanupTestCase() 0048 { 0049 delete m_collection; 0050 delete m_tmpDir; 0051 } 0052 0053 void 0054 DatabaseUpdaterTest::cleanup() 0055 { 0056 m_storage->query( "BEGIN" ); 0057 m_storage->query( "TRUNCATE TABLE tracks;" ); 0058 m_storage->query( "TRUNCATE TABLE albums;" ); 0059 m_storage->query( "TRUNCATE TABLE artists;" ); 0060 m_storage->query( "TRUNCATE TABLE composers;" ); 0061 m_storage->query( "TRUNCATE TABLE genres;" ); 0062 m_storage->query( "TRUNCATE TABLE years;" ); 0063 m_storage->query( "TRUNCATE TABLE urls;" ); 0064 m_storage->query( "TRUNCATE TABLE directories;" ); 0065 m_storage->query( "COMMIT" ); 0066 } 0067 0068 void 0069 DatabaseUpdaterTest::testNeedsUpdate() 0070 { 0071 // SqlCollection updates the table by itself 0072 DatabaseUpdater updater( m_collection ); 0073 0074 m_storage->query( QString( "UPDATE admin SET version = %1 WHERE component = 'DB_VERSION';" ).arg( updater.expectedDatabaseVersion() - 1 ) ); 0075 0076 QVERIFY( updater.needsUpdate() ); 0077 QVERIFY( updater.update() ); 0078 QVERIFY( !updater.needsUpdate() ); 0079 } 0080 0081 void 0082 DatabaseUpdaterTest::testNeedsNoUpdate() 0083 { 0084 // SqlCollection updates the table by itself 0085 DatabaseUpdater updater( m_collection ); 0086 0087 QVERIFY( !updater.needsUpdate() ); 0088 QVERIFY( !updater.update() ); 0089 } 0090 0091 void 0092 DatabaseUpdaterTest::testDeleteAllRedundant() 0093 { 0094 //setup base data 0095 m_storage->query( "INSERT INTO artists(id, name) VALUES (1, 'trackArtist');" ); 0096 m_storage->query( "INSERT INTO artists(id, name) VALUES (2, 'albumArtist');" ); 0097 m_storage->query( "INSERT INTO artists(id, name) VALUES (3, 'invalidArtist');" ); 0098 0099 m_storage->query( "INSERT INTO composers(id, name) VALUES (1, 'composer');" ); 0100 m_storage->query( "INSERT INTO composers(id, name) VALUES (2, 'invalidComposer');" ); 0101 m_storage->query( "INSERT INTO genres(id, name) VALUES (1, 'genre');" ); 0102 m_storage->query( "INSERT INTO genres(id, name) VALUES (2, 'invalidGenre');" ); 0103 m_storage->query( "INSERT INTO years(id, name) VALUES (1, '1');" ); 0104 m_storage->query( "INSERT INTO years(id, name) VALUES (2, '2');" ); 0105 0106 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (1, 'album1', 2);" ); 0107 m_storage->query( "INSERT INTO albums(id,name,artist) VALUES (2, 'albumInvalidAlbum',1);" ); 0108 0109 m_storage->query( "INSERT INTO urls(id, deviceid, rpath, uniqueid ) VALUES (1, -1, './track1.mp3', 'uid://1');" ); 0110 m_storage->query( "INSERT INTO urls(id, deviceid, rpath, uniqueid ) VALUES (2, -1, './invalidTrack.mp3', 'uid://2');" ); 0111 0112 m_storage->query( "INSERT INTO tracks(id,url,title,artist,album,genre,year,composer) " 0113 "VALUES (1,1,'track1',1,1,1,1,1 );" ); 0114 0115 DatabaseUpdater updater( m_collection ); 0116 updater.deleteAllRedundant("album"); 0117 updater.deleteAllRedundant("artist"); 0118 updater.deleteAllRedundant("genre"); 0119 updater.deleteAllRedundant("composer"); 0120 updater.deleteAllRedundant("url"); 0121 updater.deleteAllRedundant("year"); 0122 0123 QStringList count; 0124 count = m_storage->query( "SELECT COUNT(*) FROM albums;" ); 0125 QCOMPARE( count.first().toInt(), 1 ); 0126 count = m_storage->query( "SELECT COUNT(*) FROM artists;" ); 0127 QCOMPARE( count.first().toInt(), 2 ); 0128 count = m_storage->query( "SELECT COUNT(*) FROM genres;" ); 0129 QCOMPARE( count.first().toInt(), 1 ); 0130 count = m_storage->query( "SELECT COUNT(*) FROM composers;" ); 0131 QCOMPARE( count.first().toInt(), 1 ); 0132 count = m_storage->query( "SELECT COUNT(*) FROM urls;" ); 0133 QCOMPARE( count.first().toInt(), 1 ); 0134 count = m_storage->query( "SELECT COUNT(*) FROM years;" ); 0135 QCOMPARE( count.first().toInt(), 1 ); 0136 } 0137 0138 void 0139 DatabaseUpdaterTest::testCheckTables() 0140 { 0141 DatabaseUpdater updater( m_collection ); 0142 updater.checkTables(); // just execute it to get test coverage 0143 } 0144 0145 void 0146 DatabaseUpdaterTest::testCreatePermanentTables() 0147 { 0148 // actually the collection will call updater.update itself 0149 // after the update we should have 18 tables 0150 0151 QStringList tables = m_storage->query( "select table_name from INFORMATION_SCHEMA.tables WHERE table_schema='amarok'" ); 0152 QCOMPARE( tables.count(), 18 ); 0153 } 0154