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

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