File: categoryaggregationmodeltest.cpp

package info (click to toggle)
kuserfeedback 1.3.0-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,496 kB
  • sloc: cpp: 13,251; php: 2,192; xml: 224; yacc: 90; lex: 82; sh: 17; makefile: 8
file content (305 lines) | stat: -rw-r--r-- 14,142 bytes parent folder | download | duplicates (2)
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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
    SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: MIT
*/

#include <console/model/categoryaggregationmodel.h>
#include <console/model/datamodel.h>
#include <console/model/timeaggregationmodel.h>
#include <console/core/sample.h>
#include <console/core/schemaentrytemplates.h>

#include <QDebug>
#include <QtTest/qtest.h>
#include <QObject>
#include <QStandardPaths>
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
#include <QAbstractItemModelTester>
#endif

using namespace KUserFeedback::Console;

class CategoryAggregationModelTest : public QObject
{
    Q_OBJECT
private slots:
    void initTestCase()
    {
        Q_INIT_RESOURCE(schematemplates);
        QStandardPaths::setTestModeEnabled(true);
    }

    void testEmptyModel()
    {
        CategoryAggregationModel model;
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
        QAbstractItemModelTester modelTest(&model);
#endif
        model.setAggregation(Aggregation());
        AggregationElement aggrElem;
        {
            SchemaEntry entry;
            entry.setName(QLatin1String("applicationVersion"));
            aggrElem.setSchemaEntry(entry);
            SchemaEntryElement elem;
            elem.setName(QLatin1String("value"));
            aggrElem.setSchemaEntryElement(elem);
            aggrElem.setType(AggregationElement::Value);
        }
        Aggregation aggr;
        aggr.setType(Aggregation::Category);
        aggr.setElements({aggrElem});
        model.setAggregation(aggr);

        TimeAggregationModel timeModel;
        model.setSourceModel(&timeModel);

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

        Product p;
        for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
            p.addTemplate(tpl);
        p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
        srcModel.setProduct(p);
        QCOMPARE(model.columnCount(), 1);
        QCOMPARE(model.rowCount(), 0);
    }

    void testModelContentDepth1()
    {
        CategoryAggregationModel model;
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
        QAbstractItemModelTester modelTest(&model);
#endif
        AggregationElement aggrElem;
        {
            SchemaEntry entry;
            entry.setName(QLatin1String("applicationVersion"));
            aggrElem.setSchemaEntry(entry);
            SchemaEntryElement elem;
            elem.setName(QLatin1String("value"));
            aggrElem.setSchemaEntryElement(elem);
            aggrElem.setType(AggregationElement::Value);
        }
        Aggregation aggr;
        aggr.setType(Aggregation::Category);
        aggr.setElements({aggrElem});
        model.setAggregation(aggr);

        TimeAggregationModel timeModel;
        model.setSourceModel(&timeModel);

        DataModel srcModel;
        timeModel.setSourceModel(&srcModel);
        timeModel.setAggregationMode(TimeAggregationModel::AggregateDay);
        Product p;
        for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
            p.addTemplate(tpl);
        p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
        srcModel.setProduct(p);

        auto samples = Sample::fromJson(R"([
            { "timestamp": "2016-11-27 12:00:00", "applicationVersion": { "value": "1.0" } },
            { "timestamp": "2016-11-27 12:00:00", "applicationVersion": { "value": "1.9.84" } },
            { "timestamp": "2016-11-27 12:00:00", "applicationVersion": { "value": "1.9.84" } },
            { "timestamp": "2016-11-28 12:00:00", "applicationVersion": { "value": "1.9.84" } },
            { "timestamp": "2016-11-28 12:00:00", "applicationVersion": { "value": "1.9.84" } },
            { "timestamp": "2016-11-28 12:00:00" }
        ])", p);
        QCOMPARE(samples.size(), 6);
        srcModel.setSamples(samples);

        QCOMPARE(model.columnCount(), 4);
        QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("[empty]"));
        QCOMPARE(model.headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("1.0"));
        QCOMPARE(model.headerData(3, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("1.9.84"));

        QCOMPARE(model.rowCount(), 2);
        QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-27"));
        QCOMPARE(model.index(0, 1).data(Qt::DisplayRole).toInt(), 0);
        QCOMPARE(model.index(0, 2).data(Qt::DisplayRole).toInt(), 1);
        QCOMPARE(model.index(0, 3).data(Qt::DisplayRole).toInt(), 2);
        QCOMPARE(model.index(0, 3).data(TimeAggregationModel::AccumulatedDisplayRole).toInt(), 3);

        QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-28"));
        QCOMPARE(model.index(1, 1).data(Qt::DisplayRole).toInt(), 1);
        QCOMPARE(model.index(1, 2).data(Qt::DisplayRole).toInt(), 0);
        QCOMPARE(model.index(0, 3).data(Qt::DisplayRole).toInt(), 2);
        QCOMPARE(model.index(0, 3).data(TimeAggregationModel::AccumulatedDisplayRole).toInt(), 3);

        QCOMPARE(model.index(0, 0).data(TimeAggregationModel::MaximumValueRole).toInt(), 3);
    }

    void testModelContentDepth2()
    {
        const auto p = Product::fromJson(R"({
            "name": "depth2test",
            "schema": [{
                "name": "platform",
                "type": "scalar",
                "elements": [
                    { "name": "os", "type": "string" },
                    { "name": "version", "type": "string" }
                ]
            }],
            "aggregation": [{
                "type": "category",
                "name": "OS Details",
                "elements": [
                    { "type": "value", "schemaEntry": "platform", "schemaEntryElement": "os" },
                    { "type": "value", "schemaEntry": "platform", "schemaEntryElement": "version" }
                ]
            }]
        })").at(0);
        QVERIFY(p.isValid());
        QCOMPARE(p.aggregations().size(), 1);

        CategoryAggregationModel model;
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
        QAbstractItemModelTester modelTest(&model);
#endif
        model.setAggregation(p.aggregations().at(0));

        TimeAggregationModel timeModel;
        model.setSourceModel(&timeModel);

        DataModel srcModel;
        timeModel.setSourceModel(&srcModel);
        timeModel.setAggregationMode(TimeAggregationModel::AggregateDay);
        srcModel.setProduct(p);

        auto samples = Sample::fromJson(R"([
            { "timestamp": "2016-11-27 12:00:00", "platform": { "os": "windows", "version": "10" } },
            { "timestamp": "2016-11-27 12:00:00", "platform": { "os": "linux", "version": "10" } },
            { "timestamp": "2016-11-27 12:00:00", "platform": { "os": "linux", "version": "10" } },
            { "timestamp": "2016-11-28 12:00:00", "platform": { "os": "windows", "version": "10" } },
            { "timestamp": "2016-11-28 12:00:00", "platform": { "os": "linux", "version": "42" } },
            { "timestamp": "2016-11-28 12:00:00" }
        ])", p);
        QCOMPARE(samples.size(), 6);
        srcModel.setSamples(samples);

        QCOMPARE(model.columnCount(), 5);
        QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("[empty]"));
        QCOMPARE(model.headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("10")); // linux
        QCOMPARE(model.headerData(3, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("42"));
        QCOMPARE(model.headerData(4, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("10")); // windows

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

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

        QCOMPARE(model.index(0, 0).data(TimeAggregationModel::MaximumValueRole).toInt(), 3);

        model.setDepth(1);
        QCOMPARE(model.columnCount(), 4);
        QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("[empty]"));
        QCOMPARE(model.headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("linux"));
        QCOMPARE(model.headerData(3, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("windows"));

        QCOMPARE(model.rowCount(), 2);
        QCOMPARE(model.index(0, 1).data(Qt::DisplayRole).toInt(), 0);
        QCOMPARE(model.index(0, 2).data(Qt::DisplayRole).toInt(), 2);
        QCOMPARE(model.index(0, 3).data(Qt::DisplayRole).toInt(), 1);
    }

    void testModelContentDepth3()
    {
        const auto p = Product::fromJson(R"({
            "name": "depth2test",
            "schema": [{
                "name": "opengl",
                "type": "scalar",
                "elements": [
                    { "name": "os", "type": "string" },
                    { "name": "type", "type": "string" },
                    { "name": "version", "type": "string" }
                ]
            }],
            "aggregation": [{
                "type": "category",
                "name": "OpenGL Details",
                "elements": [
                    { "type": "value", "schemaEntry": "opengl", "schemaEntryElement": "os" },
                    { "type": "value", "schemaEntry": "opengl", "schemaEntryElement": "type" },
                    { "type": "value", "schemaEntry": "opengl", "schemaEntryElement": "version" }
                ]
            }]
        })").at(0);
        QVERIFY(p.isValid());
        QCOMPARE(p.aggregations().size(), 1);

        CategoryAggregationModel model;
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
        QAbstractItemModelTester modelTest(&model);
#endif
        model.setAggregation(p.aggregations().at(0));

        TimeAggregationModel timeModel;
        model.setSourceModel(&timeModel);

        DataModel srcModel;
        timeModel.setSourceModel(&srcModel);
        timeModel.setAggregationMode(TimeAggregationModel::AggregateDay);
        srcModel.setProduct(p);

        auto samples = Sample::fromJson(R"([
            { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "windows", "type": "GL", "version": "2.0" } },
            { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "windows", "type": "GLES", "version": "2.0" } },
            { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "windows" } },
            { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "linux", "type": "GL", "version": "4.4" } },
            { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "linux", "type": "GL", "version": "3.1" } },
            { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "linux", "type": "GL", "version": "4.4" } },
            { "timestamp": "2016-11-27 12:00:00", "opengl": { "os": "linux", "type": "GLES", "version": "2.0" } },
            { "timestamp": "2016-11-28 12:00:00" }
        ])", p);
        QCOMPARE(samples.size(), 8);
        srcModel.setSamples(samples);

        QCOMPARE(model.columnCount(), 8);
        QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("[empty]"));
        QCOMPARE(model.headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("3.1")); // linux GL
        QCOMPARE(model.headerData(3, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("4.4")); // linux GL
        QCOMPARE(model.headerData(4, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("2.0")); // linux GLES
        QCOMPARE(model.headerData(5, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("[empty]")); // windows empty
        QCOMPARE(model.headerData(6, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("2.0")); // windows GL
        QCOMPARE(model.headerData(7, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("2.0")); // windows GLES

        QCOMPARE(model.rowCount(), 2);
        QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-27"));
        QCOMPARE(model.index(0, 1).data(Qt::DisplayRole).toInt(), 0); // empty
        QCOMPARE(model.index(0, 2).data(Qt::DisplayRole).toInt(), 1); // linux GL
        QCOMPARE(model.index(0, 3).data(Qt::DisplayRole).toInt(), 2); // linux GL
        QCOMPARE(model.index(0, 4).data(Qt::DisplayRole).toInt(), 1); // linux GLES
        QCOMPARE(model.index(0, 5).data(Qt::DisplayRole).toInt(), 1); // windows empty
        QCOMPARE(model.index(0, 6).data(Qt::DisplayRole).toInt(), 1); // windows GL
        QCOMPARE(model.index(0, 7).data(Qt::DisplayRole).toInt(), 1); // windows GLES

        QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-28"));
        QCOMPARE(model.index(1, 1).data(Qt::DisplayRole).toInt(), 1);
        QCOMPARE(model.index(1, 2).data(Qt::DisplayRole).toInt(), 0);
        QCOMPARE(model.index(1, 3).data(Qt::DisplayRole).toInt(), 0);
        QCOMPARE(model.index(1, 4).data(Qt::DisplayRole).toInt(), 0);
        QCOMPARE(model.index(1, 5).data(Qt::DisplayRole).toInt(), 0);
        QCOMPARE(model.index(1, 6).data(Qt::DisplayRole).toInt(), 0);
        QCOMPARE(model.index(1, 7).data(Qt::DisplayRole).toInt(), 0);
    }
};

QTEST_MAIN(CategoryAggregationModelTest)

#include "categoryaggregationmodeltest.moc"