Commit 7cfd98fe authored by Volker Krause's avatar Volker Krause
Browse files

Add unit test for the time aggregation model

parent 5a14b0f4
......@@ -15,6 +15,7 @@ set(analyzer_lib_srcs
model/productmodel.cpp
model/schemamodel.cpp
model/surveymodel.cpp
model/timeaggregationmodel.cpp
schemaentrytemplates/schemaentrytemplates.qrc
)
......@@ -32,7 +33,6 @@ set(analyzer_srcs
mainwindow.cpp
numericaggregationmodel.cpp
ratiosetaggregationmodel.cpp
timeaggregationmodel.cpp
analytics/analyticsview.cpp
......
......@@ -16,7 +16,7 @@
*/
#include "aggregateddatamodel.h"
#include "timeaggregationmodel.h"
#include <model/timeaggregationmodel.h>
#include <QDebug>
......
......@@ -23,9 +23,9 @@
#include "chart.h"
#include "numericaggregationmodel.h"
#include "ratiosetaggregationmodel.h"
#include "timeaggregationmodel.h"
#include <model/datamodel.h>
#include <model/timeaggregationmodel.h>
#include <rest/restapi.h>
#include <core/sample.h>
......
......@@ -16,8 +16,8 @@
*/
#include "categoryaggregationmodel.h"
#include <model/timeaggregationmodel.h>
#include <core/sample.h>
#include "timeaggregationmodel.h"
#include <QDebug>
#include <QSet>
......
......@@ -18,7 +18,7 @@
#include "chart.h"
#include "categoryaggregationmodel.h"
#include "numericaggregationmodel.h"
#include "timeaggregationmodel.h"
#include <model/timeaggregationmodel.h>
#include <QtCharts/QAreaSeries>
#include <QtCharts/QBarCategoryAxis>
......
......@@ -55,8 +55,8 @@ public:
AggregationMode aggregationMode() const;
void setAggregationMode(AggregationMode mode);
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
......
......@@ -16,8 +16,8 @@
*/
#include "numericaggregationmodel.h"
#include <model/timeaggregationmodel.h>
#include <core/sample.h>
#include "timeaggregationmodel.h"
using namespace UserFeedback::Analyzer;
......
......@@ -16,9 +16,9 @@
*/
#include "ratiosetaggregationmodel.h"
#include <model/timeaggregationmodel.h>
#include <core/ratioset.h>
#include <core/sample.h>
#include "timeaggregationmodel.h"
#include <QSet>
......
......@@ -37,6 +37,10 @@ add_executable(datamodeltest datamodeltest.cpp)
target_link_libraries(datamodeltest UserFeedbackTestUtils Qt5::Test)
add_test(NAME datamodeltest COMMAND datamodeltest)
add_executable(timeaggregationmodeltest timeaggregationmodeltest.cpp)
target_link_libraries(timeaggregationmodeltest UserFeedbackTestUtils Qt5::Test)
add_test(NAME timeaggregationmodeltest COMMAND timeaggregationmodeltest)
set(php_test_srcs
utilstest.php
schemaentrytest.php
......
/*
Copyright (C) 2016 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <analyzer/core/sample.h>
#include <analyzer/core/schemaentrytemplates.h>
#include <analyzer/model/datamodel.h>
#include <analyzer/model/timeaggregationmodel.h>
#include <3rdparty/qt/modeltest.h>
#include <QDebug>
#include <QtTest/qtest.h>
#include <QObject>
#include <QStandardPaths>
using namespace UserFeedback::Analyzer;
class TimeAggregationModelTest : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
Q_INIT_RESOURCE(schemaentrytemplates);
QStandardPaths::setTestModeEnabled(true);
}
void testEmptyModel()
{
TimeAggregationModel model;
ModelTest modelTest(&model);
DataModel srcModel;
model.setSourceModel(&srcModel);
srcModel.setProduct({});
QCOMPARE(model.rowCount(), 0);
QCOMPARE(model.columnCount(), 2);
Product p;
p.setSchema(SchemaEntryTemplates::availableTemplates());
p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
srcModel.setProduct(p);
QCOMPARE(model.columnCount(), 2);
QCOMPARE(model.rowCount(), 0);
}
void testModelContent()
{
TimeAggregationModel model;
ModelTest modelTest(&model);
DataModel srcModel;
model.setSourceModel(&srcModel);
Product p;
p.setSchema(SchemaEntryTemplates::availableTemplates());
p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
srcModel.setProduct(p);
auto samples = Sample::fromJson(R"([{
"id": 42,
"timestamp": "2016-11-27 16:09:06",
"platform": { "os": "linux", "version": "suse" },
"applicationVersion": { "value": "1.9.84" },
"screens": [ { "width": 1920, "height": 1200 }, { "width": 640, "height": 480 } ],
"newPropertyRatio": { "value1": { "property": 0.55 }, "value2": { "property": 0.45 } }
}, {
"id": 43,
"timestamp": "2016-12-09 12:00:00",
"platform": { "os": "linux", "version": "suse" },
"applicationVersion": { "value": "1.9.84" },
"screens": [ { "width": 1920, "height": 1200 } ],
"newPropertyRatio": { "value1": { "property": 0.1 }, "value2": { "property": 0.9 } }
}])", p);
QCOMPARE(samples.size(), 2);
srcModel.setSamples(samples);
model.setAggregationMode(TimeAggregationModel::AggregateYear);
QCOMPARE(model.rowCount(), 1);
QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016"));
QCOMPARE(model.index(0, 0).data(TimeAggregationModel::DateTimeRole).toDate(), QDate(2016, 01, 01));
QCOMPARE(model.index(0, 0).data(TimeAggregationModel::SamplesRole).value<QVector<Sample>>().size(), 2);
QCOMPARE(model.index(0, 1).data().toInt(), 2);
model.setAggregationMode(TimeAggregationModel::AggregateMonth);
QCOMPARE(model.rowCount(), 2);
QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11"));
QCOMPARE(model.index(0, 0).data(TimeAggregationModel::DateTimeRole).toDate(), QDate(2016, 11, 01));
QCOMPARE(model.index(0, 1).data().toInt(), 1);
QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-12"));
QCOMPARE(model.index(1, 1).data().toInt(), 1);
model.setAggregationMode(TimeAggregationModel::AggregateWeek);
QCOMPARE(model.rowCount(), 2);
QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-w47"));
QCOMPARE(model.index(0, 0).data(TimeAggregationModel::DateTimeRole).toDate(), QDate(2016, 11, 21));
QCOMPARE(model.index(0, 1).data().toInt(), 1);
QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-w49"));
QCOMPARE(model.index(1, 1).data().toInt(), 1);
model.setAggregationMode(TimeAggregationModel::AggregateDay);
QCOMPARE(model.rowCount(), 2);
QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-27"));
QCOMPARE(model.index(0, 0).data(TimeAggregationModel::DateTimeRole).toDate(), QDate(2016, 11, 27));
QCOMPARE(model.index(0, 1).data().toInt(), 1);
QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-12-09"));
QCOMPARE(model.index(0, 0).data(TimeAggregationModel::SamplesRole).value<QVector<Sample>>().size(), 1);
QCOMPARE(model.index(1, 1).data().toInt(), 1);
}
};
QTEST_MAIN(TimeAggregationModelTest)
#include "timeaggregationmodeltest.moc"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment