File indexing completed on 2024-05-12 05:09:56

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 "csvtest.h"
0026 
0027 #include "../translators/csvparser.h"
0028 #include "../translators/csvimporter.h"
0029 #include "../translators/csvexporter.h"
0030 #include "../collectionfactory.h"
0031 #include "../collections/bookcollection.h"
0032 #include "../collections/musiccollection.h"
0033 
0034 #include <KLocalizedString>
0035 
0036 #include <QTest>
0037 #include <QStandardPaths>
0038 
0039 QTEST_MAIN( CsvTest )
0040 
0041 #define QL1(x) QStringLiteral(x)
0042 
0043 void CsvTest::initTestCase() {
0044   QStandardPaths::setTestModeEnabled(true);
0045   KLocalizedString::setApplicationDomain("tellico");
0046   Tellico::RegisterCollection<Tellico::Data::BookCollection> registerBook(Tellico::Data::Collection::Book, "book");
0047   Tellico::RegisterCollection<Tellico::Data::MusicCollection> registerAlbum(Tellico::Data::Collection::Album, "album");
0048 }
0049 
0050 void CsvTest::cleanupTestCase() {
0051 }
0052 
0053 void CsvTest::testTokens() {
0054   QFETCH(QString, line);
0055   QFETCH(QStringList, tokens);
0056   QFETCH(QString, delim);
0057 
0058   Tellico::CSVParser p(line);
0059   p.setDelimiter(delim);
0060 
0061   QStringList tokensNew = p.nextTokens();
0062 
0063   QCOMPARE(tokensNew, tokens);
0064 }
0065 
0066 void CsvTest::testTokens_data() {
0067   QTest::addColumn<QString>("line");
0068   QTest::addColumn<QString>("delim");
0069   QTest::addColumn<QStringList>("tokens");
0070 
0071   QTest::newRow("basic") << "robby,stephenson is cool\t,," << ","
0072     << (QStringList() << QL1("robby") << QL1("stephenson is cool") << QString() << QString());
0073   QTest::newRow("space") << "robby,stephenson is cool\t,," << " "
0074     << (QStringList() << QL1("robby,stephenson") << QL1("is") << QL1("cool\t,,"));
0075   QTest::newRow("tab") << "robby\t\tstephenson" << "\t"
0076     << (QStringList() << QL1("robby") << QString() << QL1("stephenson"));
0077   // quotes get swallowed
0078   QTest::newRow("quotes") << "robby,\"stephenson,is,cool\"" << ","
0079     << (QStringList() << QL1("robby") << QL1("stephenson,is,cool"));
0080   QTest::newRow("newline") << "robby,\"stephenson\n,is,cool\"" << ","
0081     << (QStringList() << QL1("robby") << QL1("stephenson\n,is,cool"));
0082 }
0083 
0084 void CsvTest::testEntry() {
0085   Tellico::Data::CollPtr coll(new Tellico::Data::Collection(true));
0086   Tellico::Data::EntryPtr entry(new Tellico::Data::Entry(coll));
0087   entry->setField(QStringLiteral("title"), QStringLiteral("title, with comma"));
0088   coll->addEntries(entry);
0089 
0090   Tellico::Export::CSVExporter exporter(coll);
0091   exporter.setEntries(coll->entries());
0092   exporter.setFields(Tellico::Data::FieldList() << coll->fieldByName(QStringLiteral("title")));
0093 
0094   QString output = exporter.text();
0095   // the header line has the field titles, skip that
0096   output = output.section(QLatin1Char('\n'), 1);
0097   output.chop(1);
0098   QCOMPARE(output, QStringLiteral("\"title, with comma\""));
0099 }
0100 
0101 void CsvTest::testImportBook() {
0102   QUrl url = QUrl::fromLocalFile(QFINDTESTDATA("data/test-book.csv"));
0103   Tellico::Import::CSVImporter importer(url);
0104   importer.setCollectionType(Tellico::Data::Collection::Book);
0105   importer.setImportColumns({0, 1, 3, 4},
0106                             {QStringLiteral("title"), QStringLiteral("author"), QStringLiteral("isbn"), QStringLiteral("binding")});
0107   importer.slotFirstRowHeader(true);
0108   Tellico::Data::CollPtr coll = importer.collection();
0109   QVERIFY(coll);
0110   QCOMPARE(coll->type(), Tellico::Data::Collection::Book);
0111 
0112   Tellico::Data::EntryList entries = coll->entries();
0113   QCOMPARE(entries.size(), 1);
0114   Tellico::Data::EntryPtr entry = entries.at(0);
0115   QVERIFY(entry);
0116   QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("Cruel as the Grave"));
0117   QCOMPARE(entry->field(QStringLiteral("author")), QStringLiteral("Penman, Sharon"));
0118   QCOMPARE(entry->field(QStringLiteral("isbn")), QStringLiteral("0140270760"));
0119   QCOMPARE(entry->field(QStringLiteral("binding")), QStringLiteral("square"));
0120 }
0121 
0122 void CsvTest::testBug386483() {
0123   QUrl url = QUrl::fromLocalFile(QFINDTESTDATA("data/test-bug386483.csv"));
0124   Tellico::Import::CSVImporter importer(url);
0125   importer.setCollectionType(Tellico::Data::Collection::Album);
0126   importer.setImportColumns({0, 1, 2, 3, 6},
0127                             {QStringLiteral("title"), QStringLiteral("label"), QStringLiteral("year"),
0128                              QStringLiteral("track"), QStringLiteral("keyword")});
0129   importer.slotFirstRowHeader(false);
0130   importer.setDelimiter(QStringLiteral(","));
0131   importer.setColumnDelimiter(QStringLiteral("::"));
0132   importer.setRowDelimiter(QStringLiteral("|"));
0133   Tellico::Data::CollPtr coll = importer.collection();
0134   QVERIFY(coll);
0135   QCOMPARE(coll->type(), Tellico::Data::Collection::Album);
0136 
0137   Tellico::Data::EntryList entries = coll->entries();
0138   QCOMPARE(entries.size(), 1);
0139   Tellico::Data::EntryPtr entry = entries.at(0);
0140   QVERIFY(entry);
0141   QCOMPARE(entry->field(QStringLiteral("title")), QStringLiteral("album"));
0142   QCOMPARE(entry->field(QStringLiteral("year")), QStringLiteral("2022"));
0143   QCOMPARE(entry->field(QStringLiteral("keyword")), QStringLiteral("https://tellico-project.org"));
0144   QStringList trackList = Tellico::FieldFormat::splitTable(entry->field(QStringLiteral("track")));
0145   QCOMPARE(trackList.size(), 2);
0146   QStringList cols = Tellico::FieldFormat::splitRow(trackList.at(1));
0147   QCOMPARE(cols.size(), 3);
0148   QCOMPARE(cols.at(0), QStringLiteral("track2"));
0149   QCOMPARE(cols.at(1), QStringLiteral("artist2"));
0150   QCOMPARE(cols.at(2), QStringLiteral("0:34"));
0151 }