File indexing completed on 2024-04-28 03:51:41
0001 /* 0002 This file is part of the KDE Baloo project. 0003 SPDX-FileCopyrightText: 2019 Stefan BrĂ¼ns <stefan.bruens@rwth-aachen.de> 0004 0005 SPDX-License-Identifier: LGPL-2.1-or-later 0006 */ 0007 0008 #include "propertydata.h" 0009 0010 #include <QTest> 0011 0012 // #include <QDebug> 0013 // #include <QJsonDocument> 0014 0015 using namespace Baloo; 0016 0017 class PropertySerializationTest : public QObject 0018 { 0019 Q_OBJECT 0020 0021 private Q_SLOTS: 0022 void testEmpty(); 0023 void testSingleProp(); 0024 void testMultiProp(); 0025 void testMultiValue(); 0026 void testLists(); 0027 }; 0028 0029 // Test empty property map 0030 void PropertySerializationTest::testEmpty() 0031 { 0032 KFileMetaData::PropertyMultiMap properties; 0033 0034 auto json = propertyMapToJson(properties); 0035 // QJsonDocument jdoc(json); 0036 // qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact); 0037 0038 auto res = jsonToPropertyMap(json); 0039 QCOMPARE(res, properties); 0040 } 0041 0042 // Test serialization/deserialization a single property 0043 void PropertySerializationTest::testSingleProp() 0044 { 0045 namespace KFMProp = KFileMetaData::Property; 0046 0047 KFileMetaData::PropertyMultiMap properties; 0048 properties.insert(KFMProp::Subject, QStringLiteral("subject")); 0049 0050 auto json = propertyMapToJson(properties); 0051 // QJsonDocument jdoc(json); 0052 // qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact); 0053 0054 auto res = jsonToPropertyMap(json); 0055 QCOMPARE(res, properties); 0056 } 0057 0058 // Test serialization/deserialization for multiple properties 0059 void PropertySerializationTest::testMultiProp() 0060 { 0061 namespace KFMProp = KFileMetaData::Property; 0062 0063 KFileMetaData::PropertyMultiMap properties; 0064 properties.insert(KFMProp::Subject, QStringLiteral("subject")); 0065 properties.insert(KFMProp::ReleaseYear, 2019); 0066 properties.insert(KFMProp::PhotoExposureTime, 0.12345); 0067 0068 auto json = propertyMapToJson(properties); 0069 // QJsonDocument jdoc(json); 0070 // qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact); 0071 0072 auto res = jsonToPropertyMap(json); 0073 0074 for (auto prop : { KFMProp::Subject, KFMProp::ReleaseYear, KFMProp::PhotoExposureTime }) { 0075 QCOMPARE(res.value(prop), properties.value(prop)); 0076 } 0077 QCOMPARE(res, properties); 0078 } 0079 0080 // Test serialization/deserialization for multiple value per property 0081 void PropertySerializationTest::testMultiValue() 0082 { 0083 namespace KFMProp = KFileMetaData::Property; 0084 0085 KFileMetaData::PropertyMultiMap properties; 0086 properties.insert(KFMProp::Genre, QStringLiteral("genre1")); 0087 properties.insert(KFMProp::Genre, QStringLiteral("genre2")); 0088 properties.insert(KFMProp::Ensemble, QStringLiteral("ensemble1")); 0089 properties.insert(KFMProp::Ensemble, QStringLiteral("ensemble2")); 0090 properties.insert(KFMProp::Rating, 4); 0091 properties.insert(KFMProp::Rating, 5); 0092 0093 auto json = propertyMapToJson(properties); 0094 // QJsonDocument jdoc(json); 0095 // qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact); 0096 0097 auto res = jsonToPropertyMap(json); 0098 0099 for (auto prop : { KFMProp::Genre, KFMProp::Ensemble, KFMProp::Rating}) { 0100 QCOMPARE(res.value(prop), properties.value(prop)); 0101 QCOMPARE(res.values(prop), properties.values(prop)); 0102 // qDebug() << res[prop]; 0103 // qDebug() << res.values(prop); 0104 // qDebug() << QVariant(res.values(prop)).toStringList().join(", "); 0105 } 0106 QCOMPARE(res, properties); 0107 } 0108 0109 // Test serialization/deserialization of lists 0110 void PropertySerializationTest::testLists() 0111 { 0112 namespace KFMProp = KFileMetaData::Property; 0113 0114 KFileMetaData::PropertyMultiMap properties; 0115 properties.insert(KFMProp::Genre, QStringList({QStringLiteral("genre1"), QStringLiteral("genre2")})); 0116 properties.insert(KFMProp::Ensemble, QVariantList({QStringLiteral("ensemble1"), QStringLiteral("ensemble2")})); 0117 properties.insert(KFMProp::Rating, QVariantList({QVariant(10), QVariant(7.7)})); 0118 0119 auto json = propertyMapToJson(properties); 0120 // QJsonDocument jdoc(json); 0121 // qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact); 0122 0123 auto res = jsonToPropertyMap(json); 0124 0125 for (auto prop : { KFMProp::Genre, KFMProp::Ensemble, KFMProp::Rating}) { 0126 // qDebug() << res.values(prop); 0127 // qDebug() << QVariant(res.values(prop)).toStringList().join(", "); 0128 // qDebug() << properties[prop].toStringList().join(", "); 0129 QCOMPARE(QVariant(res.values(prop)).toStringList(), properties.value(prop).toStringList()); 0130 } 0131 } 0132 0133 QTEST_MAIN(PropertySerializationTest) 0134 0135 #include "propertyserializationtest.moc"