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

0001 /***************************************************************************
0002     Copyright (C) 2021 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 "multifetchertest.h"
0026 
0027 #include "../fetch/multifetcher.h"
0028 #include "../fetch/execexternalfetcher.h"
0029 #include "../fetch/fetchmanager.h"
0030 #include "../fetch/messagelogger.h"
0031 #include "../collections/bookcollection.h"
0032 #include "../collectionfactory.h"
0033 #include "../entry.h"
0034 #include "../utils/datafileregistry.h"
0035 
0036 #include <KSharedConfig>
0037 #include <KConfigGroup>
0038 
0039 #include <QTest>
0040 
0041 QTEST_GUILESS_MAIN( MultiFetcherTest )
0042 
0043 MultiFetcherTest::MultiFetcherTest() : AbstractFetcherTest() {
0044 }
0045 
0046 void MultiFetcherTest::initTestCase() {
0047   Tellico::RegisterCollection<Tellico::Data::BookCollection> registerBook(Tellico::Data::Collection::Book, "book");
0048   Tellico::DataFileRegistry::self()->addDataLocation(QFINDTESTDATA("../../xslt/mods2tellico.xsl"));
0049 }
0050 
0051 void MultiFetcherTest::testEmpty() {
0052   auto config = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig)->group(QStringLiteral("multi"));
0053   config.writeEntry("CollectionType", int(Tellico::Data::Collection::Book));
0054 
0055   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::Title, QString());
0056   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::MultiFetcher(this));
0057   fetcher->readConfig(config);
0058 
0059   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0060   QVERIFY(results.isEmpty());
0061 }
0062 
0063 void MultiFetcherTest::testIsbn() {
0064   // just going to chain two trivial data sources together to test multifetcher
0065   Tellico::Fetch::Fetcher::Ptr modsFetcher1(new Tellico::Fetch::ExecExternalFetcher(this));
0066   Tellico::Fetch::Fetcher::Ptr modsFetcher2(new Tellico::Fetch::ExecExternalFetcher(this));
0067 
0068   KSharedConfig::Ptr catConfig = KSharedConfig::openConfig(QFINDTESTDATA("data/cat_mods.spec"), KConfig::SimpleConfig);
0069   KConfigGroup catConfigGroup = catConfig->group(QStringLiteral("<default>"));
0070   catConfigGroup.writeEntry("ExecPath", QFINDTESTDATA("data/cat_mods.sh")); // update command path to local script
0071   catConfigGroup.markAsClean(); // don't edit the file on sync()
0072   modsFetcher1->readConfig(catConfigGroup);
0073   modsFetcher1->setMessageHandler(new Tellico::Fetch::MessageLogger);
0074   modsFetcher2->readConfig(catConfigGroup);
0075   modsFetcher2->setMessageHandler(new Tellico::Fetch::MessageLogger);
0076 
0077   auto fetchManager = Tellico::Fetch::Manager::self();
0078   fetchManager->addFetcher(modsFetcher1);
0079   fetchManager->addFetcher(modsFetcher2);
0080 
0081   QStringList uuids;
0082   uuids << modsFetcher1->uuid() << modsFetcher2->uuid();
0083 
0084   auto multiConfig = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig)->group(QStringLiteral("multi"));
0085   multiConfig.writeEntry("Sources", uuids);
0086   multiConfig.writeEntry("CollectionType", int(Tellico::Data::Collection::Book));
0087 
0088   Tellico::Fetch::FetchRequest isbnRequest(Tellico::Data::Collection::Book,
0089                                            Tellico::Fetch::ISBN,
0090                                            QStringLiteral("0801486394"));
0091   Tellico::Fetch::Fetcher::Ptr multiFetcher(new Tellico::Fetch::MultiFetcher(this));
0092   multiFetcher->readConfig(multiConfig);
0093   multiFetcher->setMessageHandler(new Tellico::Fetch::MessageLogger);
0094 
0095   Tellico::Data::EntryList results = DO_FETCH(multiFetcher, isbnRequest);
0096 
0097   QCOMPARE(results.size(), 1);
0098 
0099   Tellico::Data::EntryPtr entry = results.at(0);
0100   QVERIFY(entry);
0101 
0102   QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Sound and fury"));
0103   QCOMPARE(entry->field(QStringLiteral("isbn")), QStringLiteral("0-8014-8639-4"));
0104 }