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

0001 /***************************************************************************
0002     Copyright (C) 2021 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 "thetvdbfetchertest.h"
0026 
0027 #include "../fetch/thetvdbfetcher.h"
0028 #include "../collections/videocollection.h"
0029 #include "../entry.h"
0030 #include "../images/imagefactory.h"
0031 
0032 #include <KConfigGroup>
0033 
0034 #include <QTest>
0035 
0036 QTEST_GUILESS_MAIN( TheTVDBFetcherTest )
0037 
0038 TheTVDBFetcherTest::TheTVDBFetcherTest() : AbstractFetcherTest() {
0039 }
0040 
0041 void TheTVDBFetcherTest::initTestCase() {
0042   Tellico::ImageFactory::init();
0043   m_hasConfigFile = QFile::exists(QFINDTESTDATA("tellicotest_private.config"));
0044   if(m_hasConfigFile) {
0045     m_config = KSharedConfig::openConfig(QFINDTESTDATA("tellicotest_private.config"), KConfig::SimpleConfig);
0046   }
0047 }
0048 
0049 void TheTVDBFetcherTest::testTitle() {
0050   const QString groupName = QStringLiteral("TheTVDB");
0051   if(!m_hasConfigFile || !m_config->hasGroup(groupName)) {
0052     QSKIP("This test requires a config file with TheTVDB settings.", SkipAll);
0053   }
0054   KConfigGroup cg(m_config, groupName);
0055 
0056   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Video, Tellico::Fetch::Title,
0057                                        QStringLiteral("Firefly"));
0058   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::TheTVDBFetcher(this));
0059   fetcher->readConfig(cg);
0060 
0061   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0062   fetcher->saveConfig();
0063 
0064   QCOMPARE(results.size(), 1);
0065 
0066   Tellico::Data::EntryPtr entry = results.at(0);
0067   QVERIFY(entry);
0068 
0069   QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Firefly"));
0070   QCOMPARE(entry->field(QStringLiteral("year")), QStringLiteral("2002"));
0071   QCOMPARE(entry->field(QStringLiteral("network")), QStringLiteral("FOX"));
0072   QCOMPARE(entry->field(QStringLiteral("language")), QStringLiteral("English"));
0073   // 2021-10-04 - content rating didn't seem to be returned in the data
0074 //  QCOMPARE(entry->field(QStringLiteral("certification")), QStringLiteral("TV-14"));
0075   QCOMPARE(entry->field(QStringLiteral("thetvdb")), QStringLiteral("https://thetvdb.com/series/firefly"));
0076   QCOMPARE(entry->field(QStringLiteral("genre")), QStringLiteral("Science Fiction; Drama; Adventure"));
0077   QVERIFY(entry->field(QStringLiteral("director")).startsWith(QStringLiteral("Joss Whedon; ")));
0078   QVERIFY(entry->field(QStringLiteral("writer")).contains(QStringLiteral("Joss Whedon")));
0079   QVERIFY(entry->field(QStringLiteral("writer")).contains(QStringLiteral("Tim Minear")));
0080   QStringList castList = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("cast")));
0081   QVERIFY(castList.size() > 2);
0082   QCOMPARE(castList.at(0), QStringLiteral("Adam Baldwin::Jayne Cobb"));
0083   QCOMPARE(castList.at(2), QStringLiteral("Gina Torres::Zoƫ Washburne"));
0084   QStringList episodeList = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("episode")));
0085   QVERIFY(!episodeList.isEmpty());
0086   QCOMPARE(episodeList.at(0), QStringLiteral("The Train Job::1::1"));
0087   QCOMPARE(entry->field(QStringLiteral("imdb")), QStringLiteral("https://www.imdb.com/title/tt0303461"));
0088   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0089   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0090   QVERIFY(!entry->field(QStringLiteral("plot")).isEmpty());
0091 }
0092 
0093 void TheTVDBFetcherTest::testUpdate() {
0094   QSKIP("This test is completely broken right now since API v4 doesn't have any of this", SkipAll);
0095   const QString groupName = QStringLiteral("TheTVDB");
0096   if(!m_hasConfigFile || !m_config->hasGroup(groupName)) {
0097     QSKIP("This test requires a config file with TheTVDB settings.", SkipAll);
0098   }
0099   KConfigGroup cg(m_config, groupName);
0100 
0101   Tellico::Data::CollPtr coll(new Tellico::Data::VideoCollection(true));
0102   Tellico::Data::FieldPtr field(new Tellico::Data::Field(QStringLiteral("thetvdb"),
0103                                                          QStringLiteral("TheTVDB"),
0104                                                          Tellico::Data::Field::URL));
0105   QCOMPARE(coll->addField(field), true);
0106   field = new Tellico::Data::Field(QStringLiteral("imdb"),
0107                                    QStringLiteral("IMDB"),
0108                                    Tellico::Data::Field::URL);
0109   QCOMPARE(coll->addField(field), true);
0110   Tellico::Data::EntryPtr oldEntry(new Tellico::Data::Entry(coll));
0111   coll->addEntries(oldEntry);
0112   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::TheTVDBFetcher(this));
0113   auto f = static_cast<Tellico::Fetch::TheTVDBFetcher*>(fetcher.data());
0114 
0115   oldEntry->setField(QStringLiteral("thetvdb"), QStringLiteral("https://thetvdb.com/series/firefly"));
0116   auto request = f->updateRequest(oldEntry);
0117   QCOMPARE(request.key(), Tellico::Fetch::Raw);
0118   QCOMPARE(request.value(), QStringLiteral("firefly"));
0119   QCOMPARE(request.data(), QStringLiteral("slug"));
0120 
0121   oldEntry->setField(QStringLiteral("imdb"), QStringLiteral("https://www.imdb.com/title/tt0303461/?ref_=nv_sr_srsg_0"));
0122   request = f->updateRequest(oldEntry);
0123   QCOMPARE(request.key(), Tellico::Fetch::Raw);
0124   QCOMPARE(request.value(), QStringLiteral("tt0303461"));
0125   QCOMPARE(request.data(), QStringLiteral("imdb"));
0126 
0127   fetcher->readConfig(cg);
0128 
0129   request.setCollectionType(coll->type());
0130   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0131 
0132   QCOMPARE(results.size(), 1);
0133 
0134   Tellico::Data::EntryPtr entry = results.at(0);
0135   QVERIFY(entry);
0136 
0137   QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Firefly"));
0138   QCOMPARE(entry->field(QStringLiteral("year")), QStringLiteral("2002"));
0139   QCOMPARE(entry->field(QStringLiteral("network")), QStringLiteral("FOX"));
0140   QCOMPARE(entry->field(QStringLiteral("language")), QStringLiteral("English"));
0141   QCOMPARE(entry->field(QStringLiteral("certification")), QStringLiteral("TV-14"));
0142   QCOMPARE(entry->field(QStringLiteral("genre")), QStringLiteral("Drama; Science Fiction"));
0143   QCOMPARE(entry->field(QStringLiteral("imdb")), QStringLiteral("https://www.imdb.com/title/tt0303461"));
0144   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0145   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0146   QVERIFY(!entry->field(QStringLiteral("plot")).isEmpty());
0147 }