File indexing completed on 2024-04-28 12:29:20

0001 /*
0002     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "backends/hafasqueryparser.h"
0008 
0009 #include <KPublicTransport/Journey>
0010 #include <KPublicTransport/Location>
0011 
0012 #include <QFile>
0013 #include <QTest>
0014 
0015 #define s(x) QStringLiteral(x)
0016 
0017 using namespace KPublicTransport;
0018 
0019 class HafasQueryParserTest : public QObject
0020 {
0021     Q_OBJECT
0022 private:
0023     QByteArray readFile(const char *fn)
0024     {
0025         QFile f(QString::fromUtf8(fn));
0026         f.open(QFile::ReadOnly);
0027         return f.readAll();
0028     }
0029 
0030 private Q_SLOTS:
0031     void initTestCase()
0032     {
0033         qputenv("TZ", "UTC");
0034     }
0035 
0036     void testParseJourneyError()
0037     {
0038 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
0039         QSKIP("not supported on big endian systems yet!", SkipAll);
0040 #endif
0041         HafasQueryParser p;
0042         const auto res = p.parseQueryJourneyResponse(readFile(SOURCE_DIR "/data/hafas/journey-binary-error.bin.gz"));
0043         QVERIFY(res.empty());
0044         QCOMPARE(p.error(), Reply::NotFoundError);
0045     }
0046 
0047     void testParseLocationByCoordinateResponse()
0048     {
0049         HafasQueryParser p;
0050         p.setLocationIdentifierTypes(s("testId"));
0051         auto res = p.parseQueryLocationResponse(readFile(SOURCE_DIR "/data/hafas/query-location-response.json"));
0052         QCOMPARE(res.size(), 1);
0053         QCOMPARE(p.error(), Reply::NoError);
0054 
0055         auto loc = res[0];
0056         QCOMPARE(loc.name(), s("Frankfurt (Main) Hauptbahnhof"));
0057         QCOMPARE(loc.identifier(s("testId")), s("3000010"));
0058         QCOMPARE((int)loc.latitude(), 50);
0059         QCOMPARE((int)loc.longitude(), 8);
0060 
0061         res = p.parseQueryLocationResponse(readFile(SOURCE_DIR "/data/hafas/query-location-response-sbb-broken-json.json"));
0062         QCOMPARE(res.size(), 1);
0063         QCOMPARE(p.error(), Reply::NoError);
0064 
0065         loc = res[0];
0066         QCOMPARE(loc.name(), s("Randa"));
0067         QCOMPARE(loc.identifier(s("testId")), s("8501687"));
0068         QCOMPARE((int)loc.latitude(), 46);
0069         QCOMPARE((int)loc.longitude(), 7);
0070     }
0071 };
0072 
0073 QTEST_GUILESS_MAIN(HafasQueryParserTest)
0074 
0075 #include "hafasqueryparsertest.moc"