diff options
-rw-r--r-- | common/datastorequery.cpp | 27 | ||||
-rw-r--r-- | tests/querytest.cpp | 109 |
2 files changed, 126 insertions, 10 deletions
diff --git a/common/datastorequery.cpp b/common/datastorequery.cpp index 3ba8f40..34d2bae 100644 --- a/common/datastorequery.cpp +++ b/common/datastorequery.cpp | |||
@@ -288,16 +288,23 @@ public: | |||
288 | 288 | ||
289 | //TODO if old and new are the same a modification would be enough | 289 | //TODO if old and new are the same a modification would be enough |
290 | auto oldSelectionResult = mSelectedValues.take(reductionValueBa); | 290 | auto oldSelectionResult = mSelectedValues.take(reductionValueBa); |
291 | //remove old result | 291 | if (oldSelectionResult == selectionResult) { |
292 | readEntity(oldSelectionResult, [&, this](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) { | 292 | mSelectedValues.insert(reductionValueBa, selectionResult); |
293 | callback({entity, Sink::Operation_Removal}); | 293 | readEntity(selectionResult, [&, this](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) { |
294 | }); | 294 | callback({entity, Sink::Operation_Modification, aggregateValues}); |
295 | 295 | }); | |
296 | //add new result | 296 | } else { |
297 | mSelectedValues.insert(reductionValueBa, selectionResult); | 297 | //remove old result |
298 | readEntity(selectionResult, [&, this](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) { | 298 | readEntity(oldSelectionResult, [&, this](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) { |
299 | callback({entity, Sink::Operation_Creation, aggregateValues}); | 299 | callback({entity, Sink::Operation_Removal}); |
300 | }); | 300 | }); |
301 | |||
302 | //add new result | ||
303 | mSelectedValues.insert(reductionValueBa, selectionResult); | ||
304 | readEntity(selectionResult, [&, this](const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Operation) { | ||
305 | callback({entity, Sink::Operation_Creation, aggregateValues}); | ||
306 | }); | ||
307 | } | ||
301 | } | 308 | } |
302 | } | 309 | } |
303 | return false; | 310 | return false; |
diff --git a/tests/querytest.cpp b/tests/querytest.cpp index ac55d0b..bd3b927 100644 --- a/tests/querytest.cpp +++ b/tests/querytest.cpp | |||
@@ -933,6 +933,115 @@ private slots: | |||
933 | QCOMPARE(layoutChangedSpy.size(), 0); | 933 | QCOMPARE(layoutChangedSpy.size(), 0); |
934 | QCOMPARE(resetSpy.size(), 0); | 934 | QCOMPARE(resetSpy.size(), 0); |
935 | } | 935 | } |
936 | |||
937 | void testLivequeryThreadleaderChange() | ||
938 | { | ||
939 | // Setup | ||
940 | auto folder1 = Folder::createEntity<Folder>("sink.dummy.instance1"); | ||
941 | VERIFYEXEC(Sink::Store::create<Folder>(folder1)); | ||
942 | |||
943 | auto folder2 = Folder::createEntity<Folder>("sink.dummy.instance1"); | ||
944 | VERIFYEXEC(Sink::Store::create<Folder>(folder2)); | ||
945 | |||
946 | QDateTime earlier{QDate{2017, 2, 3}, QTime{9, 0, 0}}; | ||
947 | QDateTime now{QDate{2017, 2, 3}, QTime{10, 0, 0}}; | ||
948 | QDateTime later{QDate{2017, 2, 3}, QTime{11, 0, 0}}; | ||
949 | |||
950 | auto mail1 = Mail::createEntity<Mail>("sink.dummy.instance1"); | ||
951 | mail1.setExtractedMessageId("mail1"); | ||
952 | mail1.setFolder(folder1); | ||
953 | mail1.setExtractedDate(now); | ||
954 | VERIFYEXEC(Sink::Store::create(mail1)); | ||
955 | |||
956 | // Ensure all local data is processed | ||
957 | VERIFYEXEC(Sink::ResourceControl::flushMessageQueue("sink.dummy.instance1")); | ||
958 | |||
959 | Query query; | ||
960 | query.setId("testLivequeryThreadleaderChange"); | ||
961 | query.setFlags(Query::LiveQuery); | ||
962 | query.reduce<Mail::Folder>(Query::Reduce::Selector::max<Mail::Date>()).count("count").collect<Mail::Folder>("folders"); | ||
963 | query.sort<Mail::Date>(); | ||
964 | query.request<Mail::MessageId>(); | ||
965 | |||
966 | auto model = Sink::Store::loadModel<Mail>(query); | ||
967 | QTRY_VERIFY(model->data(QModelIndex(), Sink::Store::ChildrenFetchedRole).toBool()); | ||
968 | QCOMPARE(model->rowCount(), 1); | ||
969 | |||
970 | QSignalSpy insertedSpy(model.data(), &QAbstractItemModel::rowsInserted); | ||
971 | QSignalSpy removedSpy(model.data(), &QAbstractItemModel::rowsRemoved); | ||
972 | QSignalSpy changedSpy(model.data(), &QAbstractItemModel::dataChanged); | ||
973 | QSignalSpy layoutChangedSpy(model.data(), &QAbstractItemModel::layoutChanged); | ||
974 | QSignalSpy resetSpy(model.data(), &QAbstractItemModel::modelReset); | ||
975 | |||
976 | //The leader shouldn't change to mail2 after the modification | ||
977 | { | ||
978 | auto mail2 = Mail::createEntity<Mail>("sink.dummy.instance1"); | ||
979 | mail2.setExtractedMessageId("mail2"); | ||
980 | mail2.setFolder(folder1); | ||
981 | mail2.setExtractedDate(earlier); | ||
982 | VERIFYEXEC(Sink::Store::create(mail2)); | ||
983 | } | ||
984 | |||
985 | VERIFYEXEC(Sink::ResourceControl::flushMessageQueue("sink.dummy.instance1")); | ||
986 | QTRY_COMPARE(model->rowCount(), 1); | ||
987 | { | ||
988 | auto mail = model->data(model->index(0, 0, QModelIndex{}), Sink::Store::DomainObjectRole).value<Mail::Ptr>(); | ||
989 | QTRY_COMPARE(mail->getMessageId(), QByteArray{"mail1"}); | ||
990 | QTRY_COMPARE(mail->getProperty("count").toInt(), 2); | ||
991 | QCOMPARE(mail->getProperty("folders").toList().size(), 2); | ||
992 | } | ||
993 | |||
994 | |||
995 | QCOMPARE(insertedSpy.size(), 0); | ||
996 | QCOMPARE(removedSpy.size(), 0); | ||
997 | QCOMPARE(changedSpy.size(), 1); | ||
998 | QCOMPARE(layoutChangedSpy.size(), 0); | ||
999 | QCOMPARE(resetSpy.size(), 0); | ||
1000 | |||
1001 | //The leader should change to mail3 after the modification | ||
1002 | { | ||
1003 | auto mail3 = Mail::createEntity<Mail>("sink.dummy.instance1"); | ||
1004 | mail3.setExtractedMessageId("mail3"); | ||
1005 | mail3.setFolder(folder1); | ||
1006 | mail3.setExtractedDate(later); | ||
1007 | VERIFYEXEC(Sink::Store::create(mail3)); | ||
1008 | } | ||
1009 | |||
1010 | VERIFYEXEC(Sink::ResourceControl::flushMessageQueue("sink.dummy.instance1")); | ||
1011 | QTRY_COMPARE(model->rowCount(), 1); | ||
1012 | { | ||
1013 | auto mail = model->data(model->index(0, 0, QModelIndex{}), Sink::Store::DomainObjectRole).value<Mail::Ptr>(); | ||
1014 | QTRY_COMPARE(mail->getMessageId(), QByteArray{"mail3"}); | ||
1015 | QCOMPARE(mail->getProperty("count").toInt(), 3); | ||
1016 | QCOMPARE(mail->getProperty("folders").toList().size(), 3); | ||
1017 | } | ||
1018 | |||
1019 | //This should eventually be just one modification instead of remove + add (See datastorequery reduce component) | ||
1020 | QCOMPARE(insertedSpy.size(), 1); | ||
1021 | QCOMPARE(removedSpy.size(), 1); | ||
1022 | QCOMPARE(changedSpy.size(), 1); | ||
1023 | QCOMPARE(layoutChangedSpy.size(), 0); | ||
1024 | QCOMPARE(resetSpy.size(), 0); | ||
1025 | |||
1026 | //Nothing should change on third mail in separate folder | ||
1027 | { | ||
1028 | auto mail = Mail::createEntity<Mail>("sink.dummy.instance1"); | ||
1029 | mail.setExtractedMessageId("mail4"); | ||
1030 | mail.setFolder(folder2); | ||
1031 | mail.setExtractedDate(now); | ||
1032 | VERIFYEXEC(Sink::Store::create(mail)); | ||
1033 | } | ||
1034 | |||
1035 | VERIFYEXEC(Sink::ResourceControl::flushMessageQueue("sink.dummy.instance1")); | ||
1036 | QTRY_COMPARE(model->rowCount(), 2); | ||
1037 | |||
1038 | //This should eventually be just one modification instead of remove + add (See datastorequery reduce component) | ||
1039 | QCOMPARE(insertedSpy.size(), 2); | ||
1040 | QCOMPARE(removedSpy.size(), 1); | ||
1041 | QCOMPARE(changedSpy.size(), 1); | ||
1042 | QCOMPARE(layoutChangedSpy.size(), 0); | ||
1043 | QCOMPARE(resetSpy.size(), 0); | ||
1044 | } | ||
936 | }; | 1045 | }; |
937 | 1046 | ||
938 | QTEST_MAIN(QueryTest) | 1047 | QTEST_MAIN(QueryTest) |