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

0001 /***************************************************************************
0002     Copyright (C) 2012-2019 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 #include "thegamesdbfetchertest.h"
0026 
0027 #include "../fetch/thegamesdbfetcher.h"
0028 #include "../collections/gamecollection.h"
0029 #include "../entry.h"
0030 #include "../images/imagefactory.h"
0031 
0032 #include <KSharedConfig>
0033 
0034 #include <QTest>
0035 
0036 QTEST_GUILESS_MAIN( TheGamesDBFetcherTest )
0037 
0038 TheGamesDBFetcherTest::TheGamesDBFetcherTest() : AbstractFetcherTest() {
0039 }
0040 
0041 void TheGamesDBFetcherTest::initTestCase() {
0042   Tellico::ImageFactory::init();
0043 
0044   QString configFile = QFINDTESTDATA("tellicotest_private.config");
0045   if(!configFile.isEmpty()) {
0046     m_config = KSharedConfig::openConfig(configFile, KConfig::SimpleConfig)->group(QStringLiteral("TGDB"));
0047   }
0048 
0049   m_fieldValues.insert(QStringLiteral("title"), QStringLiteral("GoldenEye 007"));
0050   m_fieldValues.insert(QStringLiteral("platform"), QStringLiteral("Nintendo 64"));
0051   m_fieldValues.insert(QStringLiteral("year"), QStringLiteral("1997"));
0052   m_fieldValues.insert(QStringLiteral("certification"), QStringLiteral("Teen"));
0053   m_fieldValues.insert(QStringLiteral("genre"), QStringLiteral("Action; Adventure; Shooter; Stealth"));
0054   m_fieldValues.insert(QStringLiteral("publisher"), QStringLiteral("Nintendo"));
0055   m_fieldValues.insert(QStringLiteral("developer"), QStringLiteral("Rare, Ltd."));
0056   m_fieldValues.insert(QStringLiteral("num-player"), QStringLiteral("4"));
0057 
0058   // need to delete cached data
0059   QFile genreFile(Tellico::Fetch::TheGamesDBFetcher::dataFileName(Tellico::Fetch::TheGamesDBFetcher::Genre));
0060   if(genreFile.exists()) {
0061     genreFile.remove();
0062   }
0063   QFile publisherFile(Tellico::Fetch::TheGamesDBFetcher::dataFileName(Tellico::Fetch::TheGamesDBFetcher::Publisher));
0064   if(publisherFile.exists()) {
0065     publisherFile.remove();
0066   }
0067   QFile developerFile(Tellico::Fetch::TheGamesDBFetcher::dataFileName(Tellico::Fetch::TheGamesDBFetcher::Developer));
0068   if(developerFile.exists()) {
0069     developerFile.remove();
0070   }
0071 }
0072 
0073 void TheGamesDBFetcherTest::testTitle() {
0074   if(!m_config.isValid()) {
0075     QSKIP("This test requires a config file with TGDB settings.", SkipAll);
0076   }
0077 
0078   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Game, Tellico::Fetch::Title,
0079                                        QStringLiteral("Goldeneye 007"));
0080   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::TheGamesDBFetcher(this));
0081   fetcher->readConfig(m_config);
0082   QVERIFY(fetcher->canSearch(request.key()));
0083 
0084   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0085 
0086   QCOMPARE(results.size(), 1);
0087 
0088   Tellico::Data::EntryPtr entry = results.at(0);
0089   QHashIterator<QString, QString> i(m_fieldValues);
0090   while(i.hasNext()) {
0091     i.next();
0092     QString result = entry->field(i.key()).toLower();
0093     QCOMPARE(result, i.value().toLower());
0094   }
0095   QVERIFY(!entry->field(QStringLiteral("description")).isEmpty());
0096   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0097   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0098   QVERIFY(!entry->field(QStringLiteral("screenshot")).isEmpty());
0099   QVERIFY(!entry->field(QStringLiteral("screenshot")).contains(QLatin1Char('/')));
0100 }
0101 
0102 void TheGamesDBFetcherTest::testUpdate() {
0103   if(!m_config.isValid()) {
0104     QSKIP("This test requires a config file with TGDB settings.", SkipAll);
0105   }
0106   Tellico::Data::CollPtr coll(new Tellico::Data::GameCollection(true));
0107   Tellico::Data::EntryPtr entry(new Tellico::Data::Entry(coll));
0108   coll->addEntries(entry);
0109   entry->setField(QStringLiteral("title"), QStringLiteral("GoldenEye 007"));
0110   entry->setField(QStringLiteral("platform"), QStringLiteral("Nintendo 64"));
0111 
0112   Tellico::Fetch::TheGamesDBFetcher fetcher(this);
0113   qApp->processEvents(); // allow time for timer to load cached data
0114   auto request = fetcher.updateRequest(entry);
0115   QCOMPARE(request.key(), Tellico::Fetch::Title);
0116   QCOMPARE(request.value(), QStringLiteral("GoldenEye 007"));
0117   QCOMPARE(request.data(), QLatin1String("3"));
0118 
0119   fetcher.readConfig(m_config);
0120   request.setCollectionType(coll->type());
0121   Tellico::Data::EntryList results = DO_FETCH1(&fetcher, request, 1);
0122   QCOMPARE(results.size(), 1);
0123 }