numericaggregationmodeltest.cpp 7.01 KB
Newer Older
1 2 3
/*
    Copyright (C) 2016 Volker Krause <vkrause@kde.org>

Volker Krause's avatar
Volker Krause committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 23
*/

Volker Krause's avatar
Volker Krause committed
24 25 26 27 28
#include <console/model/numericaggregationmodel.h>
#include <console/model/datamodel.h>
#include <console/model/timeaggregationmodel.h>
#include <console/core/sample.h>
#include <console/core/schemaentrytemplates.h>
29 30 31 32 33

#include <QDebug>
#include <QtTest/qtest.h>
#include <QObject>
#include <QStandardPaths>
Volker Krause's avatar
Volker Krause committed
34 35 36
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
#include <QAbstractItemModelTester>
#endif
37

38
using namespace KUserFeedback::Console;
39 40 41 42 43 44 45

class NumericAggregationModelTest : public QObject
{
    Q_OBJECT
private slots:
    void initTestCase()
    {
46
        Q_INIT_RESOURCE(schematemplates);
47 48 49 50 51 52
        QStandardPaths::setTestModeEnabled(true);
    }

    void testEmptyModel()
    {
        NumericAggregationModel model;
Volker Krause's avatar
Volker Krause committed
53 54 55
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
        QAbstractItemModelTester modelTest(&model);
#endif
56 57 58 59 60 61 62 63 64 65 66 67 68
        model.setAggregation(AggregationElement());
        AggregationElement aggr;
        {
            SchemaEntry entry;
            entry.setDataType(SchemaEntry::Scalar);
            entry.setName(QLatin1String("usageTime"));
            aggr.setSchemaEntry(entry);
            SchemaEntryElement elem;
            elem.setName(QLatin1String("value"));
            aggr.setSchemaEntryElement(elem);
            aggr.setType(AggregationElement::Value);
        }
        model.setAggregation(aggr);
69 70 71 72 73 74 75 76 77 78 79

        TimeAggregationModel timeModel;
        model.setSourceModel(&timeModel);

        DataModel srcModel;
        timeModel.setSourceModel(&srcModel);
        srcModel.setProduct({});
        QCOMPARE(model.rowCount(), 0);
        QCOMPARE(model.columnCount(), 6);

        Product p;
80 81
        for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
            p.addTemplate(tpl);
82 83 84 85 86 87 88 89 90
        p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
        srcModel.setProduct(p);
        QCOMPARE(model.columnCount(), 6);
        QCOMPARE(model.rowCount(), 0);
    }

    void testModelContent()
    {
        NumericAggregationModel model;
Volker Krause's avatar
Volker Krause committed
91 92 93
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
        QAbstractItemModelTester modelTest(&model);
#endif
94 95 96 97 98 99 100 101 102 103 104 105
        AggregationElement aggr;
        {
            SchemaEntry entry;
            entry.setDataType(SchemaEntry::Scalar);
            entry.setName(QLatin1String("usageTime"));
            aggr.setSchemaEntry(entry);
            SchemaEntryElement elem;
            elem.setName(QLatin1String("value"));
            aggr.setSchemaEntryElement(elem);
            aggr.setType(AggregationElement::Value);
        }
        model.setAggregation(aggr);
106 107 108 109 110 111 112 113

        TimeAggregationModel timeModel;
        model.setSourceModel(&timeModel);

        DataModel srcModel;
        timeModel.setSourceModel(&srcModel);
        timeModel.setAggregationMode(TimeAggregationModel::AggregateDay);
        Product p;
114 115
        for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
            p.addTemplate(tpl);
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
        srcModel.setProduct(p);

        auto samples = Sample::fromJson(R"([
            { "timestamp": "2016-11-26 12:00:00" },
            { "timestamp": "2016-11-27 12:00:00", "usageTime": { "value": "1.0" } },
            { "timestamp": "2016-11-27 12:00:00", "usageTime": { "value": "2.0" } },
            { "timestamp": "2016-11-27 12:00:00", "usageTime": { "value": "3.0" } },
            { "timestamp": "2016-11-27 12:00:00", "usageTime": { "value": "0.0" } },
            { "timestamp": "2016-11-27 12:00:00", "usageTime": { "value": "4.0" } },
            { "timestamp": "2016-11-27 12:00:00", "usageTime": { "value": "5.0" } },
            { "timestamp": "2016-11-28 12:00:00", "usageTime": { "value": "1.0" } },
            { "timestamp": "2016-11-28 12:00:00", "usageTime": { "value": "2.0" } },
            { "timestamp": "2016-11-28 12:00:00" }
        ])", p);
        QCOMPARE(samples.size(), 10);
        srcModel.setSamples(samples);

        QCOMPARE(model.columnCount(), 6);
        QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("Lower Extreme"));
        QCOMPARE(model.headerData(3, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("Median"));
        QCOMPARE(model.headerData(5, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("Upper Extreme"));

        QCOMPARE(model.rowCount(), 3);
        QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-26"));
        QCOMPARE(model.index(0, 1).data(Qt::DisplayRole).toDouble(), 0.0);
        QCOMPARE(model.index(0, 2).data(Qt::DisplayRole).toDouble(), 0.0);
        QCOMPARE(model.index(0, 3).data(Qt::DisplayRole).toDouble(), 0.0);
        QCOMPARE(model.index(0, 4).data(Qt::DisplayRole).toDouble(), 0.0);
        QCOMPARE(model.index(0, 5).data(Qt::DisplayRole).toDouble(), 0.0);

        QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-27"));
        QCOMPARE(model.index(1, 1).data(Qt::DisplayRole).toDouble(), 0.0);
        QCOMPARE(model.index(1, 2).data(Qt::DisplayRole).toDouble(), 1.0);
        QCOMPARE(model.index(1, 3).data(Qt::DisplayRole).toDouble(), 3.0);
        QCOMPARE(model.index(1, 4).data(Qt::DisplayRole).toDouble(), 4.0);
        QCOMPARE(model.index(1, 5).data(Qt::DisplayRole).toDouble(), 5.0);

        QCOMPARE(model.index(2, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-28"));
        QCOMPARE(model.index(2, 1).data(Qt::DisplayRole).toDouble(), 0.0);
        QCOMPARE(model.index(2, 2).data(Qt::DisplayRole).toDouble(), 0.0);
        QCOMPARE(model.index(2, 3).data(Qt::DisplayRole).toDouble(), 1.0);
        QCOMPARE(model.index(2, 4).data(Qt::DisplayRole).toDouble(), 2.0);
        QCOMPARE(model.index(2, 5).data(Qt::DisplayRole).toDouble(), 2.0);

        QCOMPARE(model.index(0, 0).data(TimeAggregationModel::MaximumValueRole).toDouble(), 5.0);
    }
};

QTEST_MAIN(NumericAggregationModelTest)

#include "numericaggregationmodeltest.moc"