chore(StatusQ): Generic TestModel excluded to a separate file

Closes: #12843
This commit is contained in:
Michał Cieślak 2023-12-21 16:06:52 +01:00 committed by Michał
parent 57ee01b239
commit a2aedb5c6a
8 changed files with 232 additions and 433 deletions

View File

@ -17,6 +17,8 @@ add_library(StatusQTestLib
src/TestHelpers/listmodelwrapper.h
src/TestHelpers/modelaccessobserverproxy.cpp
src/TestHelpers/modelaccessobserverproxy.h
src/TestHelpers/testmodel.cpp
src/TestHelpers/testmodel.h
)
target_link_libraries(StatusQTestLib PUBLIC Qt5::Core Qt5::Quick)
@ -49,29 +51,29 @@ add_test(NAME QmlTests WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
###########
add_executable(RolesRenamingModelTest tst_RolesRenamingModel.cpp)
target_link_libraries(RolesRenamingModelTest PRIVATE Qt5::Qml Qt5::Test StatusQ)
target_link_libraries(RolesRenamingModelTest PRIVATE Qt5::Test StatusQ StatusQTestLib)
add_test(NAME RolesRenamingModelTest COMMAND RolesRenamingModelTest)
add_executable(LeftJoinModelTest tst_LeftJoinModel.cpp)
target_link_libraries(LeftJoinModelTest PRIVATE Qt5::Test StatusQ)
target_link_libraries(LeftJoinModelTest PRIVATE Qt5::Test StatusQ StatusQTestLib)
add_test(NAME LeftJoinModelTest COMMAND LeftJoinModelTest)
add_executable(SubmodelProxyModelTest tst_SubmodelProxyModel.cpp)
target_link_libraries(SubmodelProxyModelTest PRIVATE Qt5::Qml Qt5::Test StatusQ StatusQTestLib)
target_link_libraries(SubmodelProxyModelTest PRIVATE Qt5::Test StatusQ StatusQTestLib)
add_test(NAME SubmodelProxyModelTest COMMAND SubmodelProxyModelTest)
add_executable(AggregatorTest tst_Aggregator.cpp)
target_link_libraries(AggregatorTest PRIVATE Qt5::Test StatusQ)
target_link_libraries(AggregatorTest PRIVATE Qt5::Test StatusQ StatusQTestLib)
add_test(NAME AggregatorTest COMMAND AggregatorTest)
add_executable(SingleRoleAggregatorTest tst_SingleRoleAggregator.cpp)
target_link_libraries(SingleRoleAggregatorTest PRIVATE Qt5::Test StatusQ)
target_link_libraries(SingleRoleAggregatorTest PRIVATE Qt5::Test StatusQ StatusQTestLib)
add_test(NAME SingleRoleAggregatorTest COMMAND SingleRoleAggregatorTest)
add_executable(SumAggregatorTest tst_SumAggregator.cpp)
target_link_libraries(SumAggregatorTest PRIVATE Qt5::Test StatusQ)
target_link_libraries(SumAggregatorTest PRIVATE Qt5::Test StatusQ StatusQTestLib)
add_test(NAME SumAggregatorTest COMMAND SumAggregatorTest)
add_executable(ConcatModelTest tst_ConcatModel.cpp)
target_link_libraries(ConcatModelTest PRIVATE Qt5::Qml Qt5::Test StatusQ StatusQTestLib)
target_link_libraries(ConcatModelTest PRIVATE Qt5::Test StatusQ StatusQTestLib)
add_test(NAME ConcatModelTest COMMAND ConcatModelTest)

View File

@ -0,0 +1,91 @@
#include "testmodel.h"
TestModel::TestModel(QList<QPair<QString, QVariantList>> data)
: m_data(std::move(data))
{
initRoles();
}
TestModel::TestModel(QList<QString> roles)
{
QList<QPair<QString, QVariantList>> data;
data.reserve(roles.size());
for (auto& role : roles)
data.append({std::move(role), {}});
m_data = std::move(data);
initRoles();
}
int TestModel::rowCount(const QModelIndex& parent) const
{
if(parent.isValid())
return 0;
Q_ASSERT(m_data.size());
return m_data.first().second.size();
}
QHash<int, QByteArray> TestModel::roleNames() const
{
return m_roles;
}
QVariant TestModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || role < 0 || role >= m_data.size())
return {};
const auto row = index.row();
if (role >= m_data.length() || row >= m_data.at(0).second.length())
return {};
return m_data.at(role).second.at(row);
}
void TestModel::insert(int index, QVariantList row)
{
beginInsertRows(QModelIndex{}, index, index);
Q_ASSERT(row.size() == m_data.size());
for (int i = 0; i < m_data.size(); i++) {
auto& roleVariantList = m_data[i].second;
Q_ASSERT(index <= roleVariantList.size());
roleVariantList.insert(index, std::move(row[i]));
}
endInsertRows();
}
void TestModel::update(int index, int role, QVariant value)
{
Q_ASSERT(role < m_data.size() && index < m_data[role].second.size());
m_data[role].second[index].setValue(std::move(value));
emit dataChanged(this->index(index, 0), this->index(index, 0), { role });
}
void TestModel::remove(int index)
{
beginRemoveRows(QModelIndex{}, index, index);
for (int i = 0; i < m_data.size(); i++) {
auto& roleVariantList = m_data[i].second;
Q_ASSERT(index < roleVariantList.size());
roleVariantList.removeAt(index);
}
endRemoveRows();
}
void TestModel::initRoles()
{
m_roles.reserve(m_data.size());
for (auto i = 0; i < m_data.size(); i++)
m_roles.insert(i, m_data.at(i).first.toUtf8());
}

View File

@ -0,0 +1,22 @@
#include <QAbstractListModel>
class TestModel : public QAbstractListModel {
public:
explicit TestModel(QList<QPair<QString, QVariantList>> data);
explicit TestModel(QList<QString> roles);
int rowCount(const QModelIndex& parent) const override;
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex& index, int role) const override;
void insert(int index, QVariantList row);
void update(int index, int role, QVariant value);
void remove(int index);
private:
void initRoles();
QList<QPair<QString, QVariantList>> m_data;
QHash<int, QByteArray> m_roles;
};

View File

@ -1,75 +1,10 @@
#include <QtTest>
#include <QAbstractListModel>
#include <QCoreApplication>
#include "StatusQ/aggregator.h"
#include <StatusQ/aggregator.h>
#include <TestHelpers/testmodel.h>
namespace {
// TODO: To be removed once issue #12843 is resolved and we have a testing utils
class TestSourceModel : public QAbstractListModel {
public:
explicit TestSourceModel(QList<QPair<QString, QVariantList>> data)
: m_data(std::move(data)) {
m_roles.reserve(m_data.size());
for (auto i = 0; i < m_data.size(); i++)
m_roles.insert(i, m_data.at(i).first.toUtf8());
}
int rowCount(const QModelIndex& parent) const override {
Q_ASSERT(m_data.size());
return m_data.first().second.size();
}
QVariant data(const QModelIndex& index, int role) const override {
if (!index.isValid() || role < 0 || role >= m_data.size())
return {};
const auto row = index.row();
if (role >= m_data.length() || row >= m_data.at(0).second.length())
return {};
return m_data.at(role).second.at(row);
}
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override {
beginInsertRows(parent, row, row + count - 1);
m_data.insert(row, QPair<QString, QVariantList>());
endInsertRows();
return true;
}
void update(int index, int role, QVariant value) {
Q_ASSERT(role < m_data.size() && index < m_data[role].second.size());
m_data[role].second[index].setValue(std::move(value));
emit dataChanged(this->index(index, 0), this->index(index, 0), { role });
}
void remove(int index) {
beginRemoveRows(QModelIndex{}, index, index);
for (int i = 0; i < m_data.size(); i++) {
auto& roleVariantList = m_data[i].second;
Q_ASSERT(index < roleVariantList.size());
roleVariantList.removeAt(index);
}
endRemoveRows();
}
QHash<int, QByteArray> roleNames() const override {
return m_roles;
}
private:
QList<QPair<QString, QVariantList>> m_data;
QHash<int, QByteArray> m_roles;
};
class ChildAggregator : public Aggregator {
Q_OBJECT
@ -98,10 +33,10 @@ private:
private slots:
void testModel() {
ChildAggregator aggregator;
TestSourceModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }}
});
TestModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }}
});
QSignalSpy modelChangedSpy(&aggregator, &Aggregator::modelChanged);
QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged);
@ -120,10 +55,10 @@ private slots:
void testCalculateAggregationTrigger() {
ChildAggregator aggregator;
TestSourceModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { 0.123, 1.0, 1.45, 25.45 }}
});
TestModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { 0.123, 1.0, 1.45, 25.45 }}
});
QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged);
int valueChangedSpyCount = 0;

View File

@ -5,89 +5,7 @@
#include <memory>
#include <StatusQ/leftjoinmodel.h>
namespace {
class TestSourceModel : public QAbstractListModel {
public:
explicit TestSourceModel(QList<QPair<QString, QVariantList>> data)
: m_data(std::move(data))
{
m_roles.reserve(m_data.size());
for (auto i = 0; i < m_data.size(); i++)
m_roles.insert(i, m_data.at(i).first.toUtf8());
}
int rowCount(const QModelIndex& parent) const override
{
if(parent.isValid()) return 0; //no children
assert(m_data.size());
return m_data.first().second.size();
}
QVariant data(const QModelIndex& index, int role) const override
{
if (!index.isValid() || role < 0 || role >= m_data.size())
return {};
const auto row = index.row();
if (role >= m_data.length() || row >= m_data.at(0).second.length())
return {};
return m_data.at(role).second.at(row);
}
void insert(int index, QVariantList row)
{
beginInsertRows(QModelIndex{}, index, index);
assert(row.size() == m_data.size());
for (int i = 0; i < m_data.size(); i++) {
auto& roleVariantList = m_data[i].second;
assert(index <= roleVariantList.size());
roleVariantList.insert(index, row.at(i));
}
endInsertRows();
}
void update(int index, int role, QVariant value)
{
assert(role < m_data.size() && index < m_data[role].second.size());
m_data[role].second[index].setValue(std::move(value));
emit dataChanged(this->index(index, 0), this->index(index, 0), { role });
}
void remove(int index)
{
beginRemoveRows(QModelIndex{}, index, index);
for (int i = 0; i < m_data.size(); i++) {
auto& roleVariantList = m_data[i].second;
assert(index < roleVariantList.size());
roleVariantList.removeAt(index);
}
endRemoveRows();
}
QHash<int, QByteArray> roleNames() const override
{
return m_roles;
}
private:
QList<QPair<QString, QVariantList>> m_data;
QHash<int, QByteArray> m_roles;
};
} // anonymous namespace
#include <TestHelpers/testmodel.h>
class TestLeftJoinModel: public QObject
{
@ -105,12 +23,12 @@ private slots:
void initializationTest()
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -138,12 +56,12 @@ private slots:
void collidingRolesTest()
{
TestSourceModel leftModel({
TestModel leftModel({
{ "name", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -174,13 +92,13 @@ private slots:
void duplicatedRolesTest()
{
{
TestSourceModel leftModel({
TestModel leftModel({
{ "name", { "Token 1", "Token 2" }},
{ "name", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "title", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -208,13 +126,13 @@ private slots:
QCOMPARE(model.roleNames(), {});
}
{
TestSourceModel leftModel({
TestModel leftModel({
{ "name", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "title", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -242,12 +160,12 @@ private slots:
QCOMPARE(model.roleNames(), {});
}
{
TestSourceModel leftModel({
TestModel leftModel({
{ "name", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "title", { "Community 1", "Community 2" }},
{ "title", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
@ -280,12 +198,12 @@ private slots:
void noJoinRoleTest()
{
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -313,12 +231,12 @@ private slots:
QCOMPARE(model.roleNames(), {});
}
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -346,12 +264,12 @@ private slots:
QCOMPARE(model.roleNames(), {});
}
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -382,12 +300,12 @@ private slots:
void basicAccesTest()
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "color", { "red", "blue" }},
{ "communityId", { "community_1", "community_2" }}
@ -413,12 +331,12 @@ private slots:
void changesPropagationTest()
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }},
{ "color", { "red", "green" }}
@ -472,12 +390,12 @@ private slots:
// TODO: cover also move and layoutChanged
void insertRemovePropagationTest()
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }},
{ "color", { "red", "green" }}
@ -529,12 +447,12 @@ private slots:
void rightModelJoinRoleChangesPropagationTest()
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -566,12 +484,12 @@ private slots:
void rightModelRemovalPropagationTest()
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2" }},
{ "communityId", { "community_1", "community_2" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -603,12 +521,12 @@ private slots:
void rightModelAdditionPropagationTest()
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2", "Token 3"}},
{ "communityId", { "community_1", "community_2", "community_3" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -643,12 +561,12 @@ private slots:
void leftModelJoinRoleChangesPropagationTest()
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2", "Token 3"}},
{ "communityId", { "community_1", "community_2", "community_1" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -686,13 +604,13 @@ private slots:
void modelsDeletedBeforeInitializationTest()
{
auto leftModel = std::make_unique<TestSourceModel>(
auto leftModel = std::make_unique<TestModel>(
QList<QPair<QString, QVariantList>>{
{ "title", { "Token 1", "Token 2", "Token 3"}},
{ "communityId", { "community_1", "community_2", "community_1" }}
});
auto rightModel = std::make_unique<TestSourceModel>(
auto rightModel = std::make_unique<TestModel>(
QList<QPair<QString, QVariantList>>{
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
@ -716,12 +634,12 @@ private slots:
QCOMPARE(model.data(model.index(0, 0), 0), {});
QCOMPARE(model.data(model.index(0, 0), 2), {});
TestSourceModel newLeftModel({
TestModel newLeftModel({
{ "title", { "Token 1", "Token 2", "Token 3"}},
{ "communityId", { "community_1", "community_2", "community_1" }}
});
TestSourceModel newRightModel({
TestModel newRightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -745,13 +663,13 @@ private slots:
void modelsDeletedAfterInitializationTest()
{
auto leftModel = std::make_unique<TestSourceModel>(
auto leftModel = std::make_unique<TestModel>(
QList<QPair<QString, QVariantList>>{
{ "title", { "Token 1", "Token 2", "Token 3"}},
{ "communityId", { "community_1", "community_2", "community_1" }}
});
auto rightModel = std::make_unique<TestSourceModel>(
auto rightModel = std::make_unique<TestModel>(
QList<QPair<QString, QVariantList>>{
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
@ -778,12 +696,12 @@ private slots:
QCOMPARE(model.data(model.index(0, 0), 0), {});
QCOMPARE(model.data(model.index(0, 0), 2), {});
TestSourceModel newLeftModel({
TestModel newLeftModel({
{ "title", { "Token 1", "Token 2", "Token 3"}},
{ "communityId", { "community_1", "community_2", "community_1" }}
});
TestSourceModel newRightModel({
TestModel newRightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -811,13 +729,13 @@ private slots:
void rightModelDeletedAfterInitializationTest()
{
auto leftModel = std::make_unique<TestSourceModel>(
auto leftModel = std::make_unique<TestModel>(
QList<QPair<QString, QVariantList>>{
{ "title", { "Token 1", "Token 2", "Token 3"}},
{ "communityId", { "community_1", "community_2", "community_1" }}
});
auto rightModel = std::make_unique<TestSourceModel>(
auto rightModel = std::make_unique<TestModel>(
QList<QPair<QString, QVariantList>>{
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
@ -840,12 +758,12 @@ private slots:
QCOMPARE(model.data(model.index(0, 0), 1), "community_1");
QCOMPARE(model.data(model.index(0, 0), 2), {});
TestSourceModel newLeftModel({
TestModel newLeftModel({
{ "title", { "Token 1", "Token 2", "Token 3"}},
{ "communityId", { "community_1", "community_2", "community_1" }}
});
TestSourceModel newRightModel({
TestModel newRightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -866,12 +784,12 @@ private slots:
void rightModelChangedWithSameRolesAfterInitializationTest()
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2", "Token 3"}},
{ "communityId", { "community_1", "community_2", "community_1" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -884,7 +802,7 @@ private slots:
model.setJoinRole("communityId");
TestSourceModel newRightModel({
TestModel newRightModel({
{ "name", { "Community A", "Community B" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -919,12 +837,12 @@ private slots:
void rightModelChangedWithDifferentRolesAfterInitializationTest()
{
TestSourceModel leftModel({
TestModel leftModel({
{ "title", { "Token 1", "Token 2", "Token 3"}},
{ "communityId", { "community_1", "community_2", "community_1" }}
});
TestSourceModel rightModel({
TestModel rightModel({
{ "name", { "Community 1", "Community 2" }},
{ "communityId", { "community_1", "community_2" }}
});
@ -937,7 +855,7 @@ private slots:
model.setJoinRole("communityId");
TestSourceModel newRightModel({
TestModel newRightModel({
{ "communityId", { "community_1", "community_2" }},
{ "name", { "Community A", "Community B" }}
});

View File

@ -4,47 +4,7 @@
#include <memory>
#include <StatusQ/rolesrenamingmodel.h>
namespace {
class TestSourceModel : public QAbstractListModel {
public:
explicit TestSourceModel(QList<QString> roles)
: m_roles(std::move(roles))
{
}
QVariant data(const QModelIndex& index, int role) const override
{
if(!index.isValid() || index.row() >= capacity)
return {};
return 42;
}
int rowCount(const QModelIndex& parent) const override
{
return capacity;
}
QHash<int, QByteArray> roleNames() const override
{
QHash<int, QByteArray> roles;
roles.remove(m_roles.size());
for (auto i = 0; i < m_roles.size(); i++)
roles.insert(i, m_roles.at(i).toUtf8());
return roles;
}
private:
static constexpr auto capacity = 5;
QList<QString> m_roles;
};
}
#include <TestHelpers/testmodel.h>
class TestRolesRenamingModel: public QObject
{
@ -53,7 +13,7 @@ class TestRolesRenamingModel: public QObject
private slots:
void initializationWithBrokenMappingTest()
{
TestSourceModel sourceModel({"id", "name", "color"});
TestModel sourceModel({"id", "name", "color"});
RolesRenamingModel model;
QQmlListProperty<RoleRename> mapping = model.mapping();
@ -78,7 +38,7 @@ private slots:
void remappingTest()
{
TestSourceModel sourceModel({"id", "name", "color"});
TestModel sourceModel({"id", "name", "color"});
RolesRenamingModel model;
QQmlListProperty<RoleRename> mapping = model.mapping();
@ -103,7 +63,7 @@ private slots:
void addMappingAfterFetchingRoleNamesTest()
{
TestSourceModel sourceModel({"id", "name", "color"});
TestModel sourceModel({"id", "name", "color"});
RolesRenamingModel model;
QQmlListProperty<RoleRename> mapping = model.mapping();
@ -134,7 +94,7 @@ private slots:
void duplicatedNamesTest()
{
TestSourceModel sourceModel({"id", "name", "color"});
TestModel sourceModel({"id", "name", "color"});
RolesRenamingModel model;
QQmlListProperty<RoleRename> mapping = model.mapping();
@ -182,8 +142,13 @@ private slots:
void sourceModelDeletedTest()
{
auto sourceModel = std::make_unique<TestSourceModel>(
QList<QString>{"id", "name", "color"});
auto sourceModel = std::make_unique<TestModel>(
QList<QPair<QString, QVariantList>> {
{ "id", { 1, 2, 3 }},
{ "name", { "a", "b", "c" }},
{ "color", { "red", "green", "blue" }}
});
RolesRenamingModel model;
QQmlListProperty<RoleRename> mapping = model.mapping();
@ -204,12 +169,12 @@ private slots:
{0, "tokenId"}, {1, "tokenName"}, {2, "color"}
};
QCOMPARE(model.roleNames(), expectedRoles);
QCOMPARE(model.rowCount(), 5);
QCOMPARE(model.rowCount(), 3);
QCOMPARE(model.data(model.index(0, 0), 0), 42);
QCOMPARE(model.data(model.index(0, 0), 1), 42);
QCOMPARE(model.data(model.index(5, 0), 0), {});
QCOMPARE(model.data(model.index(5, 0), 1), {});
QCOMPARE(model.data(model.index(0, 0), 0), 1);
QCOMPARE(model.data(model.index(0, 0), 1), "a");
QCOMPARE(model.data(model.index(2, 0), 0), 3);
QCOMPARE(model.data(model.index(2, 0), 1), "c");
QSignalSpy destroyedSpy(sourceModel.get(), &QObject::destroyed);
sourceModel.reset();
@ -222,8 +187,8 @@ private slots:
QCOMPARE(model.roleNames(), {});
QCOMPARE(model.data(model.index(0, 0), 0), {});
QCOMPARE(model.data(model.index(0, 0), 1), {});
QCOMPARE(model.data(model.index(5, 0), 0), {});
QCOMPARE(model.data(model.index(5, 0), 1), {});
QCOMPARE(model.data(model.index(2, 0), 0), {});
QCOMPARE(model.data(model.index(2, 0), 1), {});
}
};

View File

@ -1,75 +1,10 @@
#include <QtTest>
#include <QAbstractListModel>
#include <QCoreApplication>
#include "StatusQ/singleroleaggregator.h"
#include <StatusQ/singleroleaggregator.h>
#include <TestHelpers/testmodel.h>
namespace {
// TODO: To be removed once issue #12843 is resolved and we have a testing utils
class TestSourceModel : public QAbstractListModel {
public:
explicit TestSourceModel(QList<QPair<QString, QVariantList>> data)
: m_data(std::move(data)) {
m_roles.reserve(m_data.size());
for (auto i = 0; i < m_data.size(); i++)
m_roles.insert(i, m_data.at(i).first.toUtf8());
}
int rowCount(const QModelIndex& parent) const override {
Q_ASSERT(m_data.size());
return m_data.first().second.size();
}
QVariant data(const QModelIndex& index, int role) const override {
if (!index.isValid() || role < 0 || role >= m_data.size())
return {};
const auto row = index.row();
if (role >= m_data.length() || row >= m_data.at(0).second.length())
return {};
return m_data.at(role).second.at(row);
}
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override {
beginInsertRows(parent, row, row + count - 1);
m_data.insert(row, QPair<QString, QVariantList>());
endInsertRows();
return true;
}
void update(int index, int role, QVariant value) {
Q_ASSERT(role < m_data.size() && index < m_data[role].second.size());
m_data[role].second[index].setValue(std::move(value));
emit dataChanged(this->index(index, 0), this->index(index, 0), { role });
}
void remove(int index) {
beginRemoveRows(QModelIndex{}, index, index);
for (int i = 0; i < m_data.size(); i++) {
auto& roleVariantList = m_data[i].second;
Q_ASSERT(index < roleVariantList.size());
roleVariantList.removeAt(index);
}
endRemoveRows();
}
QHash<int, QByteArray> roleNames() const override {
return m_roles;
}
private:
QList<QPair<QString, QVariantList>> m_data;
QHash<int, QByteArray> m_roles;
};
class ChildSingleRoleAggregator : public SingleRoleAggregator {
Q_OBJECT
@ -93,10 +28,10 @@ private slots:
void testRoleName() {
ChildSingleRoleAggregator aggregator;
TestSourceModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }}
});
TestModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }}
});
QSignalSpy roleNameSpy(&aggregator, &SingleRoleAggregator::roleNameChanged);
// Test 1 - Assign role name but model is nullptr

View File

@ -1,76 +1,7 @@
#include <QtTest>
#include <QAbstractListModel>
#include <QCoreApplication>
#include "StatusQ/sumaggregator.h"
namespace {
// TODO: To be removed once issue #12843 is resolved and we have a testing utils
class TestSourceModel : public QAbstractListModel {
public:
explicit TestSourceModel(QList<QPair<QString, QVariantList>> data)
: m_data(std::move(data)) {
m_roles.reserve(m_data.size());
for (auto i = 0; i < m_data.size(); i++)
m_roles.insert(i, m_data.at(i).first.toUtf8());
}
int rowCount(const QModelIndex& parent) const override {
Q_ASSERT(m_data.size());
return m_data.first().second.size();
}
QVariant data(const QModelIndex& index, int role) const override {
if (!index.isValid() || role < 0 || role >= m_data.size())
return {};
const auto row = index.row();
if (role >= m_data.length() || row >= m_data.at(0).second.length())
return {};
return m_data.at(role).second.at(row);
}
bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override {
beginInsertRows(parent, row, row + count - 1);
m_data.insert(row, QPair<QString, QVariantList>());
endInsertRows();
return true;
}
void update(int index, int role, QVariant value) {
Q_ASSERT(role < m_data.size() && index < m_data[role].second.size());
m_data[role].second[index].setValue(std::move(value));
emit dataChanged(this->index(index, 0), this->index(index, 0), { role });
}
void remove(int index) {
beginRemoveRows(QModelIndex{}, index, index);
for (int i = 0; i < m_data.size(); i++) {
auto& roleVariantList = m_data[i].second;
Q_ASSERT(index < roleVariantList.size());
roleVariantList.removeAt(index);
}
endRemoveRows();
}
QHash<int, QByteArray> roleNames() const override {
return m_roles;
}
private:
QList<QPair<QString, QVariantList>> m_data;
QHash<int, QByteArray> m_roles;
};
} // anonymous namespace
#include <StatusQ/sumaggregator.h>
#include <TestHelpers/testmodel.h>
class TestSumAggregator : public QObject
{
@ -88,10 +19,10 @@ private slots:
void testModel() {
SumAggregator aggregator;
TestSourceModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }}
});
TestModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }}
});
QSignalSpy modelChangedSpy(&aggregator, &Aggregator::modelChanged);
QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged);
@ -110,10 +41,10 @@ private slots:
void testRoleName() {
SumAggregator aggregator;
TestSourceModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }}
});
TestModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { "0.123", "0.0000015", "1.45", "25.45221001" }}
});
QSignalSpy roleNameSpy(&aggregator, &SingleRoleAggregator::roleNameChanged);
QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged);
@ -143,10 +74,10 @@ private slots:
void testStringTypeValue() {
SumAggregator aggregator;
TestSourceModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { "0.123", "1", "1.45", "25.45" }}
});
TestModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { "0.123", "1", "1.45", "25.45" }}
});
QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged);
int valueChangedSpyCount = 0;
@ -178,10 +109,10 @@ private slots:
void testFloatTypeValue() {
SumAggregator aggregator;
TestSourceModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { 0.123, 1.0, 1.45, 25.45 }}
});
TestModel sourceModel({
{ "chainId", { "12", "13", "1", "321" }},
{ "balance", { 0.123, 1.0, 1.45, 25.45 }}
});
QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged);
int valueChangedSpyCount = 0;
@ -213,10 +144,10 @@ private slots:
void testStringUnsupportedTypeValue() {
SumAggregator aggregator;
TestSourceModel sourceModel({
{ "chainId", { "12", "13" }},
{ "balance", { "aa", "bb" }}
});
TestModel sourceModel({
{ "chainId", { "12", "13" }},
{ "balance", { "aa", "bb" }}
});
QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged);
aggregator.setModel(&sourceModel);
@ -234,10 +165,10 @@ private slots:
void testUnsupportedTypeValue() {
SumAggregator aggregator;
TestSourceModel sourceModel({
{ "chainId", { "12", "13" }},
{ "balance", { QByteArray(), QByteArray() }}
});
TestModel sourceModel({
{ "chainId", { "12", "13" }},
{ "balance", { QByteArray(), QByteArray() }}
});
QSignalSpy valueChangedSpy(&aggregator, &Aggregator::valueChanged);
aggregator.setModel(&sourceModel);