File indexing completed on 2023-09-24 07:58:21
0001 /* 0002 SPDX-FileCopyrightText: 2015 Ivan Cukic <ivan.cukic(at)kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "ResultSetTest.h" 0008 0009 #include <QCoreApplication> 0010 #include <QDBusConnection> 0011 #include <QDBusConnectionInterface> 0012 #include <QDebug> 0013 #include <QString> 0014 #include <QTemporaryDir> 0015 #include <QTest> 0016 0017 #include <query.h> 0018 #include <resultset.h> 0019 0020 #include <common/database/Database.h> 0021 #include <common/database/schema/ResourcesDatabaseSchema.h> 0022 0023 #include <numeric> 0024 0025 namespace KAStats = KActivities::Stats; 0026 0027 ResultSetTest::ResultSetTest(QObject *parent) 0028 : Test(parent) 0029 { 0030 } 0031 0032 namespace 0033 { 0034 QString getBarredUri(QString lhs, const KAStats::ResultSet::Result &result) 0035 { 0036 return lhs + result.resource() + QStringLiteral("|"); 0037 } 0038 0039 QString concatenateResults(const KAStats::ResultSet &results) 0040 { 0041 return std::accumulate(results.cbegin(), results.cend(), QStringLiteral("|"), 0042 getBarredUri); 0043 } 0044 } 0045 0046 void ResultSetTest::testConcat() 0047 { 0048 using namespace KAStats; 0049 using namespace KAStats::Terms; 0050 0051 TEST_CHUNK(QStringLiteral("Checking barred function")) 0052 { 0053 ResultSet::Result r; 0054 r.setResource(QStringLiteral("quack")); 0055 0056 QCOMPARE(getBarredUri(QString(), KAStats::ResultSet::Result{}), QStringLiteral("|")); 0057 QCOMPARE(getBarredUri(QString(), r), QStringLiteral("quack|")); 0058 r.setResource(QStringLiteral("http://www.kde.org")); 0059 QVERIFY(getBarredUri(QString(), r).startsWith(QStringLiteral("http://"))); 0060 QVERIFY(getBarredUri(QString(), r).endsWith(QStringLiteral("org|"))); 0061 } 0062 0063 TEST_CHUNK(QStringLiteral("Checking empty concatenation")) 0064 { 0065 ResultSet rs(KActivities::Stats::Terms::LinkedResources); 0066 // There is no "count" on a resultset 0067 unsigned int rs_count = 0; 0068 for(const auto& r : rs) 0069 { 0070 Q_UNUSED(r); 0071 rs_count++; 0072 } 0073 QCOMPARE(rs_count, 0); // It's empty 0074 QCOMPARE(concatenateResults(rs), QStringLiteral("|")); // 0 results pasted after a "|" to start 0075 } 0076 0077 TEST_CHUNK(QStringLiteral("Checking non-empty concatenation")) 0078 { 0079 ResultSet rs(UsedResources 0080 | HighScoredFirst 0081 | Agent{QStringLiteral("gvim")} 0082 ); 0083 // There is no "count" on a resultset 0084 unsigned int rs_count = 0; 0085 for(const auto& r : rs) 0086 { 0087 Q_UNUSED(r); 0088 rs_count++; 0089 } 0090 QCOMPARE(rs_count, 5); // Not empty (see data inserted into ResourceLink, in initTestCase() 0091 0092 const QString cat = concatenateResults(rs); 0093 QCOMPARE(cat.count(QStringLiteral("|")), 6); // 5 items, plus 1 to start 0094 } 0095 } 0096 0097 0098 void ResultSetTest::testLinkedResources() 0099 { 0100 using namespace KAStats; 0101 using namespace KAStats::Terms; 0102 0103 TEST_CHUNK(QStringLiteral("Getting the linked resources alphabetically")) 0104 { 0105 ResultSet result(LinkedResources 0106 | Agent { QStringLiteral("gvim") } 0107 | Activity { QStringLiteral("activity1") } 0108 ); 0109 QCOMPARE(result.at(0).resource(), QStringLiteral("/path/mid1_a1")); 0110 QCOMPARE(result.at(1).resource(), QStringLiteral("/path/mid2_a1")); 0111 } 0112 } 0113 0114 void ResultSetTest::testUsedResources() 0115 { 0116 using namespace KAStats; 0117 using namespace KAStats::Terms; 0118 0119 qDebug() << "Agent: " << QCoreApplication::instance()->applicationName(); 0120 0121 TEST_CHUNK(QStringLiteral("Getting the used resources by the highest score, default query")) 0122 { 0123 ResultSet result(UsedResources); 0124 0125 qDebug() << "-----------------------------"; 0126 for (const auto &item : result) { 0127 qDebug() << "Item: " << item.resource(); 0128 } 0129 qDebug() << "-----------------------------"; 0130 0131 QCOMPARE(result.at(0).resource(), QStringLiteral("/path/high5_act1_kast")); 0132 QCOMPARE(result.at(1).resource(), QStringLiteral("/path/high7_act1_kast")); 0133 QCOMPARE(result.at(2).resource(), QStringLiteral("/path/high8_act1_kast")); 0134 0135 // END! 0136 QCOMPARE(result.at(3).resource(), QString()); 0137 0138 // Testing whether range works 0139 QCOMPARE(QStringLiteral("|/path/high5_act1_kast|/path/high7_act1_kast|/path/high8_act1_kast|"), // 0140 concatenateResults(result)); 0141 } 0142 0143 TEST_CHUNK(QStringLiteral("Getting the used resources by the highest score, gvim")) 0144 { 0145 /* clang-format off */ 0146 ResultSet result(UsedResources 0147 | HighScoredFirst 0148 | Agent{QStringLiteral("gvim")} 0149 ); 0150 /* clang-format on */ 0151 0152 QCOMPARE(result.at(0).resource(), QStringLiteral("/path/high1_act1_gvim")); 0153 QCOMPARE(result.at(1).resource(), QStringLiteral("/path/high4_act1_gvim")); 0154 } 0155 0156 TEST_CHUNK(QStringLiteral("Getting the used resources by the highest score, global agent")) 0157 { 0158 /* clang-format off */ 0159 ResultSet result(UsedResources 0160 | HighScoredFirst 0161 | Agent::global() 0162 ); 0163 /* clang-format on */ 0164 0165 QCOMPARE(result.at(0).resource(), QStringLiteral("/path/mid6_act1_glob")); 0166 QCOMPARE(result.at(1).resource(), QStringLiteral("/path/mid7_act1_glob")); 0167 QCOMPARE(result.at(2).resource(), QStringLiteral("/path/mid8_act1_glob")); 0168 } 0169 0170 TEST_CHUNK(QStringLiteral("Getting the used resources by the highest score, any agent")) 0171 { 0172 /* clang-format off */ 0173 ResultSet result(UsedResources 0174 | HighScoredFirst 0175 | Agent::any() 0176 | Activity::any() 0177 ); 0178 /* clang-format on */ 0179 0180 QCOMPARE(result.at(0).resource(), QStringLiteral("/path/high1_act1_gvim")); 0181 QCOMPARE(result.at(1).resource(), QStringLiteral("/path/high2_act2_kate")); 0182 QCOMPARE(result.at(2).resource(), QStringLiteral("/path/high3_act1_kate")); 0183 } 0184 0185 TEST_CHUNK(QStringLiteral("Getting the used resources filter by Date")) 0186 { 0187 /* clang-format off */ 0188 ResultSet result(UsedResources 0189 | HighScoredFirst 0190 | Agent::any() 0191 | Activity::any() 0192 | Date::fromString(QStringLiteral("2015-01-15")) 0193 ); 0194 /* clang-format on */ 0195 0196 QCOMPARE(result.at(0).resource(), QStringLiteral("/path/high1_act1_gvim")); 0197 } 0198 0199 TEST_CHUNK(QStringLiteral("Getting the used resources filter by Date range")) 0200 { 0201 /* clang-format off */ 0202 ResultSet result(UsedResources 0203 | HighScoredFirst 0204 | Agent::any() 0205 | Activity::any() 0206 | Date::fromString(QStringLiteral("2015-01-14,2015-01-15")) 0207 ); 0208 /* clang-format on */ 0209 0210 QCOMPARE(result.at(0).resource(), QStringLiteral("/path/high1_act1_gvim")); 0211 QCOMPARE(result.at(1).resource(), QStringLiteral("/path/high2_act2_kate")); 0212 } 0213 } 0214 0215 void ResultSetTest::initTestCase() 0216 { 0217 QTemporaryDir dir(QDir::tempPath() + QStringLiteral("/KActivitiesStatsTest_ResultSetTest_XXXXXX")); 0218 dir.setAutoRemove(false); 0219 0220 if (!dir.isValid()) { 0221 qFatal("Can not create a temporary directory"); 0222 } 0223 0224 const QString databaseFile = dir.path() + QStringLiteral("/database"); 0225 0226 Common::ResourcesDatabaseSchema::overridePath(databaseFile); 0227 qDebug() << "Creating database in " << databaseFile; 0228 0229 // Creating the database, and pushing some dummy data into it 0230 auto database = Common::Database::instance(Common::Database::ResourcesDatabase, Common::Database::ReadWrite); 0231 0232 Common::ResourcesDatabaseSchema::initSchema(*database); 0233 0234 database->execQuery(QStringLiteral( 0235 "INSERT INTO ResourceScoreCache (usedActivity, initiatingAgent, targettedResource, scoreType, cachedScore, firstUpdate, lastUpdate) VALUES " 0236 0237 " ('activity1' , 'gvim' , '/path/high1_act1_gvim' , '0' , '800' , '-1' , '1421446599')" 0238 " , ('activity2' , 'kate' , '/path/high2_act2_kate' , '0' , '700' , '-1' , '1421439442')" 0239 " , ('activity1' , 'kate' , '/path/high3_act1_kate' , '0' , '600' , '-1' , '1421439442')" 0240 " , ('activity1' , 'gvim' , '/path/high4_act1_gvim' , '0' , '500' , '-1' , '1421446488')" 0241 " , ('activity1' , 'KActivitiesStatsTest' , '/path/high5_act1_kast' , '0' , '400' , '-1' , '1421446599')" 0242 " , ('activity2' , 'KActivitiesStatsTest' , '/path/high6_act2_kast' , '0' , '300' , '-1' , '1421439442')" 0243 " , ('activity1' , 'KActivitiesStatsTest' , '/path/high7_act1_kast' , '0' , '200' , '-1' , '1421439442')" 0244 " , ('activity1' , 'KActivitiesStatsTest' , '/path/high8_act1_kast' , '0' , '100' , '-1' , '1421446488')" 0245 0246 " , ('activity1' , 'gvim' , '/path/mid1_act1_gvim' , '0' , '17' , '-1' , '1421433419')" 0247 " , ('activity1' , 'gvim' , '/path/mid2_act1_gvim' , '0' , '54' , '-1' , '1421431630')" 0248 " , ('activity2' , 'gvim' , '/path/mid3_act2_gvim' , '0' , '8' , '-1' , '1421433172')" 0249 " , ('activity2' , 'gvim' , '/path/mid4_act2_gvim' , '0' , '8' , '-1' , '1421432545')" 0250 " , ('activity2' , 'gvim' , '/path/mid5_act2_gvim' , '0' , '79' , '-1' , '1421439118')" 0251 " , ('activity1' , ':global' , '/path/mid6_act1_glob' , '0' , '20' , '-1' , '1421439331')" 0252 " , ('activity1' , ':global' , '/path/mid7_act1_glob' , '0' , '8' , '-1' , '0')" 0253 " , ('activity1' , ':global' , '/path/mid8_act1_glob' , '0' , '7' , '-1' , '1421432617')" 0254 0255 " , ('activity1' , 'gvim' , '/path/low3_act1_gvim' , '0' , '6' , '-1' , '1421434704')" 0256 " , ('activity1' , 'kate' , '/path/low2_act1_kate' , '0' , '3' , '-1' , '1421433266')" 0257 " , ('activity1' , 'kate' , '/path/low1_act1_kate' , '0' , '2' , '-1' , '1421433254')")); 0258 0259 database->execQuery( 0260 QStringLiteral("INSERT INTO ResourceEvent (usedActivity, initiatingAgent, targettedResource, start, end ) VALUES" 0261 // 15 january 2015 0262 " ('activity1' , 'gvim' , '/path/high1_act1_gvim' , '1421345799', '1421345799')" 0263 // 14 january 2015 0264 " , ('activity2' , 'kate' , '/path/high2_act2_kate' , '1421259377', '1421259377')")); 0265 0266 database->execQuery( 0267 QStringLiteral("INSERT INTO ResourceInfo (targettedResource, title, mimetype, autoTitle, autoMimetype) VALUES" 0268 "('/path/high1_act1_gvim', 'high1_act1_gvim', 'text/plain', 1, 1 ) ," 0269 "('/path/high2_act2_kate', 'high2_act2_kate', 'text/plain', 1, 1 )")); 0270 0271 // Renaming the activity1 to the current acitivty 0272 KActivities::Consumer kamd; 0273 0274 while (kamd.serviceStatus() == KActivities::Consumer::Unknown) { 0275 QCoreApplication::processEvents(); 0276 } 0277 0278 database->execQuery(QStringLiteral("UPDATE ResourceScoreCache SET usedActivity = '") + kamd.currentActivity() 0279 + QStringLiteral("' WHERE usedActivity = 'activity1'")); 0280 0281 database->execQuery(QStringLiteral("UPDATE ResourceEvent SET usedActivity = '") + kamd.currentActivity() 0282 + QStringLiteral("' WHERE usedActivity = 'activity1'")); 0283 0284 database->execQuery( 0285 QStringLiteral("INSERT INTO ResourceLink (usedActivity, initiatingAgent, targettedResource) VALUES " 0286 0287 "('activity1' , 'gvim' , '/path/mid1_a1')" 0288 ", ('activity1' , 'gvim' , '/path/mid2_a1')" 0289 ", ('activity2' , 'gvim' , '/path/mid3_a2')" 0290 ", ('activity2' , 'gvim' , '/path/mid4_a2')" 0291 ", ('activity2' , 'gvim' , '/path/link5_a2')" 0292 ", ('activity1' , 'kate' , '/path/link6_a1')" 0293 ", ('activity1' , 'kate' , '/path/link7_a1')" 0294 ", ('activity1' , 'kate' , '/path/link8_a1')") 0295 0296 ); 0297 } 0298 0299 void ResultSetTest::cleanupTestCase() 0300 { 0301 Q_EMIT testFinished(); 0302 } 0303 0304 #include "moc_ResultSetTest.cpp"