File indexing completed on 2024-11-10 04:40:15
0001 /* 0002 SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "qtest_akonadi.h" 0008 0009 #include "searchquery.h" 0010 #include <qjson/parser.h> 0011 #include <qjson/serializer.h> 0012 0013 using namespace Akonadi; 0014 0015 class SearchQueryTest : public QObject 0016 { 0017 Q_OBJECT 0018 private: 0019 void verifySimpleTerm(const QVariantMap &json, const SearchTerm &term, bool *ok) 0020 { 0021 *ok = false; 0022 QCOMPARE(term.subTerms().count(), 0); 0023 QVERIFY(json.contains(QLatin1StringView("key"))); 0024 QCOMPARE(json[QStringLiteral("key")].toString(), term.key()); 0025 QVERIFY(json.contains(QLatin1StringView("value"))); 0026 QCOMPARE(json[QStringLiteral("value")], term.value()); 0027 QVERIFY(json.contains(QLatin1StringView("cond"))); 0028 QCOMPARE(static_cast<SearchTerm::Condition>(json[QStringLiteral("cond")].toInt()), term.condition()); 0029 QVERIFY(json.contains(QLatin1StringView("negated"))); 0030 QCOMPARE(json[QStringLiteral("negated")].toBool(), term.isNegated()); 0031 *ok = true; 0032 } 0033 0034 private Q_SLOTS: 0035 void testSerializer() 0036 { 0037 QJson::Parser parser; 0038 bool ok = false; 0039 0040 { 0041 SearchQuery query; 0042 query.addTerm(QStringLiteral("body"), QStringLiteral("test string"), SearchTerm::CondContains); 0043 0044 ok = false; 0045 QVariantMap map = parser.parse(query.toJSON(), &ok).toMap(); 0046 QVERIFY(ok); 0047 0048 QCOMPARE(static_cast<SearchTerm::Relation>(map[QStringLiteral("rel")].toInt()), SearchTerm::RelAnd); 0049 const QVariantList subTerms = map[QStringLiteral("subTerms")].toList(); 0050 QCOMPARE(subTerms.size(), 1); 0051 0052 ok = false; 0053 verifySimpleTerm(subTerms.first().toMap(), query.term().subTerms()[0], &ok); 0054 QVERIFY(ok); 0055 } 0056 0057 { 0058 SearchQuery query(SearchTerm::RelOr); 0059 query.addTerm(SearchTerm(QStringLiteral("to"), QStringLiteral("test@test.user"), SearchTerm::CondEqual)); 0060 SearchTerm term2(QStringLiteral("subject"), QStringLiteral("Hello"), SearchTerm::CondContains); 0061 term2.setIsNegated(true); 0062 query.addTerm(term2); 0063 0064 ok = false; 0065 QVariantMap map = parser.parse(query.toJSON(), &ok).toMap(); 0066 QVERIFY(ok); 0067 0068 QCOMPARE(static_cast<SearchTerm::Relation>(map[QStringLiteral("rel")].toInt()), query.term().relation()); 0069 const QVariantList subTerms = map[QStringLiteral("subTerms")].toList(); 0070 QCOMPARE(subTerms.size(), query.term().subTerms().count()); 0071 0072 for (int i = 0; i < subTerms.size(); ++i) { 0073 ok = false; 0074 verifySimpleTerm(subTerms[i].toMap(), query.term().subTerms()[i], &ok); 0075 QVERIFY(ok); 0076 } 0077 } 0078 } 0079 0080 void testParser() 0081 { 0082 QJson::Serializer serializer; 0083 bool ok = false; 0084 0085 { 0086 QVariantList subTerms; 0087 QVariantMap termJSON; 0088 termJSON[QStringLiteral("key")] = QStringLiteral("created"); 0089 termJSON[QStringLiteral("value")] = QDateTime(QDate(2014, 01, 24), QTime(17, 49, 00)); 0090 termJSON[QStringLiteral("cond")] = static_cast<int>(SearchTerm::CondGreaterOrEqual); 0091 termJSON[QStringLiteral("negated")] = true; 0092 subTerms << termJSON; 0093 0094 termJSON[QStringLiteral("key")] = QStringLiteral("subject"); 0095 termJSON[QStringLiteral("value")] = QStringLiteral("Hello"); 0096 termJSON[QStringLiteral("cond")] = static_cast<int>(SearchTerm::CondEqual); 0097 termJSON[QStringLiteral("negated")] = false; 0098 subTerms << termJSON; 0099 0100 QVariantMap map; 0101 map[QStringLiteral("rel")] = static_cast<int>(SearchTerm::RelAnd); 0102 map[QStringLiteral("subTerms")] = subTerms; 0103 0104 #if !defined(USE_QJSON_0_8) 0105 const QByteArray json = serializer.serialize(map); 0106 QVERIFY(!json.isNull()); 0107 #else 0108 ok = false; 0109 const QByteArray json = serializer.serialize(map, &ok); 0110 QVERIFY(ok); 0111 #endif 0112 0113 const SearchQuery query = SearchQuery::fromJSON(json); 0114 QVERIFY(!query.isNull()); 0115 const SearchTerm term = query.term(); 0116 0117 QCOMPARE(static_cast<SearchTerm::Relation>(map[QStringLiteral("rel")].toInt()), term.relation()); 0118 QCOMPARE(subTerms.count(), term.subTerms().count()); 0119 0120 for (int i = 0; i < subTerms.count(); ++i) { 0121 ok = false; 0122 verifySimpleTerm(subTerms.at(i).toMap(), term.subTerms()[i], &ok); 0123 QVERIFY(ok); 0124 } 0125 } 0126 } 0127 0128 void testFullQuery() 0129 { 0130 { 0131 SearchQuery query; 0132 query.addTerm("key", "value"); 0133 const QByteArray serialized = query.toJSON(); 0134 QCOMPARE(SearchQuery::fromJSON(serialized), query); 0135 } 0136 { 0137 SearchQuery query; 0138 query.setLimit(10); 0139 query.addTerm("key", "value"); 0140 const QByteArray serialized = query.toJSON(); 0141 QCOMPARE(SearchQuery::fromJSON(serialized), query); 0142 } 0143 } 0144 }; 0145 0146 QTEST_AKONADIMAIN(SearchQueryTest, NoGUI) 0147 0148 #include "searchquerytest.moc"