File indexing completed on 2024-05-12 16:46:16

0001 /***************************************************************************
0002     Copyright (C) 2011-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 "doubanfetchertest.h"
0028 
0029 #include "../fetch/doubanfetcher.h"
0030 #include "../collections/bookcollection.h"
0031 #include "../collections/videocollection.h"
0032 #include "../collections/musiccollection.h"
0033 #include "../collectionfactory.h"
0034 #include "../entry.h"
0035 #include "../images/imagefactory.h"
0036 
0037 #include <KSharedConfig>
0038 
0039 #include <QTest>
0040 
0041 QTEST_GUILESS_MAIN( DoubanFetcherTest )
0042 
0043 DoubanFetcherTest::DoubanFetcherTest() : AbstractFetcherTest() {
0044 }
0045 
0046 void DoubanFetcherTest::initTestCase() {
0047   Tellico::RegisterCollection<Tellico::Data::BookCollection>  registerBook(Tellico::Data::Collection::Book,   "book");
0048   Tellico::RegisterCollection<Tellico::Data::VideoCollection> registerVideo(Tellico::Data::Collection::Video, "video");
0049   Tellico::RegisterCollection<Tellico::Data::MusicCollection> registerMusic(Tellico::Data::Collection::Album, "album");
0050   Tellico::ImageFactory::init();
0051 
0052   m_config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig)->group(QStringLiteral("douban"));
0053   m_config.writeEntry("Custom Fields", QStringLiteral("douban,origtitle"));
0054 }
0055 
0056 void DoubanFetcherTest::testBookTitle() {
0057   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::Keyword,
0058                                        QStringLiteral("大设计 列纳德·蒙洛迪诺"));
0059   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DoubanFetcher(this));
0060 
0061   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0062 
0063   QCOMPARE(results.size(), 1);
0064 
0065   Tellico::Data::EntryPtr entry = results.at(0);
0066   QVERIFY(entry);
0067 
0068   QCOMPARE(entry->collection()->type(), Tellico::Data::Collection::Book);
0069 
0070   QCOMPARE(entry->field("title"), QString::fromUtf8("大设计"));
0071   QCOMPARE(entry->field("author"), QString::fromUtf8("[英] 斯蒂芬·霍金; 列纳德·蒙洛迪诺"));
0072   QCOMPARE(entry->field("translator"), QString::fromUtf8("吴忠超"));
0073   QCOMPARE(entry->field("publisher"), QString::fromUtf8("湖南科学技术出版社"));
0074   QCOMPARE(entry->field("binding"), QStringLiteral("Hardback"));
0075   QCOMPARE(entry->field("pub_year"), QStringLiteral("2011"));
0076   QCOMPARE(entry->field("isbn"), QStringLiteral("7535765440"));
0077   QCOMPARE(entry->field("pages"), QStringLiteral("176"));
0078   QVERIFY(!entry->field(QStringLiteral("keyword")).isEmpty());
0079   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0080   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0081   QVERIFY(!entry->field(QStringLiteral("plot")).isEmpty());
0082 }
0083 
0084 void DoubanFetcherTest::testISBN() {
0085   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::ISBN,
0086                                        QStringLiteral("9787535765444; 9787208104235"));
0087   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DoubanFetcher(this));
0088   fetcher->readConfig(m_config);
0089 
0090   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0091 
0092   QCOMPARE(results.size(), 2);
0093 
0094   Tellico::Data::EntryPtr entry = results.at(0);
0095   QVERIFY(entry);
0096 
0097   QCOMPARE(entry->collection()->type(), Tellico::Data::Collection::Book);
0098 
0099   QCOMPARE(entry->field("title"), QString::fromUtf8("大设计"));
0100   QCOMPARE(entry->field("author"), QString::fromUtf8("[英] 斯蒂芬·霍金; 列纳德·蒙洛迪诺"));
0101   QCOMPARE(entry->field("translator"), QString::fromUtf8("吴忠超"));
0102   QCOMPARE(entry->field("publisher"), QString::fromUtf8("湖南科学技术出版社"));
0103   QCOMPARE(entry->field("binding"), QStringLiteral("Hardback"));
0104   QCOMPARE(entry->field("pub_year"), QStringLiteral("2011"));
0105   QCOMPARE(entry->field("isbn"), QStringLiteral("7535765440"));
0106   QCOMPARE(entry->field("pages"), QStringLiteral("176"));
0107   QCOMPARE(entry->field("origtitle"), QStringLiteral("The Grand Design"));
0108   QCOMPARE(entry->field("douban"), QStringLiteral("https://book.douban.com/subject/5422665/"));
0109   QVERIFY(!entry->field(QStringLiteral("keyword")).isEmpty());
0110   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0111   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0112   QVERIFY(!entry->field(QStringLiteral("plot")).isEmpty());
0113 
0114   entry = results.at(1);
0115   QVERIFY(entry);
0116   QCOMPARE(entry->field("title"), QString::fromUtf8("太空漫游"));
0117   QCOMPARE(entry->field("isbn"), QStringLiteral("7535765440"));
0118 }
0119 
0120 void DoubanFetcherTest::testVideo() {
0121   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Video, Tellico::Fetch::Keyword,
0122                                        QStringLiteral("钢铁侠2"));
0123   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DoubanFetcher(this));
0124   fetcher->readConfig(m_config);
0125 
0126   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0127 
0128   QCOMPARE(results.size(), 1);
0129 
0130   Tellico::Data::EntryPtr entry = results.at(0);
0131   QVERIFY(entry);
0132 
0133   QCOMPARE(entry->collection()->type(), Tellico::Data::Collection::Video);
0134 
0135   QCOMPARE(entry->field("title"), QString::fromUtf8("钢铁侠2"));
0136   QCOMPARE(entry->field("origtitle"), QStringLiteral("Iron Man 2"));
0137   QCOMPARE(entry->field("year"), QStringLiteral("2010"));
0138   QCOMPARE(entry->field("director"), QString::fromUtf8("乔恩·费儒"));
0139   QCOMPARE(entry->field("writer"), QString::fromUtf8("贾斯汀·塞洛克斯"));
0140   QCOMPARE(entry->field("running-time"), QStringLiteral("124"));
0141   QVERIFY(!entry->field(QStringLiteral("genre")).isEmpty());
0142   QVERIFY(!entry->field(QStringLiteral("cast")).isEmpty());
0143 //  QVERIFY(!entry->field(QStringLiteral("nationality")).isEmpty());
0144 //  QVERIFY(!entry->field(QStringLiteral("language")).isEmpty());
0145 //  QVERIFY(!entry->field(QStringLiteral("keyword")).isEmpty());
0146   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0147   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0148   QVERIFY(!entry->field(QStringLiteral("plot")).isEmpty());
0149 }
0150 
0151 void DoubanFetcherTest::testMusic() {
0152   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Keyword,
0153                                        QLatin1String("Top Gun Original Motion Picture"));
0154   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DoubanFetcher(this));
0155 
0156   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0157 
0158   QCOMPARE(results.size(), 1);
0159 
0160   Tellico::Data::EntryPtr entry = results.at(0);
0161   QVERIFY(entry);
0162 
0163   QCOMPARE(entry->collection()->type(), Tellico::Data::Collection::Album);
0164 
0165   QCOMPARE(entry->field("title"), QStringLiteral("TOP GUN/SOUNDTRACK"));
0166   QCOMPARE(entry->field("year"), QStringLiteral("2013"));
0167   QCOMPARE(entry->field("artist"), QStringLiteral("Original Motion Picture Soundtrack"));
0168   QCOMPARE(entry->field("label"), QString::fromUtf8("索尼音乐"));
0169 //  QCOMPARE(entry->field("medium"), QStringLiteral("Compact Disc"));
0170   QStringList trackList = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("track")));
0171   QCOMPARE(trackList.count(), 10);
0172   QCOMPARE(trackList.front(), QStringLiteral("Danger Zone::Kenny Loggins"));
0173 //  QVERIFY(!entry->field(QStringLiteral("keyword")).isEmpty());
0174   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0175   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0176 }
0177 
0178 void DoubanFetcherTest::testMusicAdele() {
0179   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Keyword,
0180                                        QLatin1String("Adele 21"));
0181   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DoubanFetcher(this));
0182 
0183   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0184 
0185   QCOMPARE(results.size(), 1);
0186 
0187   Tellico::Data::EntryPtr entry = results.at(0);
0188   QVERIFY(entry);
0189 
0190   QCOMPARE(entry->collection()->type(), Tellico::Data::Collection::Album);
0191 
0192   QCOMPARE(entry->field("title"), QStringLiteral("21"));
0193   QCOMPARE(entry->field("year"), QStringLiteral("2011"));
0194   QCOMPARE(entry->field("artist"), QStringLiteral("Adele"));
0195   QCOMPARE(entry->field("medium"), QStringLiteral("Compact Disc"));
0196   QStringList trackList = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("track")));
0197   QCOMPARE(trackList.count(), 12);
0198   QCOMPARE(trackList.front(), QStringLiteral("Rolling In the Deep::Adele::3:48"));
0199   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0200   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0201 }
0202 
0203 void DoubanFetcherTest::testMusicArtPepper() {
0204   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Album, Tellico::Fetch::Keyword,
0205                                        QLatin1String("Art Pepper Meets the Rhythm Section"));
0206   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::DoubanFetcher(this));
0207 
0208   Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1);
0209 
0210   QCOMPARE(results.size(), 1);
0211 
0212   Tellico::Data::EntryPtr entry = results.at(0);
0213   QVERIFY(entry);
0214 
0215   QCOMPARE(entry->collection()->type(), Tellico::Data::Collection::Album);
0216 
0217   QCOMPARE(entry->field("title"), QStringLiteral("Art Pepper Meets The Rhythm Section"));
0218   QCOMPARE(entry->field("year"), QStringLiteral("1991"));
0219   QCOMPARE(entry->field("label"), QStringLiteral("Ojc"));
0220   QCOMPARE(entry->field("artist"), QStringLiteral("Art Pepper"));
0221   QCOMPARE(entry->field("medium"), QStringLiteral("Compact Disc"));
0222   QStringList trackList = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("track")));
0223   QCOMPARE(trackList.count(), 9);
0224   // doesn't seem to have the duration
0225   QVERIFY(trackList.front().startsWith(QStringLiteral("You'd Be So Nice To Come Home To::Art Pepper")));
0226   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0227   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0228 }