Commit 116261da authored by Volker Krause's avatar Volker Krause
Browse files

Add unit test for ratio set aggregation model

parent ffb79de7
......@@ -15,6 +15,7 @@ set(analyzer_lib_srcs
model/datamodel.cpp
model/numericaggregationmodel.cpp
model/productmodel.cpp
model/ratiosetaggregationmodel.cpp
model/schemamodel.cpp
model/surveymodel.cpp
model/timeaggregationmodel.cpp
......@@ -32,7 +33,6 @@ set(analyzer_srcs
connectdialog.cpp
main.cpp
mainwindow.cpp
ratiosetaggregationmodel.cpp
analytics/analyticsview.cpp
......
......@@ -20,11 +20,11 @@
#include "aggregateddatamodel.h"
#include "chart.h"
#include "ratiosetaggregationmodel.h"
#include <model/categoryaggregationmodel.h>
#include <model/datamodel.h>
#include <model/numericaggregationmodel.h>
#include <model/ratiosetaggregationmodel.h>
#include <model/timeaggregationmodel.h>
#include <rest/restapi.h>
#include <core/sample.h>
......
......@@ -17,7 +17,6 @@
#include "ratiosetaggregationmodel.h"
#include <model/timeaggregationmodel.h>
#include <core/ratioset.h>
#include <core/sample.h>
#include <QSet>
......@@ -122,7 +121,7 @@ void RatioSetAggregationModel::recompute()
QSet<QString> categories;
const auto allSamples = m_sourceModel->index(0, 0).data(TimeAggregationModel::AllSamplesRole).value<QVector<Sample>>();
foreach (const auto &s, allSamples) {
const auto rs = s.value(m_aggrValue).value<RatioSet>().data();
const auto rs = s.value(m_aggrValue).value<QVariantMap>();
for (auto it = rs.begin(); it != rs.end(); ++it)
categories.insert(it.key());
}
......@@ -138,12 +137,12 @@ void RatioSetAggregationModel::recompute()
for (int row = 0; row < rowCount; ++row) {
const auto samples = m_sourceModel->index(row, 0).data(TimeAggregationModel::SamplesRole).value<QVector<Sample>>();
foreach (const auto &sample, samples) {
const auto rs = sample.value(m_aggrValue).value<RatioSet>().data();
const auto rs = sample.value(m_aggrValue).value<QVariantMap>();
for (auto it = rs.begin(); it != rs.end(); ++it) {
const auto catIt = std::lower_bound(m_categories.constBegin(), m_categories.constEnd(), it.key());
Q_ASSERT(catIt != m_categories.constEnd());
const auto idx = colCount * row + std::distance(m_categories.constBegin(), catIt);
m_data[idx] += it.value();
m_data[idx] += it.value().toMap().value(QLatin1String("property")).toDouble(); // TODO: make this configurable
}
}
......
......@@ -33,8 +33,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;
......
......@@ -49,6 +49,10 @@ add_executable(numericaggregationmodeltest numericaggregationmodeltest.cpp)
target_link_libraries(numericaggregationmodeltest UserFeedbackTestUtils Qt5::Test)
add_test(NAME numericaggregationmodeltest COMMAND numericaggregationmodeltest)
add_executable(ratiosetaggregationmodeltest ratiosetaggregationmodeltest.cpp)
target_link_libraries(ratiosetaggregationmodeltest UserFeedbackTestUtils Qt5::Test)
add_test(NAME ratiosetaggregationmodeltest COMMAND ratiosetaggregationmodeltest)
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/ratiosetaggregationmodel.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 RatioSetAggregationModelTest : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
Q_INIT_RESOURCE(schemaentrytemplates);
QStandardPaths::setTestModeEnabled(true);
}
void testEmptyModel()
{
RatioSetAggregationModel 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()
{
RatioSetAggregationModel model;
ModelTest modelTest(&model);
model.setAggregationValue(QLatin1String("newPropertyRatio"));
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", "newPropertyRatio": { "cat1": { "property": 0.4 }, "cat2": { "property": 0.6 } } },
{ "timestamp": "2016-11-27 12:00:00", "newPropertyRatio": { "cat1": { "property": 0.3 }, "cat2": { "property": 0.7 } } },
{ "timestamp": "2016-11-27 12:00:00", "newPropertyRatio": { "cat1": { "property": 0.2 }, "cat2": { "property": 0.8 } } },
{ "timestamp": "2016-11-28 12:00:00", "newPropertyRatio": { "cat1": { "property": 1.0 } } },
{ "timestamp": "2016-11-28 12:00:00", "newPropertyRatio": { "cat1": { "property": 0.0 }, "cat2": { "property": 0.0 } } },
{ "timestamp": "2016-11-29 12:00:00" }
])", p);
QCOMPARE(samples.size(), 6);
srcModel.setSamples(samples);
QCOMPARE(model.columnCount(), 3);
QCOMPARE(model.headerData(1, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("cat1"));
QCOMPARE(model.headerData(2, Qt::Horizontal, Qt::DisplayRole).toString(), QLatin1String("cat2"));
QCOMPARE(model.rowCount(), 3);
QCOMPARE(model.index(0, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-27"));
QCOMPARE(model.index(0, 1).data(Qt::DisplayRole).toDouble(), 0.3);
QCOMPARE(model.index(0, 2).data(Qt::DisplayRole).toDouble(), 1.0); // cumulative
QCOMPARE(model.index(1, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-28"));
QCOMPARE(model.index(1, 1).data(Qt::DisplayRole).toDouble(), 0.5); // why?
QCOMPARE(model.index(1, 2).data(Qt::DisplayRole).toDouble(), 0.5);
QCOMPARE(model.index(2, 0).data(TimeAggregationModel::TimeDisplayRole).toString(), QLatin1String("2016-11-29"));
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(0, 0).data(TimeAggregationModel::MaximumValueRole).toDouble(), 1.0);
}
};
QTEST_MAIN(RatioSetAggregationModelTest)
#include "ratiosetaggregationmodeltest.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