File indexing completed on 2024-05-19 04:01:09

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include <console/model/ratiosetaggregationmodel.h>
0008 #include <console/model/datamodel.h>
0009 #include <console/model/timeaggregationmodel.h>
0010 #include <console/core/sample.h>
0011 #include <console/core/schemaentrytemplates.h>
0012 
0013 #include <QDebug>
0014 #include <QtTest/qtest.h>
0015 #include <QObject>
0016 #include <QStandardPaths>
0017 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0018 #include <QAbstractItemModelTester>
0019 #endif
0020 
0021 using namespace KUserFeedback::Console;
0022 
0023 class RatioSetAggregationModelTest : public QObject
0024 {
0025     Q_OBJECT
0026 private Q_SLOTS:
0027     void initTestCase()
0028     {
0029         Q_INIT_RESOURCE(schematemplates);
0030         QStandardPaths::setTestModeEnabled(true);
0031     }
0032 
0033     void testEmptyModel()
0034     {
0035         RatioSetAggregationModel model;
0036 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0037         QAbstractItemModelTester modelTest(&model);
0038 #endif
0039         model.setAggregationValue(QString());
0040         model.setAggregationValue(QLatin1String("applicationVersion.value"));
0041 
0042         TimeAggregationModel timeModel;
0043         model.setSourceModel(&timeModel);
0044 
0045         DataModel srcModel;
0046         timeModel.setSourceModel(&srcModel);
0047         srcModel.setProduct({});
0048         QCOMPARE(model.rowCount(), 0);
0049         QCOMPARE(model.columnCount(), 1);
0050 
0051         Product p;
0052         for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
0053             p.addTemplate(tpl);
0054         p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
0055         srcModel.setProduct(p);
0056         QCOMPARE(model.columnCount(), 1);
0057         QCOMPARE(model.rowCount(), 0);
0058     }
0059 
0060     void testModelContent()
0061     {
0062         RatioSetAggregationModel model;
0063 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0064         QAbstractItemModelTester modelTest(&model);
0065 #endif
0066         model.setAggregationValue(QLatin1String("newPropertyRatio"));
0067 
0068         TimeAggregationModel timeModel;
0069         model.setSourceModel(&timeModel);
0070 
0071         DataModel srcModel;
0072         timeModel.setSourceModel(&srcModel);
0073         timeModel.setAggregationMode(TimeAggregationModel::AggregateDay);
0074         Product p;
0075         for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
0076             p.addTemplate(tpl);
0077         p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
0078         srcModel.setProduct(p);
0079 
0080         auto samples = Sample::fromJson(R"([
0081             { "timestamp": "2016-11-27 12:00:00", "newPropertyRatio": { "cat1": { "property": 0.4 }, "cat2": { "property": 0.6 } } },
0082             { "timestamp": "2016-11-27 12:00:00", "newPropertyRatio": { "cat1": { "property": 0.3 }, "cat2": { "property": 0.7 } } },
0083             { "timestamp": "2016-11-27 12:00:00", "newPropertyRatio": { "cat1": { "property": 0.2 }, "cat2": { "property": 0.8 } } },
0084             { "timestamp": "2016-11-28 12:00:00", "newPropertyRatio": { "cat1": { "property": 1.0 } } },
0085             { "timestamp": "2016-11-28 12:00:00", "newPropertyRatio": { "cat1": { "property": 0.0 }, "cat2": { "property": 0.0 } } },
0086             { "timestamp": "2016-11-29 12:00:00" }
0087         ])", p);
0088         QCOMPARE(samples.size(), 6);
0089         srcModel.setSamples(samples);
0090 
0091         QCOMPARE(model.columnCount(), 3);
0092         QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("cat1"));
0093         QCOMPARE(model.headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("cat2"));
0094 
0095         QCOMPARE(model.rowCount(), 3);
0096         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-27"));
0097         QCOMPARE(model.index(0, 1).data(Qt::DisplayRole).toDouble(), 0.3);
0098         QCOMPARE(model.index(0, 2).data(Qt::DisplayRole).toDouble(), 0.7);
0099         QCOMPARE(model.index(0, 2).data(TimeAggregationModel::AccumulatedDisplayRole).toDouble(), 1.0);
0100 
0101         QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-28"));
0102         QCOMPARE(model.index(1, 1).data(Qt::DisplayRole).toDouble(), 1.0);
0103         QCOMPARE(model.index(1, 2).data(Qt::DisplayRole).toDouble(), 0.0);
0104         QCOMPARE(model.index(1, 2).data(TimeAggregationModel::AccumulatedDisplayRole).toDouble(), 1.0);
0105 
0106         QCOMPARE(model.index(2, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-29"));
0107         QCOMPARE(model.index(2, 1).data(Qt::DisplayRole).toDouble(), 0.0);
0108         QCOMPARE(model.index(2, 2).data(Qt::DisplayRole).toDouble(), 0.0);
0109 
0110         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::MaximumValueRole).toDouble(), 1.0);
0111     }
0112 };
0113 
0114 QTEST_MAIN(RatioSetAggregationModelTest)
0115 
0116 #include "ratiosetaggregationmodeltest.moc"
0117