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

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include <console/model/numericaggregationmodel.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 NumericAggregationModelTest : 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         NumericAggregationModel model;
0036 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0037         QAbstractItemModelTester modelTest(&model);
0038 #endif
0039         model.setAggregation(AggregationElement());
0040         AggregationElement aggr;
0041         {
0042             SchemaEntry entry;
0043             entry.setDataType(SchemaEntry::Scalar);
0044             entry.setName(QLatin1String("usageTime"));
0045             aggr.setSchemaEntry(entry);
0046             SchemaEntryElement elem;
0047             elem.setName(QLatin1String("value"));
0048             aggr.setSchemaEntryElement(elem);
0049             aggr.setType(AggregationElement::Value);
0050         }
0051         model.setAggregation(aggr);
0052 
0053         TimeAggregationModel timeModel;
0054         model.setSourceModel(&timeModel);
0055 
0056         DataModel srcModel;
0057         timeModel.setSourceModel(&srcModel);
0058         srcModel.setProduct({});
0059         QCOMPARE(model.rowCount(), 0);
0060         QCOMPARE(model.columnCount(), 6);
0061 
0062         Product p;
0063         for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
0064             p.addTemplate(tpl);
0065         p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
0066         srcModel.setProduct(p);
0067         QCOMPARE(model.columnCount(), 6);
0068         QCOMPARE(model.rowCount(), 0);
0069     }
0070 
0071     void testModelContent()
0072     {
0073         NumericAggregationModel model;
0074 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0075         QAbstractItemModelTester modelTest(&model);
0076 #endif
0077         AggregationElement aggr;
0078         {
0079             SchemaEntry entry;
0080             entry.setDataType(SchemaEntry::Scalar);
0081             entry.setName(QLatin1String("usageTime"));
0082             aggr.setSchemaEntry(entry);
0083             SchemaEntryElement elem;
0084             elem.setName(QLatin1String("value"));
0085             aggr.setSchemaEntryElement(elem);
0086             aggr.setType(AggregationElement::Value);
0087         }
0088         model.setAggregation(aggr);
0089 
0090         TimeAggregationModel timeModel;
0091         model.setSourceModel(&timeModel);
0092 
0093         DataModel srcModel;
0094         timeModel.setSourceModel(&srcModel);
0095         timeModel.setAggregationMode(TimeAggregationModel::AggregateDay);
0096         Product p;
0097         for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
0098             p.addTemplate(tpl);
0099         p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
0100         srcModel.setProduct(p);
0101 
0102         auto samples = Sample::fromJson(R"([
0103             { "timestamp": "2016-11-26 12:00:00" },
0104             { "timestamp": "2016-11-27 12:00:00", "usageTime": { "value": "1.0" } },
0105             { "timestamp": "2016-11-27 12:00:00", "usageTime": { "value": "2.0" } },
0106             { "timestamp": "2016-11-27 12:00:00", "usageTime": { "value": "3.0" } },
0107             { "timestamp": "2016-11-27 12:00:00", "usageTime": { "value": "0.0" } },
0108             { "timestamp": "2016-11-27 12:00:00", "usageTime": { "value": "4.0" } },
0109             { "timestamp": "2016-11-27 12:00:00", "usageTime": { "value": "5.0" } },
0110             { "timestamp": "2016-11-28 12:00:00", "usageTime": { "value": "1.0" } },
0111             { "timestamp": "2016-11-28 12:00:00", "usageTime": { "value": "2.0" } },
0112             { "timestamp": "2016-11-28 12:00:00" }
0113         ])", p);
0114         QCOMPARE(samples.size(), 10);
0115         srcModel.setSamples(samples);
0116 
0117         QCOMPARE(model.columnCount(), 6);
0118         QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("Lower Extreme"));
0119         QCOMPARE(model.headerData(3, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("Median"));
0120         QCOMPARE(model.headerData(5, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("Upper Extreme"));
0121 
0122         QCOMPARE(model.rowCount(), 3);
0123         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-26"));
0124         QCOMPARE(model.index(0, 1).data(Qt::DisplayRole).toDouble(), 0.0);
0125         QCOMPARE(model.index(0, 2).data(Qt::DisplayRole).toDouble(), 0.0);
0126         QCOMPARE(model.index(0, 3).data(Qt::DisplayRole).toDouble(), 0.0);
0127         QCOMPARE(model.index(0, 4).data(Qt::DisplayRole).toDouble(), 0.0);
0128         QCOMPARE(model.index(0, 5).data(Qt::DisplayRole).toDouble(), 0.0);
0129 
0130         QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-27"));
0131         QCOMPARE(model.index(1, 1).data(Qt::DisplayRole).toDouble(), 0.0);
0132         QCOMPARE(model.index(1, 2).data(Qt::DisplayRole).toDouble(), 1.0);
0133         QCOMPARE(model.index(1, 3).data(Qt::DisplayRole).toDouble(), 3.0);
0134         QCOMPARE(model.index(1, 4).data(Qt::DisplayRole).toDouble(), 4.0);
0135         QCOMPARE(model.index(1, 5).data(Qt::DisplayRole).toDouble(), 5.0);
0136 
0137         QCOMPARE(model.index(2, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-28"));
0138         QCOMPARE(model.index(2, 1).data(Qt::DisplayRole).toDouble(), 0.0);
0139         QCOMPARE(model.index(2, 2).data(Qt::DisplayRole).toDouble(), 0.0);
0140         QCOMPARE(model.index(2, 3).data(Qt::DisplayRole).toDouble(), 1.0);
0141         QCOMPARE(model.index(2, 4).data(Qt::DisplayRole).toDouble(), 2.0);
0142         QCOMPARE(model.index(2, 5).data(Qt::DisplayRole).toDouble(), 2.0);
0143 
0144         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::MaximumValueRole).toDouble(), 5.0);
0145     }
0146 };
0147 
0148 QTEST_MAIN(NumericAggregationModelTest)
0149 
0150 #include "numericaggregationmodeltest.moc"
0151