File indexing completed on 2024-05-12 05:17:32
0001 /* 0002 SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "extractorpostprocessor.h" 0008 #include "jsonlddocument.h" 0009 0010 #include <KItinerary/Organization> 0011 #include <KItinerary/Place> 0012 0013 #include <QDebug> 0014 #include <QDir> 0015 #include <QFile> 0016 #include <QJsonArray> 0017 #include <QJsonDocument> 0018 #include <QObject> 0019 #include <QProcess> 0020 #include <QTest> 0021 0022 using namespace KItinerary; 0023 0024 class PostprocessorTest : public QObject 0025 { 0026 Q_OBJECT 0027 private Q_SLOTS: 0028 void testPostProc_data() 0029 { 0030 QTest::addColumn<QString>("preFile"); 0031 QTest::addColumn<QString>("postFile"); 0032 0033 QDir dir(QStringLiteral(SOURCE_DIR "/postprocessordata")); 0034 const auto lst = dir.entryList(QStringList(QStringLiteral("*.pre.json")), QDir::Files | QDir::Readable | QDir::NoSymLinks); 0035 for (const auto &file : lst) { 0036 const QString refFile = dir.path() + QLatin1Char('/') + file.left(file.size() - 8) + QStringLiteral("post.json"); 0037 if (!QFile::exists(refFile)) { 0038 qDebug() << "reference file" << refFile << "does not exist, skipping test file" << file; 0039 continue; 0040 } 0041 QTest::newRow(file.toLatin1().constData()) << QString(dir.path() + QLatin1Char('/') + file) << refFile; 0042 } 0043 } 0044 0045 void testPostProc() 0046 { 0047 QFETCH(QString, preFile); 0048 QFETCH(QString, postFile); 0049 0050 QFile f(preFile); 0051 QVERIFY(f.open(QFile::ReadOnly)); 0052 const auto inArray = QJsonDocument::fromJson(f.readAll()).array(); 0053 QVERIFY(!inArray.isEmpty()); 0054 const auto preData = JsonLdDocument::fromJson(inArray); 0055 QCOMPARE(inArray.size(), preData.size()); 0056 0057 ExtractorPostprocessor postproc; 0058 postproc.setContextDate({QDate(2018, 4, 2), QTime()}); 0059 postproc.process(preData); 0060 const auto outArray = JsonLdDocument::toJson(postproc.result()); 0061 QCOMPARE(outArray.size(), postproc.result().size()); 0062 0063 QFile ref(postFile); 0064 QVERIFY(ref.open(QFile::ReadOnly)); 0065 const auto refArray = QJsonDocument::fromJson(ref.readAll()).array(); 0066 0067 if (outArray != refArray) { 0068 QFile failFile(postFile + QLatin1StringView(".fail")); 0069 QVERIFY(failFile.open(QFile::WriteOnly)); 0070 failFile.write(QJsonDocument(outArray).toJson()); 0071 failFile.close(); 0072 0073 QProcess proc; 0074 proc.setProcessChannelMode(QProcess::ForwardedChannels); 0075 proc.start(QStringLiteral("diff"), 0076 {QStringLiteral("-u"), postFile, failFile.fileName()}); 0077 QVERIFY(proc.waitForFinished()); 0078 } 0079 QCOMPARE(refArray.size(), postproc.result().size()); 0080 QCOMPARE(outArray, refArray); 0081 } 0082 }; 0083 0084 QTEST_APPLESS_MAIN(PostprocessorTest) 0085 0086 #include "postprocessortest.moc"