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

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include <console/core/sample.h>
0008 #include <console/core/schemaentrytemplates.h>
0009 #include <console/model/datamodel.h>
0010 #include <console/model/timeaggregationmodel.h>
0011 
0012 #include <QDebug>
0013 #include <QtTest/qtest.h>
0014 #include <QObject>
0015 #include <QStandardPaths>
0016 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0017 #include <QAbstractItemModelTester>
0018 #endif
0019 
0020 using namespace KUserFeedback::Console;
0021 
0022 class TimeAggregationModelTest : public QObject
0023 {
0024     Q_OBJECT
0025 private Q_SLOTS:
0026     void initTestCase()
0027     {
0028         Q_INIT_RESOURCE(schematemplates);
0029         QStandardPaths::setTestModeEnabled(true);
0030     }
0031 
0032     void testEmptyModel()
0033     {
0034         TimeAggregationModel model;
0035 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0036         QAbstractItemModelTester modelTest(&model);
0037 #endif
0038 
0039         DataModel srcModel;
0040         model.setSourceModel(&srcModel);
0041         srcModel.setProduct({});
0042         QCOMPARE(model.rowCount(), 0);
0043         QCOMPARE(model.columnCount(), 2);
0044 
0045         Product p;
0046         for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
0047             p.addTemplate(tpl);
0048         p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
0049         srcModel.setProduct(p);
0050         QCOMPARE(model.columnCount(), 2);
0051         QCOMPARE(model.rowCount(), 0);
0052     }
0053 
0054     void testModelContent()
0055     {
0056         TimeAggregationModel model;
0057 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
0058         QAbstractItemModelTester modelTest(&model);
0059 #endif
0060 
0061         DataModel srcModel;
0062         model.setSourceModel(&srcModel);
0063         Product p;
0064         for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
0065             p.addTemplate(tpl);
0066         p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
0067         srcModel.setProduct(p);
0068 
0069         auto samples = Sample::fromJson(R"([{
0070             "id": 42,
0071             "timestamp": "2016-11-27 16:09:06",
0072             "platform": { "os": "linux", "version": "suse" },
0073             "applicationVersion": { "value": "1.9.84" },
0074             "screens": [ { "width": 1920, "height": 1200 }, { "width": 640, "height": 480 } ],
0075             "newPropertyRatio": { "value1": { "property": 0.55 }, "value2": { "property": 0.45 } }
0076         }, {
0077             "id": 43,
0078             "timestamp": "2016-12-09 12:00:00",
0079             "platform": { "os": "linux", "version": "suse" },
0080             "applicationVersion": { "value": "1.9.84" },
0081             "screens": [ { "width": 1920, "height": 1200 } ],
0082             "newPropertyRatio": { "value1": { "property": 0.1 }, "value2": { "property": 0.9 } }
0083         }])", p);
0084         QCOMPARE(samples.size(), 2);
0085         srcModel.setSamples(samples);
0086 
0087         model.setAggregationMode(TimeAggregationModel::AggregateYear);
0088         QCOMPARE(model.rowCount(), 1);
0089         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016"));
0090         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::DateTimeRole).toDate(), QDate(2016, 01, 01));
0091         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::SamplesRole).value<QVector<Sample>>().size(), 2);
0092         QCOMPARE(model.index(0, 1).data().toInt(), 2);
0093 
0094         model.setAggregationMode(TimeAggregationModel::AggregateMonth);
0095         QCOMPARE(model.rowCount(), 2);
0096         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11"));
0097         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::DateTimeRole).toDate(), QDate(2016, 11, 01));
0098         QCOMPARE(model.index(0, 1).data().toInt(), 1);
0099         QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-12"));
0100         QCOMPARE(model.index(1, 1).data().toInt(), 1);
0101 
0102         model.setAggregationMode(TimeAggregationModel::AggregateWeek);
0103         QCOMPARE(model.rowCount(), 2);
0104         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-w47"));
0105         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::DateTimeRole).toDate(), QDate(2016, 11, 21));
0106         QCOMPARE(model.index(0, 1).data().toInt(), 1);
0107         QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-w49"));
0108         QCOMPARE(model.index(1, 1).data().toInt(), 1);
0109 
0110         model.setAggregationMode(TimeAggregationModel::AggregateDay);
0111         QCOMPARE(model.rowCount(), 2);
0112         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-27"));
0113         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::DateTimeRole).toDate(), QDate(2016, 11, 27));
0114         QCOMPARE(model.index(0, 1).data().toInt(), 1);
0115         QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-12-09"));
0116         QCOMPARE(model.index(0, 0).data(TimeAggregationModel::SamplesRole).value<QVector<Sample>>().size(), 1);
0117         QCOMPARE(model.index(1, 1).data().toInt(), 1);
0118     }
0119 };
0120 
0121 QTEST_MAIN(TimeAggregationModelTest)
0122 
0123 #include "timeaggregationmodeltest.moc"