File indexing completed on 2024-05-12 05:10:03

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   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 5);
0077   Tellico::Data::EntryPtr entry;
0078   for(const auto& testEntry : qAsConst(results)) {
0079     if(testEntry->field("platform") == QLatin1String("Nintendo Wii")) {
0080       entry = testEntry;
0081       break;
0082     } else {
0083       qDebug() << "...skipping" << testEntry->title();
0084     }
0085   }
0086   QVERIFY(entry);
0087 
0088   QCOMPARE(entry->field("title"), QStringLiteral("The Legend of Zelda: Twilight Princess"));
0089   QCOMPARE(entry->field("year"), QStringLiteral("2006"));
0090   QCOMPARE(entry->field("platform"), QStringLiteral("Nintendo Wii"));
0091   QVERIFY(entry->field("genre").contains(QStringLiteral("Action")));
0092 //  QCOMPARE(entry->field("certification"), QStringLiteral("Teen"));
0093   QCOMPARE(entry->field("pegi"), QStringLiteral("PEGI 12"));
0094   QCOMPARE(entry->field("publisher"), QStringLiteral("Nintendo Co., Ltd."));
0095   QCOMPARE(entry->field("developer"), QStringLiteral("Nintendo EAD"));
0096   QCOMPARE(entry->field("mobygames"), QStringLiteral("https://www.mobygames.com/game/25103/the-legend-of-zelda-twilight-princess/"));
0097   QVERIFY(!entry->field(QStringLiteral("description")).isEmpty());
0098   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0099   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0100   QVERIFY(!entry->field(QStringLiteral("screenshot")).isEmpty());
0101   QVERIFY(!entry->field(QStringLiteral("screenshot")).contains(QLatin1Char('/')));
0102 }
0103 
0104 // same search, except to include platform name in search
0105 // which is a Keyword search and the WiiU result should be first
0106 void MobyGamesFetcherTest::testKeyword() {
0107   const QString groupName = QStringLiteral("MobyGames");
0108   if(!m_hasConfigFile || !m_config->hasGroup(groupName)) {
0109     QSKIP("This test requires a config file with MobyGames settings.", SkipAll);
0110   }
0111   KConfigGroup cg(m_config, groupName);
0112 
0113   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Game, Tellico::Fetch::Keyword,
0114                                        QStringLiteral("Twilight Princess Nintendo WiiU"));
0115   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MobyGamesFetcher(this));
0116   fetcher->readConfig(cg);
0117 
0118   // since the platforms are read in the next event loop (single shot timer)
0119   // to avoid downloading again, wait a moment
0120   qApp->processEvents();
0121 
0122   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0123 
0124   QCOMPARE(results.size(), 1);
0125 
0126   Tellico::Data::EntryPtr entry = results.at(0);
0127   QVERIFY(entry);
0128   QCOMPARE(entry->field("title"), QStringLiteral("The Legend of Zelda: Twilight Princess"));
0129   QCOMPARE(entry->field("year"), QStringLiteral("2016"));
0130   QCOMPARE(entry->field("platform"), QStringLiteral("Nintendo WiiU"));
0131 }
0132 
0133 void MobyGamesFetcherTest::testRaw() {
0134   // MobyGames2 group has no image
0135   const QString groupName = QStringLiteral("MobyGames2");
0136   if(!m_hasConfigFile || !m_config->hasGroup(groupName)) {
0137     QSKIP("This test requires a config file with MobyGames settings.", SkipAll);
0138   }
0139   KConfigGroup cg(m_config, groupName);
0140 
0141   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Game, Tellico::Fetch::Raw,
0142                                        QStringLiteral("id=25103&platform=82"));
0143   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MobyGamesFetcher(this));
0144   fetcher->readConfig(cg);
0145 
0146   // since the platforms are read in the next event loop (single shot timer)
0147   // to avoid downloading again, wait a moment
0148   qApp->processEvents();
0149 
0150   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0151 
0152   QCOMPARE(results.size(), 1);
0153 
0154   Tellico::Data::EntryPtr entry = results.at(0);
0155   QVERIFY(entry);
0156   QCOMPARE(entry->field("title"), QStringLiteral("The Legend of Zelda: Twilight Princess"));
0157   QCOMPARE(entry->field("year"), QStringLiteral("2006"));
0158   QCOMPARE(entry->field("platform"), QStringLiteral("Nintendo Wii"));
0159   QVERIFY(entry->field("genre").contains(QStringLiteral("Action")));
0160 //  QCOMPARE(entry->field("certification"), QStringLiteral("Teen"));
0161   QCOMPARE(entry->field("pegi"), QStringLiteral("PEGI 12"));
0162   QCOMPARE(entry->field("publisher"), QStringLiteral("Nintendo Co., Ltd."));
0163   QCOMPARE(entry->field("developer"), QStringLiteral("Nintendo EAD"));
0164   QCOMPARE(entry->field("mobygames"), QStringLiteral("https://www.mobygames.com/game/25103/the-legend-of-zelda-twilight-princess/"));
0165   QVERIFY(!entry->field(QStringLiteral("description")).isEmpty());
0166   // no cover image downloaded
0167   QVERIFY(entry->field(QStringLiteral("cover")).isEmpty());
0168 }
0169 
0170 void MobyGamesFetcherTest::testUpdateRequest() {
0171   // MobyGames2 group has no image
0172   const QString groupName = QStringLiteral("MobyGames2");
0173   if(!m_hasConfigFile || !m_config->hasGroup(groupName)) {
0174     QSKIP("This test requires a config file with MobyGames settings.", SkipAll);
0175   }
0176   KConfigGroup cg(m_config, groupName);
0177 
0178   Tellico::Fetch::MobyGamesFetcher fetcher(this);
0179   fetcher.readConfig(cg);
0180 
0181   // since the platforms are read in the next event loop (single shot timer)
0182   // to avoid downloading again, wait a moment
0183   qApp->processEvents();
0184 
0185   // create an entry and check the update request
0186   Tellico::Data::CollPtr coll(new Tellico::Data::GameCollection(true));
0187   Tellico::Data::EntryPtr entry(new Tellico::Data::Entry(coll));
0188   entry->setField(QStringLiteral("title"), QStringLiteral("T"));
0189 
0190   Tellico::Fetch::FetchRequest req = fetcher.updateRequest(entry);
0191   QCOMPARE(req.key(), Tellico::Fetch::Title);
0192   QCOMPARE(req.value(), entry->title());
0193 
0194   // test having a user customized platform
0195   QString p(QStringLiteral("playstation 4")); // pId = 141
0196   Tellico::Data::FieldPtr f = coll->fieldByName(QStringLiteral("platform"));
0197   QVERIFY(f);
0198   if(!f->allowed().contains(p)) {
0199     f->setAllowed(QStringList(f->allowed()) << p);
0200   }
0201 
0202   entry->setField(QStringLiteral("platform"), p);
0203   req = fetcher.updateRequest(entry);
0204   QCOMPARE(req.key(), Tellico::Fetch::Raw);
0205   QCOMPARE(req.value(), QStringLiteral("title=T&platform=141"));
0206 
0207   // test having an unknown platform
0208   p = QStringLiteral("Atari 2600"); // pId = 28
0209   if(!f->allowed().contains(p)) {
0210     f->setAllowed(QStringList(f->allowed()) << p);
0211   }
0212 
0213   entry->setField(QStringLiteral("platform"), p);
0214   req = fetcher.updateRequest(entry);
0215   QCOMPARE(req.key(), Tellico::Fetch::Raw);
0216   QCOMPARE(req.value(), QStringLiteral("title=T&platform=28"));
0217 }