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

0001 /***************************************************************************
0002     Copyright (C) 2009 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 "alexandriatest.h"
0026 
0027 #include "../translators/alexandriaimporter.h"
0028 #include "../translators/alexandriaexporter.h"
0029 #include "../collections/bookcollection.h"
0030 #include "../images/imagefactory.h"
0031 
0032 #include <QTest>
0033 #include <QTemporaryDir>
0034 #include <QStandardPaths>
0035 
0036 QTEST_GUILESS_MAIN( AlexandriaTest )
0037 
0038 #define QSL(x) QStringLiteral(x)
0039 
0040 void AlexandriaTest::initTestCase() {
0041   QStandardPaths::setTestModeEnabled(true);
0042   Tellico::ImageFactory::init();
0043 }
0044 
0045 void AlexandriaTest::testImport() {
0046   Tellico::Import::AlexandriaImporter importer;
0047   importer.setLibraryPath(QFINDTESTDATA("/data/alexandria/"));
0048 
0049   // shut the importer up about current collection
0050   Tellico::Data::CollPtr tmpColl(new Tellico::Data::BookCollection(true));
0051   importer.setCurrentCollection(tmpColl);
0052 
0053   Tellico::Data::CollPtr coll = importer.collection();
0054 
0055   QVERIFY(coll);
0056   QCOMPARE(coll->type(), Tellico::Data::Collection::Book);
0057   QCOMPARE(coll->entryCount(), 2);
0058   // should be translated somehow
0059   QCOMPARE(coll->title(), QSL("My Books"));
0060 
0061   Tellico::Data::EntryPtr entry = coll->entryById(1);
0062   QCOMPARE(entry->field(QSL("title")), QSL("The Hallowed Hunt"));
0063   QCOMPARE(entry->field(QSL("comments")), QSL("first line<br/>second line"));
0064 
0065   entry = coll->entryById(2);
0066   QCOMPARE(entry->field(QSL("title")), QSL("Life Together"));
0067   QCOMPARE(entry->field(QSL("author")), QSL("Dietrich Bonhoeffer; My Other Author"));
0068   // translated
0069   QCOMPARE(entry->field(QSL("binding")), QSL("Hardback"));
0070   QCOMPARE(entry->field(QSL("isbn")), QSL("0-06-060853-6"));
0071   QCOMPARE(entry->field(QSL("pub_year")), QSL("1993"));
0072   QCOMPARE(entry->field(QSL("publisher")), QSL("Harper Collins"));
0073   QCOMPARE(entry->field(QSL("rating")), QSL("3"));
0074   QCOMPARE(entry->field(QSL("read")), QSL("true"));
0075   QCOMPARE(entry->field(QSL("loaned")), QString());
0076   QVERIFY(!entry->field(QSL("comments")).isEmpty());
0077 
0078   QTemporaryDir outputDir;
0079 
0080   Tellico::Export::AlexandriaExporter exporter(coll);
0081   exporter.setEntries(coll->entries());
0082   exporter.setURL(QUrl::fromLocalFile(outputDir.path()));
0083   QVERIFY(exporter.exec());
0084 
0085   importer.setLibraryPath(outputDir.path() + QSL("/.alexandria/") + coll->title());
0086   Tellico::Data::CollPtr coll2 = importer.collection();
0087 
0088   QVERIFY(coll2);
0089   QCOMPARE(coll2->type(), coll->type());
0090   QCOMPARE(coll2->title(), coll->title());
0091   QCOMPARE(coll2->entryCount(), coll->entryCount());
0092 
0093   foreach(Tellico::Data::EntryPtr e1, coll->entries()) {
0094     // assume IDs stay the same
0095     Tellico::Data::EntryPtr e2 = coll2->entryById(e1->id());
0096     QVERIFY(e2);
0097     foreach(Tellico::Data::FieldPtr f, coll->fields()) {
0098       // skip images
0099       if(f->type() != Tellico::Data::Field::Image) {
0100         QCOMPARE(f->name() + e1->field(f), f->name() + e2->field(f));
0101       }
0102     }
0103   }
0104 }
0105 
0106 void AlexandriaTest::testEscapeText() {
0107   // text escaping puts slashes in for quotes and remove control characters
0108   QString input(QStringLiteral("\"test \uFD3F") + QString(0x90));
0109   QCOMPARE(Tellico::Export::AlexandriaExporter::escapeText(input), QStringLiteral("\\\"test \uFD3F"));
0110 }