File indexing completed on 2024-03-24 04:43:43
0001 /* 0002 SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include <KPublicTransport/Cache> 0008 #include <KPublicTransport/Location> 0009 #include <KPublicTransport/LocationRequest> 0010 0011 #include <QDir> 0012 #include <QStandardPaths> 0013 #include <QTest> 0014 #include <QTimeZone> 0015 0016 using namespace KPublicTransport; 0017 0018 class CacheTest : public QObject 0019 { 0020 Q_OBJECT 0021 0022 private Q_SLOTS: 0023 void initTestCase() 0024 { 0025 qputenv("TZ", "UTC"); 0026 QStandardPaths::setTestModeEnabled(true); 0027 QDir(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)).removeRecursively(); 0028 } 0029 0030 void testLocationCache() 0031 { 0032 LocationRequest req; 0033 req.setCoordinate(52.5, 13.5); 0034 QVERIFY(!req.cacheKey().isEmpty()); 0035 0036 auto entry = Cache::lookupLocation(QStringLiteral("unittest"), req.cacheKey()); 0037 QCOMPARE(entry.type, CacheHitType::Miss); 0038 0039 Cache::addNegativeLocationCacheEntry(QStringLiteral("unittest"), req.cacheKey()); 0040 entry = Cache::lookupLocation(QStringLiteral("unittest"), req.cacheKey()); 0041 QCOMPARE(entry.type, CacheHitType::Negative); 0042 Cache::expire(); 0043 entry = Cache::lookupLocation(QStringLiteral("unittest"), req.cacheKey()); 0044 QCOMPARE(entry.type, CacheHitType::Negative); 0045 0046 Location loc; 0047 loc.setName(QStringLiteral("Randa")); 0048 loc.setCoordinate(7.6, 46.1); 0049 loc.setIdentifier(QStringLiteral("uic"), QStringLiteral("85xxxxx")); 0050 0051 Attribution attr; 0052 attr.setName(QStringLiteral("KDE")); 0053 attr.setLicense(QStringLiteral("LGPL")); 0054 attr.setUrl(QUrl(QStringLiteral("https://www.kde.org"))); 0055 0056 Cache::addLocationCacheEntry(QStringLiteral("unittest"), req.cacheKey(), {loc}, {attr}); 0057 entry = Cache::lookupLocation(QStringLiteral("unittest"), req.cacheKey()); 0058 QCOMPARE(entry.type, CacheHitType::Positive); 0059 QCOMPARE(entry.data.size(), 1); 0060 QCOMPARE(entry.data[0].name(), loc.name()); 0061 QCOMPARE(entry.data[0].latitude(), 7.6f); 0062 QCOMPARE(entry.data[0].longitude(), 46.1f); 0063 QCOMPARE(entry.data[0].identifiers().size(), 1); 0064 QCOMPARE(entry.data[0].identifier(QStringLiteral("uic")), QLatin1String("85xxxxx")); 0065 QCOMPARE(entry.data[0].timeZone().isValid(), false); 0066 QCOMPARE(entry.attributions.size(), 1); 0067 QCOMPARE(entry.attributions[0].name(), QLatin1String("KDE")); 0068 QCOMPARE(entry.attributions[0].license(), QLatin1String("LGPL")); 0069 QCOMPARE(entry.attributions[0].url().toString(), QLatin1String("https://www.kde.org")); 0070 0071 std::vector<Attribution> cachedAttrs; 0072 Cache::allCachedAttributions(cachedAttrs); 0073 QCOMPARE(cachedAttrs.size(), 1); 0074 QCOMPARE(cachedAttrs[0].name(), attr.name()); 0075 0076 Cache::expire(); 0077 0078 entry = Cache::lookupLocation(QStringLiteral("unittest"), req.cacheKey()); 0079 QCOMPARE(entry.type, CacheHitType::Positive); 0080 QCOMPARE(entry.data.size(), 1); 0081 QCOMPARE(entry.attributions.size(), 1); 0082 } 0083 0084 void testLocationCacheKey() 0085 { 0086 { 0087 LocationRequest req; 0088 req.setCoordinate(-52.5, -13.5); 0089 QCOMPARE(req.cacheKey(), QLatin1String("73be02317c4a7a43aff31f3186b18c22c6466250")); 0090 req.setTypes(Location::RentedVehicle|Location::RentedVehicleStation); 0091 QCOMPARE(req.cacheKey(), QLatin1String("4dfaecadb1a7d03106fe585b119948c158509807")); 0092 } 0093 { 0094 LocationRequest req; 0095 req.setName(QStringLiteral("Randa")); 0096 QCOMPARE(req.cacheKey(), QLatin1String("c4053bc0f4582f2977f3e43cad07b6587e6b6935")); 0097 req.setName(QStringLiteral("RANDA")); 0098 QCOMPARE(req.cacheKey(), QLatin1String("c4053bc0f4582f2977f3e43cad07b6587e6b6935")); 0099 } 0100 } 0101 }; 0102 0103 QTEST_GUILESS_MAIN(CacheTest) 0104 0105 #include "cachetest.moc"