Commit e3fe9220 authored by Volker Krause's avatar Volker Krause
Browse files

Unpack arrays in the reservationFor property in JSON-LD import filtering

Also, add a generic unit test for JSON-LD normalization.
parent 351f83e0
[
{
"@context": "http://schema.org",
"@type": "FlightReservation",
"reservationFor": [
{
"@type": "Flight",
"airline": {
"@type": "Airline",
"iataCode": "AB"
},
"arrivalAirport": {
"@type": "Airport",
"iataCode": "TXL"
},
"arrivalTime": "1970-01-01T01:00:00",
"departureAirport": {
"@type": "Airport",
"iataCode": "SXF"
},
"departureTime": "1970-01-01T00:45:00",
"flightNumber": "1234"
}
],
"reservationNumber": "XXX007"
}
]
[
{
"@context": "http://schema.org",
"@type": "FlightReservation",
"reservationFor": {
"@type": "Flight",
"airline": {
"@type": "Airline",
"iataCode": "AB"
},
"arrivalAirport": {
"@type": "Airport",
"iataCode": "TXL"
},
"arrivalTime": "1970-01-01T01:00:00",
"departureAirport": {
"@type": "Airport",
"iataCode": "SXF"
},
"departureTime": "1970-01-01T00:45:00",
"flightNumber": "1234"
},
"reservationNumber": "XXX007"
}
]
......@@ -32,6 +32,7 @@
#include <QJsonDocument>
#include <QJsonObject>
#include <QObject>
#include <QProcess>
#include <QTest>
#include <QTimeZone>
......@@ -40,6 +41,14 @@ using namespace KItinerary;
class JsonLdDocumentTest : public QObject
{
Q_OBJECT
private:
QByteArray readFile(const QString &fn)
{
QFile f(fn);
f.open(QFile::ReadOnly);
return f.readAll();
}
private Q_SLOTS:
void initTestCase()
{
......@@ -409,6 +418,50 @@ private Q_SLOTS:
QCOMPARE(flight.departureTime(), result);
QCOMPARE(flight.departureTime().timeSpec(), result.timeSpec());
}
void testNormalize_data()
{
QTest::addColumn<QString>("inFile");
QTest::addColumn<QString>("refFile");
QDir dir(QStringLiteral(SOURCE_DIR "/jsonlddata"));
const auto lst = dir.entryList(QStringList(QStringLiteral("*.in.json")), QDir::Files | QDir::Readable | QDir::NoSymLinks);
for (const auto &file : lst) {
const QString refFile = dir.path() + QLatin1Char('/') + file.left(file.size() - 7) + QStringLiteral("out.json");
if (!QFile::exists(refFile)) {
qDebug() << "reference file" << refFile << "does not exist, skipping test file" << file;
continue;
}
QTest::newRow(file.toLatin1().constData()) << QString(dir.path() + QLatin1Char('/') + file) << refFile;
}
}
void testNormalize()
{
QFETCH(QString, inFile);
QFETCH(QString, refFile);
const auto inJson = QJsonDocument::fromJson(readFile(inFile)).array();
QVERIFY(!inJson.isEmpty());
const auto normalizedJson = JsonLdDocument::toJson(JsonLdDocument::fromJson(inJson));
QVERIFY(!normalizedJson.isEmpty());
const auto refJson = QJsonDocument::fromJson(readFile(refFile)).array();
QVERIFY(!refJson.isEmpty());
qDebug() << normalizedJson;
if (normalizedJson != refJson) {
QFile f(refFile + QLatin1String(".fail"));
QVERIFY(f.open(QFile::WriteOnly));
f.write(QJsonDocument(normalizedJson).toJson());
f.close();
QProcess proc;
proc.setProcessChannelMode(QProcess::ForwardedChannels);
proc.start(QStringLiteral("diff"), {QStringLiteral("-u"), refFile, f.fileName()});
QVERIFY(proc.waitForFinished());
}
QCOMPARE(normalizedJson, refJson);
}
};
QTEST_APPLESS_MAIN(JsonLdDocumentTest)
......
......@@ -16,6 +16,7 @@
*/
#include "jsonldimportfilter.h"
#include "logging.h"
#include <QDebug>
#include <QJsonArray>
......@@ -138,6 +139,18 @@ static void filterReservation(QJsonObject &res)
}
}
// unpack reservationFor array - if we ever encounter more than one element in here we'd need to multiply the result
const auto resFor = res.value(QLatin1String("reservationFor"));
if (resFor.isArray()) {
const auto a = resFor.toArray();
if (a.size() > 1) {
qCWarning(Log) << "Found reservationFor array with" << a.size() << "elements!";
}
if (!a.isEmpty()) {
res.insert(QStringLiteral("reservationFor"), a.at(0));
}
}
// legacy potentialAction property
renameProperty(res, "action", "potentialAction");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment