File indexing completed on 2024-05-05 03:52:08
0001 /* 0002 This file is part of the KDE Baloo Project 0003 SPDX-FileCopyrightText: 2013-2015 Vishesh Handa <vhanda@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.1-or-later 0006 */ 0007 0008 #include "query.h" 0009 #include "term.h" 0010 0011 #include <QTest> 0012 0013 using namespace Baloo; 0014 0015 class QuerySerializationTest : public QObject 0016 { 0017 Q_OBJECT 0018 0019 private Q_SLOTS: 0020 void testBasic(); 0021 void testTerm(); 0022 void testAndTerm(); 0023 void testDateTerm(); 0024 void testDateTimeTerm(); 0025 0026 void testCustomOptions(); 0027 }; 0028 0029 // Test a simple query with no terms 0030 void QuerySerializationTest::testBasic() 0031 { 0032 Query query; 0033 query.setLimit(5); 0034 query.setOffset(1); 0035 query.setSearchString(QStringLiteral("Bookie")); 0036 query.addType(QStringLiteral("File/Audio")); 0037 0038 QByteArray json = query.toJSON(); 0039 Query q = Query::fromJSON(json); 0040 0041 QCOMPARE(q.limit(), static_cast<uint>(5)); 0042 QCOMPARE(q.offset(), static_cast<uint>(1)); 0043 QCOMPARE(q.searchString(), QLatin1String("Bookie")); 0044 QCOMPARE(q.types().size(), 2); 0045 QVERIFY(q.types().contains(QLatin1String("File"))); 0046 QVERIFY(q.types().contains(QLatin1String("Audio"))); 0047 0048 QCOMPARE(q, query); 0049 } 0050 0051 void QuerySerializationTest::testTerm() 0052 { 0053 Query query; 0054 query.setSearchString(QStringLiteral("prop:value")); 0055 0056 QByteArray json = query.toJSON(); 0057 Query q = Query::fromJSON(json); 0058 0059 QCOMPARE(q, query); 0060 } 0061 0062 void QuerySerializationTest::testAndTerm() 0063 { 0064 Query query; 0065 query.setSearchString(QStringLiteral("prop1:1 AND prop2:2")); 0066 0067 QByteArray json = query.toJSON(); 0068 Query q = Query::fromJSON(json); 0069 0070 QCOMPARE(q, query); 0071 } 0072 0073 void QuerySerializationTest::testDateTerm() 0074 { 0075 Query query; 0076 query.setSearchString(QStringLiteral("prop:2015-05-01")); 0077 0078 QByteArray json = query.toJSON(); 0079 Query q = Query::fromJSON(json); 0080 0081 QCOMPARE(q, query); 0082 } 0083 0084 void QuerySerializationTest::testDateTimeTerm() 0085 { 0086 Query query; 0087 query.setSearchString(QStringLiteral("prop:2015-05-01T23:44:11")); 0088 0089 QByteArray json = query.toJSON(); 0090 Query q = Query::fromJSON(json); 0091 0092 QCOMPARE(q, query); 0093 } 0094 0095 void QuerySerializationTest::testCustomOptions() 0096 { 0097 Query query; 0098 query.addType(QStringLiteral("File")); 0099 query.setIncludeFolder(QStringLiteral("/home/vishesh")); 0100 0101 QByteArray json = query.toJSON(); 0102 Query q = Query::fromJSON(json); 0103 0104 QString includeFolder = q.includeFolder(); 0105 QCOMPARE(includeFolder, QStringLiteral("/home/vishesh")); 0106 0107 QCOMPARE(query, q); 0108 } 0109 0110 QTEST_MAIN(QuerySerializationTest) 0111 0112 #include "queryserializationtest.moc"