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

0001 /***************************************************************************
0002     Copyright (C) 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 #include <config.h>
0026 #include "goodreadstest.h"
0027 
0028 #include "../translators/goodreadsimporter.h"
0029 #include "../collections/bookcollection.h"
0030 #include "../collectionfactory.h"
0031 #include "../images/imagefactory.h"
0032 #include "../utils/datafileregistry.h"
0033 
0034 #include <KConfig>
0035 #include <KSharedConfig>
0036 #include <KConfigGroup>
0037 
0038 #include <QTest>
0039 #include <QNetworkInterface>
0040 #include <QStandardPaths>
0041 
0042 QTEST_GUILESS_MAIN( GoodreadsTest )
0043 
0044 static bool hasNetwork() {
0045 #ifdef ENABLE_NETWORK_TESTS
0046   foreach(const QNetworkInterface& net, QNetworkInterface::allInterfaces()) {
0047     if(net.flags().testFlag(QNetworkInterface::IsUp) && !net.flags().testFlag(QNetworkInterface::IsLoopBack)) {
0048       return true;
0049     }
0050   }
0051 #endif
0052   return false;
0053 }
0054 
0055 void GoodreadsTest::initTestCase() {
0056   QStandardPaths::setTestModeEnabled(true);
0057   Tellico::RegisterCollection<Tellico::Data::BookCollection> registerBook(Tellico::Data::Collection::Book, "book");
0058   Tellico::DataFileRegistry::self()->addDataLocation(QFINDTESTDATA("../../xslt/goodreads2tellico.xsl"));
0059   Tellico::ImageFactory::init();
0060 }
0061 
0062 void GoodreadsTest::testImport() {
0063   if(!hasNetwork()) QSKIP("This test requires network access", SkipSingle);
0064 
0065   Tellico::Import::GoodreadsImporter imp;
0066   QVERIFY(imp.canImport(Tellico::Data::Collection::Book));
0067   QVERIFY(!imp.canImport(Tellico::Data::Collection::Album));
0068 
0069   KSharedConfig::Ptr config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig);
0070   KConfigGroup cg(config, QStringLiteral("ImportOptions - Goodreads"));
0071   // user name instead of user id to test conversion
0072   cg.writeEntry("User ID", QStringLiteral("robbystephenson"));
0073   config->sync();
0074   imp.setConfig(config);
0075 
0076   Tellico::Data::CollPtr coll(imp.collection());
0077   QVERIFY(coll);
0078   QCOMPARE(coll->type(), Tellico::Data::Collection::Book);
0079 
0080   Tellico::Data::EntryList entries = coll->entries();
0081   QVERIFY(!entries.isEmpty());
0082   Tellico::Data::EntryPtr entry = entries.at(0);
0083   QVERIFY(entry);
0084   QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Diplomatic Immunity"));
0085   QCOMPARE(entry->field(QStringLiteral("isbn")), QStringLiteral("0-7434-6802-3"));
0086   QCOMPARE(entry->field(QStringLiteral("pages")), QStringLiteral("367"));
0087   QCOMPARE(entry->field(QStringLiteral("goodreads")), QStringLiteral("https://www.goodreads.com/book/show/61901.Diplomatic_Immunity"));
0088   QCOMPARE(entry->field(QStringLiteral("binding")), QStringLiteral("Paperback"));
0089   QCOMPARE(entry->field(QStringLiteral("publisher")), QStringLiteral("Earthlight"));
0090   QCOMPARE(entry->field(QStringLiteral("author")), QStringLiteral("Lois McMaster Bujold"));
0091   QCOMPARE(entry->field(QStringLiteral("pub_year")), QStringLiteral("2003"));
0092   QCOMPARE(entry->field(QStringLiteral("read")), QStringLiteral("true"));
0093   QCOMPARE(entry->field(QStringLiteral("rating")), QStringLiteral("3"));
0094 
0095   // TODO: create a description field instead of using comments?
0096   QVERIFY(!entry->field(QStringLiteral("comments")).isEmpty());
0097   QVERIFY(!entry->field(QStringLiteral("cover")).isEmpty());
0098   QVERIFY(!entry->field(QStringLiteral("cover")).contains(QLatin1Char('/')));
0099 
0100   // verify user name was converted to user id
0101   QCOMPARE(cg.readEntry("User ID"), QStringLiteral("3937878"));
0102 }