1 /***************************************************************************
2     Copyright (C) 2021 Robby Stephenson <robby@periapsis.org>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or         *
8  *   modify it under the terms of the GNU General Public License as        *
9  *   published by the Free Software Foundation; either version 2 of        *
10  *   the License or (at your option) version 3 or any later version        *
11  *   accepted by the membership of KDE e.V. (or its successor approved     *
12  *   by the membership of KDE e.V.), which shall act as a proxy            *
13  *   defined in Section 14 of version 3 of the license.                    *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
22  *                                                                         *
23  ***************************************************************************/
24 
25 #include "thetvdbfetchertest.h"
26 
27 #include "../fetch/thetvdbfetcher.h"
28 #include "../collections/videocollection.h"
29 #include "../entry.h"
30 #include "../images/imagefactory.h"
31 
32 #include <KConfigGroup>
33 
34 #include <QTest>
35 
QTEST_GUILESS_MAIN(TheTVDBFetcherTest)36 QTEST_GUILESS_MAIN( TheTVDBFetcherTest )
37 
38 TheTVDBFetcherTest::TheTVDBFetcherTest() : AbstractFetcherTest() {
39 }
40 
initTestCase()41 void TheTVDBFetcherTest::initTestCase() {
42   Tellico::ImageFactory::init();
43   m_hasConfigFile = QFile::exists(QFINDTESTDATA("tellicotest_private.config"));
44   if(m_hasConfigFile) {
45     m_config = KSharedConfig::openConfig(QFINDTESTDATA("tellicotest_private.config"), KConfig::SimpleConfig);
46   }
47 }
48 
testTitle()49 void TheTVDBFetcherTest::testTitle() {
50   const QString groupName = QStringLiteral("TheTVDB");
51   if(!m_hasConfigFile || !m_config->hasGroup(groupName)) {
52     QSKIP("This test requires a config file with TheTVDB settings.", SkipAll);
53   }
54   KConfigGroup cg(m_config, groupName);
55 
56   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Video, Tellico::Fetch::Title,
57                                        QStringLiteral("Firefly"));
58   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::TheTVDBFetcher(this));
59   fetcher->readConfig(cg);
60 
61   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
62   fetcher->saveConfig();
63 
64   QCOMPARE(results.size(), 1);
65 
66   Tellico::Data::EntryPtr entry = results.at(0);
67   QVERIFY(entry);
68 
69   QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Firefly"));
70   QCOMPARE(entry->field(QStringLiteral("year")), QStringLiteral("2002"));
71   QCOMPARE(entry->field(QStringLiteral("network")), QStringLiteral("FOX"));
72   QCOMPARE(entry->field(QStringLiteral("language")), QStringLiteral("English"));
73   // 2021-10-04 - content ratign didn't seem to be returned in the data
74 //  QCOMPARE(entry->field(QStringLiteral("certification")), QStringLiteral("TV-14"));
75   QCOMPARE(entry->field(QStringLiteral("thetvdb")), QStringLiteral("https://thetvdb.com/series/firefly"));
76   QCOMPARE(entry->field(QStringLiteral("genre")), QStringLiteral("Science Fiction; Drama"));
77   QVERIFY(entry->field(QStringLiteral("director")).startsWith(QStringLiteral("Joss Whedon; ")));
78   QVERIFY(entry->field(QStringLiteral("writer")).contains(QStringLiteral("Joss Whedon")));
79   QVERIFY(entry->field(QStringLiteral("writer")).contains(QStringLiteral("Tim Minear")));
80   QStringList castList = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("cast")));
81   QVERIFY(castList.size() > 2);
82   QCOMPARE(castList.at(0), QStringLiteral("Adam Baldwin::Jayne Cobb"));
83   QCOMPARE(castList.at(2), QStringLiteral("Gina Torres::Zoë Washburne"));
84   QStringList episodeList = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("episode")));
85   QVERIFY(!episodeList.isEmpty());
86   QCOMPARE(episodeList.at(0), QStringLiteral("The Train Job::1::1"));
87   QCOMPARE(entry->field(QStringLiteral("imdb")), QStringLiteral("https://www.imdb.com/title/tt0303461"));
88   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
89   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
90   QVERIFY(!entry->field(QStringLiteral("plot")).isEmpty());
91 }
92 
testUpdate()93 void TheTVDBFetcherTest::testUpdate() {
94   QSKIP("This test is completely broken right now since API v4 doesn't have any of this", SkipAll);
95   const QString groupName = QStringLiteral("TheTVDB");
96   if(!m_hasConfigFile || !m_config->hasGroup(groupName)) {
97     QSKIP("This test requires a config file with TheTVDB settings.", SkipAll);
98   }
99   KConfigGroup cg(m_config, groupName);
100 
101   Tellico::Data::CollPtr coll(new Tellico::Data::VideoCollection(true));
102   Tellico::Data::FieldPtr field(new Tellico::Data::Field(QStringLiteral("thetvdb"),
103                                                          QStringLiteral("TheTVDB"),
104                                                          Tellico::Data::Field::URL));
105   QCOMPARE(coll->addField(field), true);
106   field = new Tellico::Data::Field(QStringLiteral("imdb"),
107                                    QStringLiteral("IMDB"),
108                                    Tellico::Data::Field::URL);
109   QCOMPARE(coll->addField(field), true);
110   Tellico::Data::EntryPtr oldEntry(new Tellico::Data::Entry(coll));
111   coll->addEntries(oldEntry);
112   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::TheTVDBFetcher(this));
113   auto f = static_cast<Tellico::Fetch::TheTVDBFetcher*>(fetcher.data());
114 
115   oldEntry->setField(QStringLiteral("thetvdb"), QStringLiteral("https://thetvdb.com/series/firefly"));
116   auto request = f->updateRequest(oldEntry);
117   QCOMPARE(request.key(), Tellico::Fetch::Raw);
118   QCOMPARE(request.value(), QStringLiteral("firefly"));
119   QCOMPARE(request.data(), QStringLiteral("slug"));
120 
121   oldEntry->setField(QStringLiteral("imdb"), QStringLiteral("https://www.imdb.com/title/tt0303461/?ref_=nv_sr_srsg_0"));
122   request = f->updateRequest(oldEntry);
123   QCOMPARE(request.key(), Tellico::Fetch::Raw);
124   QCOMPARE(request.value(), QStringLiteral("tt0303461"));
125   QCOMPARE(request.data(), QStringLiteral("imdb"));
126 
127   fetcher->readConfig(cg);
128 
129   request.setCollectionType(coll->type());
130   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
131 
132   QCOMPARE(results.size(), 1);
133 
134   Tellico::Data::EntryPtr entry = results.at(0);
135   QVERIFY(entry);
136 
137   QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Firefly"));
138   QCOMPARE(entry->field(QStringLiteral("year")), QStringLiteral("2002"));
139   QCOMPARE(entry->field(QStringLiteral("network")), QStringLiteral("FOX"));
140   QCOMPARE(entry->field(QStringLiteral("language")), QStringLiteral("English"));
141   QCOMPARE(entry->field(QStringLiteral("certification")), QStringLiteral("TV-14"));
142   QCOMPARE(entry->field(QStringLiteral("genre")), QStringLiteral("Drama; Science Fiction"));
143   QCOMPARE(entry->field(QStringLiteral("imdb")), QStringLiteral("https://www.imdb.com/title/tt0303461"));
144   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
145   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
146   QVERIFY(!entry->field(QStringLiteral("plot")).isEmpty());
147 }
148