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