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

0001 /***************************************************************************
0002     Copyright (C) 2009-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 "isbndbfetchertest.h"
0028 
0029 #include "../fetch/isbndbfetcher.h"
0030 #include "../fetch/messagelogger.h"
0031 #include "../entry.h"
0032 #include "../images/imagefactory.h"
0033 
0034 #include <KConfigGroup>
0035 
0036 #include <QTest>
0037 
0038 QTEST_GUILESS_MAIN( ISBNdbFetcherTest )
0039 
0040 ISBNdbFetcherTest::ISBNdbFetcherTest() : AbstractFetcherTest() {
0041 }
0042 
0043 void ISBNdbFetcherTest::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 ISBNdbFetcherTest::testIsbn() {
0052   QString groupName = QStringLiteral("ISBNdb");
0053   if(!m_hasConfigFile || !m_config->hasGroup(groupName)) {
0054     QSKIP("This test requires a config file with ISBNdb settings.", SkipAll);
0055   }
0056   KConfigGroup cg(m_config, groupName);
0057 
0058   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::ISBN,
0059                                        QStringLiteral("0789312239"));
0060   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::ISBNdbFetcher(this));
0061   Tellico::Fetch::MessageLogger* logger = new Tellico::Fetch::MessageLogger;
0062   fetcher->setMessageHandler(logger);
0063   fetcher->readConfig(cg);
0064 
0065   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0066   QVERIFY(logger->errorList.isEmpty());
0067 
0068   QCOMPARE(results.size(), 1);
0069 
0070   Tellico::Data::EntryPtr entry = results.at(0);
0071   QCOMPARE(entry->field(QStringLiteral("title")).toLower(), QStringLiteral("this is venice"));
0072   QCOMPARE(entry->field(QStringLiteral("author")), QStringLiteral("Sasek, M. (miroslav)"));
0073   QCOMPARE(entry->field(QStringLiteral("isbn")), QStringLiteral("0789312239"));
0074   QCOMPARE(entry->field(QStringLiteral("pub_year")), QStringLiteral("2005"));
0075   QCOMPARE(entry->field(QStringLiteral("publisher")), QStringLiteral("Universe"));
0076   QCOMPARE(entry->field(QStringLiteral("binding")), QStringLiteral("Hardback"));
0077   QCOMPARE(entry->field(QStringLiteral("genre")), QStringLiteral("Description And Travel"));
0078   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0079   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0080 }
0081 
0082 void ISBNdbFetcherTest::testIsbn13() {
0083   QString groupName = QStringLiteral("ISBNdb");
0084   if(!m_hasConfigFile || !m_config->hasGroup(groupName)) {
0085     QSKIP("This test requires a config file with ISBNdb settings.", SkipAll);
0086   }
0087   KConfigGroup cg(m_config, groupName);
0088 
0089   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::ISBN,
0090                                        QStringLiteral("9780789312235"));
0091   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::ISBNdbFetcher(this));
0092   fetcher->readConfig(cg);
0093 
0094   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0095 
0096   QCOMPARE(results.size(), 1);
0097 
0098   Tellico::Data::EntryPtr entry = results.at(0);
0099   QCOMPARE(entry->field(QStringLiteral("title")).toLower(), QStringLiteral("this is venice"));
0100   QCOMPARE(entry->field(QStringLiteral("author")), QStringLiteral("Sasek, M. (miroslav)"));
0101   QCOMPARE(entry->field(QStringLiteral("isbn")), QStringLiteral("0789312239"));
0102   QCOMPARE(entry->field(QStringLiteral("pub_year")), QStringLiteral("2005"));
0103   QCOMPARE(entry->field(QStringLiteral("publisher")), QStringLiteral("Universe"));
0104   QCOMPARE(entry->field(QStringLiteral("binding")), QStringLiteral("Hardback"));
0105   QCOMPARE(entry->field(QStringLiteral("genre")), QStringLiteral("Description And Travel"));
0106   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0107   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0108 }
0109 
0110 void ISBNdbFetcherTest::testMultipleIsbn() {
0111   const bool batchIsbn = true;
0112   QString groupName = QStringLiteral("ISBNdb");
0113   if(!m_hasConfigFile || !m_config->hasGroup(groupName)) {
0114     QSKIP("This test requires a config file with ISBNdb settings.", SkipAll);
0115   }
0116   KConfigGroup cg(m_config, groupName);
0117   cg.writeEntry("Batch ISBN", batchIsbn);
0118 
0119   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::ISBN,
0120                                        QStringLiteral("0789312239; 0393339912"));
0121   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::ISBNdbFetcher(this));
0122   fetcher->readConfig(cg);
0123 
0124   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0125 
0126   QCOMPARE(results.size(), 2);
0127 
0128   // can't be sure which entry matches which ISBN
0129 
0130   Tellico::Data::EntryPtr entry = results.at(0);
0131   QVERIFY(entry);
0132   const bool veniceIsFirst = entry->field(QStringLiteral("isbn")) == QStringLiteral("0789312239");
0133   if(!veniceIsFirst) {
0134     entry = results.at(1);
0135     QVERIFY(entry);
0136   }
0137 
0138   QCOMPARE(entry->field(QStringLiteral("title")).toLower(), QStringLiteral("this is venice"));
0139   QCOMPARE(entry->field(QStringLiteral("author")), QStringLiteral("Sasek, M. (miroslav)"));
0140   QCOMPARE(entry->field(QStringLiteral("isbn")), QStringLiteral("0789312239"));
0141   QCOMPARE(entry->field(QStringLiteral("pub_year")), QStringLiteral("2005"));
0142   QCOMPARE(entry->field(QStringLiteral("publisher")), QStringLiteral("Universe"));
0143   QCOMPARE(entry->field(QStringLiteral("binding")), QStringLiteral("Hardback"));
0144   QCOMPARE(entry->field(QStringLiteral("genre")), QStringLiteral("Description And Travel"));
0145   // no cover in batch mode
0146   if(!batchIsbn) {
0147     QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0148     QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0149   }
0150 
0151   entry = results.at(veniceIsFirst ? 1 : 0);
0152   QVERIFY(entry);
0153   QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Packing For Mars: The Curious Science Of Life In The Void"));
0154   QCOMPARE(entry->field(QStringLiteral("author")), QStringLiteral("Mary Roach"));
0155   QCOMPARE(entry->field(QStringLiteral("isbn")), QStringLiteral("0393339912"));
0156   QCOMPARE(entry->field(QStringLiteral("pub_year")), QStringLiteral("2011"));
0157   QCOMPARE(entry->field(QStringLiteral("publisher")), QStringLiteral("W. W. Norton & Company"));
0158   QCOMPARE(entry->field(QStringLiteral("binding")), QStringLiteral("Paperback"));
0159   if(!batchIsbn) {
0160     QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0161     QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0162   }
0163 }
0164 
0165 void ISBNdbFetcherTest::testTitle() {
0166   QString groupName = QStringLiteral("ISBNdb");
0167   if(!m_hasConfigFile || !m_config->hasGroup(groupName)) {
0168     QSKIP("This test requires a config file with ISBNdb settings.", SkipAll);
0169   }
0170   KConfigGroup cg(m_config, groupName);
0171 
0172   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::Title,
0173                                        QStringLiteral("PACKING FOR MARS The Curious Science of Life in the Void"));
0174   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::ISBNdbFetcher(this));
0175   fetcher->readConfig(cg);
0176 
0177   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0178 
0179   Tellico::Data::EntryPtr entry;
0180   foreach(Tellico::Data::EntryPtr testEntry, results) {
0181     if(testEntry->field(QStringLiteral("isbn")).remove('-') == QStringLiteral("0393339912")) {
0182       entry = testEntry;
0183       break;
0184     }
0185   }
0186   QVERIFY(entry);
0187 
0188   QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Packing For Mars: The Curious Science Of Life In The Void"));
0189   QCOMPARE(entry->field(QStringLiteral("author")), QStringLiteral("Mary Roach"));
0190   QCOMPARE(entry->field(QStringLiteral("isbn")), QStringLiteral("0393339912"));
0191   QCOMPARE(entry->field(QStringLiteral("pub_year")), QStringLiteral("2011"));
0192   QCOMPARE(entry->field(QStringLiteral("publisher")), QStringLiteral("W. W. Norton & Company"));
0193   QCOMPARE(entry->field(QStringLiteral("binding")), QStringLiteral("Paperback"));
0194   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0195   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0196 }
0197 
0198 void ISBNdbFetcherTest::testAuthor() {
0199   QString groupName = QStringLiteral("ISBNdb");
0200   if(!m_hasConfigFile || !m_config->hasGroup(groupName)) {
0201     QSKIP("This test requires a config file with ISBNdb settings.", SkipAll);
0202   }
0203   KConfigGroup cg(m_config, groupName);
0204 
0205   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::Person,
0206                                        QStringLiteral("Joshua Foer"));
0207   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::ISBNdbFetcher(this));
0208   fetcher->readConfig(cg);
0209 
0210   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0211 
0212   Tellico::Data::EntryPtr entry;
0213   foreach(Tellico::Data::EntryPtr testEntry, results) {
0214     if(testEntry->field(QStringLiteral("isbn")).remove('-') == QStringLiteral("0143120530")) {
0215       entry = testEntry;
0216       break;
0217     }
0218   }
0219   QVERIFY(entry);
0220 
0221   QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Moonwalking With Einstein: The Art And Science Of Remembering Everything"));
0222   QCOMPARE(entry->field(QStringLiteral("author")), QStringLiteral("Joshua Foer"));
0223   QCOMPARE(entry->field(QStringLiteral("isbn")).remove('-'), QStringLiteral("0143120530"));
0224   QCOMPARE(entry->field(QStringLiteral("pub_year")), QStringLiteral("2012"));
0225   QCOMPARE(entry->field(QStringLiteral("publisher")), QStringLiteral("Penguin Books"));
0226   QEXPECT_FAIL("", "ISBNdb author search does not seem to include binding", Continue);
0227   QCOMPARE(entry->field(QStringLiteral("binding")), QStringLiteral("Paperback"));
0228 }