Commit 06dd0ab5 authored by Volker Krause's avatar Volker Krause
Browse files

Add unit test for the category aggregation model

parent 433f2fe2
......@@ -11,6 +11,7 @@ set(analyzer_lib_srcs
rest/restclient.cpp
rest/serverinfo.cpp
model/categoryaggregationmodel.cpp
model/datamodel.cpp
model/productmodel.cpp
model/schemamodel.cpp
......@@ -26,7 +27,6 @@ target_include_directories(UserFeedbackAnalyzer PUBLIC "$<BUILD_INTERFACE:${CMAK
set(analyzer_srcs
aggregateddatamodel.cpp
categoryaggregationmodel.cpp
chart.cpp
connectdialog.cpp
main.cpp
......
......@@ -19,11 +19,11 @@
#include "ui_analyticsview.h"
#include "aggregateddatamodel.h"
#include "categoryaggregationmodel.h"
#include "chart.h"
#include "numericaggregationmodel.h"
#include "ratiosetaggregationmodel.h"
#include <model/categoryaggregationmodel.h>
#include <model/datamodel.h>
#include <model/timeaggregationmodel.h>
#include <rest/restapi.h>
......@@ -129,7 +129,7 @@ void AnalyticsView::setProduct(const Product& product)
{
auto model = new CategoryAggregationModel(this);
model->setSourceModel(m_timeAggregationModel);
model->setAggregationValue(schemaEntry.name());
model->setAggregationValue(schemaEntry.name() + QLatin1Char('.') + schemaEntry.elements().at(0).name());
m_aggregationModels.push_back(model);
m_aggregatedDataModel->addSourceModel(model, schemaEntry.name());
ui->chartType->addItem(schemaEntry.name(), QVariant::fromValue(model));
......
......@@ -16,8 +16,8 @@
*/
#include "chart.h"
#include "categoryaggregationmodel.h"
#include "numericaggregationmodel.h"
#include <model/categoryaggregationmodel.h>
#include <model/timeaggregationmodel.h>
#include <QtCharts/QAreaSeries>
......
......@@ -35,8 +35,8 @@ public:
void setSourceModel(QAbstractItemModel *model);
void setAggregationValue(const QString &aggrValue);
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;
......
......@@ -41,6 +41,10 @@ add_executable(timeaggregationmodeltest timeaggregationmodeltest.cpp)
target_link_libraries(timeaggregationmodeltest UserFeedbackTestUtils Qt5::Test)
add_test(NAME timeaggregationmodeltest COMMAND timeaggregationmodeltest)
add_executable(categoryaggregationmodeltest categoryaggregationmodeltest.cpp)
target_link_libraries(categoryaggregationmodeltest UserFeedbackTestUtils Qt5::Test)
add_test(NAME categoryaggregationmodeltest COMMAND categoryaggregationmodeltest)
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/model/categoryaggregationmodel.h>
#include <analyzer/model/datamodel.h>
#include <analyzer/model/timeaggregationmodel.h>
#include <analyzer/core/sample.h>
#include <analyzer/core/schemaentrytemplates.h>
#include <3rdparty/qt/modeltest.h>
#include <QDebug>
#include <QtTest/qtest.h>
#include <QObject>
#include <QStandardPaths>
using namespace UserFeedback::Analyzer;
class CategoryAggregationModelTest : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
Q_INIT_RESOURCE(schemaentrytemplates);
QStandardPaths::setTestModeEnabled(true);
}
void testEmptyModel()
{
CategoryAggregationModel model;
ModelTest modelTest(&model);
model.setAggregationValue(QString());
model.setAggregationValue(QLatin1String("applicationVersion.value"));
TimeAggregationModel timeModel;
model.setSourceModel(&timeModel);
DataModel srcModel;
timeModel.setSourceModel(&srcModel);
srcModel.setProduct({});
QCOMPARE(model.rowCount(), 0);
QCOMPARE(model.columnCount(), 1);
Product p;
p.setSchema(SchemaEntryTemplates::availableTemplates());
p.setName(QStringLiteral("org.kde.UserFeedback.UnitTest"));
srcModel.setProduct(p);
QCOMPARE(model.columnCount(), 1);
QCOMPARE(model.rowCount(), 0);
}
void testModelContent()
{
CategoryAggregationModel model;
ModelTest modelTest(&model);
model.setAggregationValue(QLatin1String("applicationVersion.value"));
TimeAggregationModel timeModel;
model.setSourceModel(&timeModel);
DataModel srcModel;
timeModel.setSourceModel(&srcModel);
timeModel.setAggregationMode(TimeAggregationModel::AggregateDay);
Product p;
p.setSchema(SchemaEntryTemplates::availableTemplates());
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(), 3); // values are cumulative
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(), 1);
QCOMPARE(model.index(0, 3).data(Qt::DisplayRole).toInt(), 3);
QCOMPARE(model.index(0, 0).data(TimeAggregationModel::MaximumValueRole).toInt(), 3);
}
};
QTEST_MAIN(CategoryAggregationModelTest)
#include "categoryaggregationmodeltest.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