File indexing completed on 2024-05-12 05:09:57
0001 /*************************************************************************** 0002 Copyright (C) 2009-2011 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #undef QT_NO_CAST_FROM_ASCII 0026 0027 #include "discogsfetchertest.h" 0028 #include "../fetch/discogsfetcher.h" 0029 #include "../entry.h" 0030 #include "../collections/musiccollection.h" 0031 #include "../images/imagefactory.h" 0032 #include "../images/image.h" 0033 0034 #include <KConfigGroup> 0035 #include <KLocalizedString> 0036 0037 #include <QTest> 0038 0039 QTEST_GUILESS_MAIN( DiscogsFetcherTest ) 0040 0041 DiscogsFetcherTest::DiscogsFetcherTest() : AbstractFetcherTest() 0042 , m_needToWait(false) { 0043 } 0044 0045 void DiscogsFetcherTest::initTestCase() { 0046 Tellico::ImageFactory::init(); 0047 m_hasConfigFile = QFile::exists(QFINDTESTDATA("tellicotest_private.config")); 0048 if(m_hasConfigFile) { 0049 m_config = KSharedConfig::openConfig(QFINDTESTDATA("tellicotest_private.config"), KConfig::SimpleConfig); 0050 } 0051 } 0052 0053 void DiscogsFetcherTest::cleanup() { 0054 m_needToWait = true; 0055 } 0056 0057 void DiscogsFetcherTest::testTitle() { 0058 QString groupName = QStringLiteral("Discogs"); 0059 if(!m_hasConfigFile || !m_config->hasGroup(groupName)) { 0060 QSKIP("This test requires a config file with Discogs settings.", SkipAll); 0061 } 0062 KConfigGroup cg(m_config, groupName); 0063 0064 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Title, 0065 QStringLiteral("Anywhere But Home")); 0066 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DiscogsFetcher(this)); 0067 fetcher->readConfig(cg); 0068 QVERIFY(fetcher->canSearch(request.key())); 0069 0070 Tellico::Data::EntryList results = DO_FETCH(fetcher, request); 0071 0072 QVERIFY(results.size() > 0); 0073 Tellico::Data::EntryPtr entry; // results can be randomly ordered, loop until we find the one we want 0074 for(int i = 0; i < results.size(); ++i) { 0075 Tellico::Data::EntryPtr test = results.at(i); 0076 if(test->field(QStringLiteral("artist")).toLower() == QStringLiteral("evanescence")) { 0077 entry = test; 0078 break; 0079 } else { 0080 qDebug() << "skipping" << test->title() << test->field(QStringLiteral("artist")); 0081 } 0082 } 0083 QVERIFY(entry); 0084 0085 QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Anywhere But Home")); 0086 QVERIFY(!entry->field(QStringLiteral("artist")).isEmpty()); 0087 QVERIFY(!entry->field(QStringLiteral("label")).isEmpty()); 0088 QVERIFY(!entry->field(QStringLiteral("genre")).isEmpty()); 0089 QVERIFY(!entry->field(QStringLiteral("year")).isEmpty()); 0090 QVERIFY(!entry->field(QStringLiteral("track")).isEmpty()); 0091 0092 QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty()); 0093 const Tellico::Data::Image& img = Tellico::ImageFactory::imageById(entry->field(QStringLiteral("cover"))); 0094 QVERIFY(!img.isNull()); 0095 } 0096 0097 void DiscogsFetcherTest::testPerson() { 0098 // the total test case ends up exceeding the throttle limit so pause for a second 0099 if(m_needToWait) QTest::qWait(5000); 0100 0101 QString groupName = QStringLiteral("Discogs"); 0102 if(!m_hasConfigFile || !m_config->hasGroup(groupName)) { 0103 QSKIP("This test requires a config file with Discogs settings.", SkipAll); 0104 } 0105 KConfigGroup cg(m_config, groupName); 0106 0107 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Person, 0108 QStringLiteral("Evanescence")); 0109 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DiscogsFetcher(this)); 0110 fetcher->readConfig(cg); 0111 QVERIFY(fetcher->canSearch(request.key())); 0112 0113 static_cast<Tellico::Fetch::DiscogsFetcher*>(fetcher.data())->setLimit(1); 0114 Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1); 0115 0116 QCOMPARE(results.size(), 1); 0117 0118 Tellico::Data::EntryPtr entry = results.at(0); 0119 QCOMPARE(entry->field(QStringLiteral("artist")), QStringLiteral("Evanescence")); 0120 QVERIFY(!entry->field(QStringLiteral("title")).isEmpty()); 0121 QVERIFY(!entry->field(QStringLiteral("label")).isEmpty()); 0122 0123 QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty()); 0124 const Tellico::Data::Image& img = Tellico::ImageFactory::imageById(entry->field(QStringLiteral("cover"))); 0125 QVERIFY(!img.isNull()); 0126 } 0127 0128 void DiscogsFetcherTest::testKeyword() { 0129 // the total test case ends up exceeding the throttle limit so pause for a second 0130 if(m_needToWait) QTest::qWait(5000); 0131 0132 QString groupName = QStringLiteral("Discogs"); 0133 if(!m_hasConfigFile || !m_config->hasGroup(groupName)) { 0134 QSKIP("This test requires a config file with Discogs settings.", SkipAll); 0135 } 0136 KConfigGroup cg(m_config, groupName); 0137 0138 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Keyword, 0139 QStringLiteral("Fallen Evanescence 2004")); 0140 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DiscogsFetcher(this)); 0141 fetcher->readConfig(cg); 0142 QVERIFY(fetcher->canSearch(request.key())); 0143 0144 static_cast<Tellico::Fetch::DiscogsFetcher*>(fetcher.data())->setLimit(1); 0145 Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1); 0146 0147 QCOMPARE(results.size(), 1); 0148 0149 Tellico::Data::EntryPtr entry = results.at(0); 0150 QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Fallen")); 0151 QCOMPARE(entry->field(QStringLiteral("artist")), QStringLiteral("Evanescence")); 0152 QVERIFY(!entry->field(QStringLiteral("label")).isEmpty()); 0153 QVERIFY(!entry->field(QStringLiteral("year")).isEmpty()); 0154 0155 QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty()); 0156 const Tellico::Data::Image& img = Tellico::ImageFactory::imageById(entry->field(QStringLiteral("cover"))); 0157 QVERIFY(!img.isNull()); 0158 } 0159 0160 void DiscogsFetcherTest::testBarcode() { 0161 // the total test case ends up exceeding the throttle limit so pause for a second 0162 if(m_needToWait) QTest::qWait(5000); 0163 0164 QString groupName = QStringLiteral("Discogs"); 0165 if(!m_hasConfigFile || !m_config->hasGroup(groupName)) { 0166 QSKIP("This test requires a config file with Discogs settings.", SkipAll); 0167 } 0168 KConfigGroup cg(m_config, groupName); 0169 0170 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::UPC, 0171 QStringLiteral("4 547366 014099")); 0172 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DiscogsFetcher(this)); 0173 fetcher->readConfig(cg); 0174 QVERIFY(fetcher->canSearch(request.key())); 0175 0176 static_cast<Tellico::Fetch::DiscogsFetcher*>(fetcher.data())->setLimit(1); 0177 Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1); 0178 0179 QCOMPARE(results.size(), 1); 0180 0181 Tellico::Data::EntryPtr entry = results.at(0); 0182 QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Fallen")); 0183 QCOMPARE(entry->field(QStringLiteral("artist")), QStringLiteral("Evanescence")); 0184 QVERIFY(!entry->field(QStringLiteral("label")).isEmpty()); 0185 QVERIFY(!entry->field(QStringLiteral("year")).isEmpty()); 0186 QCOMPARE(entry->field(QStringLiteral("barcode")), QStringLiteral("4 547366 014099")); 0187 0188 QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty()); 0189 const Tellico::Data::Image& img = Tellico::ImageFactory::imageById(entry->field(QStringLiteral("cover"))); 0190 QVERIFY(!img.isNull()); 0191 } 0192 0193 // use the Raw query type to fully test the data for a Discogs release 0194 void DiscogsFetcherTest::testRawData() { 0195 if(m_needToWait) QTest::qWait(5000); 0196 0197 QString groupName = QStringLiteral("Discogs"); 0198 if(!m_hasConfigFile || !m_config->hasGroup(groupName)) { 0199 QSKIP("This test requires a config file with Discogs settings.", SkipAll); 0200 } 0201 KConfigGroup cg(m_config, groupName); 0202 0203 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Raw, 0204 QStringLiteral("q=r1588789")); 0205 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DiscogsFetcher(this)); 0206 fetcher->readConfig(cg); 0207 0208 static_cast<Tellico::Fetch::DiscogsFetcher*>(fetcher.data())->setLimit(1); 0209 Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1); 0210 0211 QCOMPARE(results.size(), 1); 0212 0213 Tellico::Data::EntryPtr entry = results.at(0); 0214 QVERIFY(entry->field(QStringLiteral("title")).contains(QStringLiteral("Anywhere But Home"))); 0215 QCOMPARE(entry->field(QStringLiteral("artist")), QStringLiteral("Evanescence")); 0216 QCOMPARE(entry->field(QStringLiteral("label")), QStringLiteral("Wind-Up")); 0217 QCOMPARE(entry->field(QStringLiteral("year")), QStringLiteral("2004")); 0218 QCOMPARE(entry->field(QStringLiteral("genre")), QStringLiteral("Rock")); 0219 QCOMPARE(entry->field(QStringLiteral("discogs")), QStringLiteral("https://www.discogs.com/release/1588789-Evanescence-Anywhere-But-Home")); 0220 QCOMPARE(entry->field(QStringLiteral("nationality")), QStringLiteral("Australia & New Zealand")); 0221 QCOMPARE(entry->field(QStringLiteral("medium")), QStringLiteral("Compact Disc")); 0222 QCOMPARE(entry->field(QStringLiteral("catno")), QStringLiteral("5192073000")); 0223 0224 QStringList trackList = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("track"))); 0225 QCOMPARE(trackList.count(), 14); 0226 QCOMPARE(trackList.at(0), QStringLiteral("Haunted::Evanescence::4:04")); 0227 0228 QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty()); 0229 } 0230 0231 // do another check to make sure the Vinyl format is captured 0232 void DiscogsFetcherTest::testRawDataVinyl() { 0233 if(m_needToWait) QTest::qWait(5000); 0234 0235 QString groupName = QStringLiteral("Discogs"); 0236 if(!m_hasConfigFile || !m_config->hasGroup(groupName)) { 0237 QSKIP("This test requires a config file with Discogs settings.", SkipAll); 0238 } 0239 KConfigGroup cg(m_config, groupName); 0240 0241 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Raw, 0242 QStringLiteral("q=r456552")); 0243 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DiscogsFetcher(this)); 0244 fetcher->readConfig(cg); 0245 0246 static_cast<Tellico::Fetch::DiscogsFetcher*>(fetcher.data())->setLimit(1); 0247 Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1); 0248 0249 QCOMPARE(results.size(), 1); 0250 0251 Tellico::Data::EntryPtr entry = results.at(0); 0252 QVERIFY(entry->field(QStringLiteral("title")).contains(QStringLiteral("The Clash"))); 0253 QCOMPARE(entry->field(QStringLiteral("artist")), QStringLiteral("The Clash")); 0254 QCOMPARE(entry->field(QStringLiteral("label")), QStringLiteral("CBS; CBS")); 0255 // QCOMPARE(entry->field(QStringLiteral("year")), QStringLiteral("1977")); 0256 QCOMPARE(entry->field(QStringLiteral("genre")), QStringLiteral("Rock")); 0257 QCOMPARE(entry->field(QStringLiteral("discogs")), QStringLiteral("https://www.discogs.com/release/456552-The-Clash-The-Clash")); 0258 QCOMPARE(entry->field(QStringLiteral("nationality")), QStringLiteral("UK")); 0259 QCOMPARE(entry->field(QStringLiteral("medium")), QStringLiteral("Vinyl")); 0260 0261 QStringList trackList = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("track"))); 0262 QCOMPARE(trackList.count(), 14); 0263 QCOMPARE(trackList.at(0), QStringLiteral("Janie Jones::The Clash::2:05")); 0264 } 0265 0266 void DiscogsFetcherTest::testUpdate() { 0267 Tellico::Data::CollPtr coll(new Tellico::Data::MusicCollection(true)); 0268 Tellico::Data::EntryPtr entry(new Tellico::Data::Entry(coll)); 0269 entry->setField(QStringLiteral("title"), QStringLiteral("Nevermind")); 0270 entry->setField(QStringLiteral("artist"), QStringLiteral("Nirvana")); 0271 entry->setField(QStringLiteral("year"), QStringLiteral("1991")); 0272 coll->addEntries(entry); 0273 0274 Tellico::Fetch::DiscogsFetcher fetcher(this); 0275 auto request = fetcher.updateRequest(entry); 0276 QCOMPARE(request.key(), Tellico::Fetch::Raw); 0277 QVERIFY(request.value().contains(QStringLiteral("title=Nevermind"))); 0278 QVERIFY(request.value().contains(QStringLiteral("artist=Nirvana"))); 0279 QVERIFY(request.value().contains(QStringLiteral("year=1991"))); 0280 } 0281 0282 // bug 479503, https://bugs.kde.org/show_bug.cgi?id=479503 0283 void DiscogsFetcherTest::testMultiDisc() { 0284 // the total test case ends up exceeding the throttle limit so pause for a second 0285 if(m_needToWait) QTest::qWait(5000); 0286 0287 QString groupName = QStringLiteral("Discogs"); 0288 if(!m_hasConfigFile || !m_config->hasGroup(groupName)) { 0289 QSKIP("This test requires a config file with Discogs settings.", SkipAll); 0290 } 0291 KConfigGroup cg(m_config, groupName); 0292 0293 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::UPC, 0294 QStringLiteral("4988031446843")); 0295 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DiscogsFetcher(this)); 0296 fetcher->readConfig(cg); 0297 0298 Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1); 0299 0300 QVERIFY(!results.isEmpty()); 0301 0302 Tellico::Data::EntryPtr entry = results.at(0); 0303 QCOMPARE(entry->title(), QStringLiteral("Silver Lining Suite")); 0304 auto trackField = entry->collection()->fieldByName(QStringLiteral("track")); 0305 QVERIFY(trackField); 0306 // verify the title was updated to include the disc number 0307 QVERIFY(trackField->title() != i18n("Tracks")); 0308 QStringList tracks1 = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("track"))); 0309 QCOMPARE(tracks1.count(), 9); 0310 QCOMPARE(tracks1.first(), QStringLiteral("Isolation::Hiromi Uehara::")); 0311 // new field with Disc 2 tracks 0312 QStringList tracks2 = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("track2"))); 0313 QCOMPARE(tracks2.count(), 8); 0314 QCOMPARE(tracks2.first(), QStringLiteral("Somewhere::Hiromi Uehara::")); 0315 }