1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/*
This file is part of the KDE Baloo project.
SPDX-FileCopyrightText: 2019 Stefan BrĂ¼ns <stefan.bruens@rwth-aachen.de>
SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "propertydata.h"
#include <QTest>
// #include <QDebug>
// #include <QJsonDocument>
using namespace Baloo;
class PropertySerializationTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testEmpty();
void testSingleProp();
void testMultiProp();
void testMultiValue();
void testLists();
};
// Test empty property map
void PropertySerializationTest::testEmpty()
{
KFileMetaData::PropertyMultiMap properties;
auto json = propertyMapToJson(properties);
// QJsonDocument jdoc(json);
// qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact);
auto res = jsonToPropertyMap(json);
QCOMPARE(res, properties);
}
// Test serialization/deserialization a single property
void PropertySerializationTest::testSingleProp()
{
namespace KFMProp = KFileMetaData::Property;
KFileMetaData::PropertyMultiMap properties;
properties.insert(KFMProp::Subject, QStringLiteral("subject"));
auto json = propertyMapToJson(properties);
// QJsonDocument jdoc(json);
// qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact);
auto res = jsonToPropertyMap(json);
QCOMPARE(res, properties);
}
// Test serialization/deserialization for multiple properties
void PropertySerializationTest::testMultiProp()
{
namespace KFMProp = KFileMetaData::Property;
KFileMetaData::PropertyMultiMap properties;
properties.insert(KFMProp::Subject, QStringLiteral("subject"));
properties.insert(KFMProp::ReleaseYear, 2019);
properties.insert(KFMProp::PhotoExposureTime, 0.12345);
auto json = propertyMapToJson(properties);
// QJsonDocument jdoc(json);
// qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact);
auto res = jsonToPropertyMap(json);
for (auto prop : { KFMProp::Subject, KFMProp::ReleaseYear, KFMProp::PhotoExposureTime }) {
QCOMPARE(res.value(prop), properties.value(prop));
}
QCOMPARE(res, properties);
}
// Test serialization/deserialization for multiple value per property
void PropertySerializationTest::testMultiValue()
{
namespace KFMProp = KFileMetaData::Property;
KFileMetaData::PropertyMultiMap properties;
properties.insert(KFMProp::Genre, QStringLiteral("genre1"));
properties.insert(KFMProp::Genre, QStringLiteral("genre2"));
properties.insert(KFMProp::Ensemble, QStringLiteral("ensemble1"));
properties.insert(KFMProp::Ensemble, QStringLiteral("ensemble2"));
properties.insert(KFMProp::Rating, 4);
properties.insert(KFMProp::Rating, 5);
auto json = propertyMapToJson(properties);
// QJsonDocument jdoc(json);
// qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact);
auto res = jsonToPropertyMap(json);
for (auto prop : { KFMProp::Genre, KFMProp::Ensemble, KFMProp::Rating}) {
QCOMPARE(res.value(prop), properties.value(prop));
QCOMPARE(res.values(prop), properties.values(prop));
// qDebug() << res[prop];
// qDebug() << res.values(prop);
// qDebug() << QVariant(res.values(prop)).toStringList().join(", ");
}
QCOMPARE(res, properties);
}
// Test serialization/deserialization of lists
void PropertySerializationTest::testLists()
{
namespace KFMProp = KFileMetaData::Property;
KFileMetaData::PropertyMultiMap properties;
properties.insert(KFMProp::Genre, QStringList({QStringLiteral("genre1"), QStringLiteral("genre2")}));
properties.insert(KFMProp::Ensemble, QVariantList({QStringLiteral("ensemble1"), QStringLiteral("ensemble2")}));
properties.insert(KFMProp::Rating, QVariantList({QVariant(10), QVariant(7.7)}));
auto json = propertyMapToJson(properties);
// QJsonDocument jdoc(json);
// qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact);
auto res = jsonToPropertyMap(json);
for (auto prop : { KFMProp::Genre, KFMProp::Ensemble, KFMProp::Rating}) {
// qDebug() << res.values(prop);
// qDebug() << QVariant(res.values(prop)).toStringList().join(", ");
// qDebug() << properties[prop].toStringList().join(", ");
QCOMPARE(QVariant(res.values(prop)).toStringList(), properties.value(prop).toStringList());
}
}
QTEST_MAIN(PropertySerializationTest)
#include "propertyserializationtest.moc"
|