File indexing completed on 2024-05-12 05:13:46

0001 /*
0002     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "publictransport.h"
0008 
0009 #include <KItinerary/Reservation>
0010 #include <KItinerary/TrainTrip>
0011 #include <KItinerary/Ticket>
0012 
0013 #include <KPublicTransport/Journey>
0014 #include <KPublicTransport/JourneyRequest>
0015 #include <KPublicTransport/Manager>
0016 
0017 #include <QtTest/qtest.h>
0018 #include <QStandardPaths>
0019 #include <QTimeZone>
0020 
0021 using namespace KItinerary;
0022 
0023 class PublicTransportTest : public QObject
0024 {
0025     Q_OBJECT
0026 private Q_SLOTS:
0027     void initTestCase()
0028     {
0029         qputenv("TZ", "Europe/Zurich");
0030         QStandardPaths::setTestModeEnabled(true);
0031     }
0032 
0033     void testApplyJourneySection()
0034     {
0035         TrainStation dep;
0036         dep.setName(QStringLiteral("Visp"));
0037         dep.setGeo(GeoCoordinates(46.2939, 7.8814));
0038         TrainStation arr;
0039         arr.setName(QStringLiteral("Randa"));
0040         arr.setGeo(GeoCoordinates(46.0998, 7.7814));
0041         TrainTrip trip;
0042         trip.setDepartureStation(dep);
0043         trip.setDepartureTime(QDateTime({2017, 9, 10}, {14, 8}));
0044         trip.setDeparturePlatform(QStringLiteral("3"));
0045         trip.setArrivalStation(arr);
0046         trip.setArrivalTime(QDateTime({2017, 9, 10}, {14, 53}));
0047         trip.setArrivalPlatform(QStringLiteral("1"));
0048         trip.setTrainName(QStringLiteral("R"));
0049         trip.setTrainNumber(QStringLiteral("241"));
0050 
0051         Seat seat;
0052         seat.setSeatNumber(QStringLiteral("42"));
0053         seat.setSeatingType(QStringLiteral("2nd Class"));
0054         Ticket ticket;
0055         ticket.setTicketToken(QStringLiteral("qr:TICKETTOKEN"));
0056         ticket.setTicketedSeat(seat);
0057 
0058         TrainReservation res;
0059         res.setReservationFor(trip);
0060         res.setReservedTicket(ticket);
0061 
0062         KPublicTransport::Location from, to;
0063         from.setName(QStringLiteral("Visp"));
0064         to.setName(QStringLiteral("Zermatt"));
0065         KPublicTransport::Route route;
0066         KPublicTransport::Line line;
0067         line.setName(QStringLiteral("R 342"));
0068         route.setLine(line);
0069         KPublicTransport::JourneySection section;
0070         section.setFrom(from);
0071         section.setScheduledDepartureTime(QDateTime({2017, 9, 10}, {15, 8}));
0072         section.setTo(to);
0073         section.setScheduledArrivalTime(QDateTime({2017, 9, 10}, {16, 15}));
0074         section.setRoute(route);
0075         section.setScheduledArrivalPlatform(QStringLiteral("2"));
0076 
0077         const auto newRes = PublicTransport::applyJourneySection(res, section).value<TrainReservation>();
0078         const auto newTrip = newRes.reservationFor().value<TrainTrip>();
0079 
0080         // times need to be updated
0081         QCOMPARE(newTrip.departureTime(), section.scheduledDepartureTime());
0082         QCOMPARE(newTrip.arrivalTime(), section.scheduledArrivalTime());
0083 
0084         // times have correct timezones now (while input was in local time)
0085         QCOMPARE(newTrip.departureTime().timeSpec(), Qt::TimeZone);
0086         QCOMPARE(newTrip.departureTime().timeZone().id(), "Europe/Zurich");
0087 
0088         // old line name parts need to be cleared
0089         QCOMPARE(newTrip.trainName(), QString());
0090         QCOMPARE(newTrip.trainNumber(), QLatin1StringView("R 342"));
0091 
0092         // old platform information need to be cleared
0093         QCOMPARE(newTrip.departurePlatform(), QString());
0094         QCOMPARE(newTrip.arrivalPlatform(), section.scheduledArrivalPlatform());
0095 
0096         // departure location didn't change, so retaining details is ok
0097         const auto newDep = newTrip.departureStation();
0098         QCOMPARE(newDep.name(), section.from().name());
0099         QVERIFY(newDep.geo().isValid());
0100 
0101         // arrival location did change, so old data must not remain
0102         const auto newArr = newTrip.arrivalStation();
0103         QCOMPARE(newArr.name(), section.to().name());
0104         QVERIFY(!newArr.geo().isValid());
0105 
0106         // locations got country data assigned
0107         QCOMPARE(newDep.address().addressCountry(), QLatin1StringView("CH"));
0108 
0109         // ticket token is preserved, but seat reservation is cleared
0110         const auto newTicket = newRes.reservedTicket().value<Ticket>();
0111         QCOMPARE(newTicket.ticketToken(), ticket.ticketToken());
0112         QCOMPARE(newTicket.ticketedSeat().seatNumber(), QString());
0113         QCOMPARE(newTicket.ticketedSeat().seatingType(), seat.seatingType()); // class is preserved
0114     }
0115 
0116     void testWarn()
0117     {
0118         KPublicTransport::JourneySection section;
0119         QVERIFY(!PublicTransport().warnAboutSection(section));
0120         section.setMode(KPublicTransport::JourneySection::Walking);
0121         QVERIFY(!PublicTransport().warnAboutSection(section));
0122         section.setDistance(2000);
0123         QVERIFY(PublicTransport().warnAboutSection(section));
0124     }
0125 
0126     void testBackendSelection()
0127     {
0128         Organization provider;
0129         provider.setIdentifier(QStringLiteral("uic:1181"));
0130         TrainTrip trip;
0131         trip.setProvider(provider);
0132         TrainReservation res;
0133         res.setReservationFor(trip);
0134 
0135         KPublicTransport::Manager mgr;
0136         KPublicTransport::JourneyRequest req;
0137 
0138         mgr.setBackendEnabled(QLatin1StringView("at_oebb"), true);
0139         PublicTransport::selectBackends(req, &mgr, res);
0140         QCOMPARE(req.backendIds(), QStringList({ QLatin1StringView("at_oebb") }));
0141         mgr.setBackendEnabled(QLatin1StringView("at_oebb"), false);
0142         PublicTransport::selectBackends(req, &mgr, res);
0143         QCOMPARE(req.backendIds(), QStringList());
0144 
0145         provider.setIdentifier(QStringLiteral("uic:80"));
0146         trip.setProvider(provider);
0147         res.setReservationFor(trip);
0148         mgr.setBackendEnabled(QLatin1StringView("de_db"), true);
0149         PublicTransport::selectBackends(req, &mgr, res);
0150         QCOMPARE(req.backendIds(), QStringList({ QLatin1StringView("de_db") }));
0151     }
0152 };
0153 
0154 QTEST_GUILESS_MAIN(PublicTransportTest)
0155 
0156 #include "publictransporttest.moc"