File indexing completed on 2024-05-12 05:10:04
0001 /*************************************************************************** 0002 Copyright (C) 2009-2011 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 "openlibraryfetchertest.h" 0028 0029 #include "../fetch/openlibraryfetcher.h" 0030 #include "../entry.h" 0031 #include "../collections/bookcollection.h" 0032 #include "../images/imagefactory.h" 0033 0034 #include <KSharedConfig> 0035 #include <KConfigGroup> 0036 0037 #include <QTest> 0038 0039 QTEST_GUILESS_MAIN( OpenLibraryFetcherTest ) 0040 0041 OpenLibraryFetcherTest::OpenLibraryFetcherTest() : AbstractFetcherTest() { 0042 } 0043 0044 void OpenLibraryFetcherTest::initTestCase() { 0045 Tellico::ImageFactory::init(); 0046 } 0047 0048 void OpenLibraryFetcherTest::testTitle() { 0049 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::Title, 0050 QStringLiteral("Barrayar")); 0051 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OpenLibraryFetcher(this)); 0052 QVERIFY(fetcher->canSearch(request.key())); 0053 0054 Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1); 0055 0056 QCOMPARE(results.size(), 1); 0057 0058 Tellico::Data::EntryPtr entry = results.at(0); 0059 QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Barrayar")); 0060 } 0061 0062 void OpenLibraryFetcherTest::testAuthor() { 0063 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::Person, 0064 QStringLiteral("Bujold")); 0065 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OpenLibraryFetcher(this)); 0066 QVERIFY(fetcher->canSearch(request.key())); 0067 0068 Tellico::Data::EntryList results = DO_FETCH1(fetcher, request, 1); 0069 0070 QCOMPARE(results.size(), 1); 0071 Tellico::Data::EntryPtr entry = results.at(0); 0072 QCOMPARE(entry->field(QStringLiteral("author")), QStringLiteral("Lois McMaster Bujold")); 0073 } 0074 0075 void OpenLibraryFetcherTest::testIsbn() { 0076 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::ISBN, 0077 QStringLiteral("0789312239")); 0078 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OpenLibraryFetcher(this)); 0079 QVERIFY(fetcher->canSearch(request.key())); 0080 0081 Tellico::Data::EntryList results = DO_FETCH(fetcher, request); 0082 0083 QCOMPARE(results.size(), 1); 0084 0085 Tellico::Data::EntryPtr entry = results.at(0); 0086 QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("This is Venice")); 0087 QCOMPARE(entry->field(QStringLiteral("author")), QStringLiteral("M. Sasek")); 0088 QCOMPARE(entry->field(QStringLiteral("isbn")), QStringLiteral("0-7893-1223-9")); 0089 QCOMPARE(entry->field(QStringLiteral("lccn")), QStringLiteral("2004110229")); 0090 QCOMPARE(entry->field(QStringLiteral("pub_year")), QStringLiteral("2005")); 0091 QCOMPARE(entry->field(QStringLiteral("genre")), QStringLiteral("Juvenile literature.")); 0092 QCOMPARE(entry->field(QStringLiteral("keyword")), QStringLiteral("Venice (Italy) -- Description and travel -- Juvenile literature")); 0093 QCOMPARE(entry->field(QStringLiteral("publisher")), QStringLiteral("Universe")); 0094 QCOMPARE(entry->field(QStringLiteral("language")), QStringLiteral("English")); 0095 QCOMPARE(entry->field(QStringLiteral("pages")), QStringLiteral("56")); 0096 QVERIFY(!entry->field(QStringLiteral("comments")).isEmpty()); 0097 } 0098 0099 void OpenLibraryFetcherTest::testIsbn13() { 0100 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::ISBN, 0101 QStringLiteral("9780596004361")); 0102 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OpenLibraryFetcher(this)); 0103 QVERIFY(fetcher->canSearch(request.key())); 0104 0105 Tellico::Data::EntryList results = DO_FETCH(fetcher, request); 0106 0107 // sometimes OpenLibrary has multiple works defined, so just verify one or more 0108 QVERIFY(results.size() >= 1); 0109 0110 Tellico::Data::EntryPtr entry = results.at(0); 0111 QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("C Pocket Reference")); 0112 QCOMPARE(entry->field(QStringLiteral("isbn")), QStringLiteral("978-0-596-00436-1")); 0113 } 0114 0115 void OpenLibraryFetcherTest::testLccn() { 0116 KConfigGroup cg = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig)->group(QStringLiteral("OpenLibrary")); 0117 cg.writeEntry("Custom Fields", QStringLiteral("openlibrary")); 0118 0119 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::LCCN, 0120 QStringLiteral("2004110229")); 0121 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OpenLibraryFetcher(this)); 0122 fetcher->readConfig(cg); 0123 QVERIFY(fetcher->canSearch(request.key())); 0124 0125 Tellico::Data::EntryList results = DO_FETCH(fetcher, request); 0126 0127 QCOMPARE(results.size(), 1); 0128 0129 Tellico::Data::EntryPtr entry = results.at(0); 0130 QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("This is Venice")); 0131 QCOMPARE(entry->field(QStringLiteral("author")), QStringLiteral("M. Sasek")); 0132 QCOMPARE(entry->field(QStringLiteral("isbn")), QStringLiteral("0-7893-1223-9")); 0133 QCOMPARE(entry->field(QStringLiteral("lccn")), QStringLiteral("2004110229")); 0134 QCOMPARE(entry->field(QStringLiteral("pub_year")), QStringLiteral("2005")); 0135 QCOMPARE(entry->field(QStringLiteral("genre")), QStringLiteral("Juvenile literature.")); 0136 QCOMPARE(entry->field(QStringLiteral("keyword")), QStringLiteral("Venice (Italy) -- Description and travel -- Juvenile literature")); 0137 QCOMPARE(entry->field(QStringLiteral("publisher")), QStringLiteral("Universe")); 0138 QCOMPARE(entry->field(QStringLiteral("language")), QStringLiteral("English")); 0139 QCOMPARE(entry->field(QStringLiteral("pages")), QStringLiteral("56")); 0140 QCOMPARE(entry->field(QStringLiteral("openlibrary")), QStringLiteral("https://openlibrary.org/books/OL3315616M")); 0141 QVERIFY(!entry->field(QStringLiteral("comments")).isEmpty()); 0142 } 0143 0144 void OpenLibraryFetcherTest::testMultipleIsbn() { 0145 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::ISBN, 0146 QStringLiteral("0789312239; 9780596000486")); 0147 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OpenLibraryFetcher(this)); 0148 0149 Tellico::Data::EntryList results = DO_FETCH(fetcher, request); 0150 0151 QCOMPARE(results.size(), 2); 0152 } 0153 0154 void OpenLibraryFetcherTest::testUpdate() { 0155 Tellico::Data::CollPtr coll(new Tellico::Data::BookCollection(true)); 0156 Tellico::Data::EntryPtr oldEntry(new Tellico::Data::Entry(coll)); 0157 coll->addEntries(oldEntry); 0158 0159 oldEntry->setField(QStringLiteral("title"), QStringLiteral("The Great Hunt")); 0160 oldEntry->setField(QStringLiteral("publisher"), QStringLiteral("Orbit")); 0161 0162 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OpenLibraryFetcher(this)); 0163 auto f = static_cast<Tellico::Fetch::OpenLibraryFetcher*>(fetcher.data()); 0164 0165 auto request = f->updateRequest(oldEntry); 0166 QCOMPARE(request.key(), Tellico::Fetch::Raw); 0167 request.setCollectionType(coll->type()); 0168 Tellico::Data::EntryList results = DO_FETCH(fetcher, request); 0169 0170 QCOMPARE(results.size(), 1); 0171 0172 Tellico::Data::EntryPtr entry = results.at(0); 0173 QVERIFY(entry); 0174 0175 QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("The Great Hunt")); 0176 QCOMPARE(entry->field(QStringLiteral("pub_year")), QStringLiteral("2014")); 0177 QCOMPARE(entry->field(QStringLiteral("publisher")), QStringLiteral("Orbit")); 0178 } 0179 0180 void OpenLibraryFetcherTest::testComic() { 0181 Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::ComicBook, Tellico::Fetch::ISBN, 0182 QStringLiteral("4048690663")); 0183 Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OpenLibraryFetcher(this)); 0184 QVERIFY(fetcher->canSearch(request.key())); 0185 0186 Tellico::Data::EntryList results = DO_FETCH(fetcher, request); 0187 0188 // sometimes OpenLibrary has multiple works defined, so just verify one or more 0189 QVERIFY(!results.isEmpty()); 0190 0191 Tellico::Data::EntryPtr entry = results.at(0); 0192 QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Yotsuba& Volume 1")); 0193 QCOMPARE(entry->field(QStringLiteral("isbn")), QStringLiteral("4-04869066-3")); 0194 }