File indexing completed on 2024-05-12 16:46:22
0001 /*************************************************************************** 0002 Copyright (C) 2019-2020 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 "mobygamesfetchertest.h" 0028 0029 #include "../fetch/mobygamesfetcher.h" 0030 #include "../collections/gamecollection.h" 0031 #include "../entry.h" 0032 #include "../images/imagefactory.h" 0033 0034 #include <KConfigGroup> 0035 0036 #include <QTest> 0037 0038 QTEST_GUILESS_MAIN( MobyGamesFetcherTest ) 0039 0040 MobyGamesFetcherTest::MobyGamesFetcherTest() : AbstractFetcherTest(), m_needToWait(false) { 0041 } 0042 0043 void MobyGamesFetcherTest::initTestCase() { 0044 Tellico::ImageFactory::init(); 0045 m_hasConfigFile = QFile::exists(QFINDTESTDATA("tellicotest_private.config")); 0046 if(m_hasConfigFile) { 0047 m_config = KSharedConfig::openConfig(QFINDTESTDATA("tellicotest_private.config"), KConfig::SimpleConfig); 0048 } 0049 } 0050 0051 void MobyGamesFetcherTest::init() { 0052 if(m_needToWait) QTest::qSleep(1000); 0053 m_needToWait = false; 0054 } 0055 0056 void MobyGamesFetcherTest::cleanup() { 0057 m_needToWait = true; 0058 } 0059 0060 void MobyGamesFetcherTest::testTitle() { 0061 const QString groupName = QStringLiteral("MobyGames"); 0062 if(!m_hasConfigFile || !m_config->hasGroup(groupName)) { 0063 QSKIP("This test requires a config file with MobyGames settings.", SkipAll); 0064 } 0065 KConfigGroup cg(m_config, groupName); 0066 0067 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Game, Tellico::Fetch::Title, 0068 QStringLiteral("Twilight Princess")); 0069 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MobyGamesFetcher(this)); 0070 fetcher->readConfig(cg); 0071 0072 // since the platforms are read in the next event loop (single shot timer) 0073 // to avoid downloading again, wait a moment 0074 qApp->processEvents(); 0075 0076 // assuming the Wii result will be first 0077 Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1); 0078 0079 QCOMPARE(results.size(), 1); 0080 0081 Tellico::Data::EntryPtr entry = results.at(0); 0082 QVERIFY(entry); 0083 QCOMPARE(entry->field("title"), QStringLiteral("The Legend of Zelda: Twilight Princess")); 0084 QCOMPARE(entry->field("year"), QStringLiteral("2006")); 0085 QCOMPARE(entry->field("platform"), QStringLiteral("Nintendo Wii")); 0086 QVERIFY(entry->field("genre").contains(QStringLiteral("Action"))); 0087 // QCOMPARE(entry->field("certification"), QStringLiteral("Teen")); 0088 QCOMPARE(entry->field("pegi"), QStringLiteral("PEGI 12")); 0089 QCOMPARE(entry->field("publisher"), QStringLiteral("Nintendo of America Inc.")); 0090 QCOMPARE(entry->field("developer"), QStringLiteral("Nintendo EAD")); 0091 QCOMPARE(entry->field("mobygames"), QStringLiteral("http://www.mobygames.com/game/legend-of-zelda-twilight-princess")); 0092 QVERIFY(!entry->field(QStringLiteral("description")).isEmpty()); 0093 QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty()); 0094 QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/'))); 0095 } 0096 0097 // same search, except to include platform name in search 0098 // which is a Keyword search and the WiiU result should be first 0099 void MobyGamesFetcherTest::testKeyword() { 0100 const QString groupName = QStringLiteral("MobyGames"); 0101 if(!m_hasConfigFile || !m_config->hasGroup(groupName)) { 0102 QSKIP("This test requires a config file with MobyGames settings.", SkipAll); 0103 } 0104 KConfigGroup cg(m_config, groupName); 0105 0106 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Game, Tellico::Fetch::Keyword, 0107 QStringLiteral("Twilight Princess Nintendo WiiU")); 0108 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MobyGamesFetcher(this)); 0109 fetcher->readConfig(cg); 0110 0111 // since the platforms are read in the next event loop (single shot timer) 0112 // to avoid downloading again, wait a moment 0113 qApp->processEvents(); 0114 0115 Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1); 0116 0117 QCOMPARE(results.size(), 1); 0118 0119 Tellico::Data::EntryPtr entry = results.at(0); 0120 QVERIFY(entry); 0121 QCOMPARE(entry->field("title"), QStringLiteral("The Legend of Zelda: Twilight Princess")); 0122 QCOMPARE(entry->field("year"), QStringLiteral("2016")); 0123 QCOMPARE(entry->field("platform"), QStringLiteral("Nintendo WiiU")); 0124 } 0125 0126 void MobyGamesFetcherTest::testRaw() { 0127 // MobyGames2 group has no image 0128 const QString groupName = QStringLiteral("MobyGames2"); 0129 if(!m_hasConfigFile || !m_config->hasGroup(groupName)) { 0130 QSKIP("This test requires a config file with MobyGames settings.", SkipAll); 0131 } 0132 KConfigGroup cg(m_config, groupName); 0133 0134 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Game, Tellico::Fetch::Raw, 0135 QStringLiteral("id=25103&platform=82")); 0136 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MobyGamesFetcher(this)); 0137 fetcher->readConfig(cg); 0138 0139 // since the platforms are read in the next event loop (single shot timer) 0140 // to avoid downloading again, wait a moment 0141 qApp->processEvents(); 0142 0143 Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1); 0144 0145 QCOMPARE(results.size(), 1); 0146 0147 Tellico::Data::EntryPtr entry = results.at(0); 0148 QVERIFY(entry); 0149 QCOMPARE(entry->field("title"), QStringLiteral("The Legend of Zelda: Twilight Princess")); 0150 QCOMPARE(entry->field("year"), QStringLiteral("2006")); 0151 QCOMPARE(entry->field("platform"), QStringLiteral("Nintendo Wii")); 0152 QVERIFY(entry->field("genre").contains(QStringLiteral("Action"))); 0153 // QCOMPARE(entry->field("certification"), QStringLiteral("Teen")); 0154 QCOMPARE(entry->field("pegi"), QStringLiteral("PEGI 12")); 0155 QCOMPARE(entry->field("publisher"), QStringLiteral("Nintendo of America Inc.")); 0156 QCOMPARE(entry->field("developer"), QStringLiteral("Nintendo EAD")); 0157 QCOMPARE(entry->field("mobygames"), QStringLiteral("http://www.mobygames.com/game/legend-of-zelda-twilight-princess")); 0158 QVERIFY(!entry->field(QStringLiteral("description")).isEmpty()); 0159 // no cover image downloaded 0160 QVERIFY(entry->field(QStringLiteral("cover")).isEmpty()); 0161 } 0162 0163 void MobyGamesFetcherTest::testUpdateRequest() { 0164 // MobyGames2 group has no image 0165 const QString groupName = QStringLiteral("MobyGames2"); 0166 if(!m_hasConfigFile || !m_config->hasGroup(groupName)) { 0167 QSKIP("This test requires a config file with MobyGames settings.", SkipAll); 0168 } 0169 KConfigGroup cg(m_config, groupName); 0170 0171 Tellico::Fetch::MobyGamesFetcher fetcher(this); 0172 fetcher.readConfig(cg); 0173 0174 // since the platforms are read in the next event loop (single shot timer) 0175 // to avoid downloading again, wait a moment 0176 qApp->processEvents(); 0177 0178 // create an entry and check the update request 0179 Tellico::Data::CollPtr coll(new Tellico::Data::GameCollection(true)); 0180 Tellico::Data::EntryPtr entry(new Tellico::Data::Entry(coll)); 0181 entry->setField(QStringLiteral("title"), QStringLiteral("T")); 0182 0183 Tellico::Fetch::FetchRequest req = fetcher.updateRequest(entry); 0184 QCOMPARE(req.key(), Tellico::Fetch::Title); 0185 QCOMPARE(req.value(), entry->title()); 0186 0187 // test having a user customized platform 0188 QString p(QStringLiteral("playstation 4")); // pId = 141 0189 Tellico::Data::FieldPtr f = coll->fieldByName(QStringLiteral("platform")); 0190 QVERIFY(f); 0191 if(!f->allowed().contains(p)) { 0192 f->setAllowed(QStringList(f->allowed()) << p); 0193 } 0194 0195 entry->setField(QStringLiteral("platform"), p); 0196 req = fetcher.updateRequest(entry); 0197 QCOMPARE(req.key(), Tellico::Fetch::Raw); 0198 QCOMPARE(req.value(), QStringLiteral("title=T&platform=141")); 0199 0200 // test having an unknown platform 0201 p = QStringLiteral("Atari 2600"); // pId = 28 0202 if(!f->allowed().contains(p)) { 0203 f->setAllowed(QStringList(f->allowed()) << p); 0204 } 0205 0206 entry->setField(QStringLiteral("platform"), p); 0207 req = fetcher.updateRequest(entry); 0208 QCOMPARE(req.key(), Tellico::Fetch::Raw); 0209 QCOMPARE(req.value(), QStringLiteral("title=T&platform=28")); 0210 }